@yongdall/cli-parse 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 ADDED
@@ -0,0 +1,31 @@
1
+ //#region cli/cli-parse/index.d.mts
2
+ /**
3
+ *
4
+ * @param {string[]} argv
5
+ * @returns {Cli.Env}
6
+ */
7
+ declare function parse(argv: string[]): Cli.Env;
8
+ type Cli = {
9
+ module: string;
10
+ isPlugin: boolean;
11
+ modulePath: string;
12
+ exec: Cli.Command;
13
+ execPath: string;
14
+ description: string;
15
+ };
16
+ declare namespace Cli {
17
+ type Env = {
18
+ development: boolean;
19
+ help: boolean;
20
+ force: boolean;
21
+ plugins: Set<string>;
22
+ locale: string;
23
+ locales: string[];
24
+ parameters: string[];
25
+ options: Record<string, (string | null)[] | undefined>;
26
+ optionsList: [key: string, value: string | null][];
27
+ };
28
+ type Command = (env: Cli.Env) => void | PromiseLike<void>;
29
+ }
30
+ //#endregion
31
+ export { Cli, parse as default };
package/index.mjs ADDED
@@ -0,0 +1,93 @@
1
+ //#region cli/cli-parse/index.mjs
2
+ /**
3
+ * @typedef {object} Cli.Env
4
+ * @property {boolean} development
5
+ * @property {boolean} help
6
+ * @property {boolean} force
7
+ * @property {Set<string>} plugins
8
+ * @property {string} locale
9
+ * @property {string[]} locales
10
+ * @property {string[]} parameters
11
+ * @property {Record<string, (string | null)[] | undefined>} options
12
+ * @property {[key: string, value: string | null][]} optionsList
13
+ */
14
+ /**
15
+ * @typedef {(env: Cli.Env) => void | PromiseLike<void>} Cli.Command
16
+ */
17
+ /**
18
+ * @typedef {object} Cli
19
+ * @property {string} module
20
+ * @property {boolean} isPlugin
21
+ * @property {string} modulePath
22
+ * @property {Cli.Command} exec
23
+ * @property {string} execPath
24
+ * @property {string} description
25
+ */
26
+ function getLocales() {
27
+ const env = process.env;
28
+ return ((env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE || "").replace(/[.:@].*/, "").replace(/_/g, "-") || new Intl.DateTimeFormat().resolvedOptions().locale || "zh").split("-").filter(Boolean).map((_, i, list) => list.slice(0, i + 1).join("-")).reverse();
29
+ }
30
+ /**
31
+ *
32
+ * @param {string[]} argv
33
+ * @returns {Cli.Env}
34
+ */
35
+ function parse(argv) {
36
+ let development = false;
37
+ let help = false;
38
+ let force = false;
39
+ const locales = getLocales();
40
+ const locale = locales[0];
41
+ /** @type {Set<string>} */
42
+ const plugins = /* @__PURE__ */ new Set();
43
+ /** @type {[key: string, value: string | null][]} */
44
+ const optionsList = [];
45
+ /** @type {string[]} */
46
+ const parameters = [];
47
+ const index = argv.indexOf("--");
48
+ for (const s of index < 0 ? argv : argv.slice(0, index)) {
49
+ if (s[0] !== "-") {
50
+ parameters.push(s);
51
+ continue;
52
+ }
53
+ const index = s.indexOf("=");
54
+ const name = index < 0 ? s : s.slice(0, index);
55
+ const value = index < 0 ? null : s.slice(index + 1);
56
+ switch (name) {
57
+ case "-f":
58
+ case "--force":
59
+ force = true;
60
+ continue;
61
+ case "--development":
62
+ development = true;
63
+ continue;
64
+ case "--help":
65
+ help = true;
66
+ continue;
67
+ case "--plugins":
68
+ case "--plugin":
69
+ for (const plugin of value?.split(",") || []) {
70
+ if (!plugin) continue;
71
+ plugins.add(plugin);
72
+ }
73
+ continue;
74
+ default: optionsList.push([name, value]);
75
+ }
76
+ }
77
+ if (index >= 0) parameters.push(...argv.slice(index + 1));
78
+ return {
79
+ parameters,
80
+ development,
81
+ help,
82
+ plugins,
83
+ force,
84
+ locale,
85
+ locales: locales.map((v) => v.toLowerCase()),
86
+ optionsList,
87
+ options: Object.fromEntries(Object.entries(Object.groupBy(optionsList.filter(([k]) => k.slice(0, 2) === "--"), (v) => v[0])).map(([k, l]) => [k.slice(2), l?.map((v) => v[1])]))
88
+ };
89
+ }
90
+
91
+ //#endregion
92
+ export { parse as default };
93
+ //# sourceMappingURL=index.mjs.map
package/index.mjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../cli/cli-parse/index.mjs"],"sourcesContent":["/**\n * @typedef {object} Cli.Env\n * @property {boolean} development\n * @property {boolean} help\n * @property {boolean} force\n * @property {Set<string>} plugins\n * @property {string} locale\n * @property {string[]} locales\n * @property {string[]} parameters\n * @property {Record<string, (string | null)[] | undefined>} options\n * @property {[key: string, value: string | null][]} optionsList\n */\n/**\n * @typedef {(env: Cli.Env) => void | PromiseLike<void>} Cli.Command\n */\n/**\n * @typedef {object} Cli\n * @property {string} module\n * @property {boolean} isPlugin\n * @property {string} modulePath\n * @property {Cli.Command} exec\n * @property {string} execPath\n * @property {string} description\n */\n\nfunction getLocales() {\n\tconst env = process.env;\n\tconst locale =\n\t\t(env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE || '')\n\t\t\t.replace(/[.:@].*/, '').replace(/_/g, '-')\n\t\t|| new Intl.DateTimeFormat().resolvedOptions().locale\n\t\t|| 'zh';\n\n\treturn locale.split('-').filter(Boolean)\n\t\t.map((_, i, list) => list.slice(0, i + 1).join('-'))\n\t\t.reverse();\n}\n\n\n\n\n\n/**\n * \n * @param {string[]} argv \n * @returns {Cli.Env}\n */\nexport default function parse(argv) {\n\tlet development = false;\n\tlet help = false;\n\tlet force = false;\n\tconst locales = getLocales();\n\tconst locale = locales[0];\n\t/** @type {Set<string>} */\n\tconst plugins = new Set();\n\t/** @type {[key: string, value: string | null][]} */\n\tconst optionsList = [];\n\t/** @type {string[]} */\n\tconst parameters = [];\n\tconst index = argv.indexOf('--');\n\tfor (const s of index < 0 ? argv : argv.slice(0, index)) {\n\t\tif (s[0] !== '-') {\n\t\t\tparameters.push(s);\n\t\t\tcontinue;\n\t\t}\n\t\tconst index = s.indexOf('=');\n\t\tconst name = index < 0 ? s : s.slice(0, index);\n\t\tconst value = index < 0 ? null : s.slice(index + 1);\n\t\tswitch (name) {\n\t\t\tcase '-f':\n\t\t\tcase '--force':\n\t\t\t\tforce = true;\n\t\t\t\tcontinue;\n\t\t\tcase '--development':\n\t\t\t\tdevelopment = true;\n\t\t\t\tcontinue;\n\t\t\tcase '--help':\n\t\t\t\thelp = true;\n\t\t\t\tcontinue;\n\t\t\tcase '--plugins':\n\t\t\tcase '--plugin':\n\t\t\t\tfor (const plugin of value?.split(',') || []) {\n\t\t\t\t\tif (!plugin) { continue; }\n\t\t\t\t\tplugins.add(plugin);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\tdefault:\n\t\t\t\toptionsList.push([name, value]);\n\t\t}\n\t}\n\tif (index >= 0) {\n\t\tparameters.push(...argv.slice(index + 1));\n\t}\n\treturn {\n\t\tparameters,\n\t\tdevelopment,\n\t\thelp,\n\t\tplugins,\n\t\tforce, locale, locales: locales.map(v => v.toLowerCase()),\n\t\toptionsList,\n\t\toptions: Object.fromEntries(\n\t\t\tObject.entries(Object.groupBy(\n\t\t\t\toptionsList.filter(([k]) => k.slice(0, 2) === '--'),\n\t\t\t\tv => v[0],\n\t\t\t)).map(([k, l]) => [k.slice(2), l?.map(v => v[1])])\n\t\t)\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAS,aAAa;CACrB,MAAM,MAAM,QAAQ;AAOpB,UALE,IAAI,UAAU,IAAI,eAAe,IAAI,QAAQ,IAAI,YAAY,IAC5D,QAAQ,WAAW,GAAG,CAAC,QAAQ,MAAM,IAAI,IACxC,IAAI,KAAK,gBAAgB,CAAC,iBAAiB,CAAC,UAC5C,MAEU,MAAM,IAAI,CAAC,OAAO,QAAQ,CACtC,KAAK,GAAG,GAAG,SAAS,KAAK,MAAM,GAAG,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC,CACnD,SAAS;;;;;;;AAYZ,SAAwB,MAAM,MAAM;CACnC,IAAI,cAAc;CAClB,IAAI,OAAO;CACX,IAAI,QAAQ;CACZ,MAAM,UAAU,YAAY;CAC5B,MAAM,SAAS,QAAQ;;CAEvB,MAAM,0BAAU,IAAI,KAAK;;CAEzB,MAAM,cAA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