From cf8f8b3e3844c5bc2a7ca50956ecaccc1f1ea543 Mon Sep 17 00:00:00 2001 From: Eric Woodward Date: Sat, 27 Jan 2024 16:29:33 -0500 Subject: [PATCH] fix the broken package --- .gitignore | 3 -- lib/cjs/index.d.ts | 3 ++ lib/cjs/index.d.ts.map | 1 + lib/cjs/index.js | 24 +++++++++++++ lib/cjs/loadConfig.d.ts | 5 +++ lib/cjs/loadConfig.d.ts.map | 1 + lib/cjs/loadConfig.js | 43 ++++++++++++++++++++++++ lib/cjs/utils.d.ts | 16 +++++++++ lib/cjs/utils.d.ts.map | 1 + lib/cjs/utils.js | 67 +++++++++++++++++++++++++++++++++++++ lib/esm/index.d.ts | 3 ++ lib/esm/index.d.ts.map | 1 + lib/esm/index.js | 24 +++++++++++++ lib/esm/loadConfig.d.ts | 5 +++ lib/esm/loadConfig.d.ts.map | 1 + lib/esm/loadConfig.js | 43 ++++++++++++++++++++++++ lib/esm/utils.d.ts | 16 +++++++++ lib/esm/utils.d.ts.map | 1 + lib/esm/utils.js | 67 +++++++++++++++++++++++++++++++++++++ package.json | 2 +- 20 files changed, 323 insertions(+), 4 deletions(-) create mode 100644 lib/cjs/index.d.ts create mode 100644 lib/cjs/index.d.ts.map create mode 100644 lib/cjs/index.js create mode 100644 lib/cjs/loadConfig.d.ts create mode 100644 lib/cjs/loadConfig.d.ts.map create mode 100644 lib/cjs/loadConfig.js create mode 100644 lib/cjs/utils.d.ts create mode 100644 lib/cjs/utils.d.ts.map create mode 100644 lib/cjs/utils.js create mode 100644 lib/esm/index.d.ts create mode 100644 lib/esm/index.d.ts.map create mode 100644 lib/esm/index.js create mode 100644 lib/esm/loadConfig.d.ts create mode 100644 lib/esm/loadConfig.d.ts.map create mode 100644 lib/esm/loadConfig.js create mode 100644 lib/esm/utils.d.ts create mode 100644 lib/esm/utils.d.ts.map create mode 100644 lib/esm/utils.js diff --git a/.gitignore b/.gitignore index fb2775d..4024acd 100644 --- a/.gitignore +++ b/.gitignore @@ -131,6 +131,3 @@ dist # TODO TODO.md - -# Built libraries -/lib diff --git a/lib/cjs/index.d.ts b/lib/cjs/index.d.ts new file mode 100644 index 0000000..f154cdb --- /dev/null +++ b/lib/cjs/index.d.ts @@ -0,0 +1,3 @@ +export * from "./utils.js"; +export { default as loadConfig } from "./loadConfig.js"; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/lib/cjs/index.d.ts.map b/lib/cjs/index.d.ts.map new file mode 100644 index 0000000..8124025 --- /dev/null +++ b/lib/cjs/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,iBAAiB,CAAC"} \ No newline at end of file diff --git a/lib/cjs/index.js b/lib/cjs/index.js new file mode 100644 index 0000000..ed45c8a --- /dev/null +++ b/lib/cjs/index.js @@ -0,0 +1,24 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.loadConfig = void 0; +__exportStar(require("./utils.js"), exports); +var loadConfig_js_1 = require("./loadConfig.js"); +Object.defineProperty(exports, "loadConfig", { enumerable: true, get: function () { return __importDefault(loadConfig_js_1).default; } }); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSw2Q0FBMkI7QUFDM0IsaURBQXdEO0FBQS9DLDRIQUFBLE9BQU8sT0FBYyIsInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCAqIGZyb20gXCIuL3V0aWxzLmpzXCI7XG5leHBvcnQgeyBkZWZhdWx0IGFzIGxvYWRDb25maWcgfSBmcm9tIFwiLi9sb2FkQ29uZmlnLmpzXCI7XG4iXX0= \ No newline at end of file diff --git a/lib/cjs/loadConfig.d.ts b/lib/cjs/loadConfig.d.ts new file mode 100644 index 0000000..f7434fa --- /dev/null +++ b/lib/cjs/loadConfig.d.ts @@ -0,0 +1,5 @@ +declare const _default: (opts: Partial, envKey: string, localDefaultFilePath?: string) => { + [x: string]: any; +}; +export default _default; +//# sourceMappingURL=loadConfig.d.ts.map \ No newline at end of file diff --git a/lib/cjs/loadConfig.d.ts.map b/lib/cjs/loadConfig.d.ts.map new file mode 100644 index 0000000..795eba3 --- /dev/null +++ b/lib/cjs/loadConfig.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"loadConfig.d.ts","sourceRoot":"","sources":["../../src/loadConfig.ts"],"names":[],"mappings":"sDAUS,MAAM;;;AAFf,wBAyCE"} \ No newline at end of file diff --git a/lib/cjs/loadConfig.js b/lib/cjs/loadConfig.js new file mode 100644 index 0000000..7077409 --- /dev/null +++ b/lib/cjs/loadConfig.js @@ -0,0 +1,43 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const path_1 = __importDefault(require("path")); +const utils_1 = require("./utils"); +exports.default = (opts, envKey, localDefaultFilePath = "") => { + const { env } = process, { __dirname } = (0, utils_1.getDirname)(), def = (0, utils_1.readJsonIfExists)(path_1.default.join(__dirname, localDefaultFilePath || "./defaults.json5")), + // gets value from ENV || options || defaults (in that order) + getVal = (envName) => { + const snakeEnvName = `${envKey}_${(0, utils_1.convertCamelToUpperSnakeCase)(envName)}`; + if (env[snakeEnvName]) + return env[snakeEnvName]; + if (opts[envName]) + return opts[envName]; + return def[envName]; + }, + // gets array from ENV || options || defaults (in that order) + getArray = (envName, optName = "") => { + let newEnvName; + if (optName === "") { + optName = envName; + newEnvName = (0, utils_1.convertCamelToUpperSnakeCase)(envName); + } + newEnvName = `${envKey}_${String(envName)}`; + if (env[newEnvName]) + return env?.[newEnvName]?.split(path_1.default.delimiter); + if (Array.isArray(opts[optName]) && opts[optName].length) + return opts[optName]; + return def[optName]; + }; + return { + ...Object.keys(def).reduce((acc, curr) => { + if (Array.isArray(def[curr])) + acc[curr] = getArray(curr); + else + acc[curr] = getVal(curr); + return acc; + }, {}), + }; +}; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9hZENvbmZpZy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9sb2FkQ29uZmlnLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsZ0RBQXdCO0FBRXhCLG1DQUlpQjtBQUVqQixrQkFBZSxDQUNkLElBQWdCLEVBQ2hCLE1BQWMsRUFDZCxvQkFBb0IsR0FBRyxFQUFFLEVBQ3hCLEVBQUU7SUFDSCxNQUFNLEVBQUUsR0FBRyxFQUFFLEdBQUcsT0FBTyxFQUN0QixFQUFFLFNBQVMsRUFBRSxHQUFHLElBQUEsa0JBQVUsR0FBRSxFQUM1QixHQUFHLEdBQUcsSUFBQSx3QkFBZ0IsRUFDckIsY0FBSSxDQUFDLElBQUksQ0FBQyxTQUFTLEVBQUUsb0JBQW9CLElBQUksa0JBQWt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x0cmV0dXJuIGVudj8uW25ld0Vudk5hbWUgYXMga2V5b2YgdHlwZW9mIGVudl0/LnNwbGl0KHBhdGguZGVsaW1pdGVyKTtcblx0XHRcdGlmIChBcnJheS5pc0FycmF5KG9wdHNbb3B0TmFtZV0pICYmIChvcHRzW29wdE5hbWVdIGFzIFtdKS5sZW5ndGgpXG5cdFx0XHRcdHJldHVybiBvcHRzW29wdE5hbWVdO1xuXHRcdFx0cmV0dXJuIGRlZltvcHROYW1lXTtcblx0XHR9O1xuXG5cdHJldHVybiB7XG5cdFx0Li4uT2JqZWN0LmtleXMoZGVmKS5yZWR1Y2UoKGFjYzogUGFydGlhbDx0eXBlb2YgZGVmPiwgY3VycikgPT4ge1xuXHRcdFx0aWYgKEFycmF5LmlzQXJyYXkoZGVmW2N1cnJdKSkgYWNjW2N1cnJdID0gZ2V0QXJyYXkoY3VyciBhcyBrZXlvZiBUKTtcblx0XHRcdGVsc2UgYWNjW2N1cnJdID0gZ2V0VmFsKGN1cnIgYXMga2V5b2YgVCk7XG5cdFx0XHRyZXR1cm4gYWNjO1xuXHRcdH0sIHt9KSxcblx0fTtcbn07XG4iXX0= \ No newline at end of file diff --git a/lib/cjs/utils.d.ts b/lib/cjs/utils.d.ts new file mode 100644 index 0000000..991d592 --- /dev/null +++ b/lib/cjs/utils.d.ts @@ -0,0 +1,16 @@ +/// +export interface ErrorWithCode { + code: string; +} +export declare const convertCamelToUpperSnakeCase: (str: string) => string; +/** + * From: https://www.webmanajemen.com/2022/05/use-import-meta-cjs.html + */ +export declare const getDirname: () => { + __dirname: string; + __filename: string; +}; +export declare const getTime: () => string; +export declare const log: (...msg: unknown[]) => void; +export declare const readJsonIfExists: (filePath: string | URL) => any; +//# sourceMappingURL=utils.d.ts.map \ No newline at end of file diff --git a/lib/cjs/utils.d.ts.map b/lib/cjs/utils.d.ts.map new file mode 100644 index 0000000..5d0f3f3 --- /dev/null +++ b/lib/cjs/utils.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";AAIA,MAAM,WAAW,aAAa;IAC7B,IAAI,EAAE,MAAM,CAAC;CACb;AAED,eAAO,MAAM,4BAA4B,QAAS,MAAM,WACM,CAAC;AAE/D;;GAEG;AACH,eAAO,MAAM,UAAU;;;CAYtB,CAAC;AAEF,eAAO,MAAM,OAAO,cAyBnB,CAAC;AAEF,eAAO,MAAM,GAAG,WAAY,OAAO,EAAE,SACgB,CAAC;AAEtD,eAAO,MAAM,gBAAgB,aAAc,MAAM,GAAG,GAAG,QAQtD,CAAC"} \ No newline at end of file diff --git a/lib/cjs/utils.js b/lib/cjs/utils.js new file mode 100644 index 0000000..5120555 --- /dev/null +++ b/lib/cjs/utils.js @@ -0,0 +1,67 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.readJsonIfExists = exports.log = exports.getTime = exports.getDirname = exports.convertCamelToUpperSnakeCase = void 0; +const chalk_1 = __importDefault(require("chalk")); +const fs_1 = __importDefault(require("fs")); +const json5_1 = __importDefault(require("json5")); +const convertCamelToUpperSnakeCase = (str) => str.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase(); +exports.convertCamelToUpperSnakeCase = convertCamelToUpperSnakeCase; +/** + * From: https://www.webmanajemen.com/2022/05/use-import-meta-cjs.html + */ +const getDirname = () => { + // get the stack + const { stack } = new Error(); + // get the third line (the original invoker) + const invokeFileLine = (stack || "").split(`\n`)[2]; + // match the file url from file://(.+.(ts|js)) and get the first capturing group + const __filename = (invokeFileLine.match(/file:\/\/(.+.(ts|js))/) || + [])[1].slice(1); + // match the file URL from file://(.+)/ and get the first capturing group + // the (.+) is a greedy quantifier and will make the RegExp expand to the largest match + const __dirname = (invokeFileLine.match(/file:\/\/(.+)\//) || [])[1].slice(1); + return { __dirname, __filename }; +}; +exports.getDirname = getDirname; +const getTime = () => { + const now = new Date(), tzo = -now.getTimezoneOffset(), dif = tzo >= 0 ? "+" : "-", pad = (num) => { + const norm = Math.floor(Math.abs(num)); + return `${norm < 10 ? "0" : ""}${norm}`; + }; + return [ + now.getFullYear(), + "-", + pad(now.getMonth() + 1), + "-", + pad(now.getDate()), + "T", + pad(now.getHours()), + ":", + pad(now.getMinutes()), + ":", + pad(now.getSeconds()), + dif, + pad(tzo / 60), + ":", + pad(tzo % 60), + ].join(""); +}; +exports.getTime = getTime; +const log = (...msg) => console.log(`${chalk_1.default.grey(`${(0, exports.getTime)()}:`)} ${msg}`); +exports.log = log; +const readJsonIfExists = (filePath) => { + try { + return json5_1.default.parse(fs_1.default.readFileSync(filePath, { encoding: "utf8" })); + } + catch (err) { + // if no file, return empty object + if (err?.code === "ENOENT") + return {}; + throw err; + } +}; +exports.readJsonIfExists = readJsonIfExists; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUEsa0RBQTBCO0FBQzFCLDRDQUFvQjtBQUNwQixrREFBMEI7QUFNbkIsTUFBTSw0QkFBNEIsR0FBRyxDQUFDLEdBQVcsRUFBRSxFQUFFLENBQzNELEdBQUcsQ0FBQyxPQUFPLENBQUMsUUFBUSxFQUFFLENBQUMsTUFBTSxFQUFFLEVBQUUsQ0FBQyxJQUFJLE1BQU0sRUFBRSxDQUFDLENBQUMsV0FBVyxFQUFFLENBQUM7QUFEbEQsUUFBQSw0QkFBNEIsZ0NBQ3NCO0FBRS9EOztHQUVHO0FBQ0ksTUFBTSxVQUFVLEdBQUcsR0FBRyxFQUFFO0lBQzlCLGdCQUFnQjtJQUNoQixNQUFNLEVBQUUsS0FBSyxFQUFFLEdBQUcsSUFBSSxLQUFLLEVBQUUsQ0FBQztJQU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RcdG5vdy5nZXRGdWxsWWVhcigpLFxuXHRcdFwiLVwiLFxuXHRcdHBhZChub3cuZ2V0TW9udGgoKSArIDEpLFxuXHRcdFwiLVwiLFxuXHRcdHBhZChub3cuZ2V0RGF0ZSgpKSxcblx0XHRcIlRcIixcblx0XHRwYWQobm93LmdldEhvdXJzKCkpLFxuXHRcdFwiOlwiLFxuXHRcdHBhZChub3cuZ2V0TWludXRlcygpKSxcblx0XHRcIjpcIixcblx0XHRwYWQobm93LmdldFNlY29uZHMoKSksXG5cdFx0ZGlmLFxuXHRcdHBhZCh0em8gLyA2MCksXG5cdFx0XCI6XCIsXG5cdFx0cGFkKHR6byAlIDYwKSxcblx0XS5qb2luKFwiXCIpO1xufTtcblxuZXhwb3J0IGNvbnN0IGxvZyA9ICguLi5tc2c6IHVua25vd25bXSkgPT5cblx0Y29uc29sZS5sb2coYCR7Y2hhbGsuZ3JleShgJHtnZXRUaW1lKCl9OmApfSAke21zZ31gKTtcblxuZXhwb3J0IGNvbnN0IHJlYWRKc29uSWZFeGlzdHMgPSAoZmlsZVBhdGg6IHN0cmluZyB8IFVSTCkgPT4ge1xuXHR0cnkge1xuXHRcdHJldHVybiBqc29uNS5wYXJzZShmcy5yZWFkRmlsZVN5bmMoZmlsZVBhdGgsIHsgZW5jb2Rpbmc6IFwidXRmOFwiIH0pKTtcblx0fSBjYXRjaCAoZXJyKSB7XG5cdFx0Ly8gaWYgbm8gZmlsZSwgcmV0dXJuIGVtcHR5IG9iamVjdFxuXHRcdGlmICgoZXJyIGFzIEVycm9yV2l0aENvZGUpPy5jb2RlID09PSBcIkVOT0VOVFwiKSByZXR1cm4ge307XG5cdFx0dGhyb3cgZXJyO1xuXHR9XG59O1xuIl19 \ No newline at end of file diff --git a/lib/esm/index.d.ts b/lib/esm/index.d.ts new file mode 100644 index 0000000..f154cdb --- /dev/null +++ b/lib/esm/index.d.ts @@ -0,0 +1,3 @@ +export * from "./utils.js"; +export { default as loadConfig } from "./loadConfig.js"; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/lib/esm/index.d.ts.map b/lib/esm/index.d.ts.map new file mode 100644 index 0000000..8124025 --- /dev/null +++ b/lib/esm/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,iBAAiB,CAAC"} \ No newline at end of file diff --git a/lib/esm/index.js b/lib/esm/index.js new file mode 100644 index 0000000..ed45c8a --- /dev/null +++ b/lib/esm/index.js @@ -0,0 +1,24 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.loadConfig = void 0; +__exportStar(require("./utils.js"), exports); +var loadConfig_js_1 = require("./loadConfig.js"); +Object.defineProperty(exports, "loadConfig", { enumerable: true, get: function () { return __importDefault(loadConfig_js_1).default; } }); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSw2Q0FBMkI7QUFDM0IsaURBQXdEO0FBQS9DLDRIQUFBLE9BQU8sT0FBYyIsInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCAqIGZyb20gXCIuL3V0aWxzLmpzXCI7XG5leHBvcnQgeyBkZWZhdWx0IGFzIGxvYWRDb25maWcgfSBmcm9tIFwiLi9sb2FkQ29uZmlnLmpzXCI7XG4iXX0= \ No newline at end of file diff --git a/lib/esm/loadConfig.d.ts b/lib/esm/loadConfig.d.ts new file mode 100644 index 0000000..f7434fa --- /dev/null +++ b/lib/esm/loadConfig.d.ts @@ -0,0 +1,5 @@ +declare const _default: (opts: Partial, envKey: string, localDefaultFilePath?: string) => { + [x: string]: any; +}; +export default _default; +//# sourceMappingURL=loadConfig.d.ts.map \ No newline at end of file diff --git a/lib/esm/loadConfig.d.ts.map b/lib/esm/loadConfig.d.ts.map new file mode 100644 index 0000000..795eba3 --- /dev/null +++ b/lib/esm/loadConfig.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"loadConfig.d.ts","sourceRoot":"","sources":["../../src/loadConfig.ts"],"names":[],"mappings":"sDAUS,MAAM;;;AAFf,wBAyCE"} \ No newline at end of file diff --git a/lib/esm/loadConfig.js b/lib/esm/loadConfig.js new file mode 100644 index 0000000..7077409 --- /dev/null +++ b/lib/esm/loadConfig.js @@ -0,0 +1,43 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const path_1 = __importDefault(require("path")); +const utils_1 = require("./utils"); +exports.default = (opts, envKey, localDefaultFilePath = "") => { + const { env } = process, { __dirname } = (0, utils_1.getDirname)(), def = (0, utils_1.readJsonIfExists)(path_1.default.join(__dirname, localDefaultFilePath || "./defaults.json5")), + // gets value from ENV || options || defaults (in that order) + getVal = (envName) => { + const snakeEnvName = `${envKey}_${(0, utils_1.convertCamelToUpperSnakeCase)(envName)}`; + if (env[snakeEnvName]) + return env[snakeEnvName]; + if (opts[envName]) + return opts[envName]; + return def[envName]; + }, + // gets array from ENV || options || defaults (in that order) + getArray = (envName, optName = "") => { + let newEnvName; + if (optName === "") { + optName = envName; + newEnvName = (0, utils_1.convertCamelToUpperSnakeCase)(envName); + } + newEnvName = `${envKey}_${String(envName)}`; + if (env[newEnvName]) + return env?.[newEnvName]?.split(path_1.default.delimiter); + if (Array.isArray(opts[optName]) && opts[optName].length) + return opts[optName]; + return def[optName]; + }; + return { + ...Object.keys(def).reduce((acc, curr) => { + if (Array.isArray(def[curr])) + acc[curr] = getArray(curr); + else + acc[curr] = getVal(curr); + return acc; + }, {}), + }; +}; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9hZENvbmZpZy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9sb2FkQ29uZmlnLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsZ0RBQXdCO0FBRXhCLG1DQUlpQjtBQUVqQixrQkFBZSxDQUNkLElBQWdCLEVBQ2hCLE1BQWMsRUFDZCxvQkFBb0IsR0FBRyxFQUFFLEVBQ3hCLEVBQUU7SUFDSCxNQUFNLEVBQUUsR0FBRyxFQUFFLEdBQUcsT0FBTyxFQUN0QixFQUFFLFNBQVMsRUFBRSxHQUFHLElBQUEsa0JBQVUsR0FBRSxFQUM1QixHQUFHLEdBQUcsSUFBQSx3QkFBZ0IsRUFDckIsY0FBSSxDQUFDLElBQUksQ0FBQyxTQUFTLEVBQUUsb0JBQW9CLElBQUksa0JBQWtCLENBQUMsQ0FDaEU7SUFDRCw2REFBNkQ7SUFDN0QsTUFBTSxHQUFHLENBQUMsT0FBZ0IsRUFBRSxFQUFFO1FBQzdCLE1BQU0sWUFBWSxHQUFHLEdBQUcsTUFBTSxJQUFJLElBQUEsb0NBQTRCLEVBQzdELE9BQWlCLENBQ2pCLEVBQUUs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GgpXG5cdFx0XHRcdHJldHVybiBvcHRzW29wdE5hbWVdO1xuXHRcdFx0cmV0dXJuIGRlZltvcHROYW1lXTtcblx0XHR9O1xuXG5cdHJldHVybiB7XG5cdFx0Li4uT2JqZWN0LmtleXMoZGVmKS5yZWR1Y2UoKGFjYzogUGFydGlhbDx0eXBlb2YgZGVmPiwgY3VycikgPT4ge1xuXHRcdFx0aWYgKEFycmF5LmlzQXJyYXkoZGVmW2N1cnJdKSkgYWNjW2N1cnJdID0gZ2V0QXJyYXkoY3VyciBhcyBrZXlvZiBUKTtcblx0XHRcdGVsc2UgYWNjW2N1cnJdID0gZ2V0VmFsKGN1cnIgYXMga2V5b2YgVCk7XG5cdFx0XHRyZXR1cm4gYWNjO1xuXHRcdH0sIHt9KSxcblx0fTtcbn07XG4iXX0= \ No newline at end of file diff --git a/lib/esm/utils.d.ts b/lib/esm/utils.d.ts new file mode 100644 index 0000000..991d592 --- /dev/null +++ b/lib/esm/utils.d.ts @@ -0,0 +1,16 @@ +/// +export interface ErrorWithCode { + code: string; +} +export declare const convertCamelToUpperSnakeCase: (str: string) => string; +/** + * From: https://www.webmanajemen.com/2022/05/use-import-meta-cjs.html + */ +export declare const getDirname: () => { + __dirname: string; + __filename: string; +}; +export declare const getTime: () => string; +export declare const log: (...msg: unknown[]) => void; +export declare const readJsonIfExists: (filePath: string | URL) => any; +//# sourceMappingURL=utils.d.ts.map \ No newline at end of file diff --git a/lib/esm/utils.d.ts.map b/lib/esm/utils.d.ts.map new file mode 100644 index 0000000..5d0f3f3 --- /dev/null +++ b/lib/esm/utils.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";AAIA,MAAM,WAAW,aAAa;IAC7B,IAAI,EAAE,MAAM,CAAC;CACb;AAED,eAAO,MAAM,4BAA4B,QAAS,MAAM,WACM,CAAC;AAE/D;;GAEG;AACH,eAAO,MAAM,UAAU;;;CAYtB,CAAC;AAEF,eAAO,MAAM,OAAO,cAyBnB,CAAC;AAEF,eAAO,MAAM,GAAG,WAAY,OAAO,EAAE,SACgB,CAAC;AAEtD,eAAO,MAAM,gBAAgB,aAAc,MAAM,GAAG,GAAG,QAQtD,CAAC"} \ No newline at end of file diff --git a/lib/esm/utils.js b/lib/esm/utils.js new file mode 100644 index 0000000..5120555 --- /dev/null +++ b/lib/esm/utils.js @@ -0,0 +1,67 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.readJsonIfExists = exports.log = exports.getTime = exports.getDirname = exports.convertCamelToUpperSnakeCase = void 0; +const chalk_1 = __importDefault(require("chalk")); +const fs_1 = __importDefault(require("fs")); +const json5_1 = __importDefault(require("json5")); +const convertCamelToUpperSnakeCase = (str) => str.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase(); +exports.convertCamelToUpperSnakeCase = convertCamelToUpperSnakeCase; +/** + * From: https://www.webmanajemen.com/2022/05/use-import-meta-cjs.html + */ +const getDirname = () => { + // get the stack + const { stack } = new Error(); + // get the third line (the original invoker) + const invokeFileLine = (stack || "").split(`\n`)[2]; + // match the file url from file://(.+.(ts|js)) and get the first capturing group + const __filename = (invokeFileLine.match(/file:\/\/(.+.(ts|js))/) || + [])[1].slice(1); + // match the file URL from file://(.+)/ and get the first capturing group + // the (.+) is a greedy quantifier and will make the RegExp expand to the largest match + const __dirname = (invokeFileLine.match(/file:\/\/(.+)\//) || [])[1].slice(1); + return { __dirname, __filename }; +}; +exports.getDirname = getDirname; +const getTime = () => { + const now = new Date(), tzo = -now.getTimezoneOffset(), dif = tzo >= 0 ? "+" : "-", pad = (num) => { + const norm = Math.floor(Math.abs(num)); + return `${norm < 10 ? "0" : ""}${norm}`; + }; + return [ + now.getFullYear(), + "-", + pad(now.getMonth() + 1), + "-", + pad(now.getDate()), + "T", + pad(now.getHours()), + ":", + pad(now.getMinutes()), + ":", + pad(now.getSeconds()), + dif, + pad(tzo / 60), + ":", + pad(tzo % 60), + ].join(""); +}; +exports.getTime = getTime; +const log = (...msg) => console.log(`${chalk_1.default.grey(`${(0, exports.getTime)()}:`)} ${msg}`); +exports.log = log; +const readJsonIfExists = (filePath) => { + try { + return json5_1.default.parse(fs_1.default.readFileSync(filePath, { encoding: "utf8" })); + } + catch (err) { + // if no file, return empty object + if (err?.code === "ENOENT") + return {}; + throw err; + } +}; +exports.readJsonIfExists = readJsonIfExists; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUEsa0RBQTBCO0FBQzFCLDRDQUFvQjtBQUNwQixrREFBMEI7QUFNbkIsTUFBTSw0QkFBNEIsR0FBRyxDQUFDLEdBQVcsRUFBRSxFQUFFLENBQzNELEdBQUcsQ0FBQyxPQUFPLENBQUMsUUFBUSxFQUFFLENBQUMsTUFBTSxFQUFFLEVBQUUsQ0FBQyxJQUFJLE1BQU0sRUFBRSxDQUFDLENBQUMsV0FBVyxFQUFFLENBQUM7QUFEbEQsUUFBQSw0QkFBNEIsZ0NBQ3NCO0FBRS9EOztHQUVHO0FBQ0ksTUFBTSxVQUFVLEdBQUcsR0FBRyxFQUFFO0lBQzlCLGdCQUFnQjtJQUNoQixNQUFNLEVBQUUsS0FBSyxFQUFFLEdBQUcsSUFBSSxLQUFLLEVBQUUsQ0FBQztJQUM5Qiw0Q0FBNEM7SUFDNUMsTUFBTSxjQUFjLEdBQUcsQ0FBQyxLQUFLLElBQUksRUFBRSxDQUFDLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQ3BELGdGQUFnRjtJQUNoRixNQUFNLFVBQVUsR0FBRyxDQUFD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OlwiLFxuXHRcdHBhZChub3cuZ2V0TWludXRlcygpKSxcblx0XHRcIjpcIixcblx0XHRwYWQobm93LmdldFNlY29uZHMoKSksXG5cdFx0ZGlmLFxuXHRcdHBhZCh0em8gLyA2MCksXG5cdFx0XCI6XCIsXG5cdFx0cGFkKHR6byAlIDYwKSxcblx0XS5qb2luKFwiXCIpO1xufTtcblxuZXhwb3J0IGNvbnN0IGxvZyA9ICguLi5tc2c6IHVua25vd25bXSkgPT5cblx0Y29uc29sZS5sb2coYCR7Y2hhbGsuZ3JleShgJHtnZXRUaW1lKCl9OmApfSAke21zZ31gKTtcblxuZXhwb3J0IGNvbnN0IHJlYWRKc29uSWZFeGlzdHMgPSAoZmlsZVBhdGg6IHN0cmluZyB8IFVSTCkgPT4ge1xuXHR0cnkge1xuXHRcdHJldHVybiBqc29uNS5wYXJzZShmcy5yZWFkRmlsZVN5bmMoZmlsZVBhdGgsIHsgZW5jb2Rpbmc6IFwidXRmOFwiIH0pKTtcblx0fSBjYXRjaCAoZXJyKSB7XG5cdFx0Ly8gaWYgbm8gZmlsZSwgcmV0dXJuIGVtcHR5IG9iamVjdFxuXHRcdGlmICgoZXJyIGFzIEVycm9yV2l0aENvZGUpPy5jb2RlID09PSBcIkVOT0VOVFwiKSByZXR1cm4ge307XG5cdFx0dGhyb3cgZXJyO1xuXHR9XG59O1xuIl19 \ No newline at end of file diff --git a/package.json b/package.json index 30a6a48..a9c5f00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@itsericwoodward/utils", - "version": "0.1.2", + "version": "0.1.3", "description": "A small collection of common libraries and functions the author likes to use in his Node applications.", "main": "./lib/cjs/index.js", "module": "./lib/esm/index.js",