moar TS fixes

This commit is contained in:
Eric Woodward 2024-01-27 20:42:43 -05:00
parent e5fb81c8db
commit c30c09adaf
15 changed files with 51 additions and 96 deletions

View File

@ -1,4 +1,4 @@
declare const _default: <T>(opts: Partial<T>, envKey: string, localDefaultFilePath?: string) => {
declare const _default: <T>(opts: Partial<T>, envKey: string, filePath?: string) => {
[x: string]: any;
};
export default _default;

View File

@ -1 +1 @@
{"version":3,"file":"loadConfig.d.ts","sourceRoot":"","sources":["../../src/loadConfig.ts"],"names":[],"mappings":"sDAUS,MAAM;;;AAFf,wBAyCE"}
{"version":3,"file":"loadConfig.d.ts","sourceRoot":"","sources":["../../src/loadConfig.ts"],"names":[],"mappings":"sDAI6C,MAAM;;;AAAnD,wBA+CE"}

File diff suppressed because one or more lines are too long

7
lib/cjs/utils.d.ts vendored
View File

@ -3,13 +3,6 @@ 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;

View File

@ -1 +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"}
{"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,eAAO,MAAM,OAAO,cAyBnB,CAAC;AAEF,eAAO,MAAM,GAAG,WAAY,OAAO,EAAE,SACgB,CAAC;AAEtD,eAAO,MAAM,gBAAgB,aAAc,MAAM,GAAG,GAAG,QAQtD,CAAC"}

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
declare const _default: <T>(opts: Partial<T>, envKey: string, localDefaultFilePath?: string) => {
declare const _default: <T>(opts: Partial<T>, envKey: string, filePath?: string) => {
[x: string]: any;
};
export default _default;

View File

@ -1 +1 @@
{"version":3,"file":"loadConfig.d.ts","sourceRoot":"","sources":["../../src/loadConfig.ts"],"names":[],"mappings":"sDAUS,MAAM;;;AAFf,wBAyCE"}
{"version":3,"file":"loadConfig.d.ts","sourceRoot":"","sources":["../../src/loadConfig.ts"],"names":[],"mappings":"sDAI6C,MAAM;;;AAAnD,wBA+CE"}

File diff suppressed because one or more lines are too long

7
lib/esm/utils.d.ts vendored
View File

@ -3,13 +3,6 @@ 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;

View File

@ -1 +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"}
{"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,eAAO,MAAM,OAAO,cAyBnB,CAAC;AAEF,eAAO,MAAM,GAAG,WAAY,OAAO,EAAE,SACgB,CAAC;AAEtD,eAAO,MAAM,gBAAgB,aAAc,MAAM,GAAG,GAAG,QAQtD,CAAC"}

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"name": "@itsericwoodward/utils",
"version": "0.1.4",
"version": "0.1.5",
"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",

View File

@ -1,21 +1,11 @@
import path from "path";
import {
convertCamelToUpperSnakeCase,
getDirname,
readJsonIfExists,
} from "./utils";
import { convertCamelToUpperSnakeCase, readJsonIfExists } from "./utils";
export default <T>(
opts: Partial<T>,
envKey: string,
localDefaultFilePath = ""
) => {
export default <T>(opts: Partial<T>, envKey: string, filePath = "") => {
const { env } = process,
{ __dirname } = getDirname(),
def = readJsonIfExists(
path.join(__dirname, localDefaultFilePath || "./defaults.json5")
),
defPath = filePath || path.join(process.cwd() ?? ".", "defaults.json5"),
def = readJsonIfExists(defPath),
// gets value from ENV || options || defaults (in that order)
getVal = (envName: keyof T) => {
const snakeEnvName = `${envKey}_${convertCamelToUpperSnakeCase(
@ -40,6 +30,14 @@ export default <T>(
return def[optName];
};
const retVal = {
...Object.keys(def).reduce((acc: Partial<typeof def>, curr) => {
if (Array.isArray(def[curr])) acc[curr] = getArray(curr as keyof T);
else acc[curr] = getVal(curr as keyof T);
return acc;
}, {}),
};
return {
...Object.keys(def).reduce((acc: Partial<typeof def>, curr) => {
if (Array.isArray(def[curr])) acc[curr] = getArray(curr as keyof T);

View File

@ -9,23 +9,6 @@ export interface ErrorWithCode {
export const convertCamelToUpperSnakeCase = (str: string) =>
str.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase();
/**
* From: https://www.webmanajemen.com/2022/05/use-import-meta-cjs.html
*/
export 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 };
};
export const getTime = () => {
const now = new Date(),
tzo = -now.getTimezoneOffset(),