@yongdall/load-configuration 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.mts +5 -3
- package/index.mjs +25 -11
- package/index.mjs.map +1 -1
- package/package.json +1 -1
package/index.d.mts
CHANGED
|
@@ -2,14 +2,16 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* @template {object} T
|
|
4
4
|
* @param {string} path
|
|
5
|
-
* @
|
|
5
|
+
* @param {boolean} [scriptOnly]
|
|
6
|
+
* @returns {Promise<[string, T | null] | null>}
|
|
6
7
|
*/
|
|
7
|
-
declare function loadConfigurationWithPath<T extends object>(path: string): Promise<[string, T] | null>;
|
|
8
|
+
declare function loadConfigurationWithPath<T extends object>(path: string, scriptOnly?: boolean): Promise<[string, T | null] | null>;
|
|
8
9
|
/**
|
|
9
10
|
* @template {object} T
|
|
10
11
|
* @param {string} path
|
|
12
|
+
* @param {boolean} [scriptOnly]
|
|
11
13
|
* @returns {Promise<T | null>}
|
|
12
14
|
*/
|
|
13
|
-
declare function loadConfiguration<T extends object>(path: string): Promise<T | null>;
|
|
15
|
+
declare function loadConfiguration<T extends object>(path: string, scriptOnly?: boolean): Promise<T | null>;
|
|
14
16
|
//#endregion
|
|
15
17
|
export { loadConfiguration as default, loadConfigurationWithPath };
|
package/index.mjs
CHANGED
|
@@ -43,9 +43,18 @@ function loadMjs(path) {
|
|
|
43
43
|
return v;
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
|
-
/** @type {[string, <T>(path: string) => Promise<T | null
|
|
46
|
+
/** @type {[string, <T>(path: string) => Promise<T | null>, script?: boolean][]} */
|
|
47
47
|
const loaders = [
|
|
48
|
-
[
|
|
48
|
+
[
|
|
49
|
+
".mjs",
|
|
50
|
+
loadMjs,
|
|
51
|
+
true
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
".mts",
|
|
55
|
+
loadMjs,
|
|
56
|
+
true
|
|
57
|
+
],
|
|
49
58
|
[".json", loadJson],
|
|
50
59
|
[".yml", loadYaml],
|
|
51
60
|
[".yaml", loadYaml],
|
|
@@ -54,13 +63,16 @@ const loaders = [
|
|
|
54
63
|
/**
|
|
55
64
|
* @template {object} T
|
|
56
65
|
* @param {string} path
|
|
66
|
+
* @param {boolean} [scriptOnly]
|
|
57
67
|
* @returns {Promise<[path: string, loader: <T>(path: string) => Promise<T | null>] | null>}
|
|
58
68
|
*/
|
|
59
|
-
async function find(path) {
|
|
69
|
+
async function find(path, scriptOnly) {
|
|
60
70
|
const ext = (await fsPromises.stat(path).catch(() => null))?.isFile() ? pathFn.extname(path).toLowerCase() : "";
|
|
61
|
-
let loader = loaders.find((v) => v[0] === ext)
|
|
62
|
-
|
|
63
|
-
|
|
71
|
+
let loader = loaders.find((v) => v[0] === ext);
|
|
72
|
+
let loaderFn = (!scriptOnly || loader?.[2]) && loader?.[1];
|
|
73
|
+
if (loaderFn) return [path, loaderFn];
|
|
74
|
+
for (const [e, l, script] of loaders) {
|
|
75
|
+
if (scriptOnly && !script) continue;
|
|
64
76
|
const p = path + e;
|
|
65
77
|
if ((await fsPromises.stat(p).catch(() => null))?.isFile()) return [p, l];
|
|
66
78
|
}
|
|
@@ -69,10 +81,11 @@ async function find(path) {
|
|
|
69
81
|
/**
|
|
70
82
|
* @template {object} T
|
|
71
83
|
* @param {string} path
|
|
72
|
-
* @
|
|
84
|
+
* @param {boolean} [scriptOnly]
|
|
85
|
+
* @returns {Promise<[string, T | null] | null>}
|
|
73
86
|
*/
|
|
74
|
-
async function loadConfigurationWithPath(path) {
|
|
75
|
-
const result = await find(path);
|
|
87
|
+
async function loadConfigurationWithPath(path, scriptOnly) {
|
|
88
|
+
const result = await find(path, scriptOnly);
|
|
76
89
|
if (!result) return null;
|
|
77
90
|
const [filepath, loader] = result;
|
|
78
91
|
return Promise.all([fsPromises.realpath(filepath), loader(filepath).then((v) => typeof v === "object" ? v : null)]);
|
|
@@ -80,10 +93,11 @@ async function loadConfigurationWithPath(path) {
|
|
|
80
93
|
/**
|
|
81
94
|
* @template {object} T
|
|
82
95
|
* @param {string} path
|
|
96
|
+
* @param {boolean} [scriptOnly]
|
|
83
97
|
* @returns {Promise<T | null>}
|
|
84
98
|
*/
|
|
85
|
-
async function loadConfiguration(path) {
|
|
86
|
-
const result = await find(path);
|
|
99
|
+
async function loadConfiguration(path, scriptOnly) {
|
|
100
|
+
const result = await find(path, scriptOnly);
|
|
87
101
|
if (!result) return null;
|
|
88
102
|
const [filepath, loader] = result;
|
|
89
103
|
return loader(filepath).then((v) => typeof v === "object" ? v : null);
|
package/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../cli/load-configuration/index.mjs"],"sourcesContent":["import * as fsPromises from 'node:fs/promises';\nimport * as pathFn from 'node:path';\nimport * as yaml from 'yaml';\nimport * as toml from 'smol-toml';\n\n/**\n * \n * @template T\n * @param {string} path\n * @returns {Promise<T>}\n */\nasync function loadToml(path) {\n\t/** @type {any} */\n\tconst value = toml.parse(await fsPromises.readFile(path, 'utf-8'));\n\treturn value;\n}\n/**\n * \n * @template T\n * @param {string} path\n * @returns {Promise<T>}\n */\nasync function loadYaml(path) {\n\treturn yaml.parse(await fsPromises.readFile(path, 'utf-8'));\n}\n/**\n * \n * @template T\n * @param {string} path\n * @returns {Promise<T>}\n */\nasync function loadJson(path) {\n\treturn JSON.parse(await fsPromises.readFile(path, 'utf-8'));\n}\n/**\n * \n * @template T\n * @param {string} path\n * @returns {Promise<T>}\n */\nfunction loadMjs(path) {\n\treturn import(path).then(v => {\n\t\tif ('default' in v) { return v.default; }\n\t\treturn v;\n\t});\n}\n/** @type {[string, <T>(path: string) => Promise<T | null
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../cli/load-configuration/index.mjs"],"sourcesContent":["import * as fsPromises from 'node:fs/promises';\nimport * as pathFn from 'node:path';\nimport * as yaml from 'yaml';\nimport * as toml from 'smol-toml';\n\n/**\n * \n * @template T\n * @param {string} path\n * @returns {Promise<T>}\n */\nasync function loadToml(path) {\n\t/** @type {any} */\n\tconst value = toml.parse(await fsPromises.readFile(path, 'utf-8'));\n\treturn value;\n}\n/**\n * \n * @template T\n * @param {string} path\n * @returns {Promise<T>}\n */\nasync function loadYaml(path) {\n\treturn yaml.parse(await fsPromises.readFile(path, 'utf-8'));\n}\n/**\n * \n * @template T\n * @param {string} path\n * @returns {Promise<T>}\n */\nasync function loadJson(path) {\n\treturn JSON.parse(await fsPromises.readFile(path, 'utf-8'));\n}\n/**\n * \n * @template T\n * @param {string} path\n * @returns {Promise<T>}\n */\nfunction loadMjs(path) {\n\treturn import(path).then(v => {\n\t\tif ('default' in v) { return v.default; }\n\t\treturn v;\n\t});\n}\n/** @type {[string, <T>(path: string) => Promise<T | null>, script?: boolean][]} */\nconst loaders = [\n\t['.mjs', loadMjs, true],\n\t['.mts', loadMjs, true],\n\t['.json', loadJson],\n\t['.yml', loadYaml],\n\t['.yaml', loadYaml],\n\t['.toml', loadToml],\n];\n\n/**\n * @template {object} T\n * @param {string} path \n * @param {boolean} [scriptOnly] \n * @returns {Promise<[path: string, loader: <T>(path: string) => Promise<T | null>] | null>}\n */\n\nasync function find(path, scriptOnly) {\n\tconst stat = await fsPromises.stat(path).catch(() => null);\n\tconst ext = stat?.isFile() ? pathFn.extname(path).toLowerCase() : '';\n\tlet loader = loaders.find(v => v[0] === ext);\n\tlet loaderFn = (!scriptOnly || loader?.[2]) && loader?.[1];\n\tif (loaderFn) { return [path, loaderFn]; }\n\tfor (const [e, l, script] of loaders) {\n\t\tif (scriptOnly && !script) { continue; }\n\t\tconst p = path + e;\n\t\tconst stat = await fsPromises.stat(p).catch(() => null);\n\t\tif (stat?.isFile()) {\n\t\t\treturn [p, l];\n\t\t}\n\t}\n\treturn null;\n}\n\n/**\n * @template {object} T\n * @param {string} path \n * @param {boolean} [scriptOnly] \n * @returns {Promise<[string, T | null] | null>}\n */\n\nexport async function loadConfigurationWithPath(path, scriptOnly) {\n\tconst result = await find(path, scriptOnly);\n\tif (!result) { return null; }\n\tconst [filepath, loader] = result;\n\treturn Promise.all([\n\t\tfsPromises.realpath(filepath),\n\t\tloader(filepath).then(v => typeof v === 'object' ? v : null),\n\t]);\n}\n\n/**\n * @template {object} T\n * @param {string} path \n * @param {boolean} [scriptOnly] \n * @returns {Promise<T | null>}\n */\n\nexport default async function loadConfiguration(path, scriptOnly) {\n\tconst result = await find(path, scriptOnly);\n\tif (!result) { return null; }\n\tconst [filepath, loader] = result;\n\treturn loader(filepath).then(v => typeof v === 'object' ? v : null);\n}\n"],"mappings":";;;;;;;;;;;;AAWA,eAAe,SAAS,MAAM;AAG7B,QADc,KAAK,MAAM,MAAM,WAAW,SAAS,MAAM,QAAQ,CAAC;;;;;;;;AASnE,eAAe,SAAS,MAAM;AAC7B,QAAO,KAAK,MAAM,MAAM,WAAW,SAAS,MAAM,QAAQ,CAAC;;;;;;;;AAQ5D,eAAe,SAAS,MAAM;AAC7B,QAAO,KAAK,MAAM,MAAM,WAAW,SAAS,MAAM,QAAQ,CAAC;;;;;;;;AAQ5D,SAAS,QAAQ,MAAM;AACtB,QAAO,OAAO,MAAM,MAAK,MAAK;AAC7B,MAAI,aAAa,EAAK,QAAO,EAAE;AAC/B,SAAO;GACN;;;AAGH,MAAM,UAAU;CACf;EAAC;EAAQ;EAAS;EAAK;CACvB;EAAC;EAAQ;EAAS;EAAK;CACvB,CAAC,SAAS,SAAS;CACnB,CAAC,QAAQ,SAAS;CAClB,CAAC,SAAS,SAAS;CACnB,CAAC,SAAS,SAAS;CACnB;;;;;;;AASD,eAAe,KAAK,MAAM,YAAY;CAErC,MAAM,OADO,MAAM,WAAW,KAAK,KAAK,CAAC,YAAY,KAAK,GACxC,QAAQ,GAAG,OAAO,QAAQ,KAAK,CAAC,aAAa,GAAG;CAClE,IAAI,SAAS,QAAQ,MAAK,MAAK,EAAE,OAAO,IAAI;CAC5C,IAAI,YAAY,CAAC,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