@shell-shock/core 0.6.0 → 0.8.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/dist/components/docs.cjs +1 -1
- package/dist/components/docs.cjs.map +1 -1
- package/dist/components/docs.d.cts +5 -5
- package/dist/components/docs.d.mts +5 -5
- package/dist/components/docs.mjs +1 -1
- package/dist/components/docs.mjs.map +1 -1
- package/dist/components/helpers.cjs +15 -0
- package/dist/components/helpers.cjs.map +1 -0
- package/dist/components/helpers.d.cts +13 -0
- package/dist/components/helpers.d.cts.map +1 -0
- package/dist/components/helpers.d.mts +13 -0
- package/dist/components/helpers.d.mts.map +1 -0
- package/dist/components/helpers.mjs +14 -0
- package/dist/components/helpers.mjs.map +1 -0
- package/dist/components/index.cjs +5 -1
- package/dist/components/index.d.cts +3 -2
- package/dist/components/index.d.mts +3 -2
- package/dist/components/index.mjs +3 -2
- package/dist/components/options-parser-logic.cjs +156 -31
- package/dist/components/options-parser-logic.cjs.map +1 -1
- package/dist/components/options-parser-logic.d.cts +47 -8
- package/dist/components/options-parser-logic.d.cts.map +1 -1
- package/dist/components/options-parser-logic.d.mts +53 -14
- package/dist/components/options-parser-logic.d.mts.map +1 -1
- package/dist/components/options-parser-logic.mjs +154 -31
- package/dist/components/options-parser-logic.mjs.map +1 -1
- package/dist/components/usage.cjs +38 -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 +40 -7
- package/dist/components/usage.mjs.map +1 -1
- package/dist/helpers/persistence.cjs +31 -3
- package/dist/helpers/persistence.cjs.map +1 -1
- package/dist/helpers/persistence.mjs +31 -3
- package/dist/helpers/persistence.mjs.map +1 -1
- package/dist/helpers/resolve-command.cjs +68 -47
- package/dist/helpers/resolve-command.cjs.map +1 -1
- package/dist/helpers/resolve-command.mjs +70 -49
- package/dist/helpers/resolve-command.mjs.map +1 -1
- package/dist/helpers/validations.cjs +13 -36
- package/dist/helpers/validations.cjs.map +1 -1
- package/dist/helpers/validations.mjs +12 -36
- package/dist/helpers/validations.mjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.mts +3 -2
- package/dist/plugin-utils/context-helpers.cjs +3 -45
- package/dist/plugin-utils/context-helpers.cjs.map +1 -1
- package/dist/plugin-utils/context-helpers.d.cts +1 -37
- package/dist/plugin-utils/context-helpers.d.cts.map +1 -1
- package/dist/plugin-utils/context-helpers.d.mts +1 -37
- package/dist/plugin-utils/context-helpers.d.mts.map +1 -1
- package/dist/plugin-utils/context-helpers.mjs +4 -44
- package/dist/plugin-utils/context-helpers.mjs.map +1 -1
- package/dist/plugin-utils/index.cjs +0 -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 +9 -15
- 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 +9 -15
- package/dist/plugin.mjs.map +1 -1
- package/dist/types/command.d.cts +206 -33
- package/dist/types/command.d.cts.map +1 -1
- package/dist/types/command.d.mts +206 -33
- package/dist/types/command.d.mts.map +1 -1
- package/dist/types/index.d.cts +3 -2
- package/dist/types/index.d.mts +3 -2
- package/dist/types/runtime.d.cts +5 -15
- package/dist/types/runtime.d.cts.map +1 -1
- package/dist/types/runtime.d.mts +5 -15
- package/dist/types/runtime.d.mts.map +1 -1
- package/package.json +25 -11
|
@@ -30,6 +30,19 @@ let powerlines_lib_utilities_resolve = require("powerlines/lib/utilities/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
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
|
}
|
|
@@ -65,51 +78,51 @@ 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 positional argument information from a type parameter reflection.
|
|
89
103
|
*
|
|
90
104
|
* @param command - The command tree to which the parameter belongs.
|
|
91
|
-
* @param segment - The command path segment corresponding to the parameter.
|
|
92
105
|
* @param reflection - The type parameter reflection to extract information from.
|
|
93
|
-
* @returns The extracted command
|
|
106
|
+
* @returns The extracted command positional argument information.
|
|
94
107
|
*/
|
|
95
|
-
function
|
|
96
|
-
|
|
108
|
+
function extractCommandArgument(command, reflection) {
|
|
109
|
+
const type = reflection.getType();
|
|
110
|
+
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 ")}.`);
|
|
97
111
|
const option = {
|
|
98
|
-
name:
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
112
|
+
name: reflection.getName(),
|
|
113
|
+
kind: type.kind,
|
|
114
|
+
title: (0, __stryke_string_format_title_case.titleCase)(reflection.getName()),
|
|
115
|
+
description: reflection.parameter.description || resolveCommandParameterDescription(type.kind, !!reflection.isOptional(), reflection.getName(), (0, __stryke_string_format_title_case.titleCase)(reflection.getName()), reflection.getDefaultValue()),
|
|
116
|
+
env: (0, __stryke_string_format_constant_case.constantCase)(reflection.getName()),
|
|
117
|
+
optional: reflection.isOptional(),
|
|
118
|
+
default: reflection.getDefaultValue(),
|
|
119
|
+
reflection
|
|
104
120
|
};
|
|
105
|
-
if (
|
|
106
|
-
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.`);
|
|
121
|
+
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) {
|
|
107
122
|
option.variadic = true;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
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.`);
|
|
112
|
-
}
|
|
123
|
+
option.kind = type.type.kind;
|
|
124
|
+
} 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 ")}.`);
|
|
125
|
+
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 ")}.`);
|
|
113
126
|
return option;
|
|
114
127
|
}
|
|
115
128
|
/**
|
|
@@ -124,15 +137,14 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
124
137
|
const title = command.title || `${parent?.title ? `${parent.isVirtual ? parent.title.replace(/ Commands$/, "") : parent.title} - ` : ""}${(0, __stryke_string_format_title_case.titleCase)(command.name)}${command.isVirtual ? " Commands" : ""}`;
|
|
125
138
|
const tree = {
|
|
126
139
|
alias: [],
|
|
140
|
+
icon: parent?.icon,
|
|
127
141
|
...command,
|
|
128
142
|
title,
|
|
129
|
-
path: {
|
|
130
|
-
...command.path,
|
|
131
|
-
dynamics: {}
|
|
132
|
-
},
|
|
133
143
|
options: require_utilities.getDefaultOptions(context, command),
|
|
144
|
+
arguments: [],
|
|
134
145
|
parent: parent ?? null,
|
|
135
|
-
children: {}
|
|
146
|
+
children: {},
|
|
147
|
+
reflection: null
|
|
136
148
|
};
|
|
137
149
|
if (!command.isVirtual) {
|
|
138
150
|
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}"`);
|
|
@@ -145,24 +157,20 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
145
157
|
if ((0, __stryke_type_checks_is_set_string.isSetString)(metadata.title)) tree.title = metadata.title;
|
|
146
158
|
if ((0, __stryke_type_checks_is_set_string.isSetString)(metadata.description)) tree.description = metadata.description;
|
|
147
159
|
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);
|
|
160
|
+
if ((0, __stryke_type_checks_is_set_string.isSetString)(metadata.icon)) tree.icon = metadata.icon;
|
|
148
161
|
const type = (0, __powerlines_deepkit_vendor_type.reflect)(resolved);
|
|
149
162
|
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.`);
|
|
150
|
-
tree.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
163
|
+
tree.reflection = new __powerlines_deepkit_vendor_type.ReflectionFunction(type);
|
|
164
|
+
tree.description ??= command.description || type.description || `The ${tree.title} executable command line interface.`;
|
|
165
|
+
const parameters = tree.reflection.getParameters();
|
|
166
|
+
if (parameters.length > 0 && parameters[0]) {
|
|
167
|
+
if (parameters[0].type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.objectLiteral || parameters[0].type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.class) {
|
|
168
|
+
const optionsReflection = __powerlines_deepkit_vendor_type.ReflectionClass.from(parameters[0].type);
|
|
155
169
|
for (const propertyReflection of optionsReflection.getProperties()) tree.options[propertyReflection.getNameAsString()] = extractCommandOption(command, propertyReflection);
|
|
156
|
-
}
|
|
157
|
-
tree.
|
|
158
|
-
if (type.parameters.length < index + 2 || !type.parameters[index + 1]) return obj;
|
|
159
|
-
const paramName = require_plugin_utils_context_helpers.getDynamicPathSegmentName(segment);
|
|
160
|
-
obj[paramName] = extractCommandDynamicSegment(command, paramName, type.parameters[index + 1]);
|
|
161
|
-
obj[paramName].description = obj[paramName].description || `The ${paramName} ${obj[paramName].catchAll ? `${obj[paramName].optional ? "optional " : ""}catch-all` : "dynamic "} segment for the ${command.name} command.`;
|
|
162
|
-
return obj;
|
|
163
|
-
}, {});
|
|
170
|
+
} 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.`);
|
|
171
|
+
tree.arguments = parameters.slice(1).map((arg) => extractCommandArgument(command, arg));
|
|
164
172
|
}
|
|
165
|
-
} 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
|
|
173
|
+
} 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.`;
|
|
166
174
|
if (context.env) {
|
|
167
175
|
if ((0, __stryke_type_checks_is_set_object.isSetObject)(tree.options)) Object.values(tree.options).filter((option) => option.env !== false).forEach((option) => {
|
|
168
176
|
context.env.types.env.addProperty({
|
|
@@ -170,20 +178,33 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
170
178
|
optional: option.optional ? true : void 0,
|
|
171
179
|
description: option.description,
|
|
172
180
|
visibility: __powerlines_deepkit_vendor_type.ReflectionVisibility.public,
|
|
173
|
-
type: option.
|
|
181
|
+
type: option.reflection?.getType() ?? (option.variadic ? {
|
|
174
182
|
kind: __powerlines_deepkit_vendor_type.ReflectionKind.array,
|
|
175
183
|
type: { kind: option.kind }
|
|
176
|
-
} : { kind: option.kind }
|
|
184
|
+
} : { kind: option.kind }),
|
|
177
185
|
default: option.default,
|
|
178
186
|
tags: {
|
|
187
|
+
...option.reflection?.getTags(),
|
|
179
188
|
title: option.title,
|
|
180
189
|
alias: option.alias.filter((alias) => alias.length > 0).map((alias) => (0, __stryke_string_format_constant_case.constantCase)(alias)),
|
|
181
190
|
domain: "cli"
|
|
182
191
|
}
|
|
183
192
|
});
|
|
184
193
|
});
|
|
194
|
+
Object.values(tree.arguments).filter((arg) => arg.env !== false).forEach((arg) => context.env.types.env.addProperty({
|
|
195
|
+
name: (0, __stryke_string_format_constant_case.constantCase)(arg.name),
|
|
196
|
+
optional: arg.optional ? true : void 0,
|
|
197
|
+
description: arg.description,
|
|
198
|
+
visibility: __powerlines_deepkit_vendor_type.ReflectionVisibility.public,
|
|
199
|
+
type: arg.reflection.getType(),
|
|
200
|
+
default: arg.default,
|
|
201
|
+
tags: {
|
|
202
|
+
...arg.reflection.getTags(),
|
|
203
|
+
domain: "cli"
|
|
204
|
+
}
|
|
205
|
+
}));
|
|
185
206
|
}
|
|
186
|
-
for (const input of context.inputs.filter((input$1) => input$1.
|
|
207
|
+
for (const input of context.inputs.filter((input$1) => input$1.segments.filter((segment) => !require_plugin_utils_context_helpers.isDynamicPathSegment(segment)).length === command.segments.filter((segment) => !require_plugin_utils_context_helpers.isDynamicPathSegment(segment)).length + 1 && input$1.segments.slice(0, command.segments.length).every((value, index) => value === command.segments[index]))) tree.children[input.name] = await reflectCommandTree(context, input, tree);
|
|
187
208
|
return tree;
|
|
188
209
|
}
|
|
189
210
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve-command.cjs","names":["esbuildPlugin","reflect","ReflectionClass","ReflectionKind","ReflectionVisibility","stringifyType","toArray","appendPath","commonPath","findFilePath","findFolderName","stripStars","replacePath","resolveParentPath","constantCase","titleCase","isSetObject","isSetString","resolveModule","getAppTitle","getDynamicPathSegmentName","isCatchAllPathSegment","isDynamicPathSegment","isOptionalCatchAllPathSegment","isPathSegmentGroup","getDefaultOptions","resolveCommandOptionDescription","kind","optional","name","title","defaultValue","boolean","array","trim","number","resolveCommandId","context","file","commandsPath","split","filter","p","Boolean","join","replaceAll","resolveCommandName","path","requireExtension","resolveCommandPath","resolveCommandDynamicPathSegments","map","findCommandsRoot","config","entry","projectRoot","workspaceConfig","workspaceRoot","Array","isArray","length","sourceRoot","extractCommandOption","command","reflection","propertyType","getType","option","getNameAsString","alias","getTags","description","getDescription","getKind","isOptional","getDefaultValue","env","default","variadic","type","string","Error","extractCommandDynamicSegment","segment","catchAll","segments","some","seg","reflectCommandTree","parent","isVirtual","replace","tree","dynamics","options","children","input","fs","existsSync","debug","id","resolved","plugins","reflectionLevel","metadata","function","parameters","firstParam","objectLiteral","class","optionsReflection","from","propertyReflection","getProperties","reduce","obj","index","paramName","Object","values","forEach","types","addProperty","undefined","visibility","public","tags","domain","inputs","slice","every","value"],"sources":["../../src/helpers/resolve-command.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 { esbuildPlugin } from \"@powerlines/deepkit/esbuild-plugin\";\nimport type {\n ReflectionProperty,\n TypeParameter\n} from \"@powerlines/deepkit/vendor/type\";\nimport {\n reflect,\n ReflectionClass,\n ReflectionKind,\n ReflectionVisibility,\n stringifyType\n} from \"@powerlines/deepkit/vendor/type\";\nimport { toArray } from \"@stryke/convert/to-array\";\nimport { appendPath } from \"@stryke/path/append\";\nimport { commonPath } from \"@stryke/path/common\";\nimport { findFilePath, findFolderName } from \"@stryke/path/file-path-fns\";\nimport { stripStars } from \"@stryke/path/normalize\";\nimport { replacePath } from \"@stryke/path/replace\";\nimport { resolveParentPath } from \"@stryke/path/resolve-parent-path\";\nimport { constantCase } from \"@stryke/string-format/constant-case\";\nimport { titleCase } from \"@stryke/string-format/title-case\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\nimport { resolveModule } from \"powerlines/lib/utilities/resolve\";\nimport {\n getAppTitle,\n getDynamicPathSegmentName,\n isCatchAllPathSegment,\n isDynamicPathSegment,\n isOptionalCatchAllPathSegment,\n isPathSegmentGroup\n} from \"../plugin-utils/context-helpers\";\nimport type {\n CommandDynamicSegment,\n CommandInput,\n CommandModule,\n CommandOption,\n CommandTree,\n NumberCommandOption,\n StringCommandOption\n} from \"../types/command\";\nimport type { Context } from \"../types/context\";\nimport { getDefaultOptions } from \"./utilities\";\n\n/**\n * Resolves the description for a command option based on its reflection.\n *\n * @param kind - The reflection kind of the command option.\n * @param optional - Whether the command option is optional.\n * @param name - The name of the command option.\n * @param title - The title of the command option, if any.\n * @param defaultValue - The default value of the command option, if any.\n * @returns The resolved description for the command option.\n */\nexport function resolveCommandOptionDescription(\n kind: ReflectionKind,\n optional: boolean,\n name: string,\n title?: string,\n defaultValue?: any\n): string {\n return `A${optional && !defaultValue ? \"n optional\" : \"\"} ${\n kind === ReflectionKind.boolean\n ? \"flag provided via the command-line\"\n : \"command-line option\"\n } that allows the user to ${\n kind === ReflectionKind.boolean\n ? \"set the\"\n : kind === ReflectionKind.array\n ? \"specify custom\"\n : \"specify a custom\"\n } ${title?.trim() || titleCase(name)} ${\n kind === ReflectionKind.boolean\n ? \"indicator\"\n : `${kind === ReflectionKind.number ? \"numeric\" : \"string\"} value${\n kind === ReflectionKind.array ? \"s\" : \"\"\n }`\n } that will be used in the application.`;\n}\n\nexport function resolveCommandId(context: Context, file: string): string {\n return replacePath(findFilePath(file), context.commandsPath)\n .split(\"/\")\n .filter(p => Boolean(p) && !isDynamicPathSegment(p))\n .join(\"/\")\n .replaceAll(/^\\/+/g, \"\")\n .replaceAll(/\\/+$/g, \"\")\n .replaceAll(\"/\", \"-\");\n}\n\n/**\n * Finds the command name from the given file path.\n *\n * @param file - The file path to extract the command name from.\n * @returns The command name.\n */\nexport function resolveCommandName(file: string) {\n let path = findFilePath(file);\n let name = findFolderName(file, {\n requireExtension: true\n });\n\n while (isDynamicPathSegment(name)) {\n path = resolveParentPath(path);\n name = findFolderName(path, {\n requireExtension: true\n });\n }\n\n return name;\n}\n\nexport function resolveCommandPath(context: Context, file: string): string {\n return replacePath(findFilePath(file), context.commandsPath)\n .replaceAll(/^\\/+/g, \"\")\n .replaceAll(/\\/+$/g, \"\")\n .split(\"/\")\n .filter(path => path && !isPathSegmentGroup(path))\n .join(\"/\");\n}\n\nexport function resolveCommandDynamicPathSegments(\n context: Context,\n file: string\n): string[] {\n return replacePath(findFilePath(file), context.commandsPath)\n .split(\"/\")\n .filter(path => Boolean(path) && isDynamicPathSegment(path))\n .map(path => getDynamicPathSegmentName(path));\n}\n\nexport function findCommandsRoot(context: Context): string {\n if (isSetString(context.config.entry)) {\n return appendPath(\n appendPath(stripStars(context.config.entry), context.config.projectRoot),\n context.workspaceConfig.workspaceRoot\n );\n } else if (\n isSetObject(context.config.entry) &&\n \"file\" in context.config.entry\n ) {\n return appendPath(\n appendPath(\n stripStars(context.config.entry.file),\n context.config.projectRoot\n ),\n context.workspaceConfig.workspaceRoot\n );\n } else if (\n Array.isArray(context.config.entry) &&\n context.config.entry.length > 0\n ) {\n return commonPath(\n context.config.entry.map(entry =>\n appendPath(\n appendPath(\n stripStars(isSetString(entry) ? entry : entry.file),\n context.config.projectRoot\n ),\n context.workspaceConfig.workspaceRoot\n )\n )\n );\n }\n\n return appendPath(\n context.config.sourceRoot || context.config.projectRoot,\n context.workspaceConfig.workspaceRoot\n );\n}\n\n/**\n * Extracts command parameter information from a type parameter reflection.\n *\n * @param command - The command tree to which the parameter belongs.\n * @param reflection - The type parameter reflection to extract information from.\n * @returns The extracted command option information.\n */\nexport function extractCommandOption(\n command: CommandInput,\n reflection: ReflectionProperty\n): CommandOption {\n const propertyType = reflection.getType();\n\n const option = {\n name: reflection.getNameAsString(),\n alias: reflection.getTags().alias ?? [],\n title:\n reflection.getTags().title?.trim() ||\n titleCase(reflection.getNameAsString()),\n description:\n reflection.getDescription() ||\n resolveCommandOptionDescription(\n reflection.getKind(),\n reflection.isOptional(),\n reflection.getNameAsString(),\n reflection.getTags().title,\n reflection.getDefaultValue()\n ),\n env: constantCase(reflection.getNameAsString()),\n kind: propertyType.kind as\n | ReflectionKind.string\n | ReflectionKind.number\n | ReflectionKind.boolean,\n optional: reflection.isOptional(),\n default: reflection.getDefaultValue(),\n variadic: false\n } as CommandOption;\n if (propertyType.kind === ReflectionKind.array) {\n if (\n propertyType.type.kind === ReflectionKind.string ||\n propertyType.type.kind === ReflectionKind.number\n ) {\n (option as StringCommandOption | NumberCommandOption).variadic = true;\n (option as StringCommandOption | NumberCommandOption).kind =\n propertyType.type.kind;\n } else {\n throw new Error(\n `Unsupported array type for option \"${reflection.getNameAsString()}\" in command \"${\n command.name\n }\". Only string[] and number[] are supported.`\n );\n }\n } else if (\n propertyType.kind !== ReflectionKind.boolean &&\n propertyType.kind !== ReflectionKind.string &&\n propertyType.kind !== ReflectionKind.number\n ) {\n throw new Error(\n `Unsupported type for option \"${reflection.getNameAsString()}\" in command \"${\n command.name\n }\". Only string, number, boolean, string[] and number[] are supported, received ${stringifyType(\n propertyType\n )\n .trim()\n .replaceAll(\" | \", \", or \")}.`\n );\n }\n\n return option;\n}\n\n/**\n * Extracts command parameter information from a type parameter reflection.\n *\n * @param command - The command tree to which the parameter belongs.\n * @param segment - The command path segment corresponding to the parameter.\n * @param reflection - The type parameter reflection to extract information from.\n * @returns The extracted command option information.\n */\nexport function extractCommandDynamicSegment(\n command: CommandInput,\n segment: string,\n reflection: TypeParameter\n): CommandDynamicSegment {\n if (\n reflection.type.kind !== ReflectionKind.string &&\n !(\n reflection.type.kind === ReflectionKind.array &&\n reflection.type.type.kind === ReflectionKind.string\n )\n ) {\n throw new Error(\n `Unsupported type for dynamic path segment \"${segment}\" in command \"${\n command.name\n }\". Only string types (or an array of strings) are supported, received ${stringifyType(\n reflection.type\n )\n .trim()\n .replaceAll(\" | \", \", or \")}.`\n );\n }\n\n const option = {\n name: segment,\n title: titleCase(segment),\n description:\n reflection.description ||\n resolveCommandOptionDescription(\n ReflectionKind.string,\n !!reflection.optional,\n segment,\n titleCase(segment),\n reflection.default\n ),\n optional: reflection.optional,\n default: reflection.default,\n catchAll: command.path.segments.some(\n seg =>\n getDynamicPathSegmentName(segment) === seg && isCatchAllPathSegment(seg)\n )\n } as CommandDynamicSegment;\n\n if (reflection.type.kind === ReflectionKind.array) {\n if (!option.catchAll) {\n throw new Error(\n `Dynamic path segment \"${segment}\" in command \"${\n command.name\n }\" 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.`\n );\n }\n\n option.variadic = true;\n }\n\n if (option.catchAll) {\n if (\n !option.optional &&\n command.path.segments.some(\n seg =>\n getDynamicPathSegmentName(segment) === seg &&\n isOptionalCatchAllPathSegment(seg)\n )\n ) {\n throw new Error(\n `Dynamic path segment \"${segment}\" in command \"${\n command.name\n }\" is defined as a catch-all segment but is not optional. To define an optional catch-all segment, use the \"[[...segment]]\" syntax.`\n );\n } else if (\n option.optional &&\n !command.path.segments.some(\n seg =>\n getDynamicPathSegmentName(segment) === seg &&\n isOptionalCatchAllPathSegment(seg)\n )\n ) {\n throw new Error(\n `Dynamic path segment \"${segment}\" in command \"${\n command.name\n }\" 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.`\n );\n }\n }\n\n return option;\n}\n\n/**\n * Reflects the command tree for a given command input.\n *\n * @param context - The context in which the command is being reflected.\n * @param command - The command input to reflect.\n * @param parent - The parent command tree, if any.\n * @returns The reflected command tree.\n */\nexport async function reflectCommandTree<TContext extends Context = Context>(\n context: TContext,\n command: CommandInput,\n parent?: CommandTree\n): Promise<CommandTree> {\n const title =\n command.title ||\n `${parent?.title ? `${parent.isVirtual ? parent.title.replace(/ Commands$/, \"\") : parent.title} - ` : \"\"}${titleCase(command.name)}${\n command.isVirtual ? \" Commands\" : \"\"\n }`;\n\n const tree = {\n alias: [],\n ...command,\n title,\n path: {\n ...command.path,\n dynamics: {}\n },\n options: getDefaultOptions(context, command),\n parent: parent ?? null,\n children: {}\n } as CommandTree;\n\n if (!command.isVirtual) {\n if (\n !command.entry.input?.file ||\n !context.fs.existsSync(command.entry.input.file)\n ) {\n throw new Error(\n `${\n !command.entry.input?.file ? \"Missing\" : \"Non-existent\"\n } command entry file for \"${command.name}\"`\n );\n }\n\n context.debug(\n `Adding reflection for user-defined command: ${command.id} (file: ${\n command.entry.input.file\n })`\n );\n\n const resolved = await resolveModule<CommandModule>(\n context,\n command.entry.input,\n {\n plugins: [\n esbuildPlugin(context, {\n reflection: \"default\",\n reflectionLevel: \"verbose\"\n })\n ]\n }\n );\n\n const metadata = resolved.metadata ?? {};\n if (isSetString(metadata.title)) {\n tree.title = metadata.title;\n }\n if (isSetString(metadata.description)) {\n tree.description = metadata.description;\n }\n if (\n isSetString(metadata.alias) ||\n (Array.isArray(metadata.alias) && metadata.alias.length > 0)\n ) {\n tree.alias = toArray(metadata.alias);\n }\n\n const type = reflect(resolved);\n\n // const type = await reflectType<TContext>(context, command.entry.input);\n if (type.kind !== ReflectionKind.function) {\n throw new Error(\n `The command entry file \"${command.entry.input.file}\" does not export a valid function.`\n );\n }\n\n tree.description ??=\n command.description ||\n type.description ||\n `The ${tree.title} executable command-line interface.`;\n\n if (type.parameters.length > 0 && type.parameters[0]) {\n const firstParam = type.parameters[0];\n if (\n firstParam.type.kind === ReflectionKind.objectLiteral ||\n firstParam.type.kind === ReflectionKind.class\n ) {\n const optionsReflection = ReflectionClass.from(firstParam.type);\n for (const propertyReflection of optionsReflection.getProperties()) {\n tree.options[propertyReflection.getNameAsString()] =\n extractCommandOption(command, propertyReflection);\n }\n }\n\n tree.path.dynamics = tree.path.segments\n .filter(segment => isDynamicPathSegment(segment))\n .reduce(\n (obj, segment, index) => {\n if (\n type.parameters.length < index + 2 ||\n !type.parameters[index + 1]\n ) {\n return obj;\n }\n\n const paramName = getDynamicPathSegmentName(segment);\n obj[paramName] = extractCommandDynamicSegment(\n command,\n paramName,\n type.parameters[index + 1]!\n );\n\n obj[paramName].description =\n obj[paramName].description ||\n `The ${paramName} ${\n obj[paramName].catchAll\n ? `${obj[paramName].optional ? \"optional \" : \"\"}catch-all`\n : \"dynamic \"\n } segment for the ${command.name} command.`;\n\n return obj;\n },\n {} as Record<string, CommandDynamicSegment>\n );\n }\n } else {\n tree.description ??= `A collection of available ${\n tree.title || titleCase(tree.name)\n } commands that are included in the ${getAppTitle(\n context\n )} command-line application.`;\n }\n\n if (context.env) {\n if (isSetObject(tree.options)) {\n Object.values(tree.options)\n .filter(option => option.env !== false)\n .forEach(option => {\n context.env.types.env.addProperty({\n name: option.env as string,\n optional: option.optional ? true : undefined,\n description: option.description,\n visibility: ReflectionVisibility.public,\n type:\n option.kind === ReflectionKind.string ||\n option.kind === ReflectionKind.number\n ? option.variadic\n ? { kind: ReflectionKind.array, type: { kind: option.kind } }\n : { kind: option.kind }\n : { kind: ReflectionKind.boolean },\n default: option.default,\n tags: {\n title: option.title,\n alias: option.alias\n .filter(alias => alias.length > 0)\n .map(alias => constantCase(alias)),\n domain: \"cli\"\n }\n });\n });\n }\n\n // if (\n // Object.values(tree.path.dynamics).filter(option => option.env !== false)\n // .length > 0\n // ) {\n // Object.values(tree.path.dynamics)\n // .filter(option => option.env !== false)\n // .forEach(option =>\n // context.env.types.env.addProperty({\n // name: constantCase(option.name),\n // optional: option.optional ? true : undefined,\n // description: option.description,\n // visibility: ReflectionVisibility.public,\n // type: option.variadic\n // ? {\n // kind: ReflectionKind.array,\n // type: { kind: ReflectionKind.string }\n // }\n // : { kind: ReflectionKind.string },\n // default: option.default,\n // tags: {\n // domain: \"cli\"\n // }\n // })\n // );\n // }\n }\n\n for (const input of context.inputs.filter(\n input =>\n input.path.segments.filter(segment => !isDynamicPathSegment(segment))\n .length ===\n command.path.segments.filter(segment => !isDynamicPathSegment(segment))\n .length +\n 1 &&\n input.path.segments\n .slice(0, command.path.segments.length)\n .every((value, index) => value === command.path.segments[index])\n )) {\n tree.children[input.name] = await reflectCommandTree(context, input, tree);\n }\n\n return tree;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,SAAgB0B,gCACdC,MACAC,UACAC,MACAC,OACAC,cACQ;AACR,QAAO,IAAIH,YAAY,CAACG,eAAe,eAAe,GAAE,GACtDJ,SAASxB,gDAAe6B,UACpB,uCACA,sBAAqB,2BAEzBL,SAASxB,gDAAe6B,UACpB,YACAL,SAASxB,gDAAe8B,QACtB,mBACA,mBAAkB,GACtBH,OAAOI,MAAM,qDAAcL,KAAK,CAAA,GAClCF,SAASxB,gDAAe6B,UACpB,cACA,GAAGL,SAASxB,gDAAegC,SAAS,YAAY,SA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AAKsC,SAASzB,OAAO,CACtCsE,OAAOC,OAAOf,UAAUe,UAAUpE,QAAQhB,KAAKsC,SAAS+B,OAC/D,CAAC,CACCxB,MAAKG,SAASC,MAAMnE,QAAQ,MAAM2D,mBAAmBnD,SAAS2D,OAAOJ,KAAK;AAG5E,QAAOA"}
|
|
1
|
+
{"version":3,"file":"resolve-command.cjs","names":["esbuildPlugin","reflect","ReflectionClass","ReflectionFunction","ReflectionKind","ReflectionVisibility","stringifyType","toArray","appendPath","commonPath","findFilePath","findFolderName","stripStars","replacePath","resolveParentPath","constantCase","titleCase","isSetObject","isSetString","resolveModule","getAppTitle","getDynamicPathSegmentName","isDynamicPathSegment","isPathSegmentGroup","getDefaultOptions","resolveCommandOptionDescription","kind","optional","name","title","defaultValue","boolean","array","trim","number","resolveCommandParameterDescription","resolveCommandId","context","file","commandsPath","split","filter","p","Boolean","join","replaceAll","resolveCommandName","path","requireExtension","resolveCommandPath","resolveCommandDynamicPathSegments","map","findCommandsRoot","config","entry","projectRoot","workspaceConfig","workspaceRoot","Array","isArray","length","sourceRoot","extractCommandOption","command","reflection","type","getType","option","getNameAsString","alias","getTags","description","getDescription","getKind","isOptional","getDefaultValue","env","default","variadic","string","Error","extractCommandArgument","getName","parameter","reflectCommandTree","parent","isVirtual","replace","tree","icon","options","arguments","children","input","fs","existsSync","debug","id","resolved","plugins","reflectionLevel","metadata","function","parameters","getParameters","objectLiteral","class","optionsReflection","from","propertyReflection","getProperties","slice","arg","Object","values","forEach","types","addProperty","undefined","visibility","public","tags","domain","inputs","segments","segment","every","value","index"],"sources":["../../src/helpers/resolve-command.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 { esbuildPlugin } from \"@powerlines/deepkit/esbuild-plugin\";\nimport type {\n ReflectionParameter,\n ReflectionProperty,\n TypeArray\n} from \"@powerlines/deepkit/vendor/type\";\nimport {\n reflect,\n ReflectionClass,\n ReflectionFunction,\n ReflectionKind,\n ReflectionVisibility,\n stringifyType\n} from \"@powerlines/deepkit/vendor/type\";\nimport { toArray } from \"@stryke/convert/to-array\";\nimport { appendPath } from \"@stryke/path/append\";\nimport { commonPath } from \"@stryke/path/common\";\nimport { findFilePath, findFolderName } from \"@stryke/path/file-path-fns\";\nimport { stripStars } from \"@stryke/path/normalize\";\nimport { replacePath } from \"@stryke/path/replace\";\nimport { resolveParentPath } from \"@stryke/path/resolve-parent-path\";\nimport { constantCase } from \"@stryke/string-format/constant-case\";\nimport { titleCase } from \"@stryke/string-format/title-case\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\nimport { resolveModule } from \"powerlines/lib/utilities/resolve\";\nimport {\n getAppTitle,\n getDynamicPathSegmentName,\n isDynamicPathSegment,\n isPathSegmentGroup\n} from \"../plugin-utils/context-helpers\";\nimport type {\n CommandArgument,\n CommandInput,\n CommandModule,\n CommandOption,\n CommandTree,\n NumberCommandArgument,\n NumberCommandOption,\n StringCommandArgument,\n StringCommandOption\n} from \"../types/command\";\nimport type { Context } from \"../types/context\";\nimport { getDefaultOptions } from \"./utilities\";\n\n/**\n * Resolves the description for a command option based on its reflection.\n *\n * @param kind - The reflection kind of the command option.\n * @param optional - Whether the command option is optional.\n * @param name - The name of the command option.\n * @param title - The title of the command option, if any.\n * @param defaultValue - The default value of the command option, if any.\n * @returns The resolved description for the command option.\n */\nexport function resolveCommandOptionDescription(\n kind: ReflectionKind,\n optional: boolean,\n name: string,\n title?: string,\n defaultValue?: any\n): string {\n return `A${optional && !defaultValue ? \"n optional\" : \"\"} ${\n kind === ReflectionKind.boolean\n ? \"flag provided via the command-line\"\n : \"command-line option\"\n } that allows the user to ${\n kind === ReflectionKind.boolean\n ? \"set the\"\n : kind === ReflectionKind.array\n ? \"specify custom\"\n : \"specify a custom\"\n } ${title?.trim() || titleCase(name)} ${\n kind === ReflectionKind.boolean\n ? \"indicator\"\n : `${kind === ReflectionKind.number ? \"numeric\" : \"string\"} value${\n kind === ReflectionKind.array ? \"s\" : \"\"\n }`\n } that will be used in the application.`;\n}\n\n/**\n * Resolves the description for a command parameter based on its reflection.\n *\n * @param kind - The reflection kind of the command parameter.\n * @param optional - Whether the command parameter is optional.\n * @param name - The name of the command parameter.\n * @param title - The title of the command parameter, if any.\n * @param defaultValue - The default value of the command parameter, if any.\n * @returns The resolved description for the command parameter.\n */\nexport function resolveCommandParameterDescription(\n kind: ReflectionKind,\n optional: boolean,\n name: string,\n title?: string,\n defaultValue?: any\n): string {\n return `A${optional && !defaultValue ? \"n optional\" : \"\"} command-line positional parameter that allows the user to ${\n kind === ReflectionKind.boolean\n ? \"set the\"\n : kind === ReflectionKind.array\n ? \"specify custom\"\n : \"specify a custom\"\n } ${title?.trim() || titleCase(name)} ${\n kind === ReflectionKind.boolean\n ? \"indicator\"\n : `${kind === ReflectionKind.number ? \"numeric\" : \"string\"} value${\n kind === ReflectionKind.array ? \"s\" : \"\"\n }`\n } that will be used in the application.`;\n}\n\nexport function resolveCommandId(context: Context, file: string): string {\n return replacePath(findFilePath(file), context.commandsPath)\n .split(\"/\")\n .filter(p => Boolean(p) && !isDynamicPathSegment(p))\n .join(\"/\")\n .replaceAll(/^\\/+/g, \"\")\n .replaceAll(/\\/+$/g, \"\")\n .replaceAll(\"/\", \"-\");\n}\n\n/**\n * Finds the command name from the given file path.\n *\n * @param file - The file path to extract the command name from.\n * @returns The command name.\n */\nexport function resolveCommandName(file: string) {\n let path = findFilePath(file);\n let name = findFolderName(file, {\n requireExtension: true\n });\n\n while (isDynamicPathSegment(name)) {\n path = resolveParentPath(path);\n name = findFolderName(path, {\n requireExtension: true\n });\n }\n\n return name;\n}\n\nexport function resolveCommandPath(context: Context, file: string): string {\n return replacePath(findFilePath(file), context.commandsPath)\n .replaceAll(/^\\/+/g, \"\")\n .replaceAll(/\\/+$/g, \"\")\n .split(\"/\")\n .filter(path => path && !isPathSegmentGroup(path))\n .join(\"/\");\n}\n\nexport function resolveCommandDynamicPathSegments(\n context: Context,\n file: string\n): string[] {\n return replacePath(findFilePath(file), context.commandsPath)\n .split(\"/\")\n .filter(path => Boolean(path) && isDynamicPathSegment(path))\n .map(path => getDynamicPathSegmentName(path));\n}\n\nexport function findCommandsRoot(context: Context): string {\n if (isSetString(context.config.entry)) {\n return appendPath(\n appendPath(stripStars(context.config.entry), context.config.projectRoot),\n context.workspaceConfig.workspaceRoot\n );\n } else if (\n isSetObject(context.config.entry) &&\n \"file\" in context.config.entry\n ) {\n return appendPath(\n appendPath(\n stripStars(context.config.entry.file),\n context.config.projectRoot\n ),\n context.workspaceConfig.workspaceRoot\n );\n } else if (\n Array.isArray(context.config.entry) &&\n context.config.entry.length > 0\n ) {\n return commonPath(\n context.config.entry.map(entry =>\n appendPath(\n appendPath(\n stripStars(isSetString(entry) ? entry : entry.file),\n context.config.projectRoot\n ),\n context.workspaceConfig.workspaceRoot\n )\n )\n );\n }\n\n return appendPath(\n context.config.sourceRoot || context.config.projectRoot,\n context.workspaceConfig.workspaceRoot\n );\n}\n\n/**\n * Extracts command parameter information from a type parameter reflection.\n *\n * @param command - The command tree to which the parameter belongs.\n * @param reflection - The type parameter reflection to extract information from.\n * @returns The extracted command option information.\n */\nexport function extractCommandOption(\n command: CommandInput,\n reflection: ReflectionProperty\n): CommandOption {\n const type = reflection.getType();\n\n const option = {\n name: reflection.getNameAsString(),\n alias: reflection.getTags().alias ?? [],\n title:\n reflection.getTags().title?.trim() ||\n titleCase(reflection.getNameAsString()),\n description:\n reflection.getDescription() ||\n resolveCommandOptionDescription(\n reflection.getKind(),\n reflection.isOptional(),\n reflection.getNameAsString(),\n reflection.getTags().title,\n reflection.getDefaultValue()\n ),\n env: constantCase(reflection.getNameAsString()),\n kind: type.kind as\n | ReflectionKind.string\n | ReflectionKind.number\n | ReflectionKind.boolean,\n optional: reflection.isOptional(),\n default: reflection.getDefaultValue(),\n variadic: reflection.isArray(),\n reflection\n } as CommandOption;\n\n if (reflection.isArray()) {\n if (\n (type as TypeArray).type.kind === ReflectionKind.string ||\n (type as TypeArray).type.kind === ReflectionKind.number\n ) {\n (option as StringCommandOption | NumberCommandOption).variadic = true;\n (option as StringCommandOption | NumberCommandOption).kind = (\n type as TypeArray\n ).type.kind as ReflectionKind.string | ReflectionKind.number;\n } else {\n throw new Error(\n `Unsupported array type for option \"${reflection.getNameAsString()}\" in command \"${\n command.name\n }\". Only string[] and number[] are supported, received ${stringifyType(\n type\n )\n .trim()\n .replaceAll(\" | \", \", or \")}.`\n );\n }\n } else if (\n type.kind !== ReflectionKind.boolean &&\n type.kind !== ReflectionKind.string &&\n type.kind !== ReflectionKind.number\n ) {\n throw new Error(\n `Unsupported type for option \"${reflection.getNameAsString()}\" in command \"${\n command.name\n }\". Only string, number, boolean, string[] and number[] are supported, received ${stringifyType(\n type\n )\n .trim()\n .replaceAll(\" | \", \", or \")}.`\n );\n }\n\n return option;\n}\n\n/**\n * Extracts command positional argument information from a type parameter reflection.\n *\n * @param command - The command tree to which the parameter belongs.\n * @param reflection - The type parameter reflection to extract information from.\n * @returns The extracted command positional argument information.\n */\nexport function extractCommandArgument(\n command: CommandInput,\n reflection: ReflectionParameter\n): CommandArgument {\n const type = reflection.getType();\n\n if (\n type.kind !== ReflectionKind.string &&\n type.kind !== ReflectionKind.number &&\n type.kind !== ReflectionKind.boolean &&\n !(\n type.kind === ReflectionKind.array &&\n (type.type.kind === ReflectionKind.string ||\n type.type.kind === ReflectionKind.number)\n )\n ) {\n throw new Error(\n `Unsupported type for positional parameter \"${reflection.getName()}\" in command \"${\n command.name\n }\". Only string types (or an array of strings) are supported, received ${stringifyType(\n type\n )\n .trim()\n .replaceAll(\" | \", \", or \")}.`\n );\n }\n\n const option = {\n name: reflection.getName(),\n kind: type.kind,\n title: titleCase(reflection.getName()),\n description:\n reflection.parameter.description ||\n resolveCommandParameterDescription(\n type.kind,\n !!reflection.isOptional(),\n reflection.getName(),\n titleCase(reflection.getName()),\n reflection.getDefaultValue()\n ),\n env: constantCase(reflection.getName()),\n optional: reflection.isOptional(),\n default: reflection.getDefaultValue(),\n reflection\n } as CommandArgument;\n\n if (type.kind === ReflectionKind.array) {\n if (\n type.type.kind === ReflectionKind.string ||\n type.type.kind === ReflectionKind.number\n ) {\n (option as StringCommandArgument | NumberCommandArgument).variadic = true;\n (option as StringCommandArgument | NumberCommandArgument).kind =\n type.type.kind;\n } else {\n throw new Error(\n `Unsupported array type for positional parameter \"${reflection.getName()}\" in command \"${\n command.name\n }\". Only string[] and number[] are supported, received ${stringifyType(\n type\n )\n .trim()\n .replaceAll(\" | \", \", or \")}.`\n );\n }\n } else if (\n type.kind !== ReflectionKind.boolean &&\n type.kind !== ReflectionKind.string &&\n type.kind !== ReflectionKind.number\n ) {\n throw new Error(\n `Unsupported type for positional parameter \"${reflection.getName()}\" in command \"${\n command.name\n }\". Only string, number, boolean, string[] and number[] are supported, received ${stringifyType(\n type\n )\n .trim()\n .replaceAll(\" | \", \", or \")}.`\n );\n }\n\n return option;\n}\n\n/**\n * Reflects the command tree for a given command input.\n *\n * @param context - The context in which the command is being reflected.\n * @param command - The command input to reflect.\n * @param parent - The parent command tree, if any.\n * @returns The reflected command tree.\n */\nexport async function reflectCommandTree<TContext extends Context = Context>(\n context: TContext,\n command: CommandInput,\n parent?: CommandTree\n): Promise<CommandTree> {\n const title =\n command.title ||\n `${\n parent?.title\n ? `${\n parent.isVirtual\n ? parent.title.replace(/ Commands$/, \"\")\n : parent.title\n } - `\n : \"\"\n }${titleCase(command.name)}${command.isVirtual ? \" Commands\" : \"\"}`;\n\n const tree = {\n alias: [],\n icon: parent?.icon,\n ...command,\n title,\n options: getDefaultOptions(context, command),\n arguments: [],\n parent: parent ?? null,\n children: {},\n reflection: null\n } as CommandTree;\n\n if (!command.isVirtual) {\n if (\n !command.entry.input?.file ||\n !context.fs.existsSync(command.entry.input.file)\n ) {\n throw new Error(\n `${\n !command.entry.input?.file ? \"Missing\" : \"Non-existent\"\n } command entry file for \"${command.name}\"`\n );\n }\n\n context.debug(\n `Adding reflection for user-defined command: ${command.id} (file: ${\n command.entry.input.file\n })`\n );\n\n const resolved = await resolveModule<CommandModule>(\n context,\n command.entry.input,\n {\n plugins: [\n esbuildPlugin(context, {\n reflection: \"default\",\n reflectionLevel: \"verbose\"\n })\n ]\n }\n );\n\n const metadata = resolved.metadata ?? {};\n if (isSetString(metadata.title)) {\n tree.title = metadata.title;\n }\n if (isSetString(metadata.description)) {\n tree.description = metadata.description;\n }\n if (\n isSetString(metadata.alias) ||\n (Array.isArray(metadata.alias) && metadata.alias.length > 0)\n ) {\n tree.alias = toArray(metadata.alias);\n }\n if (isSetString(metadata.icon)) {\n tree.icon = metadata.icon;\n }\n\n const type = reflect(resolved);\n if (type.kind !== ReflectionKind.function) {\n throw new Error(\n `The command entry file \"${command.entry.input.file}\" does not export a valid function.`\n );\n }\n\n tree.reflection = new ReflectionFunction(type);\n tree.description ??=\n command.description ||\n type.description ||\n `The ${tree.title} executable command line interface.`;\n\n const parameters = tree.reflection.getParameters();\n if (parameters.length > 0 && parameters[0]) {\n if (\n parameters[0].type.kind === ReflectionKind.objectLiteral ||\n parameters[0].type.kind === ReflectionKind.class\n ) {\n const optionsReflection = ReflectionClass.from(parameters[0].type);\n for (const propertyReflection of optionsReflection.getProperties()) {\n tree.options[propertyReflection.getNameAsString()] =\n extractCommandOption(command, propertyReflection);\n }\n } else {\n throw new Error(\n `The first parameter of the command handler function in \"${\n command.entry.input.file\n }\" must be an object literal or class type representing the command options.`\n );\n }\n\n tree.arguments = parameters\n .slice(1)\n .map(arg => extractCommandArgument(command, arg));\n }\n } else {\n tree.description ??= `A collection of available ${\n tree.title || titleCase(tree.name)\n } commands that are included in the ${getAppTitle(\n context\n )} command line application.`;\n }\n\n if (context.env) {\n if (isSetObject(tree.options)) {\n Object.values(tree.options)\n .filter(option => option.env !== false)\n .forEach(option => {\n context.env.types.env.addProperty({\n name: option.env as string,\n optional: option.optional ? true : undefined,\n description: option.description,\n visibility: ReflectionVisibility.public,\n type:\n option.reflection?.getType() ??\n ((option as StringCommandOption | NumberCommandOption).variadic\n ? { kind: ReflectionKind.array, type: { kind: option.kind } }\n : { kind: option.kind }),\n default: option.default,\n tags: {\n ...option.reflection?.getTags(),\n title: option.title,\n alias: option.alias\n .filter(alias => alias.length > 0)\n .map(alias => constantCase(alias)),\n domain: \"cli\"\n }\n });\n });\n }\n\n Object.values(tree.arguments)\n .filter(arg => arg.env !== false)\n .forEach(arg =>\n context.env.types.env.addProperty({\n name: constantCase(arg.name),\n optional: arg.optional ? true : undefined,\n description: arg.description,\n visibility: ReflectionVisibility.public,\n type: arg.reflection.getType(),\n default: arg.default,\n tags: {\n ...arg.reflection.getTags(),\n domain: \"cli\"\n }\n })\n );\n }\n\n for (const input of context.inputs.filter(\n input =>\n input.segments.filter(segment => !isDynamicPathSegment(segment))\n .length ===\n command.segments.filter(segment => !isDynamicPathSegment(segment))\n .length +\n 1 &&\n input.segments\n .slice(0, command.segments.length)\n .every((value, index) => value === command.segments[index])\n )) {\n tree.children[input.name] = await reflectCommandTree(context, input, tree);\n }\n\n return tree;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0EA,SAAgByB,gCACdC,MACAC,UACAC,MACAC,OACAC,cACQ;AACR,QAAO,IAAIH,YAAY,CAACG,eAAe,eAAe,GAAE,GACtDJ,SAAStB,gDAAe2B,UACpB,uCACA,sBAAqB,2BAEzBL,SAAStB,gDAAe2B,UACpB,YACAL,SAAStB,gDAAe4B,QACtB,mBACA,mBAAkB,GACtBH,OAAOI,MAA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MACX5B,QAAO4B,UAASA,MAAMT,SAAS,EAAE,CACjCT,KAAIkB,iEAAsBA,MAAM,CAAC;KACpCqD,QAAQ;KACV;IACD,CAAC;IACF;AAGNT,SAAOC,OAAO1B,KAAKG,UAAU,CAC1BlD,QAAOuE,QAAOA,IAAIpC,QAAQ,MAAM,CAChCuC,SAAQH,QACP3E,QAAQuC,IAAIwC,MAAMxC,IAAIyC,YAAY;GAChCzF,6DAAmBoF,IAAIpF,KAAK;GAC5BD,UAAUqF,IAAIrF,WAAW,OAAO2F;GAChC/C,aAAayC,IAAIzC;GACjBgD,YAAYlH,sDAAqBmH;GACjCvD,MAAM+C,IAAIhD,WAAWE,SAAS;GAC9BW,SAASmC,IAAInC;GACb4C,MAAM;IACJ,GAAGT,IAAIhD,WAAWM,SAAS;IAC3BoD,QAAQ;IACV;GACD,CACH,CAAC;;AAGL,MAAK,MAAM7B,SAASxD,QAAQsF,OAAOlF,QACjCoD,YACEA,QAAM+B,SAASnF,QAAOoF,YAAW,CAACvG,0DAAqBuG,QAAQ,CAAC,CAC7DjE,WACDG,QAAQ6D,SAASnF,QAAOoF,YAAW,CAACvG,0DAAqBuG,QAAQ,CAAC,CAC/DjE,SACD,KACJiC,QAAM+B,SACHb,MAAM,GAAGhD,QAAQ6D,SAAShE,OAAO,CACjCkE,OAAOC,OAAOC,UAAUD,UAAUhE,QAAQ6D,SAASI,OAC1D,CAAC,CACCxC,MAAKI,SAASC,MAAMjE,QAAQ,MAAMwD,mBAAmB/C,SAASwD,OAAOL,KAAK;AAG5E,QAAOA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getAppTitle,
|
|
1
|
+
import { getAppTitle, isDynamicPathSegment, isPathSegmentGroup } from "../plugin-utils/context-helpers.mjs";
|
|
2
2
|
import { getDefaultOptions } from "./utilities.mjs";
|
|
3
3
|
import { toArray } from "@stryke/convert/to-array";
|
|
4
4
|
import { appendPath } from "@stryke/path/append";
|
|
@@ -9,7 +9,7 @@ import { constantCase } from "@stryke/string-format/constant-case";
|
|
|
9
9
|
import { isSetObject } from "@stryke/type-checks/is-set-object";
|
|
10
10
|
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
11
11
|
import { titleCase } from "@stryke/string-format/title-case";
|
|
12
|
-
import { ReflectionClass, ReflectionKind, ReflectionVisibility, reflect, stringifyType } from "@powerlines/deepkit/vendor/type";
|
|
12
|
+
import { ReflectionClass, ReflectionFunction, ReflectionKind, ReflectionVisibility, reflect, stringifyType } from "@powerlines/deepkit/vendor/type";
|
|
13
13
|
import { esbuildPlugin } from "@powerlines/deepkit/esbuild-plugin";
|
|
14
14
|
import { commonPath } from "@stryke/path/common";
|
|
15
15
|
import { stripStars } from "@stryke/path/normalize";
|
|
@@ -29,6 +29,19 @@ import { resolveModule } from "powerlines/lib/utilities/resolve";
|
|
|
29
29
|
function resolveCommandOptionDescription(kind, optional, name, title, defaultValue) {
|
|
30
30
|
return `A${optional && !defaultValue ? "n optional" : ""} ${kind === ReflectionKind.boolean ? "flag provided via the command-line" : "command-line option"} that allows the user to ${kind === ReflectionKind.boolean ? "set the" : kind === ReflectionKind.array ? "specify custom" : "specify a custom"} ${title?.trim() || titleCase(name)} ${kind === ReflectionKind.boolean ? "indicator" : `${kind === ReflectionKind.number ? "numeric" : "string"} value${kind === ReflectionKind.array ? "s" : ""}`} that will be used in the application.`;
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Resolves the description for a command parameter based on its reflection.
|
|
34
|
+
*
|
|
35
|
+
* @param kind - The reflection kind of the command parameter.
|
|
36
|
+
* @param optional - Whether the command parameter is optional.
|
|
37
|
+
* @param name - The name of the command parameter.
|
|
38
|
+
* @param title - The title of the command parameter, if any.
|
|
39
|
+
* @param defaultValue - The default value of the command parameter, if any.
|
|
40
|
+
* @returns The resolved description for the command parameter.
|
|
41
|
+
*/
|
|
42
|
+
function resolveCommandParameterDescription(kind, optional, name, title, defaultValue) {
|
|
43
|
+
return `A${optional && !defaultValue ? "n optional" : ""} command-line positional parameter that allows the user to ${kind === ReflectionKind.boolean ? "set the" : kind === ReflectionKind.array ? "specify custom" : "specify a custom"} ${title?.trim() || titleCase(name)} ${kind === ReflectionKind.boolean ? "indicator" : `${kind === ReflectionKind.number ? "numeric" : "string"} value${kind === ReflectionKind.array ? "s" : ""}`} that will be used in the application.`;
|
|
44
|
+
}
|
|
32
45
|
function resolveCommandId(context, file) {
|
|
33
46
|
return replacePath(findFilePath(file), context.commandsPath).split("/").filter((p) => Boolean(p) && !isDynamicPathSegment(p)).join("/").replaceAll(/^\/+/g, "").replaceAll(/\/+$/g, "").replaceAll("/", "-");
|
|
34
47
|
}
|
|
@@ -64,51 +77,51 @@ function findCommandsRoot(context) {
|
|
|
64
77
|
* @returns The extracted command option information.
|
|
65
78
|
*/
|
|
66
79
|
function extractCommandOption(command, reflection) {
|
|
67
|
-
const
|
|
80
|
+
const type = reflection.getType();
|
|
68
81
|
const option = {
|
|
69
82
|
name: reflection.getNameAsString(),
|
|
70
83
|
alias: reflection.getTags().alias ?? [],
|
|
71
84
|
title: reflection.getTags().title?.trim() || titleCase(reflection.getNameAsString()),
|
|
72
85
|
description: reflection.getDescription() || resolveCommandOptionDescription(reflection.getKind(), reflection.isOptional(), reflection.getNameAsString(), reflection.getTags().title, reflection.getDefaultValue()),
|
|
73
86
|
env: constantCase(reflection.getNameAsString()),
|
|
74
|
-
kind:
|
|
87
|
+
kind: type.kind,
|
|
75
88
|
optional: reflection.isOptional(),
|
|
76
89
|
default: reflection.getDefaultValue(),
|
|
77
|
-
variadic:
|
|
90
|
+
variadic: reflection.isArray(),
|
|
91
|
+
reflection
|
|
78
92
|
};
|
|
79
|
-
if (
|
|
93
|
+
if (reflection.isArray()) if (type.type.kind === ReflectionKind.string || type.type.kind === ReflectionKind.number) {
|
|
80
94
|
option.variadic = true;
|
|
81
|
-
option.kind =
|
|
82
|
-
} else throw new Error(`Unsupported array type for option "${reflection.getNameAsString()}" in command "${command.name}". Only string[] and number[] are supported.`);
|
|
83
|
-
else if (
|
|
95
|
+
option.kind = type.type.kind;
|
|
96
|
+
} else throw new Error(`Unsupported array type for option "${reflection.getNameAsString()}" in command "${command.name}". Only string[] and number[] are supported, received ${stringifyType(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
97
|
+
else if (type.kind !== ReflectionKind.boolean && type.kind !== ReflectionKind.string && type.kind !== 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 ${stringifyType(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
84
98
|
return option;
|
|
85
99
|
}
|
|
86
100
|
/**
|
|
87
|
-
* Extracts command
|
|
101
|
+
* Extracts command positional argument information from a type parameter reflection.
|
|
88
102
|
*
|
|
89
103
|
* @param command - The command tree to which the parameter belongs.
|
|
90
|
-
* @param segment - The command path segment corresponding to the parameter.
|
|
91
104
|
* @param reflection - The type parameter reflection to extract information from.
|
|
92
|
-
* @returns The extracted command
|
|
105
|
+
* @returns The extracted command positional argument information.
|
|
93
106
|
*/
|
|
94
|
-
function
|
|
95
|
-
|
|
107
|
+
function extractCommandArgument(command, reflection) {
|
|
108
|
+
const type = reflection.getType();
|
|
109
|
+
if (type.kind !== ReflectionKind.string && type.kind !== ReflectionKind.number && type.kind !== ReflectionKind.boolean && !(type.kind === ReflectionKind.array && (type.type.kind === ReflectionKind.string || type.type.kind === 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 ${stringifyType(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
96
110
|
const option = {
|
|
97
|
-
name:
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
111
|
+
name: reflection.getName(),
|
|
112
|
+
kind: type.kind,
|
|
113
|
+
title: titleCase(reflection.getName()),
|
|
114
|
+
description: reflection.parameter.description || resolveCommandParameterDescription(type.kind, !!reflection.isOptional(), reflection.getName(), titleCase(reflection.getName()), reflection.getDefaultValue()),
|
|
115
|
+
env: constantCase(reflection.getName()),
|
|
116
|
+
optional: reflection.isOptional(),
|
|
117
|
+
default: reflection.getDefaultValue(),
|
|
118
|
+
reflection
|
|
103
119
|
};
|
|
104
|
-
if (
|
|
105
|
-
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.`);
|
|
120
|
+
if (type.kind === ReflectionKind.array) if (type.type.kind === ReflectionKind.string || type.type.kind === ReflectionKind.number) {
|
|
106
121
|
option.variadic = true;
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
else if (option.optional && !command.path.segments.some((seg) => getDynamicPathSegmentName(segment) === seg && 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.`);
|
|
111
|
-
}
|
|
122
|
+
option.kind = type.type.kind;
|
|
123
|
+
} else throw new Error(`Unsupported array type for positional parameter "${reflection.getName()}" in command "${command.name}". Only string[] and number[] are supported, received ${stringifyType(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
124
|
+
else if (type.kind !== ReflectionKind.boolean && type.kind !== ReflectionKind.string && type.kind !== 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 ${stringifyType(type).trim().replaceAll(" | ", ", or ")}.`);
|
|
112
125
|
return option;
|
|
113
126
|
}
|
|
114
127
|
/**
|
|
@@ -123,15 +136,14 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
123
136
|
const title = command.title || `${parent?.title ? `${parent.isVirtual ? parent.title.replace(/ Commands$/, "") : parent.title} - ` : ""}${titleCase(command.name)}${command.isVirtual ? " Commands" : ""}`;
|
|
124
137
|
const tree = {
|
|
125
138
|
alias: [],
|
|
139
|
+
icon: parent?.icon,
|
|
126
140
|
...command,
|
|
127
141
|
title,
|
|
128
|
-
path: {
|
|
129
|
-
...command.path,
|
|
130
|
-
dynamics: {}
|
|
131
|
-
},
|
|
132
142
|
options: getDefaultOptions(context, command),
|
|
143
|
+
arguments: [],
|
|
133
144
|
parent: parent ?? null,
|
|
134
|
-
children: {}
|
|
145
|
+
children: {},
|
|
146
|
+
reflection: null
|
|
135
147
|
};
|
|
136
148
|
if (!command.isVirtual) {
|
|
137
149
|
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 +156,20 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
144
156
|
if (isSetString(metadata.title)) tree.title = metadata.title;
|
|
145
157
|
if (isSetString(metadata.description)) tree.description = metadata.description;
|
|
146
158
|
if (isSetString(metadata.alias) || Array.isArray(metadata.alias) && metadata.alias.length > 0) tree.alias = toArray(metadata.alias);
|
|
159
|
+
if (isSetString(metadata.icon)) tree.icon = metadata.icon;
|
|
147
160
|
const type = reflect(resolved);
|
|
148
161
|
if (type.kind !== 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
|
-
|
|
162
|
+
tree.reflection = new ReflectionFunction(type);
|
|
163
|
+
tree.description ??= command.description || type.description || `The ${tree.title} executable command line interface.`;
|
|
164
|
+
const parameters = tree.reflection.getParameters();
|
|
165
|
+
if (parameters.length > 0 && parameters[0]) {
|
|
166
|
+
if (parameters[0].type.kind === ReflectionKind.objectLiteral || parameters[0].type.kind === ReflectionKind.class) {
|
|
167
|
+
const optionsReflection = ReflectionClass.from(parameters[0].type);
|
|
154
168
|
for (const propertyReflection of optionsReflection.getProperties()) tree.options[propertyReflection.getNameAsString()] = extractCommandOption(command, propertyReflection);
|
|
155
|
-
}
|
|
156
|
-
tree.
|
|
157
|
-
if (type.parameters.length < index + 2 || !type.parameters[index + 1]) return obj;
|
|
158
|
-
const paramName = getDynamicPathSegmentName(segment);
|
|
159
|
-
obj[paramName] = extractCommandDynamicSegment(command, paramName, type.parameters[index + 1]);
|
|
160
|
-
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
|
-
return obj;
|
|
162
|
-
}, {});
|
|
169
|
+
} 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.`);
|
|
170
|
+
tree.arguments = parameters.slice(1).map((arg) => extractCommandArgument(command, arg));
|
|
163
171
|
}
|
|
164
|
-
} else tree.description ??= `A collection of available ${tree.title || titleCase(tree.name)} commands that are included in the ${getAppTitle(context)} command
|
|
172
|
+
} else tree.description ??= `A collection of available ${tree.title || titleCase(tree.name)} commands that are included in the ${getAppTitle(context)} command line application.`;
|
|
165
173
|
if (context.env) {
|
|
166
174
|
if (isSetObject(tree.options)) Object.values(tree.options).filter((option) => option.env !== false).forEach((option) => {
|
|
167
175
|
context.env.types.env.addProperty({
|
|
@@ -169,20 +177,33 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
169
177
|
optional: option.optional ? true : void 0,
|
|
170
178
|
description: option.description,
|
|
171
179
|
visibility: ReflectionVisibility.public,
|
|
172
|
-
type: option.
|
|
180
|
+
type: option.reflection?.getType() ?? (option.variadic ? {
|
|
173
181
|
kind: ReflectionKind.array,
|
|
174
182
|
type: { kind: option.kind }
|
|
175
|
-
} : { kind: option.kind }
|
|
183
|
+
} : { kind: option.kind }),
|
|
176
184
|
default: option.default,
|
|
177
185
|
tags: {
|
|
186
|
+
...option.reflection?.getTags(),
|
|
178
187
|
title: option.title,
|
|
179
188
|
alias: option.alias.filter((alias) => alias.length > 0).map((alias) => constantCase(alias)),
|
|
180
189
|
domain: "cli"
|
|
181
190
|
}
|
|
182
191
|
});
|
|
183
192
|
});
|
|
193
|
+
Object.values(tree.arguments).filter((arg) => arg.env !== false).forEach((arg) => context.env.types.env.addProperty({
|
|
194
|
+
name: constantCase(arg.name),
|
|
195
|
+
optional: arg.optional ? true : void 0,
|
|
196
|
+
description: arg.description,
|
|
197
|
+
visibility: ReflectionVisibility.public,
|
|
198
|
+
type: arg.reflection.getType(),
|
|
199
|
+
default: arg.default,
|
|
200
|
+
tags: {
|
|
201
|
+
...arg.reflection.getTags(),
|
|
202
|
+
domain: "cli"
|
|
203
|
+
}
|
|
204
|
+
}));
|
|
184
205
|
}
|
|
185
|
-
for (const input of context.inputs.filter((input$1) => input$1.
|
|
206
|
+
for (const input of context.inputs.filter((input$1) => input$1.segments.filter((segment) => !isDynamicPathSegment(segment)).length === command.segments.filter((segment) => !isDynamicPathSegment(segment)).length + 1 && input$1.segments.slice(0, command.segments.length).every((value, index) => value === command.segments[index]))) tree.children[input.name] = await reflectCommandTree(context, input, tree);
|
|
186
207
|
return tree;
|
|
187
208
|
}
|
|
188
209
|
|