@shell-shock/core 0.5.1 → 0.7.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/README.md +1 -1
- package/dist/components/docs.d.cts +5 -5
- package/dist/components/docs.d.mts +5 -5
- package/dist/components/index.cjs +3 -1
- package/dist/components/index.d.cts +2 -2
- package/dist/components/index.d.mts +2 -2
- package/dist/components/index.mjs +2 -2
- package/dist/components/options-parser-logic.cjs +127 -31
- package/dist/components/options-parser-logic.cjs.map +1 -1
- package/dist/components/options-parser-logic.d.cts +37 -10
- package/dist/components/options-parser-logic.d.cts.map +1 -1
- package/dist/components/options-parser-logic.d.mts +31 -4
- package/dist/components/options-parser-logic.d.mts.map +1 -1
- package/dist/components/options-parser-logic.mjs +126 -32
- package/dist/components/options-parser-logic.mjs.map +1 -1
- package/dist/components/usage.cjs +6 -5
- package/dist/components/usage.cjs.map +1 -1
- package/dist/components/usage.d.cts +2 -4
- package/dist/components/usage.d.cts.map +1 -1
- package/dist/components/usage.d.mts +2 -4
- package/dist/components/usage.d.mts.map +1 -1
- package/dist/components/usage.mjs +7 -6
- package/dist/components/usage.mjs.map +1 -1
- package/dist/helpers/persistence.cjs +48 -3
- package/dist/helpers/persistence.cjs.map +1 -1
- package/dist/helpers/persistence.mjs +48 -3
- package/dist/helpers/persistence.mjs.map +1 -1
- package/dist/helpers/resolve-command.cjs +103 -53
- package/dist/helpers/resolve-command.cjs.map +1 -1
- package/dist/helpers/resolve-command.mjs +105 -55
- package/dist/helpers/resolve-command.mjs.map +1 -1
- package/dist/helpers/validations.cjs +20 -20
- package/dist/helpers/validations.cjs.map +1 -1
- package/dist/helpers/validations.mjs +21 -21
- package/dist/helpers/validations.mjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/plugin-utils/context-helpers.cjs +105 -4
- package/dist/plugin-utils/context-helpers.cjs.map +1 -1
- package/dist/plugin-utils/context-helpers.d.cts +89 -3
- package/dist/plugin-utils/context-helpers.d.cts.map +1 -1
- package/dist/plugin-utils/context-helpers.d.mts +89 -3
- package/dist/plugin-utils/context-helpers.d.mts.map +1 -1
- package/dist/plugin-utils/context-helpers.mjs +99 -3
- package/dist/plugin-utils/context-helpers.mjs.map +1 -1
- package/dist/plugin-utils/get-command-tree.cjs +1 -1
- package/dist/plugin-utils/get-command-tree.cjs.map +1 -1
- package/dist/plugin-utils/get-command-tree.mjs +2 -2
- package/dist/plugin-utils/get-command-tree.mjs.map +1 -1
- package/dist/plugin-utils/index.cjs +7 -2
- package/dist/plugin-utils/index.d.cts +2 -2
- package/dist/plugin-utils/index.d.mts +2 -2
- package/dist/plugin-utils/index.mjs +2 -2
- package/dist/plugin.cjs +1 -1
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts.map +1 -1
- package/dist/plugin.d.mts.map +1 -1
- package/dist/plugin.mjs +2 -2
- package/dist/plugin.mjs.map +1 -1
- package/dist/types/command.d.cts +291 -9
- package/dist/types/command.d.cts.map +1 -1
- package/dist/types/command.d.mts +291 -9
- package/dist/types/command.d.mts.map +1 -1
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.mts +2 -2
- package/package.json +3 -3
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getDynamicPathSegmentName, isDynamicPathSegment } from "../plugin-utils/context-helpers.mjs";
|
|
2
2
|
import { createComponent, memo } from "@alloy-js/core/jsx-runtime";
|
|
3
3
|
import { Match, Switch, code } from "@alloy-js/core";
|
|
4
|
+
import { ReflectionKind } from "@powerlines/deepkit/vendor/type";
|
|
4
5
|
import { snakeCase } from "@stryke/string-format/snake-case";
|
|
5
6
|
|
|
6
7
|
//#region src/components/usage.tsx
|
|
@@ -13,6 +14,10 @@ function Usage(props) {
|
|
|
13
14
|
code`$ `,
|
|
14
15
|
createComponent(Switch, { get children() {
|
|
15
16
|
return [
|
|
17
|
+
createComponent(Match, {
|
|
18
|
+
when: packageManager === "npm",
|
|
19
|
+
children: `npx `
|
|
20
|
+
}),
|
|
16
21
|
createComponent(Match, {
|
|
17
22
|
when: packageManager === "yarn",
|
|
18
23
|
children: `yarn exec `
|
|
@@ -24,14 +29,10 @@ function Usage(props) {
|
|
|
24
29
|
createComponent(Match, {
|
|
25
30
|
when: packageManager === "bun",
|
|
26
31
|
children: `bun x `
|
|
27
|
-
}),
|
|
28
|
-
createComponent(Match, {
|
|
29
|
-
"else": true,
|
|
30
|
-
children: `npx `
|
|
31
32
|
})
|
|
32
33
|
];
|
|
33
34
|
} }),
|
|
34
|
-
memo(() => code`${bin} ${command.path.segments.map((segment) =>
|
|
35
|
+
memo(() => code`${bin}${command.path.segments.length > 0 ? ` ${command.path.segments.map((segment) => isDynamicPathSegment(segment) ? `[${snakeCase(command.path.dynamics[segment]?.name || getDynamicPathSegmentName(segment))}${command.path.dynamics[segment]?.variadic ? "..." : ""}]` : segment).join(" ")}` : ""}${Object.values(command.children).length > 0 ? " [commands]" : ""}${command.params.length > 0 ? ` ${command.params.map((param) => `<${snakeCase(param.name)}${(param.kind === ReflectionKind.string || param.kind === ReflectionKind.number) && param.variadic ? "..." : ""}>`).join(" ")}` : ""} [options]`)
|
|
35
36
|
];
|
|
36
37
|
}
|
|
37
38
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usage.mjs","names":["code","Match","Switch","snakeCase","
|
|
1
|
+
{"version":3,"file":"usage.mjs","names":["code","Match","Switch","ReflectionKind","snakeCase","getDynamicPathSegmentName","isDynamicPathSegment","Usage","props","command","bin","packageManager","_$createComponent","children","when","_$memo","path","segments","length","map","segment","dynamics","name","variadic","join","Object","values","params","param","kind","string","number"],"sources":["../../src/components/usage.tsx"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Shell Shock\n\n This code was released as part of the Shell Shock project. Shell Shock\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/shell-shock.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/shell-shock\n Documentation: https://docs.stormsoftware.com/projects/shell-shock\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { code, Match, Switch } from \"@alloy-js/core\";\nimport { ReflectionKind } from \"@powerlines/deepkit/vendor/type\";\nimport { snakeCase } from \"@stryke/string-format/snake-case\";\nimport {\n getDynamicPathSegmentName,\n isDynamicPathSegment\n} from \"../plugin-utils/context-helpers\";\nimport type { CommandTree } from \"../types/command\";\n\nexport interface UsageProps {\n /**\n * The command to generate help for.\n */\n command: CommandTree;\n\n /**\n * The package manager to generate the usage example for.\n *\n * @remarks\n * If not specified, examples for all supported package managers will be generated.\n */\n packageManager?: \"npm\" | \"yarn\" | \"pnpm\" | \"bun\";\n\n /**\n * The bin name to use in the usage display.\n */\n bin: string;\n}\n\n/**\n * A component that generates the usage display for a command.\n */\nexport function Usage(props: UsageProps) {\n const { command, bin, packageManager } = props;\n\n return (\n <>\n {code`$ `}\n <Switch>\n <Match when={packageManager === \"npm\"}>{`npx `}</Match>\n <Match when={packageManager === \"yarn\"}>{`yarn exec `}</Match>\n <Match when={packageManager === \"pnpm\"}>{`pnpm exec `}</Match>\n <Match when={packageManager === \"bun\"}>{`bun x `}</Match>\n </Switch>\n {code`${bin}${\n command.path.segments.length > 0\n ? ` ${command.path.segments\n .map(segment =>\n isDynamicPathSegment(segment)\n ? `[${snakeCase(\n command.path.dynamics[segment]?.name ||\n getDynamicPathSegmentName(segment)\n )}${command.path.dynamics[segment]?.variadic ? \"...\" : \"\"}]`\n : segment\n )\n .join(\" \")}`\n : \"\"\n }${Object.values(command.children).length > 0 ? \" [commands]\" : \"\"}${\n command.params.length > 0\n ? ` ${command.params\n .map(\n param =>\n `<${snakeCase(param.name)}${\n (param.kind === ReflectionKind.string ||\n param.kind === ReflectionKind.number) &&\n param.variadic\n ? \"...\"\n : \"\"\n }>`\n )\n .join(\" \")}`\n : \"\"\n } [options]`}\n </>\n );\n}\n"],"mappings":";;;;;;;;;;AAkDA,SAAgBO,MAAMC,OAAmB;CACvC,MAAM,EAAEC,SAASC,KAAKC,mBAAmBH;AAEzC,QAAA;EAEKR,IAAI;EAAIY,gBACRV,QAAM,EAAA,IAAAW,WAAA;AAAA,UAAA;IAAAD,gBACJX,OAAK;KAACa,MAAMH,mBAAmB;KAAKE,UAAG;KAAM,CAAA;IAAAD,gBAC7CX,OAAK;KAACa,MAAMH,mBAAmB;KAAME,UAAG;KAAY,CAAA;IAAAD,gBACpDX,OAAK;KAACa,MAAMH,mBAAmB;KAAME,UAAG;KAAY,CAAA;IAAAD,gBACpDX,OAAK;KAACa,MAAMH,mBAAmB;KAAKE,UAAG;KAAQ,CAAA;IAAA;KAAA,CAAA;EAAAE,WAEjDf,IAAI,GAAGU,MACND,QAAQO,KAAKC,SAASC,SAAS,IAC3B,IAAIT,QAAQO,KAAKC,SACdE,KAAIC,YACHd,qBAAqBc,QAAQ,GACzB,IAAIhB,UACFK,QAAQO,KAAKK,SAASD,UAAUE,QAC9BjB,0BAA0Be,QAC9B,CAAC,GAAGX,QAAQO,KAAKK,SAASD,UAAUG,WAAW,QAAQ,GAAE,KACzDH,QACL,CACAI,KAAK,IAAI,KACZ,KACHC,OAAOC,OAAOjB,QAAQI,SAAS,CAACK,SAAS,IAAI,gBAAgB,KAC9DT,QAAQkB,OAAOT,SAAS,IACpB,IAAIT,QAAQkB,OACTR,KACCS,UACE,IAAIxB,UAAUwB,MAAMN,KAAK,IACtBM,MAAMC,SAAS1B,eAAe2B,UAC7BF,MAAMC,SAAS1B,eAAe4B,WAChCH,MAAML,WACF,QACA,GAAE,GAEX,CACAC,KAAK,IAAI,KACZ,GAAE,YACI;EAAA"}
|
|
@@ -1,16 +1,31 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
2
|
let __stryke_path_join_paths = require("@stryke/path/join-paths");
|
|
3
|
+
let __powerlines_deepkit_vendor_type = require("@powerlines/deepkit/vendor/type");
|
|
4
|
+
let __stryke_helpers_omit = require("@stryke/helpers/omit");
|
|
3
5
|
|
|
4
6
|
//#region src/helpers/persistence.ts
|
|
5
7
|
function getCommandsPersistencePath(context) {
|
|
6
|
-
return (0, __stryke_path_join_paths.joinPaths)(context.dataPath,
|
|
8
|
+
return (0, __stryke_path_join_paths.joinPaths)(context.dataPath, "reflections", "commands.json");
|
|
7
9
|
}
|
|
8
10
|
function serializedCommandTree(commands) {
|
|
9
11
|
const serialize = (node, parent = null) => {
|
|
10
12
|
const serializedNode = {
|
|
11
13
|
...node,
|
|
14
|
+
options: Object.entries(node.options).reduce((ret, [key, option]) => {
|
|
15
|
+
ret[key] = { ...(0, __stryke_helpers_omit.omit)(option, ["reflection"]) };
|
|
16
|
+
return ret;
|
|
17
|
+
}, {}),
|
|
18
|
+
path: {
|
|
19
|
+
...node.path,
|
|
20
|
+
dynamics: Object.entries(node.path.dynamics).reduce((ret, [key, dynamic]) => {
|
|
21
|
+
ret[key] = { ...(0, __stryke_helpers_omit.omit)(dynamic, ["reflection"]) };
|
|
22
|
+
return ret;
|
|
23
|
+
}, {})
|
|
24
|
+
},
|
|
25
|
+
params: node.params.map((param) => ({ ...(0, __stryke_helpers_omit.omit)(param, ["reflection"]) })),
|
|
12
26
|
parent,
|
|
13
|
-
children: {}
|
|
27
|
+
children: {},
|
|
28
|
+
reflection: node.reflection ? (0, __powerlines_deepkit_vendor_type.serializeType)(node.reflection.type) : void 0
|
|
14
29
|
};
|
|
15
30
|
for (const [key, child] of Object.entries(node.children || {})) serializedNode.children[key] = serialize(child, node.id);
|
|
16
31
|
return serializedNode;
|
|
@@ -21,10 +36,40 @@ function serializedCommandTree(commands) {
|
|
|
21
36
|
}
|
|
22
37
|
function deserializeCommandTree(serializedCommands) {
|
|
23
38
|
const deserialize = (node, parent = null) => {
|
|
39
|
+
const type = (0, __powerlines_deepkit_vendor_type.deserializeType)(node.reflection);
|
|
40
|
+
if (type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.function) throw new Error(`Failed to deserialize persisted data - invalid command handler reflection type for command ${node.id}`);
|
|
41
|
+
const reflection = new __powerlines_deepkit_vendor_type.ReflectionFunction(type);
|
|
42
|
+
let optionsReflection;
|
|
43
|
+
if (reflection.getParameters().length > 0 && reflection.getParameters()[0]) {
|
|
44
|
+
const optionsType = reflection.getParameters()[0].type;
|
|
45
|
+
if (optionsType.kind === __powerlines_deepkit_vendor_type.ReflectionKind.objectLiteral) optionsReflection = (0, __powerlines_deepkit_vendor_type.resolveClassType)(optionsType);
|
|
46
|
+
}
|
|
24
47
|
const deserializedNode = {
|
|
25
48
|
...node,
|
|
49
|
+
options: optionsReflection ? Object.entries(node.options).reduce((ret, [key, option]) => {
|
|
50
|
+
ret[key] = {
|
|
51
|
+
...option,
|
|
52
|
+
reflection: optionsReflection.getProperty(option.name)
|
|
53
|
+
};
|
|
54
|
+
return ret;
|
|
55
|
+
}, {}) : {},
|
|
56
|
+
path: {
|
|
57
|
+
...node.path,
|
|
58
|
+
dynamics: Object.entries(node.path.dynamics).reduce((ret, [key, dynamic]) => {
|
|
59
|
+
ret[key] = {
|
|
60
|
+
...dynamic,
|
|
61
|
+
reflection: reflection.getParameter(dynamic.name)
|
|
62
|
+
};
|
|
63
|
+
return ret;
|
|
64
|
+
}, {})
|
|
65
|
+
},
|
|
66
|
+
params: node.params.map((param) => ({
|
|
67
|
+
...param,
|
|
68
|
+
reflection: reflection.getParameter(param.name)
|
|
69
|
+
})),
|
|
26
70
|
parent,
|
|
27
|
-
children: {}
|
|
71
|
+
children: {},
|
|
72
|
+
reflection
|
|
28
73
|
};
|
|
29
74
|
for (const [key, child] of Object.entries(node.children || {})) deserializedNode.children[key] = deserialize(child, deserializedNode);
|
|
30
75
|
return deserializedNode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persistence.cjs","names":["joinPaths","getCommandsPersistencePath","context","dataPath","serializedCommandTree","commands","serialize","node","parent","serializedNode","
|
|
1
|
+
{"version":3,"file":"persistence.cjs","names":["deserializeType","ReflectionFunction","ReflectionKind","resolveClassType","serializeType","omit","joinPaths","getCommandsPersistencePath","context","dataPath","serializedCommandTree","commands","serialize","node","parent","serializedNode","options","Object","entries","reduce","ret","key","option","path","dynamics","dynamic","params","map","param","children","reflection","type","undefined","child","id","result","deserializeCommandTree","serializedCommands","deserialize","kind","function","Error","optionsReflection","getParameters","length","optionsType","objectLiteral","deserializedNode","getProperty","name","getParameter","readCommandsPersistence","reflections","fs","read","JSON","parse","writeCommandsPersistence","filePath","write","stringify","config","mode"],"sources":["../../src/helpers/persistence.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Shell Shock\n\n This code was released as part of the Shell Shock project. Shell Shock\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/shell-shock.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/shell-shock\n Documentation: https://docs.stormsoftware.com/projects/shell-shock\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { ReflectionClass } from \"@powerlines/deepkit/vendor/type\";\nimport {\n deserializeType,\n ReflectionFunction,\n ReflectionKind,\n resolveClassType,\n serializeType\n} from \"@powerlines/deepkit/vendor/type\";\nimport { omit } from \"@stryke/helpers/omit\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport type {\n CommandDynamicSegment,\n CommandOption,\n CommandParameter,\n CommandTree,\n SerializedCommandDynamicSegment,\n SerializedCommandOption,\n SerializedCommandTree\n} from \"../types\";\nimport type { Context } from \"../types/context\";\n\nexport function getCommandsPersistencePath(context: Context): string {\n return joinPaths(context.dataPath, \"reflections\", \"commands.json\");\n}\n\nexport function serializedCommandTree(\n commands: Record<string, CommandTree>\n): Record<string, SerializedCommandTree> {\n const serialize = (\n node: CommandTree,\n parent: string | null = null\n ): SerializedCommandTree => {\n const serializedNode: SerializedCommandTree = {\n ...node,\n options: Object.entries(node.options).reduce(\n (ret, [key, option]) => {\n ret[key] = {\n ...omit(option, [\"reflection\"])\n };\n return ret;\n },\n {} as Record<string, SerializedCommandOption>\n ),\n path: {\n ...node.path,\n dynamics: Object.entries(node.path.dynamics).reduce(\n (ret, [key, dynamic]) => {\n ret[key] = {\n ...omit(dynamic, [\"reflection\"])\n };\n return ret;\n },\n {} as Record<string, SerializedCommandDynamicSegment>\n )\n },\n params: node.params.map(param => ({\n ...omit(param, [\"reflection\"])\n })),\n parent,\n children: {},\n reflection: node.reflection\n ? serializeType(node.reflection.type)\n : undefined\n };\n\n for (const [key, child] of Object.entries(node.children || {})) {\n serializedNode.children[key] = serialize(child, node.id);\n }\n\n return serializedNode;\n };\n\n const result: Record<string, SerializedCommandTree> = {};\n for (const [key, child] of Object.entries(commands)) {\n result[key] = serialize(child, null);\n }\n\n return result;\n}\n\nexport function deserializeCommandTree(\n serializedCommands: Record<string, SerializedCommandTree>\n): Record<string, CommandTree> {\n const deserialize = (\n node: SerializedCommandTree,\n parent: CommandTree | null = null\n ): CommandTree => {\n const type = deserializeType(node.reflection);\n if (type.kind !== ReflectionKind.function) {\n throw new Error(\n `Failed to deserialize persisted data - invalid command handler reflection type for command ${node.id}`\n );\n }\n\n const reflection = new ReflectionFunction(type);\n\n let optionsReflection: ReflectionClass<any> | undefined;\n if (\n reflection.getParameters().length > 0 &&\n reflection.getParameters()[0]\n ) {\n const optionsType = reflection.getParameters()[0]!.type;\n if (optionsType.kind === ReflectionKind.objectLiteral) {\n optionsReflection = resolveClassType(optionsType);\n }\n }\n\n const deserializedNode: CommandTree = {\n ...node,\n options: optionsReflection\n ? Object.entries(node.options).reduce(\n (ret, [key, option]) => {\n ret[key] = {\n ...option,\n reflection: optionsReflection.getProperty(option.name)\n } as CommandOption;\n return ret;\n },\n {} as Record<string, CommandOption>\n )\n : {},\n path: {\n ...node.path,\n dynamics: Object.entries(node.path.dynamics).reduce(\n (ret, [key, dynamic]) => {\n ret[key] = {\n ...dynamic,\n reflection: reflection.getParameter(dynamic.name)\n } as CommandDynamicSegment;\n return ret;\n },\n {} as Record<string, CommandDynamicSegment>\n )\n },\n params: node.params.map(param => ({\n ...param,\n reflection: reflection.getParameter(param.name)\n })) as CommandParameter[],\n parent,\n children: {},\n reflection\n };\n\n for (const [key, child] of Object.entries(node.children || {})) {\n deserializedNode.children[key] = deserialize(child, deserializedNode);\n }\n\n return deserializedNode;\n };\n\n const result: Record<string, CommandTree> = {};\n for (const [key, child] of Object.entries(serializedCommands)) {\n result[key] = deserialize(child, null);\n }\n\n return result;\n}\n\nexport async function readCommandsPersistence(context: Context) {\n const reflections = await context.fs.read(\n getCommandsPersistencePath(context)\n );\n if (!reflections || !reflections.length) {\n throw new Error(\n `CLI Command reflection file ${getCommandsPersistencePath(context)} is empty`\n );\n }\n\n context.commands = deserializeCommandTree(JSON.parse(reflections));\n}\n\nexport async function writeCommandsPersistence(context: Context) {\n const filePath = getCommandsPersistencePath(context);\n\n await context.fs.write(\n filePath,\n JSON.stringify(\n serializedCommandTree(context.commands),\n null,\n context.config.mode === \"development\" ? 2 : 0\n )\n );\n}\n"],"mappings":";;;;;;AAuCA,SAAgBO,2BAA2BC,SAA0B;AACnE,gDAAiBA,QAAQC,UAAU,eAAe,gBAAgB;;AAGpE,SAAgBC,sBACdC,UACuC;CACvC,MAAMC,aACJC,MACAC,SAAwB,SACE;EAC1B,MAAMC,iBAAwC;GAC5C,GAAGF;GACHG,SAASC,OAAOC,QAAQL,KAAKG,QAAQ,CAACG,QACnCC,KAAK,CAACC,KAAKC,YAAY;AACtBF,QAAIC,OAAO,EACT,mCAAQC,QAAQ,CAAC,aAAa,CAAA,EAC/B;AACD,WAAOF;MAET,EACF,CAAC;GACDG,MAAM;IACJ,GAAGV,KAAKU;IACRC,UAAUP,OAAOC,QAAQL,KAAKU,KAAKC,SAAS,CAACL,QAC1CC,KAAK,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AI,EAEhD,CAAC"}
|
|
@@ -1,15 +1,30 @@
|
|
|
1
1
|
import { joinPaths } from "@stryke/path/join-paths";
|
|
2
|
+
import { ReflectionFunction, ReflectionKind, deserializeType, resolveClassType, serializeType } from "@powerlines/deepkit/vendor/type";
|
|
3
|
+
import { omit } from "@stryke/helpers/omit";
|
|
2
4
|
|
|
3
5
|
//#region src/helpers/persistence.ts
|
|
4
6
|
function getCommandsPersistencePath(context) {
|
|
5
|
-
return joinPaths(context.dataPath,
|
|
7
|
+
return joinPaths(context.dataPath, "reflections", "commands.json");
|
|
6
8
|
}
|
|
7
9
|
function serializedCommandTree(commands) {
|
|
8
10
|
const serialize = (node, parent = null) => {
|
|
9
11
|
const serializedNode = {
|
|
10
12
|
...node,
|
|
13
|
+
options: Object.entries(node.options).reduce((ret, [key, option]) => {
|
|
14
|
+
ret[key] = { ...omit(option, ["reflection"]) };
|
|
15
|
+
return ret;
|
|
16
|
+
}, {}),
|
|
17
|
+
path: {
|
|
18
|
+
...node.path,
|
|
19
|
+
dynamics: Object.entries(node.path.dynamics).reduce((ret, [key, dynamic]) => {
|
|
20
|
+
ret[key] = { ...omit(dynamic, ["reflection"]) };
|
|
21
|
+
return ret;
|
|
22
|
+
}, {})
|
|
23
|
+
},
|
|
24
|
+
params: node.params.map((param) => ({ ...omit(param, ["reflection"]) })),
|
|
11
25
|
parent,
|
|
12
|
-
children: {}
|
|
26
|
+
children: {},
|
|
27
|
+
reflection: node.reflection ? serializeType(node.reflection.type) : void 0
|
|
13
28
|
};
|
|
14
29
|
for (const [key, child] of Object.entries(node.children || {})) serializedNode.children[key] = serialize(child, node.id);
|
|
15
30
|
return serializedNode;
|
|
@@ -20,10 +35,40 @@ function serializedCommandTree(commands) {
|
|
|
20
35
|
}
|
|
21
36
|
function deserializeCommandTree(serializedCommands) {
|
|
22
37
|
const deserialize = (node, parent = null) => {
|
|
38
|
+
const type = deserializeType(node.reflection);
|
|
39
|
+
if (type.kind !== ReflectionKind.function) throw new Error(`Failed to deserialize persisted data - invalid command handler reflection type for command ${node.id}`);
|
|
40
|
+
const reflection = new ReflectionFunction(type);
|
|
41
|
+
let optionsReflection;
|
|
42
|
+
if (reflection.getParameters().length > 0 && reflection.getParameters()[0]) {
|
|
43
|
+
const optionsType = reflection.getParameters()[0].type;
|
|
44
|
+
if (optionsType.kind === ReflectionKind.objectLiteral) optionsReflection = resolveClassType(optionsType);
|
|
45
|
+
}
|
|
23
46
|
const deserializedNode = {
|
|
24
47
|
...node,
|
|
48
|
+
options: optionsReflection ? Object.entries(node.options).reduce((ret, [key, option]) => {
|
|
49
|
+
ret[key] = {
|
|
50
|
+
...option,
|
|
51
|
+
reflection: optionsReflection.getProperty(option.name)
|
|
52
|
+
};
|
|
53
|
+
return ret;
|
|
54
|
+
}, {}) : {},
|
|
55
|
+
path: {
|
|
56
|
+
...node.path,
|
|
57
|
+
dynamics: Object.entries(node.path.dynamics).reduce((ret, [key, dynamic]) => {
|
|
58
|
+
ret[key] = {
|
|
59
|
+
...dynamic,
|
|
60
|
+
reflection: reflection.getParameter(dynamic.name)
|
|
61
|
+
};
|
|
62
|
+
return ret;
|
|
63
|
+
}, {})
|
|
64
|
+
},
|
|
65
|
+
params: node.params.map((param) => ({
|
|
66
|
+
...param,
|
|
67
|
+
reflection: reflection.getParameter(param.name)
|
|
68
|
+
})),
|
|
25
69
|
parent,
|
|
26
|
-
children: {}
|
|
70
|
+
children: {},
|
|
71
|
+
reflection
|
|
27
72
|
};
|
|
28
73
|
for (const [key, child] of Object.entries(node.children || {})) deserializedNode.children[key] = deserialize(child, deserializedNode);
|
|
29
74
|
return deserializedNode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persistence.mjs","names":["joinPaths","getCommandsPersistencePath","context","dataPath","serializedCommandTree","commands","serialize","node","parent","serializedNode","
|
|
1
|
+
{"version":3,"file":"persistence.mjs","names":["deserializeType","ReflectionFunction","ReflectionKind","resolveClassType","serializeType","omit","joinPaths","getCommandsPersistencePath","context","dataPath","serializedCommandTree","commands","serialize","node","parent","serializedNode","options","Object","entries","reduce","ret","key","option","path","dynamics","dynamic","params","map","param","children","reflection","type","undefined","child","id","result","deserializeCommandTree","serializedCommands","deserialize","kind","function","Error","optionsReflection","getParameters","length","optionsType","objectLiteral","deserializedNode","getProperty","name","getParameter","readCommandsPersistence","reflections","fs","read","JSON","parse","writeCommandsPersistence","filePath","write","stringify","config","mode"],"sources":["../../src/helpers/persistence.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Shell Shock\n\n This code was released as part of the Shell Shock project. Shell Shock\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/shell-shock.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/shell-shock\n Documentation: https://docs.stormsoftware.com/projects/shell-shock\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { ReflectionClass } from \"@powerlines/deepkit/vendor/type\";\nimport {\n deserializeType,\n ReflectionFunction,\n ReflectionKind,\n resolveClassType,\n serializeType\n} from \"@powerlines/deepkit/vendor/type\";\nimport { omit } from \"@stryke/helpers/omit\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport type {\n CommandDynamicSegment,\n CommandOption,\n CommandParameter,\n CommandTree,\n SerializedCommandDynamicSegment,\n SerializedCommandOption,\n SerializedCommandTree\n} from \"../types\";\nimport type { Context } from \"../types/context\";\n\nexport function getCommandsPersistencePath(context: Context): string {\n return joinPaths(context.dataPath, \"reflections\", \"commands.json\");\n}\n\nexport function serializedCommandTree(\n commands: Record<string, CommandTree>\n): Record<string, SerializedCommandTree> {\n const serialize = (\n node: CommandTree,\n parent: string | null = null\n ): SerializedCommandTree => {\n const serializedNode: SerializedCommandTree = {\n ...node,\n options: Object.entries(node.options).reduce(\n (ret, [key, option]) => {\n ret[key] = {\n ...omit(option, [\"reflection\"])\n };\n return ret;\n },\n {} as Record<string, SerializedCommandOption>\n ),\n path: {\n ...node.path,\n dynamics: Object.entries(node.path.dynamics).reduce(\n (ret, [key, dynamic]) => {\n ret[key] = {\n ...omit(dynamic, [\"reflection\"])\n };\n return ret;\n },\n {} as Record<string, SerializedCommandDynamicSegment>\n )\n },\n params: node.params.map(param => ({\n ...omit(param, [\"reflection\"])\n })),\n parent,\n children: {},\n reflection: node.reflection\n ? serializeType(node.reflection.type)\n : undefined\n };\n\n for (const [key, child] of Object.entries(node.children || {})) {\n serializedNode.children[key] = serialize(child, node.id);\n }\n\n return serializedNode;\n };\n\n const result: Record<string, SerializedCommandTree> = {};\n for (const [key, child] of Object.entries(commands)) {\n result[key] = serialize(child, null);\n }\n\n return result;\n}\n\nexport function deserializeCommandTree(\n serializedCommands: Record<string, SerializedCommandTree>\n): Record<string, CommandTree> {\n const deserialize = (\n node: SerializedCommandTree,\n parent: CommandTree | null = null\n ): CommandTree => {\n const type = deserializeType(node.reflection);\n if (type.kind !== ReflectionKind.function) {\n throw new Error(\n `Failed to deserialize persisted data - invalid command handler reflection type for command ${node.id}`\n );\n }\n\n const reflection = new ReflectionFunction(type);\n\n let optionsReflection: ReflectionClass<any> | undefined;\n if (\n reflection.getParameters().length > 0 &&\n reflection.getParameters()[0]\n ) {\n const optionsType = reflection.getParameters()[0]!.type;\n if (optionsType.kind === ReflectionKind.objectLiteral) {\n optionsReflection = resolveClassType(optionsType);\n }\n }\n\n const deserializedNode: CommandTree = {\n ...node,\n options: optionsReflection\n ? Object.entries(node.options).reduce(\n (ret, [key, option]) => {\n ret[key] = {\n ...option,\n reflection: optionsReflection.getProperty(option.name)\n } as CommandOption;\n return ret;\n },\n {} as Record<string, CommandOption>\n )\n : {},\n path: {\n ...node.path,\n dynamics: Object.entries(node.path.dynamics).reduce(\n (ret, [key, dynamic]) => {\n ret[key] = {\n ...dynamic,\n reflection: reflection.getParameter(dynamic.name)\n } as CommandDynamicSegment;\n return ret;\n },\n {} as Record<string, CommandDynamicSegment>\n )\n },\n params: node.params.map(param => ({\n ...param,\n reflection: reflection.getParameter(param.name)\n })) as CommandParameter[],\n parent,\n children: {},\n reflection\n };\n\n for (const [key, child] of Object.entries(node.children || {})) {\n deserializedNode.children[key] = deserialize(child, deserializedNode);\n }\n\n return deserializedNode;\n };\n\n const result: Record<string, CommandTree> = {};\n for (const [key, child] of Object.entries(serializedCommands)) {\n result[key] = deserialize(child, null);\n }\n\n return result;\n}\n\nexport async function readCommandsPersistence(context: Context) {\n const reflections = await context.fs.read(\n getCommandsPersistencePath(context)\n );\n if (!reflections || !reflections.length) {\n throw new Error(\n `CLI Command reflection file ${getCommandsPersistencePath(context)} is empty`\n );\n }\n\n context.commands = deserializeCommandTree(JSON.parse(reflections));\n}\n\nexport async function writeCommandsPersistence(context: Context) {\n const filePath = getCommandsPersistencePath(context);\n\n await context.fs.write(\n filePath,\n JSON.stringify(\n serializedCommandTree(context.commands),\n null,\n context.config.mode === \"development\" ? 2 : 0\n )\n );\n}\n"],"mappings":";;;;;AAuCA,SAAgBO,2BAA2BC,SAA0B;AACnE,QAAOF,UAAUE,QAAQC,UAAU,eAAe,gBAAgB;;AAGpE,SAAgBC,sBACdC,UACuC;CACvC,MAAMC,aACJC,MACAC,SAAwB,SACE;EAC1B,MAAMC,iBAAwC;GAC5C,GAAGF;GACHG,SAASC,OAAOC,QAAQL,KAAKG,QAAQ,CAACG,QACnCC,KAAK,CAACC,KAAKC,YAAY;AACtBF,QAAIC,OAAO,EACT,GAAGhB,KAAKiB,QAAQ,CAAC,aAAa,CAAA,EAC/B;AACD,WAAOF;MAET,EACF,CAAC;GACDG,MAAM;IACJ,GAAGV,KAAKU;IACRC,UAAUP,OAAOC,QAAQL,KAAKU,KAAKC,SAAS,CAACL,QAC1CC,KAAK,CAACC,KAAKI,aAAa;AACvBL,SAAIC,OAAO,EACT,GAAGhB,KAAKoB,SAAS,CAAC,aAAa,CAAA,EAChC;AACD,YAAOL;OAET,EACF,CAAA;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/resolve
|
|
|
30
30
|
function resolveCommandOptionDescription(kind, optional, name, title, defaultValue) {
|
|
31
31
|
return `A${optional && !defaultValue ? "n optional" : ""} ${kind === __powerlines_deepkit_vendor_type.ReflectionKind.boolean ? "flag provided via the command-line" : "command-line option"} that allows the user to ${kind === __powerlines_deepkit_vendor_type.ReflectionKind.boolean ? "set the" : kind === __powerlines_deepkit_vendor_type.ReflectionKind.array ? "specify custom" : "specify a custom"} ${title?.trim() || (0, __stryke_string_format_title_case.titleCase)(name)} ${kind === __powerlines_deepkit_vendor_type.ReflectionKind.boolean ? "indicator" : `${kind === __powerlines_deepkit_vendor_type.ReflectionKind.number ? "numeric" : "string"} value${kind === __powerlines_deepkit_vendor_type.ReflectionKind.array ? "s" : ""}`} that will be used in the application.`;
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Resolves the description for a command parameter based on its reflection.
|
|
35
|
+
*
|
|
36
|
+
* @param kind - The reflection kind of the command parameter.
|
|
37
|
+
* @param optional - Whether the command parameter is optional.
|
|
38
|
+
* @param name - The name of the command parameter.
|
|
39
|
+
* @param title - The title of the command parameter, if any.
|
|
40
|
+
* @param defaultValue - The default value of the command parameter, if any.
|
|
41
|
+
* @returns The resolved description for the command parameter.
|
|
42
|
+
*/
|
|
43
|
+
function resolveCommandParameterDescription(kind, optional, name, title, defaultValue) {
|
|
44
|
+
return `A${optional && !defaultValue ? "n optional" : ""} command-line positional parameter that allows the user to ${kind === __powerlines_deepkit_vendor_type.ReflectionKind.boolean ? "set the" : kind === __powerlines_deepkit_vendor_type.ReflectionKind.array ? "specify custom" : "specify a custom"} ${title?.trim() || (0, __stryke_string_format_title_case.titleCase)(name)} ${kind === __powerlines_deepkit_vendor_type.ReflectionKind.boolean ? "indicator" : `${kind === __powerlines_deepkit_vendor_type.ReflectionKind.number ? "numeric" : "string"} value${kind === __powerlines_deepkit_vendor_type.ReflectionKind.array ? "s" : ""}`} that will be used in the application.`;
|
|
45
|
+
}
|
|
33
46
|
function resolveCommandId(context, file) {
|
|
34
|
-
return (0, __stryke_path_replace.replacePath)((0, __stryke_path_file_path_fns.findFilePath)(file), context.commandsPath).split("/").filter((p) => Boolean(p) && !require_plugin_utils_context_helpers.
|
|
47
|
+
return (0, __stryke_path_replace.replacePath)((0, __stryke_path_file_path_fns.findFilePath)(file), context.commandsPath).split("/").filter((p) => Boolean(p) && !require_plugin_utils_context_helpers.isDynamicPathSegment(p)).join("/").replaceAll(/^\/+/g, "").replaceAll(/\/+$/g, "").replaceAll("/", "-");
|
|
35
48
|
}
|
|
36
49
|
/**
|
|
37
50
|
* Finds the command name from the given file path.
|
|
@@ -42,14 +55,14 @@ function resolveCommandId(context, file) {
|
|
|
42
55
|
function resolveCommandName(file) {
|
|
43
56
|
let path = (0, __stryke_path_file_path_fns.findFilePath)(file);
|
|
44
57
|
let name = (0, __stryke_path_file_path_fns.findFolderName)(file, { requireExtension: true });
|
|
45
|
-
while (require_plugin_utils_context_helpers.
|
|
58
|
+
while (require_plugin_utils_context_helpers.isDynamicPathSegment(name)) {
|
|
46
59
|
path = (0, __stryke_path_resolve_parent_path.resolveParentPath)(path);
|
|
47
60
|
name = (0, __stryke_path_file_path_fns.findFolderName)(path, { requireExtension: true });
|
|
48
61
|
}
|
|
49
62
|
return name;
|
|
50
63
|
}
|
|
51
64
|
function resolveCommandPath(context, file) {
|
|
52
|
-
return (0, __stryke_path_replace.replacePath)((0, __stryke_path_file_path_fns.findFilePath)(file), context.commandsPath).replaceAll(/^\/+/g, "").replaceAll(/\/+$/g, "");
|
|
65
|
+
return (0, __stryke_path_replace.replacePath)((0, __stryke_path_file_path_fns.findFilePath)(file), context.commandsPath).replaceAll(/^\/+/g, "").replaceAll(/\/+$/g, "").split("/").filter((path) => path && !require_plugin_utils_context_helpers.isPathSegmentGroup(path)).join("/");
|
|
53
66
|
}
|
|
54
67
|
function findCommandsRoot(context) {
|
|
55
68
|
if ((0, __stryke_type_checks_is_set_string.isSetString)(context.config.entry)) return (0, __stryke_path_append.appendPath)((0, __stryke_path_append.appendPath)((0, __stryke_path_normalize.stripStars)(context.config.entry), context.config.projectRoot), context.workspaceConfig.workspaceRoot);
|
|
@@ -65,50 +78,81 @@ function findCommandsRoot(context) {
|
|
|
65
78
|
* @returns The extracted command option information.
|
|
66
79
|
*/
|
|
67
80
|
function extractCommandOption(command, reflection) {
|
|
68
|
-
const
|
|
81
|
+
const type = reflection.getType();
|
|
69
82
|
const option = {
|
|
70
83
|
name: reflection.getNameAsString(),
|
|
71
84
|
alias: reflection.getTags().alias ?? [],
|
|
72
85
|
title: reflection.getTags().title?.trim() || (0, __stryke_string_format_title_case.titleCase)(reflection.getNameAsString()),
|
|
73
86
|
description: reflection.getDescription() || resolveCommandOptionDescription(reflection.getKind(), reflection.isOptional(), reflection.getNameAsString(), reflection.getTags().title, reflection.getDefaultValue()),
|
|
74
87
|
env: (0, __stryke_string_format_constant_case.constantCase)(reflection.getNameAsString()),
|
|
75
|
-
kind:
|
|
88
|
+
kind: type.kind,
|
|
76
89
|
optional: reflection.isOptional(),
|
|
77
90
|
default: reflection.getDefaultValue(),
|
|
78
|
-
variadic:
|
|
91
|
+
variadic: reflection.isArray(),
|
|
92
|
+
reflection
|
|
79
93
|
};
|
|
80
|
-
if (
|
|
94
|
+
if (reflection.isArray()) if (type.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.string || type.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.number) {
|
|
81
95
|
option.variadic = true;
|
|
82
|
-
option.kind =
|
|
83
|
-
} else throw new Error(`Unsupported array type for option "${reflection.getNameAsString()}" in command "${command.name}". Only string[] and number[] are supported.`);
|
|
84
|
-
else if (
|
|
96
|
+
option.kind = type.type.kind;
|
|
97
|
+
} else throw new Error(`Unsupported array type for option "${reflection.getNameAsString()}" in command "${command.name}". Only string[] and number[] are supported, received ${(0, __powerlines_deepkit_vendor_type.stringifyType)(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
98
|
+
else if (type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.boolean && type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.string && type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.number) throw new Error(`Unsupported type for option "${reflection.getNameAsString()}" in command "${command.name}". Only string, number, boolean, string[] and number[] are supported, received ${(0, __powerlines_deepkit_vendor_type.stringifyType)(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
85
99
|
return option;
|
|
86
100
|
}
|
|
87
101
|
/**
|
|
88
|
-
* Extracts command
|
|
102
|
+
* Extracts command dynamic path segment information from a type parameter reflection.
|
|
89
103
|
*
|
|
90
104
|
* @param command - The command tree to which the parameter belongs.
|
|
91
105
|
* @param segment - The command path segment corresponding to the parameter.
|
|
92
106
|
* @param reflection - The type parameter reflection to extract information from.
|
|
93
|
-
* @returns The extracted command
|
|
107
|
+
* @returns The extracted command dynamic segment information.
|
|
94
108
|
*/
|
|
95
|
-
function
|
|
96
|
-
|
|
109
|
+
function extractCommandDynamicSegment(command, segment, reflection) {
|
|
110
|
+
const type = reflection.getType();
|
|
111
|
+
if (type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.string && !(type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.array && type.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.string)) throw new Error(`Unsupported type for dynamic path segment "${segment}" in command "${command.name}". Only string types (or an array of strings) are supported, received ${(0, __powerlines_deepkit_vendor_type.stringifyType)(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
97
112
|
const option = {
|
|
98
|
-
name:
|
|
99
|
-
|
|
100
|
-
title: (0, __stryke_string_format_title_case.titleCase)(segment),
|
|
101
|
-
description: reflection.description || resolveCommandOptionDescription(
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
variadic: false
|
|
113
|
+
name: reflection.getName(),
|
|
114
|
+
segment,
|
|
115
|
+
title: reflection.getTags().title || (0, __stryke_string_format_title_case.titleCase)(segment),
|
|
116
|
+
description: reflection.parameter.description || resolveCommandOptionDescription(type.kind, !!reflection.isOptional(), segment, (0, __stryke_string_format_title_case.titleCase)(segment), reflection.getDefaultValue()),
|
|
117
|
+
optional: reflection.isOptional(),
|
|
118
|
+
default: reflection.getDefaultValue(),
|
|
119
|
+
catchAll: command.path.segments.some((seg) => require_plugin_utils_context_helpers.getDynamicPathSegmentName(segment) === seg && require_plugin_utils_context_helpers.isCatchAllPathSegment(seg)),
|
|
120
|
+
reflection
|
|
107
121
|
};
|
|
108
|
-
if (
|
|
122
|
+
if (type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.array) {
|
|
123
|
+
if (!option.catchAll) throw new Error(`Dynamic path segment "${segment}" in command "${command.name}" is an array type but is not defined as a catch-all segment. To use an array type for a dynamic path segment, it must be defined as a catch-all segment using the "[...segment]" syntax.`);
|
|
109
124
|
option.variadic = true;
|
|
110
|
-
|
|
111
|
-
|
|
125
|
+
}
|
|
126
|
+
if (option.catchAll) {
|
|
127
|
+
if (!option.optional && command.path.segments.some((seg) => require_plugin_utils_context_helpers.getDynamicPathSegmentName(segment) === seg && require_plugin_utils_context_helpers.isOptionalCatchAllPathSegment(seg))) throw new Error(`Dynamic path segment "${segment}" in command "${command.name}" is defined as a catch-all segment but is not optional. To define an optional catch-all segment, use the "[[...segment]]" syntax.`);
|
|
128
|
+
else if (option.optional && !command.path.segments.some((seg) => require_plugin_utils_context_helpers.getDynamicPathSegmentName(segment) === seg && require_plugin_utils_context_helpers.isOptionalCatchAllPathSegment(seg))) throw new Error(`Dynamic path segment "${segment}" in command "${command.name}" is defined as an optional segment but is not defined as an optional catch-all segment. To define an optional catch-all segment, use the "[[...segment]]" syntax.`);
|
|
129
|
+
}
|
|
130
|
+
return option;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Extracts command positional parameter information from a type parameter reflection.
|
|
134
|
+
*
|
|
135
|
+
* @param command - The command tree to which the parameter belongs.
|
|
136
|
+
* @param reflection - The type parameter reflection to extract information from.
|
|
137
|
+
* @returns The extracted command positional parameter information.
|
|
138
|
+
*/
|
|
139
|
+
function extractCommandParameter(command, reflection) {
|
|
140
|
+
const type = reflection.getType();
|
|
141
|
+
if (type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.string && type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.number && type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.boolean && !(type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.array && (type.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.string || type.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.number))) throw new Error(`Unsupported type for positional parameter "${reflection.getName()}" in command "${command.name}". Only string types (or an array of strings) are supported, received ${(0, __powerlines_deepkit_vendor_type.stringifyType)(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
142
|
+
const option = {
|
|
143
|
+
name: reflection.getName(),
|
|
144
|
+
title: (0, __stryke_string_format_title_case.titleCase)(reflection.getName()),
|
|
145
|
+
description: reflection.parameter.description || resolveCommandParameterDescription(type.kind, !!reflection.isOptional(), reflection.getName(), (0, __stryke_string_format_title_case.titleCase)(reflection.getName()), reflection.getDefaultValue()),
|
|
146
|
+
env: (0, __stryke_string_format_constant_case.constantCase)(reflection.getName()),
|
|
147
|
+
optional: reflection.isOptional(),
|
|
148
|
+
default: reflection.getDefaultValue(),
|
|
149
|
+
reflection
|
|
150
|
+
};
|
|
151
|
+
if (type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.array) if (type.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.string || type.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.number) {
|
|
152
|
+
option.variadic = true;
|
|
153
|
+
option.kind = type.type.kind;
|
|
154
|
+
} else throw new Error(`Unsupported array type for positional parameter "${reflection.getName()}" in command "${command.name}". Only string[] and number[] are supported, received ${(0, __powerlines_deepkit_vendor_type.stringifyType)(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
155
|
+
else if (type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.boolean && type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.string && type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.number) throw new Error(`Unsupported type for positional parameter "${reflection.getName()}" in command "${command.name}". Only string, number, boolean, string[] and number[] are supported, received ${(0, __powerlines_deepkit_vendor_type.stringifyType)(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
112
156
|
return option;
|
|
113
157
|
}
|
|
114
158
|
/**
|
|
@@ -127,11 +171,13 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
127
171
|
title,
|
|
128
172
|
path: {
|
|
129
173
|
...command.path,
|
|
130
|
-
|
|
174
|
+
dynamics: {}
|
|
131
175
|
},
|
|
132
176
|
options: require_utilities.getDefaultOptions(context, command),
|
|
177
|
+
params: [],
|
|
133
178
|
parent: parent ?? null,
|
|
134
|
-
children: {}
|
|
179
|
+
children: {},
|
|
180
|
+
reflection: null
|
|
135
181
|
};
|
|
136
182
|
if (!command.isVirtual) {
|
|
137
183
|
if (!command.entry.input?.file || !context.fs.existsSync(command.entry.input.file)) throw new Error(`${!command.entry.input?.file ? "Missing" : "Non-existent"} command entry file for "${command.name}"`);
|
|
@@ -144,24 +190,27 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
144
190
|
if ((0, __stryke_type_checks_is_set_string.isSetString)(metadata.title)) tree.title = metadata.title;
|
|
145
191
|
if ((0, __stryke_type_checks_is_set_string.isSetString)(metadata.description)) tree.description = metadata.description;
|
|
146
192
|
if ((0, __stryke_type_checks_is_set_string.isSetString)(metadata.alias) || Array.isArray(metadata.alias) && metadata.alias.length > 0) tree.alias = (0, __stryke_convert_to_array.toArray)(metadata.alias);
|
|
193
|
+
if ((0, __stryke_type_checks_is_set_string.isSetString)(metadata.icon)) tree.icon = metadata.icon;
|
|
147
194
|
const type = (0, __powerlines_deepkit_vendor_type.reflect)(resolved);
|
|
148
195
|
if (type.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.function) throw new Error(`The command entry file "${command.entry.input.file}" does not export a valid function.`);
|
|
149
|
-
tree.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
196
|
+
tree.reflection = new __powerlines_deepkit_vendor_type.ReflectionFunction(type);
|
|
197
|
+
tree.description ??= command.description || type.description || `The ${tree.title} executable command line interface.`;
|
|
198
|
+
const parameters = tree.reflection.getParameters();
|
|
199
|
+
if (parameters.length > 0 && parameters[0]) {
|
|
200
|
+
if (parameters[0].type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.objectLiteral || parameters[0].type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.class) {
|
|
201
|
+
const optionsReflection = __powerlines_deepkit_vendor_type.ReflectionClass.from(parameters[0].type);
|
|
154
202
|
for (const propertyReflection of optionsReflection.getProperties()) tree.options[propertyReflection.getNameAsString()] = extractCommandOption(command, propertyReflection);
|
|
155
|
-
}
|
|
156
|
-
tree.path.
|
|
157
|
-
if (
|
|
158
|
-
const paramName = require_plugin_utils_context_helpers.
|
|
159
|
-
obj[paramName] =
|
|
160
|
-
obj[paramName].description = obj[paramName].description || `The ${paramName}
|
|
203
|
+
} else throw new Error(`The first parameter of the command handler function in "${command.entry.input.file}" must be an object literal or class type representing the command options.`);
|
|
204
|
+
tree.path.dynamics = tree.path.segments.filter((segment) => require_plugin_utils_context_helpers.isDynamicPathSegment(segment)).reduce((obj, segment, index) => {
|
|
205
|
+
if (parameters.length < index + 2 || !parameters[index + 1]) return obj;
|
|
206
|
+
const paramName = require_plugin_utils_context_helpers.getDynamicPathSegmentName(segment);
|
|
207
|
+
obj[paramName] = extractCommandDynamicSegment(command, paramName, parameters[index + 1]);
|
|
208
|
+
obj[paramName].description = obj[paramName].description || `The ${paramName} ${obj[paramName].catchAll ? `${obj[paramName].optional ? "optional " : ""}catch-all` : "dynamic "} segment for the ${command.name} command.`;
|
|
161
209
|
return obj;
|
|
162
210
|
}, {});
|
|
211
|
+
if (parameters.length > Object.keys(tree.path.dynamics).length + 1) tree.params = parameters.slice(Object.keys(tree.path.dynamics).length + 1).map((param) => extractCommandParameter(command, param));
|
|
163
212
|
}
|
|
164
|
-
} else tree.description ??= `A collection of available ${tree.title || (0, __stryke_string_format_title_case.titleCase)(tree.name)} commands that are included in the ${require_plugin_utils_context_helpers.getAppTitle(context)} command
|
|
213
|
+
} else tree.description ??= `A collection of available ${tree.title || (0, __stryke_string_format_title_case.titleCase)(tree.name)} commands that are included in the ${require_plugin_utils_context_helpers.getAppTitle(context)} command line application.`;
|
|
165
214
|
if (context.env) {
|
|
166
215
|
if ((0, __stryke_type_checks_is_set_object.isSetObject)(tree.options)) Object.values(tree.options).filter((option) => option.env !== false).forEach((option) => {
|
|
167
216
|
context.env.types.env.addProperty({
|
|
@@ -169,32 +218,33 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
169
218
|
optional: option.optional ? true : void 0,
|
|
170
219
|
description: option.description,
|
|
171
220
|
visibility: __powerlines_deepkit_vendor_type.ReflectionVisibility.public,
|
|
172
|
-
type: option.
|
|
221
|
+
type: option.reflection?.getType() ?? (option.variadic ? {
|
|
173
222
|
kind: __powerlines_deepkit_vendor_type.ReflectionKind.array,
|
|
174
223
|
type: { kind: option.kind }
|
|
175
|
-
} : { kind: option.kind }
|
|
224
|
+
} : { kind: option.kind }),
|
|
176
225
|
default: option.default,
|
|
177
226
|
tags: {
|
|
227
|
+
...option.reflection?.getTags(),
|
|
178
228
|
title: option.title,
|
|
179
229
|
alias: option.alias.filter((alias) => alias.length > 0).map((alias) => (0, __stryke_string_format_constant_case.constantCase)(alias)),
|
|
180
230
|
domain: "cli"
|
|
181
231
|
}
|
|
182
232
|
});
|
|
183
233
|
});
|
|
184
|
-
|
|
185
|
-
name: (0, __stryke_string_format_constant_case.constantCase)(
|
|
186
|
-
optional:
|
|
187
|
-
description:
|
|
234
|
+
Object.values(tree.params).filter((param) => param.env !== false).forEach((param) => context.env.types.env.addProperty({
|
|
235
|
+
name: (0, __stryke_string_format_constant_case.constantCase)(param.name),
|
|
236
|
+
optional: param.optional ? true : void 0,
|
|
237
|
+
description: param.description,
|
|
188
238
|
visibility: __powerlines_deepkit_vendor_type.ReflectionVisibility.public,
|
|
189
|
-
type:
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
239
|
+
type: param.reflection.getType(),
|
|
240
|
+
default: param.default,
|
|
241
|
+
tags: {
|
|
242
|
+
...param.reflection.getTags(),
|
|
243
|
+
domain: "cli"
|
|
244
|
+
}
|
|
195
245
|
}));
|
|
196
246
|
}
|
|
197
|
-
for (const input of context.inputs.filter((input$1) => input$1.path.segments.filter((segment) => !require_plugin_utils_context_helpers.
|
|
247
|
+
for (const input of context.inputs.filter((input$1) => input$1.path.segments.filter((segment) => !require_plugin_utils_context_helpers.isDynamicPathSegment(segment)).length === command.path.segments.filter((segment) => !require_plugin_utils_context_helpers.isDynamicPathSegment(segment)).length + 1 && input$1.path.segments.slice(0, command.path.segments.length).every((value, index) => value === command.path.segments[index]))) tree.children[input.name] = await reflectCommandTree(context, input, tree);
|
|
198
248
|
return tree;
|
|
199
249
|
}
|
|
200
250
|
|