@yongdall/load-configuration 0.1.0 → 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 CHANGED
@@ -2,8 +2,16 @@
2
2
  /**
3
3
  * @template {object} T
4
4
  * @param {string} path
5
+ * @param {boolean} [scriptOnly]
6
+ * @returns {Promise<[string, T | null] | null>}
7
+ */
8
+ declare function loadConfigurationWithPath<T extends object>(path: string, scriptOnly?: boolean): Promise<[string, T | null] | null>;
9
+ /**
10
+ * @template {object} T
11
+ * @param {string} path
12
+ * @param {boolean} [scriptOnly]
5
13
  * @returns {Promise<T | null>}
6
14
  */
7
- 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>;
8
16
  //#endregion
9
- export { loadConfiguration as default };
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
- [".mjs", loadMjs],
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,23 +63,46 @@ const loaders = [
54
63
  /**
55
64
  * @template {object} T
56
65
  * @param {string} path
57
- * @returns {Promise<T | null>}
66
+ * @param {boolean} [scriptOnly]
67
+ * @returns {Promise<[path: string, loader: <T>(path: string) => Promise<T | null>] | null>}
58
68
  */
59
- async function loadConfiguration(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)?.[1];
62
- if (!loader) for (const [e, l] of loaders) {
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;
63
76
  const p = path + e;
64
- if ((await fsPromises.stat(p).catch(() => null))?.isFile()) {
65
- loader = l;
66
- path = p;
67
- break;
68
- }
77
+ if ((await fsPromises.stat(p).catch(() => null))?.isFile()) return [p, l];
69
78
  }
70
- if (!loader) return null;
71
- return loader(path).then((v) => typeof v === "object" ? v : null);
79
+ return null;
80
+ }
81
+ /**
82
+ * @template {object} T
83
+ * @param {string} path
84
+ * @param {boolean} [scriptOnly]
85
+ * @returns {Promise<[string, T | null] | null>}
86
+ */
87
+ async function loadConfigurationWithPath(path, scriptOnly) {
88
+ const result = await find(path, scriptOnly);
89
+ if (!result) return null;
90
+ const [filepath, loader] = result;
91
+ return Promise.all([fsPromises.realpath(filepath), loader(filepath).then((v) => typeof v === "object" ? v : null)]);
92
+ }
93
+ /**
94
+ * @template {object} T
95
+ * @param {string} path
96
+ * @param {boolean} [scriptOnly]
97
+ * @returns {Promise<T | null>}
98
+ */
99
+ async function loadConfiguration(path, scriptOnly) {
100
+ const result = await find(path, scriptOnly);
101
+ if (!result) return null;
102
+ const [filepath, loader] = result;
103
+ return loader(filepath).then((v) => typeof v === "object" ? v : null);
72
104
  }
73
105
 
74
106
  //#endregion
75
- export { loadConfiguration as default };
107
+ export { loadConfiguration as default, loadConfigurationWithPath };
76
108
  //# sourceMappingURL=index.mjs.map
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>][]} */\nconst loaders = [\n\t['.mjs', loadMjs],\n\t['.json', loadJson],\n\t['.yml', loadYaml],\n\t['.yaml', loadYaml],\n\t['.toml', loadToml],\n];\n/**\n * @template {object} T\n * @param {string} path \n * @returns {Promise<T | null>}\n */\n\nexport default async function loadConfiguration(path) {\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)?.[1];\n\tif (!loader) {\n\t\tfor(const [e, l] of loaders) {\n\t\t\tconst p = path + e;\n\t\t\tconst stat = await fsPromises.stat(p).catch(() => null);\n\t\t\tif (stat?.isFile()) {\n\t\t\t\tloader = l;\n\t\t\t\tpath = p;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tif (!loader) { return null }\n\treturn loader(path).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,CAAC,QAAQ,QAAQ;CACjB,CAAC,SAAS,SAAS;CACnB,CAAC,QAAQ,SAAS;CAClB,CAAC,SAAS,SAAS;CACnB,CAAC,SAAS,SAAS;CACnB;;;;;;AAOD,eAA8B,kBAAkB,MAAM;CAErD,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,GAAG;AAC/C,KAAI,CAAC,OACJ,MAAI,MAAM,CAAC,GAAG,MAAM,SAAS;EAC5B,MAAM,IAAI,OAAO;AAEjB,OADa,MAAM,WAAW,KAAK,EAAE,CAAC,YAAY,KAAK,GAC7C,QAAQ,EAAE;AACnB,YAAS;AACT,UAAO;AACP;;;AAIH,KAAI,CAAC,OAAU,QAAO;AACtB,QAAO,OAAO,KAAK,CAAC,MAAK,MAAK,OAAO,MAAM,WAAW,IAAI,KAAK"}
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,cAAc,SAAS,OAAO,SAAS;AACxD,KAAI,SAAY,QAAO,CAAC,MAAM,SAAS;AACvC,MAAK,MAAM,CAAC,GAAG,GAAG,WAAW,SAAS;AACrC,MAAI,cAAc,CAAC,OAAU;EAC7B,MAAM,IAAI,OAAO;AAEjB,OADa,MAAM,WAAW,KAAK,EAAE,CAAC,YAAY,KAAK,GAC7C,QAAQ,CACjB,QAAO,CAAC,GAAG,EAAE;;AAGf,QAAO;;;;;;;;AAUR,eAAsB,0BAA0B,MAAM,YAAY;CACjE,MAAM,SAAS,MAAM,KAAK,MAAM,WAAW;AAC3C,KAAI,CAAC,OAAU,QAAO;CACtB,MAAM,CAAC,UAAU,UAAU;AAC3B,QAAO,QAAQ,IAAI,CAClB,WAAW,SAAS,SAAS,EAC7B,OAAO,SAAS,CAAC,MAAK,MAAK,OAAO,MAAM,WAAW,IAAI,KAAK,CAC5D,CAAC;;;;;;;;AAUH,eAA8B,kBAAkB,MAAM,YAAY;CACjE,MAAM,SAAS,MAAM,KAAK,MAAM,WAAW;AAC3C,KAAI,CAAC,OAAU,QAAO;CACtB,MAAM,CAAC,UAAU,UAAU;AAC3B,QAAO,OAAO,SAAS,CAAC,MAAK,MAAK,OAAO,MAAM,WAAW,IAAI,KAAK"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yongdall/load-configuration",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "load-configuration.yongdall": "./bin.mjs"