@yongdall/load-configuration 0.3.0 → 0.5.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 +4 -4
- package/index.mjs +14 -10
- package/index.mjs.map +1 -1
- package/package.json +1 -1
package/index.d.mts
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* @template {object} T
|
|
4
4
|
* @param {string} path
|
|
5
|
-
* @param {
|
|
5
|
+
* @param {'script' | 'data'} [type]
|
|
6
6
|
* @returns {Promise<[string, T | null] | null>}
|
|
7
7
|
*/
|
|
8
|
-
declare function loadConfigurationWithPath<T extends object>(path: string,
|
|
8
|
+
declare function loadConfigurationWithPath<T extends object>(path: string, type?: "script" | "data"): Promise<[string, T | null] | null>;
|
|
9
9
|
/**
|
|
10
10
|
* @template {object} T
|
|
11
11
|
* @param {string} path
|
|
12
|
-
* @param {
|
|
12
|
+
* @param {'script' | 'data'} [type]
|
|
13
13
|
* @returns {Promise<T | null>}
|
|
14
14
|
*/
|
|
15
|
-
declare function loadConfiguration<T extends object>(path: string,
|
|
15
|
+
declare function loadConfiguration<T extends object>(path: string, type?: "script" | "data"): Promise<T | null>;
|
|
16
16
|
//#endregion
|
|
17
17
|
export { loadConfiguration as default, loadConfigurationWithPath };
|
package/index.mjs
CHANGED
|
@@ -60,19 +60,23 @@ const loaders = [
|
|
|
60
60
|
[".yaml", loadYaml],
|
|
61
61
|
[".toml", loadToml]
|
|
62
62
|
];
|
|
63
|
+
const scriptExts = new Set(loaders.filter((v) => v[2]).map((v) => v[0]));
|
|
64
|
+
const dataExts = new Set(loaders.filter((v) => !v[2]).map((v) => v[0]));
|
|
65
|
+
const allExts = new Set(loaders.map((v) => v[0]));
|
|
63
66
|
/**
|
|
64
67
|
* @template {object} T
|
|
65
68
|
* @param {string} path
|
|
66
|
-
* @param {
|
|
69
|
+
* @param {'script' | 'data'} [type]
|
|
67
70
|
* @returns {Promise<[path: string, loader: <T>(path: string) => Promise<T | null>] | null>}
|
|
68
71
|
*/
|
|
69
|
-
async function find(path,
|
|
72
|
+
async function find(path, type) {
|
|
73
|
+
const exts = type === "script" ? scriptExts : type === "data" ? dataExts : allExts;
|
|
70
74
|
const ext = (await fsPromises.stat(path).catch(() => null))?.isFile() ? pathFn.extname(path).toLowerCase() : "";
|
|
71
75
|
let loader = loaders.find((v) => v[0] === ext);
|
|
72
|
-
let loaderFn =
|
|
76
|
+
let loaderFn = loader && exts.has(loader[0]) && loader[1];
|
|
73
77
|
if (loaderFn) return [path, loaderFn];
|
|
74
78
|
for (const [e, l, script] of loaders) {
|
|
75
|
-
if (
|
|
79
|
+
if (!exts.has(e)) continue;
|
|
76
80
|
const p = path + e;
|
|
77
81
|
if ((await fsPromises.stat(p).catch(() => null))?.isFile()) return [p, l];
|
|
78
82
|
}
|
|
@@ -81,11 +85,11 @@ async function find(path, scriptOnly) {
|
|
|
81
85
|
/**
|
|
82
86
|
* @template {object} T
|
|
83
87
|
* @param {string} path
|
|
84
|
-
* @param {
|
|
88
|
+
* @param {'script' | 'data'} [type]
|
|
85
89
|
* @returns {Promise<[string, T | null] | null>}
|
|
86
90
|
*/
|
|
87
|
-
async function loadConfigurationWithPath(path,
|
|
88
|
-
const result = await find(path,
|
|
91
|
+
async function loadConfigurationWithPath(path, type) {
|
|
92
|
+
const result = await find(path, type);
|
|
89
93
|
if (!result) return null;
|
|
90
94
|
const [filepath, loader] = result;
|
|
91
95
|
return Promise.all([fsPromises.realpath(filepath), loader(filepath).then((v) => typeof v === "object" ? v : null)]);
|
|
@@ -93,11 +97,11 @@ async function loadConfigurationWithPath(path, scriptOnly) {
|
|
|
93
97
|
/**
|
|
94
98
|
* @template {object} T
|
|
95
99
|
* @param {string} path
|
|
96
|
-
* @param {
|
|
100
|
+
* @param {'script' | 'data'} [type]
|
|
97
101
|
* @returns {Promise<T | null>}
|
|
98
102
|
*/
|
|
99
|
-
async function loadConfiguration(path,
|
|
100
|
-
const result = await find(path,
|
|
103
|
+
async function loadConfiguration(path, type) {
|
|
104
|
+
const result = await find(path, type);
|
|
101
105
|
if (!result) return null;
|
|
102
106
|
const [filepath, loader] = result;
|
|
103
107
|
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>, 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 {
|
|
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\nconst scriptExts = new Set(loaders.filter(v => v[2]).map(v => v[0]));\nconst dataExts = new Set(loaders.filter(v => !v[2]).map(v => v[0]));\nconst allExts = new Set(loaders.map(v => v[0]));\n/**\n * @template {object} T\n * @param {string} path \n * @param {'script' | 'data'} [type] \n * @returns {Promise<[path: string, loader: <T>(path: string) => Promise<T | null>] | null>}\n */\nasync function find(path, type) {\n\tconst exts = type === 'script' ? scriptExts : type === 'data' ? dataExts : allExts;\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 = loader && (exts.has(loader[0])) && loader[1];\n\tif (loaderFn) { return [path, loaderFn]; }\n\tfor (const [e, l, script] of loaders) {\n\t\tif (!exts.has(e)) { 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 {'script' | 'data'} [type] \n * @returns {Promise<[string, T | null] | null>}\n */\nexport async function loadConfigurationWithPath(path, type) {\n\tconst result = await find(path, type);\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 {'script' | 'data'} [type] \n * @returns {Promise<T | null>}\n */\nexport default async function loadConfiguration(path, type) {\n\tconst result = await find(path, type);\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;AAED,MAAM,aAAa,IAAI,IAAI,QAAQ,QAAO,MAAK,EAAE,GAAG,CAAC,KAAI,MAAK,EAAE,GAAG,CAAC;AACpE,MAAM,WAAW,IAAI,IAAI,QAAQ,QAAO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