@yongdall/load-configuration 0.1.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/bin.mjs ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env node
2
+ import loadConfiguration from "#index";
3
+
4
+ //#region cli/load-configuration/bin.mjs
5
+ const path = process.argv[3];
6
+ if (path) {
7
+ const config = await loadConfiguration(path);
8
+ console.log(config);
9
+ }
10
+
11
+ //#endregion
12
+ //# sourceMappingURL=bin.mjs.map
package/bin.mjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin.mjs","names":[],"sources":["../../cli/load-configuration/bin.mjs"],"sourcesContent":["#!/usr/bin/env node\n\nimport loadConfiguration from '#index';\n\nconst path = process.argv[3];\n\nif (path) {\n\tconst config = await loadConfiguration(path);\n\tconsole.log(config);\n}\n"],"mappings":";;;;AAIA,MAAM,OAAO,QAAQ,KAAK;AAE1B,IAAI,MAAM;CACT,MAAM,SAAS,MAAM,kBAAkB,KAAK;AAC5C,SAAQ,IAAI,OAAO"}
package/index.d.mts ADDED
@@ -0,0 +1,9 @@
1
+ //#region cli/load-configuration/index.d.mts
2
+ /**
3
+ * @template {object} T
4
+ * @param {string} path
5
+ * @returns {Promise<T | null>}
6
+ */
7
+ declare function loadConfiguration<T extends object>(path: string): Promise<T | null>;
8
+ //#endregion
9
+ export { loadConfiguration as default };
package/index.mjs ADDED
@@ -0,0 +1,76 @@
1
+ import * as fsPromises from "node:fs/promises";
2
+ import * as pathFn from "node:path";
3
+ import * as yaml from "yaml";
4
+ import * as toml from "smol-toml";
5
+
6
+ //#region cli/load-configuration/index.mjs
7
+ /**
8
+ *
9
+ * @template T
10
+ * @param {string} path
11
+ * @returns {Promise<T>}
12
+ */
13
+ async function loadToml(path) {
14
+ return toml.parse(await fsPromises.readFile(path, "utf-8"));
15
+ }
16
+ /**
17
+ *
18
+ * @template T
19
+ * @param {string} path
20
+ * @returns {Promise<T>}
21
+ */
22
+ async function loadYaml(path) {
23
+ return yaml.parse(await fsPromises.readFile(path, "utf-8"));
24
+ }
25
+ /**
26
+ *
27
+ * @template T
28
+ * @param {string} path
29
+ * @returns {Promise<T>}
30
+ */
31
+ async function loadJson(path) {
32
+ return JSON.parse(await fsPromises.readFile(path, "utf-8"));
33
+ }
34
+ /**
35
+ *
36
+ * @template T
37
+ * @param {string} path
38
+ * @returns {Promise<T>}
39
+ */
40
+ function loadMjs(path) {
41
+ return import(path).then((v) => {
42
+ if ("default" in v) return v.default;
43
+ return v;
44
+ });
45
+ }
46
+ /** @type {[string, <T>(path: string) => Promise<T | null>][]} */
47
+ const loaders = [
48
+ [".mjs", loadMjs],
49
+ [".json", loadJson],
50
+ [".yml", loadYaml],
51
+ [".yaml", loadYaml],
52
+ [".toml", loadToml]
53
+ ];
54
+ /**
55
+ * @template {object} T
56
+ * @param {string} path
57
+ * @returns {Promise<T | null>}
58
+ */
59
+ async function loadConfiguration(path) {
60
+ 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) {
63
+ const p = path + e;
64
+ if ((await fsPromises.stat(p).catch(() => null))?.isFile()) {
65
+ loader = l;
66
+ path = p;
67
+ break;
68
+ }
69
+ }
70
+ if (!loader) return null;
71
+ return loader(path).then((v) => typeof v === "object" ? v : null);
72
+ }
73
+
74
+ //#endregion
75
+ export { loadConfiguration as default };
76
+ //# sourceMappingURL=index.mjs.map
package/index.mjs.map ADDED
@@ -0,0 +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"}
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@yongdall/load-configuration",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "bin": {
6
+ "load-configuration.yongdall": "./bin.mjs"
7
+ },
8
+ "main": "./index.mjs",
9
+ "imports": {
10
+ "#index": "./index.mjs"
11
+ },
12
+ "dependencies": {
13
+ "smol-toml": "^1.6.0",
14
+ "yaml": "^2.5.0"
15
+ },
16
+ "exports": {
17
+ ".": "./index.mjs"
18
+ }
19
+ }