From a9c0dd4b63433868f35fa48e49ca7c469dcad85a Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Thu, 17 Apr 2025 11:24:53 +0100 Subject: [PATCH] Updates --- dist/engine/schema-to-typedef.js | 26 ++++++++++++++++++-- dist/index.d.ts | 2 ++ dist/index.js | 2 ++ dist/package-shared/utils/parse-env.d.ts | 3 +++ dist/package-shared/utils/parse-env.js | 19 +++++++++++++++ engine/schema-to-typedef.ts | 31 +++++++++++++++++++++--- index.ts | 2 ++ package-shared/utils/parse-env.ts | 16 ++++++++++++ package.json | 2 +- 9 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 dist/package-shared/utils/parse-env.d.ts create mode 100644 dist/package-shared/utils/parse-env.js create mode 100644 package-shared/utils/parse-env.ts diff --git a/dist/engine/schema-to-typedef.js b/dist/engine/schema-to-typedef.js index ea9871d..00c911d 100644 --- a/dist/engine/schema-to-typedef.js +++ b/dist/engine/schema-to-typedef.js @@ -19,6 +19,7 @@ const util_1 = require("util"); const db_schema_to_type_1 = __importDefault(require("../package-shared/functions/dsql/db-schema-to-type")); const path_1 = __importDefault(require("path")); const debug_log_1 = __importDefault(require("../package-shared/utils/logging/debug-log")); +const parse_env_1 = __importDefault(require("../package-shared/utils/parse-env")); const args = (0, util_1.parseArgs)({ args: process.argv, options: { @@ -36,16 +37,37 @@ const args = (0, util_1.parseArgs)({ type: "string", short: "o", }, + envfile: { + type: "string", + short: "e", + }, debug: { type: "boolean", - short: "e", + short: "u", }, }, strict: false, }); +const { apiKey: key, database, outfile, debug, envfile } = args.values; +if (envfile && typeof envfile == "string") { + const finalEnvPath = path_1.default.resolve(process.cwd(), envfile); + if (fs_1.default.existsSync(finalEnvPath)) { + const appendedEnv = (0, parse_env_1.default)(finalEnvPath); + if (debug) { + (0, debug_log_1.default)({ + log: appendedEnv, + label: "Appended env", + title: "Schema to Typedef", + addTime: true, + }); + } + for (const [key, value] of Object.entries(appendedEnv)) { + process.env[key] = value; + } + } +} (() => __awaiter(void 0, void 0, void 0, function* () { try { - const { apiKey: key, database, outfile, debug } = args.values; if (debug) { (0, debug_log_1.default)({ log: args.values, diff --git a/dist/index.d.ts b/dist/index.d.ts index 37f84fe..572f05b 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -40,6 +40,7 @@ import dsqlCrud from "./package-shared/utils/data-fetching/crud"; import dsqlMethodCrud from "./package-shared/utils/data-fetching/method-crud"; import debugLog from "./package-shared/utils/logging/debug-log"; import { ErrorCallback } from "./package-shared/types"; +import parseEnv from "./package-shared/utils/parse-env"; /** * Main Export */ @@ -129,6 +130,7 @@ declare const datasquirel: { httpRequest: typeof httpRequest; connDbHandler: typeof connDbHandler; debugLog: typeof debugLog; + parseEnv: typeof parseEnv; }; /** * Run Crud actions `get`, `insert`, `update`, `delete` diff --git a/dist/index.js b/dist/index.js index f98d164..63273b9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -37,6 +37,7 @@ const delete_user_1 = __importDefault(require("./package-shared/actions/users/de const crud_1 = __importDefault(require("./package-shared/utils/data-fetching/crud")); const method_crud_1 = __importDefault(require("./package-shared/utils/data-fetching/method-crud")); const debug_log_1 = __importDefault(require("./package-shared/utils/logging/debug-log")); +const parse_env_1 = __importDefault(require("./package-shared/utils/parse-env")); /** * User Functions Object */ @@ -102,6 +103,7 @@ const datasquirel = { httpRequest: httpRequest_1.default, connDbHandler: conn_db_handler_1.default, debugLog: debug_log_1.default, + parseEnv: parse_env_1.default, }, /** * Run Crud actions `get`, `insert`, `update`, `delete` diff --git a/dist/package-shared/utils/parse-env.d.ts b/dist/package-shared/utils/parse-env.d.ts new file mode 100644 index 0000000..6fa5e62 --- /dev/null +++ b/dist/package-shared/utils/parse-env.d.ts @@ -0,0 +1,3 @@ +export default function parseEnv(/** Env File Path */ envPath: string): { + [k: string]: string; +}; diff --git a/dist/package-shared/utils/parse-env.js b/dist/package-shared/utils/parse-env.js new file mode 100644 index 0000000..25151d1 --- /dev/null +++ b/dist/package-shared/utils/parse-env.js @@ -0,0 +1,19 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = parseEnv; +const fs_1 = __importDefault(require("fs")); +const path_1 = __importDefault(require("path")); +function parseEnv(/** Env File Path */ envPath) { + let obj = {}; + const finalEnvPath = path_1.default.resolve(process.cwd(), envPath); + const envFile = fs_1.default.readFileSync(finalEnvPath, "utf-8"); + const envLinesArr = envFile.split(/\r\n/).filter((ln) => ln.match(/\=/)); + envLinesArr.forEach((ln) => { + const keyValArr = ln.split("="); + obj[keyValArr[0]] = keyValArr[1] || ""; + }); + return obj; +} diff --git a/engine/schema-to-typedef.ts b/engine/schema-to-typedef.ts index e239a16..c37aefd 100644 --- a/engine/schema-to-typedef.ts +++ b/engine/schema-to-typedef.ts @@ -8,6 +8,7 @@ import { DSQL_DatabaseSchemaType } from "../package-shared/types"; import dbSchemaToType from "../package-shared/functions/dsql/db-schema-to-type"; import path from "path"; import debugLog from "../package-shared/utils/logging/debug-log"; +import parseEnv from "../package-shared/utils/parse-env"; const args = parseArgs({ args: process.argv, @@ -26,18 +27,42 @@ const args = parseArgs({ type: "string", short: "o", }, + envfile: { + type: "string", + short: "e", + }, debug: { type: "boolean", - short: "e", + short: "u", }, }, strict: false, }); +const { apiKey: key, database, outfile, debug, envfile } = args.values; + +if (envfile && typeof envfile == "string") { + const finalEnvPath = path.resolve(process.cwd(), envfile); + if (fs.existsSync(finalEnvPath)) { + const appendedEnv = parseEnv(finalEnvPath); + + if (debug) { + debugLog({ + log: appendedEnv, + label: "Appended env", + title: "Schema to Typedef", + addTime: true, + }); + } + + for (const [key, value] of Object.entries(appendedEnv)) { + process.env[key] = value; + } + } +} + (async () => { try { - const { apiKey: key, database, outfile, debug } = args.values; - if (debug) { debugLog({ log: args.values, diff --git a/index.ts b/index.ts index 73c1302..01d586a 100644 --- a/index.ts +++ b/index.ts @@ -48,6 +48,7 @@ import dsqlCrud from "./package-shared/utils/data-fetching/crud"; import dsqlMethodCrud from "./package-shared/utils/data-fetching/method-crud"; import debugLog from "./package-shared/utils/logging/debug-log"; import { ErrorCallback } from "./package-shared/types"; +import parseEnv from "./package-shared/utils/parse-env"; /** * User Functions Object @@ -117,6 +118,7 @@ const datasquirel = { httpRequest, connDbHandler, debugLog, + parseEnv, }, /** * Run Crud actions `get`, `insert`, `update`, `delete` diff --git a/package-shared/utils/parse-env.ts b/package-shared/utils/parse-env.ts new file mode 100644 index 0000000..8c8642a --- /dev/null +++ b/package-shared/utils/parse-env.ts @@ -0,0 +1,16 @@ +import fs from "fs"; +import path from "path"; + +export default function parseEnv(/** Env File Path */ envPath: string) { + let obj: { [k: string]: string } = {}; + const finalEnvPath = path.resolve(process.cwd(), envPath); + const envFile = fs.readFileSync(finalEnvPath, "utf-8"); + const envLinesArr = envFile.split(/\r\n/).filter((ln) => ln.match(/\=/)); + + envLinesArr.forEach((ln) => { + const keyValArr = ln.split("="); + obj[keyValArr[0]] = keyValArr[1] || ""; + }); + + return obj; +} diff --git a/package.json b/package.json index 7ea680a..cbc4e66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "4.3.2", + "version": "4.3.3", "description": "Cloud-based SQL data management tool", "main": "dist/index.js", "bin": {