@shell-shock/core 0.2.0 → 0.3.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 +221 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/api.cjs +57 -1
- package/dist/api.cjs.map +1 -0
- package/dist/api.d.cts +9 -1
- package/dist/api.d.cts.map +1 -0
- package/dist/api.d.mts +9 -1
- package/dist/api.d.mts.map +1 -0
- package/dist/api.mjs +55 -1
- package/dist/api.mjs.map +1 -0
- package/dist/components/docs.cjs +105 -0
- package/dist/components/docs.cjs.map +1 -0
- package/dist/components/docs.d.cts +56 -0
- package/dist/components/docs.d.cts.map +1 -0
- package/dist/components/docs.d.mts +56 -0
- package/dist/components/docs.d.mts.map +1 -0
- package/dist/components/docs.mjs +102 -0
- package/dist/components/docs.mjs.map +1 -0
- package/dist/components/index.cjs +5 -0
- package/dist/components/index.d.cts +2 -0
- package/dist/components/index.d.mts +2 -0
- package/dist/components/index.mjs +3 -0
- package/dist/config.cjs +18 -1
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.cts +2 -1
- package/dist/config.d.cts.map +1 -0
- package/dist/config.d.mts +2 -1
- package/dist/config.d.mts.map +1 -0
- package/dist/config.mjs +17 -1
- package/dist/config.mjs.map +1 -0
- package/dist/contexts/command.cjs +21 -0
- package/dist/contexts/command.cjs.map +1 -0
- package/dist/contexts/command.d.cts +18 -0
- package/dist/contexts/command.d.cts.map +1 -0
- package/dist/contexts/command.d.mts +18 -0
- package/dist/contexts/command.d.mts.map +1 -0
- package/dist/contexts/command.mjs +19 -0
- package/dist/contexts/command.mjs.map +1 -0
- package/dist/contexts/index.cjs +4 -0
- package/dist/contexts/index.d.cts +2 -0
- package/dist/contexts/index.d.mts +2 -0
- package/dist/contexts/index.mjs +3 -0
- package/dist/helpers/docs-helpers.cjs +17 -0
- package/dist/helpers/docs-helpers.cjs.map +1 -0
- package/dist/helpers/docs-helpers.mjs +16 -0
- package/dist/helpers/docs-helpers.mjs.map +1 -0
- package/dist/helpers/persistence.cjs +50 -1
- package/dist/helpers/persistence.cjs.map +1 -0
- package/dist/helpers/persistence.mjs +47 -1
- package/dist/helpers/persistence.mjs.map +1 -0
- package/dist/helpers/resolve-command.cjs +177 -1
- package/dist/helpers/resolve-command.cjs.map +1 -0
- package/dist/helpers/resolve-command.mjs +172 -1
- package/dist/helpers/resolve-command.mjs.map +1 -0
- package/dist/helpers/update-package-json.cjs +36 -1
- package/dist/helpers/update-package-json.cjs.map +1 -0
- package/dist/helpers/update-package-json.mjs +34 -1
- package/dist/helpers/update-package-json.mjs.map +1 -0
- package/dist/helpers/utilities.cjs +41 -1
- package/dist/helpers/utilities.cjs.map +1 -0
- package/dist/helpers/utilities.mjs +39 -1
- package/dist/helpers/utilities.mjs.map +1 -0
- package/dist/helpers/validations.cjs +97 -0
- package/dist/helpers/validations.cjs.map +1 -0
- package/dist/helpers/validations.mjs +97 -0
- package/dist/helpers/validations.mjs.map +1 -0
- package/dist/index.cjs +16 -1
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +7 -18
- package/dist/index.d.mts +6 -18
- package/dist/index.mjs +10 -1
- package/dist/index.mjs.map +1 -0
- package/dist/plugin-utils/context-helpers.cjs +86 -0
- package/dist/plugin-utils/context-helpers.cjs.map +1 -0
- package/dist/plugin-utils/context-helpers.d.cts +58 -0
- package/dist/plugin-utils/context-helpers.d.cts.map +1 -0
- package/dist/plugin-utils/context-helpers.d.mts +58 -0
- package/dist/plugin-utils/context-helpers.d.mts.map +1 -0
- package/dist/plugin-utils/context-helpers.mjs +79 -0
- package/dist/plugin-utils/context-helpers.mjs.map +1 -0
- package/dist/plugin-utils/get-command-tree.cjs +24 -0
- package/dist/plugin-utils/get-command-tree.cjs.map +1 -0
- package/dist/plugin-utils/get-command-tree.d.cts +16 -0
- package/dist/plugin-utils/get-command-tree.d.cts.map +1 -0
- package/dist/plugin-utils/get-command-tree.d.mts +16 -0
- package/dist/plugin-utils/get-command-tree.d.mts.map +1 -0
- package/dist/plugin-utils/get-command-tree.mjs +24 -0
- package/dist/plugin-utils/get-command-tree.mjs.map +1 -0
- package/dist/plugin-utils/index.cjs +16 -0
- package/dist/plugin-utils/index.d.cts +5 -0
- package/dist/plugin-utils/index.d.mts +5 -0
- package/dist/plugin-utils/index.mjs +6 -0
- package/dist/plugin-utils/reflect.cjs +25 -0
- package/dist/plugin-utils/reflect.cjs.map +1 -0
- package/dist/plugin-utils/reflect.d.cts +14 -0
- package/dist/plugin-utils/reflect.d.cts.map +1 -0
- package/dist/plugin-utils/reflect.d.mts +14 -0
- package/dist/plugin-utils/reflect.d.mts.map +1 -0
- package/dist/plugin-utils/reflect.mjs +24 -0
- package/dist/plugin-utils/reflect.mjs.map +1 -0
- package/dist/plugin-utils/traverse-command-tree.cjs +33 -0
- package/dist/plugin-utils/traverse-command-tree.cjs.map +1 -0
- package/dist/plugin-utils/traverse-command-tree.d.cts +23 -0
- package/dist/plugin-utils/traverse-command-tree.d.cts.map +1 -0
- package/dist/plugin-utils/traverse-command-tree.d.mts +23 -0
- package/dist/plugin-utils/traverse-command-tree.d.mts.map +1 -0
- package/dist/plugin-utils/traverse-command-tree.mjs +31 -0
- package/dist/plugin-utils/traverse-command-tree.mjs.map +1 -0
- package/dist/plugin.cjs +267 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.d.cts +13 -0
- package/dist/plugin.d.cts.map +1 -0
- package/dist/plugin.d.mts +13 -0
- package/dist/plugin.d.mts.map +1 -0
- package/dist/plugin.mjs +261 -0
- package/dist/plugin.mjs.map +1 -0
- package/dist/types/command.d.cts +13 -4
- package/dist/types/command.d.cts.map +1 -0
- package/dist/types/command.d.mts +13 -4
- package/dist/types/command.d.mts.map +1 -0
- package/dist/types/command.mjs +1 -1
- package/dist/types/config.d.cts +71 -23
- package/dist/types/config.d.cts.map +1 -0
- package/dist/types/config.d.mts +71 -23
- package/dist/types/config.d.mts.map +1 -0
- package/dist/types/config.mjs +1 -1
- package/dist/types/context.d.cts +7 -3
- package/dist/types/context.d.cts.map +1 -0
- package/dist/types/context.d.mts +7 -3
- package/dist/types/context.d.mts.map +1 -0
- package/dist/types/context.mjs +1 -1
- package/dist/types/index.d.cts +4 -4
- package/dist/types/index.d.mts +4 -4
- package/dist/types/index.mjs +1 -1
- package/dist/types/internal.cjs +0 -0
- package/dist/types/internal.d.cts +24 -0
- package/dist/types/internal.d.cts.map +1 -0
- package/dist/types/internal.d.mts +24 -0
- package/dist/types/internal.d.mts.map +1 -0
- package/dist/types/internal.mjs +1 -0
- package/dist/types/options.d.cts +2 -1
- package/dist/types/options.d.cts.map +1 -0
- package/dist/types/options.d.mts +2 -1
- package/dist/types/options.d.mts.map +1 -0
- package/dist/types/options.mjs +1 -1
- package/package.json +230 -55
- package/dist/helpers/get-default-options.cjs +0 -1
- package/dist/helpers/get-default-options.d.cts +0 -15
- package/dist/helpers/get-default-options.d.mts +0 -15
- package/dist/helpers/get-default-options.mjs +0 -1
- package/dist/powerlines.cjs +0 -3
- package/dist/powerlines.d.cts +0 -12
- package/dist/powerlines.d.mts +0 -12
- package/dist/powerlines.mjs +0 -3
|
@@ -1 +1,177 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_plugin_utils_context_helpers = require('../plugin-utils/context-helpers.cjs');
|
|
3
|
+
const require_utilities = require('./utilities.cjs');
|
|
4
|
+
let __stryke_path_append = require("@stryke/path/append");
|
|
5
|
+
let __stryke_path_file_path_fns = require("@stryke/path/file-path-fns");
|
|
6
|
+
let __stryke_path_replace = require("@stryke/path/replace");
|
|
7
|
+
let __stryke_path_resolve_parent_path = require("@stryke/path/resolve-parent-path");
|
|
8
|
+
let __stryke_string_format_constant_case = require("@stryke/string-format/constant-case");
|
|
9
|
+
let __stryke_type_checks_is_set_object = require("@stryke/type-checks/is-set-object");
|
|
10
|
+
let __stryke_type_checks_is_set_string = require("@stryke/type-checks/is-set-string");
|
|
11
|
+
let __stryke_string_format_title_case = require("@stryke/string-format/title-case");
|
|
12
|
+
let __powerlines_deepkit_vendor_type = require("@powerlines/deepkit/vendor/type");
|
|
13
|
+
let __powerlines_deepkit_reflect_type = require("@powerlines/deepkit/reflect-type");
|
|
14
|
+
let __stryke_path_common = require("@stryke/path/common");
|
|
15
|
+
let __stryke_path_normalize = require("@stryke/path/normalize");
|
|
16
|
+
|
|
17
|
+
//#region src/helpers/resolve-command.ts
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the description for a command option based on its reflection.
|
|
20
|
+
*
|
|
21
|
+
* @param propertyReflection - The reflection property of the command option.
|
|
22
|
+
* @returns The resolved description for the command option.
|
|
23
|
+
*/
|
|
24
|
+
function resolveCommandDescription(propertyReflection) {
|
|
25
|
+
return propertyReflection.getDescription()?.trim() || `A${propertyReflection.isOptional() && !propertyReflection.getDefaultValue() ? "n optional" : ""} ${propertyReflection.getType().kind === __powerlines_deepkit_vendor_type.ReflectionKind.boolean ? "flag provided via the command-line" : "command-line option"} that allows the user to ${propertyReflection.getType().kind === __powerlines_deepkit_vendor_type.ReflectionKind.boolean ? "set the" : propertyReflection.getType().kind === __powerlines_deepkit_vendor_type.ReflectionKind.array ? "specify custom" : "specify a custom"} ${propertyReflection.getTags().title?.trim() || (0, __stryke_string_format_title_case.titleCase)(propertyReflection.getNameAsString())} ${propertyReflection.getType().kind === __powerlines_deepkit_vendor_type.ReflectionKind.boolean ? "indicator" : `${propertyReflection.getType().kind === __powerlines_deepkit_vendor_type.ReflectionKind.number ? "numeric" : "string"} value${propertyReflection.getType().kind === __powerlines_deepkit_vendor_type.ReflectionKind.array ? "s" : ""}`} that will be used in the application.`;
|
|
26
|
+
}
|
|
27
|
+
function resolveCommandId(context, file) {
|
|
28
|
+
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.isVariableCommandPath(p)).join("/").replaceAll(/^\/+/g, "").replaceAll(/\/+$/g, "").replaceAll("/", "-");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Finds the command name from the given file path.
|
|
32
|
+
*
|
|
33
|
+
* @param file - The file path to extract the command name from.
|
|
34
|
+
* @returns The command name.
|
|
35
|
+
*/
|
|
36
|
+
function resolveCommandName(file) {
|
|
37
|
+
let path = (0, __stryke_path_file_path_fns.findFilePath)(file);
|
|
38
|
+
let name = (0, __stryke_path_file_path_fns.findFolderName)(file, { requireExtension: true });
|
|
39
|
+
while (require_plugin_utils_context_helpers.isVariableCommandPath(name)) {
|
|
40
|
+
path = (0, __stryke_path_resolve_parent_path.resolveParentPath)(path);
|
|
41
|
+
name = (0, __stryke_path_file_path_fns.findFolderName)(path, { requireExtension: true });
|
|
42
|
+
}
|
|
43
|
+
return name;
|
|
44
|
+
}
|
|
45
|
+
function resolveCommandPath(context, file) {
|
|
46
|
+
return (0, __stryke_path_replace.replacePath)((0, __stryke_path_file_path_fns.findFilePath)(file), context.commandsPath).replaceAll(/^\/+/g, "").replaceAll(/\/+$/g, "");
|
|
47
|
+
}
|
|
48
|
+
function findCommandsRoot(context) {
|
|
49
|
+
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);
|
|
50
|
+
else if ((0, __stryke_type_checks_is_set_object.isSetObject)(context.config.entry) && "file" in context.config.entry) return (0, __stryke_path_append.appendPath)((0, __stryke_path_append.appendPath)((0, __stryke_path_normalize.stripStars)(context.config.entry.file), context.config.projectRoot), context.workspaceConfig.workspaceRoot);
|
|
51
|
+
else if (Array.isArray(context.config.entry) && context.config.entry.length > 0) return (0, __stryke_path_common.commonPath)(context.config.entry.map((entry) => (0, __stryke_path_append.appendPath)((0, __stryke_path_append.appendPath)((0, __stryke_path_normalize.stripStars)((0, __stryke_type_checks_is_set_string.isSetString)(entry) ? entry : entry.file), context.config.projectRoot), context.workspaceConfig.workspaceRoot)));
|
|
52
|
+
return (0, __stryke_path_append.appendPath)(context.config.sourceRoot || context.config.projectRoot, context.workspaceConfig.workspaceRoot);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Extracts command parameter information from a type parameter reflection.
|
|
56
|
+
*
|
|
57
|
+
* @param param - The type parameter reflection to extract information from.
|
|
58
|
+
* @returns The extracted command parameter information.
|
|
59
|
+
*/
|
|
60
|
+
function extractCommandParameters(param) {
|
|
61
|
+
return {
|
|
62
|
+
name: param.name,
|
|
63
|
+
description: param.description,
|
|
64
|
+
optional: !!param.optional,
|
|
65
|
+
variadic: param.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.array,
|
|
66
|
+
default: param.default
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Reflects the command tree for a given command input.
|
|
71
|
+
*
|
|
72
|
+
* @param context - The context in which the command is being reflected.
|
|
73
|
+
* @param command - The command input to reflect.
|
|
74
|
+
* @param parent - The parent command tree, if any.
|
|
75
|
+
* @returns The reflected command tree.
|
|
76
|
+
*/
|
|
77
|
+
async function reflectCommandTree(context, command, parent) {
|
|
78
|
+
const title = command.title || `${parent?.title ? `${parent.title} - ` : ""}${(0, __stryke_string_format_title_case.titleCase)(command.name)}${command.isVirtual ? " Commands" : ""}`;
|
|
79
|
+
const tree = {
|
|
80
|
+
...command,
|
|
81
|
+
title,
|
|
82
|
+
description: command.description || (command.isVirtual ? `A collection of available ${command.title || (0, __stryke_string_format_title_case.titleCase)(command.name)} commands that are included in the ${require_plugin_utils_context_helpers.getAppTitle(context)} command-line application.` : `The ${title} executable command-line interface.`),
|
|
83
|
+
path: {
|
|
84
|
+
...command.path,
|
|
85
|
+
variables: {}
|
|
86
|
+
},
|
|
87
|
+
options: require_utilities.getDefaultOptions(context, command),
|
|
88
|
+
params: [],
|
|
89
|
+
parent: parent ?? null,
|
|
90
|
+
children: {}
|
|
91
|
+
};
|
|
92
|
+
if (!command.isVirtual) {
|
|
93
|
+
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}"`);
|
|
94
|
+
context.debug(`Adding reflection for user-defined command: ${command.id} (file: ${command.entry.input.file})`);
|
|
95
|
+
const type = await (0, __powerlines_deepkit_reflect_type.reflectType)(context, command.entry.input);
|
|
96
|
+
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.`);
|
|
97
|
+
if (type.parameters.length > 0 && type.parameters[0]) {
|
|
98
|
+
const firstParam = type.parameters[0];
|
|
99
|
+
if (firstParam.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.objectLiteral || firstParam.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.class) {
|
|
100
|
+
const optionsReflection = __powerlines_deepkit_vendor_type.ReflectionClass.from(firstParam.type);
|
|
101
|
+
for (const propertyReflection of optionsReflection.getProperties()) {
|
|
102
|
+
const propertyType = propertyReflection.getType();
|
|
103
|
+
tree.options[propertyReflection.getNameAsString()] = {
|
|
104
|
+
name: propertyReflection.getNameAsString(),
|
|
105
|
+
alias: propertyReflection.getTags().alias ?? [],
|
|
106
|
+
title: propertyReflection.getTags().title?.trim() || (0, __stryke_string_format_title_case.titleCase)(propertyReflection.getNameAsString()),
|
|
107
|
+
description: resolveCommandDescription(propertyReflection),
|
|
108
|
+
env: (0, __stryke_string_format_constant_case.constantCase)(propertyReflection.getNameAsString()),
|
|
109
|
+
kind: propertyType.kind,
|
|
110
|
+
optional: propertyReflection.isOptional(),
|
|
111
|
+
default: propertyReflection.getDefaultValue(),
|
|
112
|
+
variadic: false
|
|
113
|
+
};
|
|
114
|
+
if (propertyType.kind === __powerlines_deepkit_vendor_type.ReflectionKind.array) if (propertyType.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.string || propertyType.type.kind === __powerlines_deepkit_vendor_type.ReflectionKind.number) {
|
|
115
|
+
tree.options[propertyReflection.getNameAsString()].variadic = true;
|
|
116
|
+
tree.options[propertyReflection.getNameAsString()].kind = propertyType.type.kind;
|
|
117
|
+
} else throw new Error(`Unsupported array type for option "${propertyReflection.getNameAsString()}" in command "${command.name}". Only string[] and number[] are supported.`);
|
|
118
|
+
else if (propertyType.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.boolean && propertyType.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.string && propertyType.kind !== __powerlines_deepkit_vendor_type.ReflectionKind.number) throw new Error(`Unsupported type for option "${propertyReflection.getNameAsString()}" in command "${command.name}". Only string, number, boolean, string[] and number[] are supported, received ${(0, __powerlines_deepkit_vendor_type.stringifyType)(propertyType).trim().replaceAll(" | ", ", or ")}.`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
tree.path.variables = tree.path.segments.filter((segment) => require_plugin_utils_context_helpers.isVariableCommandPath(segment)).reduce((obj, segment, index) => {
|
|
122
|
+
if (type.parameters.length < index + 2 || !type.parameters[index + 1]) return obj;
|
|
123
|
+
const paramName = require_plugin_utils_context_helpers.getVariableCommandPathName(segment);
|
|
124
|
+
obj[paramName] = extractCommandParameters(type.parameters[index + 1]);
|
|
125
|
+
obj[paramName].description = obj[paramName].description || `The ${paramName} variable for the ${command.name} command.`;
|
|
126
|
+
return obj;
|
|
127
|
+
}, {});
|
|
128
|
+
if (type.parameters.length > 1) type.parameters.slice(tree.path.segments.filter((segment) => require_plugin_utils_context_helpers.isVariableCommandPath(segment)).length + 1).forEach((param) => {
|
|
129
|
+
tree.params.push(extractCommandParameters(param));
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (context.env) {
|
|
134
|
+
if ((0, __stryke_type_checks_is_set_object.isSetObject)(tree.options)) Object.values(tree.options).filter((option) => option.env !== false).forEach((option) => {
|
|
135
|
+
context.env.types.env.addProperty({
|
|
136
|
+
name: option.env,
|
|
137
|
+
optional: option.optional ? true : void 0,
|
|
138
|
+
description: option.description,
|
|
139
|
+
visibility: __powerlines_deepkit_vendor_type.ReflectionVisibility.public,
|
|
140
|
+
type: option.kind === __powerlines_deepkit_vendor_type.ReflectionKind.string || option.kind === __powerlines_deepkit_vendor_type.ReflectionKind.number ? option.variadic ? {
|
|
141
|
+
kind: __powerlines_deepkit_vendor_type.ReflectionKind.array,
|
|
142
|
+
type: { kind: option.kind }
|
|
143
|
+
} : { kind: option.kind } : { kind: __powerlines_deepkit_vendor_type.ReflectionKind.boolean },
|
|
144
|
+
default: option.default,
|
|
145
|
+
tags: {
|
|
146
|
+
title: option.title,
|
|
147
|
+
alias: option.alias.filter((alias) => alias.length > 0).map((alias) => (0, __stryke_string_format_constant_case.constantCase)(alias)),
|
|
148
|
+
domain: "cli"
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
if (tree.params) tree.params.forEach((param) => {
|
|
153
|
+
context.env.types.env.addProperty({
|
|
154
|
+
name: (0, __stryke_string_format_constant_case.constantCase)(param.name),
|
|
155
|
+
optional: param.optional ? true : void 0,
|
|
156
|
+
description: param.description,
|
|
157
|
+
visibility: __powerlines_deepkit_vendor_type.ReflectionVisibility.public,
|
|
158
|
+
type: param.variadic ? {
|
|
159
|
+
kind: __powerlines_deepkit_vendor_type.ReflectionKind.array,
|
|
160
|
+
type: { kind: __powerlines_deepkit_vendor_type.ReflectionKind.string }
|
|
161
|
+
} : { kind: __powerlines_deepkit_vendor_type.ReflectionKind.string },
|
|
162
|
+
default: param.default,
|
|
163
|
+
tags: { domain: "cli" }
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
for (const input of context.inputs.filter((input$1) => input$1.path.segments.filter((segment) => !require_plugin_utils_context_helpers.isVariableCommandPath(segment)).length === command.path.segments.filter((segment) => !require_plugin_utils_context_helpers.isVariableCommandPath(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);
|
|
168
|
+
return tree;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
exports.findCommandsRoot = findCommandsRoot;
|
|
173
|
+
exports.reflectCommandTree = reflectCommandTree;
|
|
174
|
+
exports.resolveCommandId = resolveCommandId;
|
|
175
|
+
exports.resolveCommandName = resolveCommandName;
|
|
176
|
+
exports.resolveCommandPath = resolveCommandPath;
|
|
177
|
+
//# sourceMappingURL=resolve-command.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-command.cjs","names":["reflectType","ReflectionClass","ReflectionKind","ReflectionVisibility","stringifyType","appendPath","commonPath","findFilePath","findFolderName","stripStars","replacePath","resolveParentPath","constantCase","titleCase","isSetObject","isSetString","getAppTitle","getVariableCommandPathName","isVariableCommandPath","getDefaultOptions","resolveCommandDescription","propertyReflection","getDescription","trim","isOptional","getDefaultValue","getType","kind","boolean","array","getTags","title","getNameAsString","number","resolveCommandId","context","file","commandsPath","split","filter","p","Boolean","join","replaceAll","resolveCommandName","path","name","requireExtension","resolveCommandPath","resolveCommandParams","map","findCommandsRoot","config","entry","projectRoot","workspaceConfig","workspaceRoot","Array","isArray","length","sourceRoot","extractCommandParameters","param","description","optional","variadic","type","default","reflectCommandTree","command","parent","isVirtual","tree","variables","options","params","children","input","fs","existsSync","Error","debug","id","function","parameters","firstParam","objectLiteral","class","optionsReflection","from","getProperties","propertyType","alias","env","string","segments","segment","reduce","obj","index","paramName","slice","forEach","push","Object","values","option","types","addProperty","undefined","visibility","public","tags","domain","inputs","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 { reflectType } from \"@powerlines/deepkit/reflect-type\";\nimport type {\n ReflectionProperty,\n TypeParameter\n} from \"@powerlines/deepkit/vendor/type\";\nimport {\n ReflectionClass,\n ReflectionKind,\n ReflectionVisibility,\n stringifyType\n} from \"@powerlines/deepkit/vendor/type\";\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 {\n getAppTitle,\n getVariableCommandPathName,\n isVariableCommandPath\n} from \"../plugin-utils/context-helpers\";\nimport type {\n CommandInput,\n CommandParam,\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 propertyReflection - The reflection property of the command option.\n * @returns The resolved description for the command option.\n */\nexport function resolveCommandDescription(\n propertyReflection: ReflectionProperty\n): string {\n return (\n propertyReflection.getDescription()?.trim() ||\n `A${\n propertyReflection.isOptional() && !propertyReflection.getDefaultValue()\n ? \"n optional\"\n : \"\"\n } ${\n propertyReflection.getType().kind === ReflectionKind.boolean\n ? \"flag provided via the command-line\"\n : \"command-line option\"\n } that allows the user to ${\n propertyReflection.getType().kind === ReflectionKind.boolean\n ? \"set the\"\n : propertyReflection.getType().kind === ReflectionKind.array\n ? \"specify custom\"\n : \"specify a custom\"\n } ${\n propertyReflection.getTags().title?.trim() ||\n titleCase(propertyReflection.getNameAsString())\n } ${\n propertyReflection.getType().kind === ReflectionKind.boolean\n ? \"indicator\"\n : `${propertyReflection.getType().kind === ReflectionKind.number ? \"numeric\" : \"string\"} value${\n propertyReflection.getType().kind === ReflectionKind.array\n ? \"s\"\n : \"\"\n }`\n } that will be used in the application.`\n );\n}\n\nexport function resolveCommandId(context: Context, file: string): string {\n return replacePath(findFilePath(file), context.commandsPath)\n .split(\"/\")\n .filter(p => Boolean(p) && !isVariableCommandPath(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 (isVariableCommandPath(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}\n\nexport function resolveCommandParams(context: Context, file: string): string[] {\n return replacePath(findFilePath(file), context.commandsPath)\n .split(\"/\")\n .filter(p => Boolean(p) && isVariableCommandPath(p))\n .map(p => p.replaceAll(/^\\[+/g, \"\").replaceAll(/\\]+$/g, \"\"));\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 param - The type parameter reflection to extract information from.\n * @returns The extracted command parameter information.\n */\nexport function extractCommandParameters(param: TypeParameter): CommandParam {\n return {\n name: param.name,\n description: param.description,\n optional: !!param.optional,\n variadic: param.type.kind === ReflectionKind.array,\n default: param.default\n } as CommandParam;\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.title} - ` : \"\"}${titleCase(command.name)}${\n command.isVirtual ? \" Commands\" : \"\"\n }`;\n\n const tree = {\n ...command,\n title,\n description:\n command.description ||\n (command.isVirtual\n ? `A collection of available ${command.title || titleCase(command.name)} commands that are included in the ${getAppTitle(\n context\n )} command-line application.`\n : `The ${title} executable command-line interface.`),\n path: {\n ...command.path,\n variables: {}\n },\n options: getDefaultOptions(context, command),\n params: [],\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 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 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 const propertyType = propertyReflection.getType();\n\n tree.options[propertyReflection.getNameAsString()] = {\n name: propertyReflection.getNameAsString(),\n alias: propertyReflection.getTags().alias ?? [],\n title:\n propertyReflection.getTags().title?.trim() ||\n titleCase(propertyReflection.getNameAsString()),\n description: resolveCommandDescription(propertyReflection),\n env: constantCase(propertyReflection.getNameAsString()),\n kind: propertyType.kind as\n | ReflectionKind.string\n | ReflectionKind.number\n | ReflectionKind.boolean,\n optional: propertyReflection.isOptional(),\n default: propertyReflection.getDefaultValue(),\n variadic: false\n };\n if (propertyType.kind === ReflectionKind.array) {\n if (\n propertyType.type.kind === ReflectionKind.string ||\n propertyType.type.kind === ReflectionKind.number\n ) {\n (\n tree.options[propertyReflection.getNameAsString()] as\n | StringCommandOption\n | NumberCommandOption\n ).variadic = true;\n (\n tree.options[propertyReflection.getNameAsString()] as\n | StringCommandOption\n | NumberCommandOption\n ).kind = propertyType.type.kind;\n } else {\n throw new Error(\n `Unsupported array type for option \"${propertyReflection.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 \"${propertyReflection.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 }\n\n tree.path.variables = tree.path.segments\n .filter(segment => isVariableCommandPath(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 = getVariableCommandPathName(segment);\n obj[paramName] = extractCommandParameters(\n type.parameters[index + 1]!\n );\n obj[paramName].description =\n obj[paramName].description ||\n `The ${paramName} variable for the ${command.name} command.`;\n\n return obj;\n },\n {} as Record<string, CommandParam>\n );\n\n if (type.parameters.length > 1) {\n type.parameters\n .slice(\n tree.path.segments.filter(segment => isVariableCommandPath(segment))\n .length + 1\n )\n .forEach(param => {\n tree.params.push(extractCommandParameters(param));\n });\n }\n }\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 (tree.params) {\n tree.params.forEach(param => {\n context.env.types.env.addProperty({\n name: constantCase(param.name),\n optional: param.optional ? true : undefined,\n description: param.description,\n visibility: ReflectionVisibility.public,\n type: param.variadic\n ? {\n kind: ReflectionKind.array,\n type: { kind: ReflectionKind.string }\n }\n : { kind: ReflectionKind.string },\n default: param.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 => !isVariableCommandPath(segment))\n .length ===\n command.path.segments.filter(segment => !isVariableCommandPath(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":";;;;;;;;;;;;;;;;;;;;;;;AA4DA,SAAgBoB,0BACdC,oBACQ;AACR,QACEA,mBAAmBC,gBAAgB,EAAEC,MAAM,IAC3C,IACEF,mBAAmBG,YAAY,IAAI,CAACH,mBAAmBI,iBAAiB,GACpE,eACA,GAAE,GAENJ,mBAAmBK,SAAS,CAACC,SAASzB,gDAAe0B,UACjD,uCACA,sBAAqB,2BAEzBP,mBAAmBK,SAAS,CAACC,SAASzB,gDAAe0B,UACjD,YACAP,mBAAmBK,SAAS,CAACC,SAASzB,gDAAe2B,QACnD,mBACA,mBAAkB,GAExBR,mBAAmBS,SAAS,CAACC,OAAOR,MAAM,qDAChCF,mBAAmBW,iBAAiB,CAAC,CAAA,GAE/CX,mBAAmBK,SAAS,CAACC,SAASzB,gDAAe0B,UACjD,cACA,GAAGP,mBAAmBK,SAAS,CAACC,SAASzB,gDAAe+B,SAAS,YAAY,SAAQ,QACnFZ,mBAAmBK,SAAS,CAACC,SAASzB,gDAAe2B,QACjD,MACA,KACJ;;AAKZ,SAAgBK,iBAAiBC,SAAkBC,MAAsB;AACvE,6FAAgCA,KAAK,EAAED,QAAQE,aAAa,CACzDC,MAAM,IAAI,CACVC,QAAOC,MAAKC,QAAQD,EAAE,IAAI,CAACtB,2DAAsBsB,EAAE,CAAC,CACpDE,KAAK,IAAI,CACTC,WAAW,SAAS,GAAG,CACvBA,WAAW,SAAS,GAAG,CACvBA,WAAW,KAAK,IAAI;;;;;;;;AASzB,SAAgBC,mBAAmBR,MAAc;CAC/C,IAAIS,qDAAoBT,KAAK;CAC7B,IAAIU,uDAAsBV,MAAM,EAC9BW,kBAAkB,MACnB,CAAC;AAEF,QAAO7B,2DAAsB4B,KAAK,EAAE;AAClCD,kEAAyBA,KAAK;AAC9BC,yDAAsBD,MAAM,EAC1BE,kBAAkB,MACnB,CAAC;;AAGJ,QAAOD;;AAGT,SAA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AAQ0D,IAAIc,MAAMd,IAAIe,YAAY;IAChC9D,6DAAmBgB,MAAMhB,KAAK;IAC9BkB,UAAUF,MAAME,WAAW,OAAO6C;IAClC9C,aAAaD,MAAMC;IACnB+C,YAAY3G,sDAAqB4G;IACjC7C,MAAMJ,MAAMG,WACR;KACEtC,MAAMzB,gDAAe2B;KACrBqC,MAAM,EAAEvC,MAAMzB,gDAAe4F,QAAO;KACrC,GACD,EAAEnE,MAAMzB,gDAAe4F,QAAQ;IACnC3B,SAASL,MAAMK;IACf6C,MAAM,EACJC,QAAQ,OACV;IACD,CAAC;IACF;;AAIN,MAAK,MAAMpC,SAAS1C,QAAQ+E,OAAO3E,QACjCsC,YACEA,QAAMhC,KAAKkD,SAASxD,QAAOyD,YAAW,CAAC9E,2DAAsB8E,QAAQ,CAAC,CACnErC,WACDU,QAAQxB,KAAKkD,SAASxD,QAAOyD,YAAW,CAAC9E,2DAAsB8E,QAAQ,CAAC,CACrErC,SACD,KACJkB,QAAMhC,KAAKkD,SACRM,MAAM,GAAGhC,QAAQxB,KAAKkD,SAASpC,OAAO,CACtCwD,OAAOC,OAAOjB,UAAUiB,UAAU/C,QAAQxB,KAAKkD,SAASI,OAC/D,CAAC,CACC3B,MAAKI,SAASC,MAAM/B,QAAQ,MAAMsB,mBAAmBjC,SAAS0C,OAAOL,KAAK;AAG5E,QAAOA"}
|
|
@@ -1 +1,172 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { getAppTitle, getVariableCommandPathName, isVariableCommandPath } from "../plugin-utils/context-helpers.mjs";
|
|
2
|
+
import { getDefaultOptions } from "./utilities.mjs";
|
|
3
|
+
import { appendPath } from "@stryke/path/append";
|
|
4
|
+
import { findFilePath, findFolderName } from "@stryke/path/file-path-fns";
|
|
5
|
+
import { replacePath } from "@stryke/path/replace";
|
|
6
|
+
import { resolveParentPath } from "@stryke/path/resolve-parent-path";
|
|
7
|
+
import { constantCase } from "@stryke/string-format/constant-case";
|
|
8
|
+
import { isSetObject } from "@stryke/type-checks/is-set-object";
|
|
9
|
+
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
10
|
+
import { titleCase } from "@stryke/string-format/title-case";
|
|
11
|
+
import { ReflectionClass, ReflectionKind, ReflectionVisibility, stringifyType } from "@powerlines/deepkit/vendor/type";
|
|
12
|
+
import { reflectType } from "@powerlines/deepkit/reflect-type";
|
|
13
|
+
import { commonPath } from "@stryke/path/common";
|
|
14
|
+
import { stripStars } from "@stryke/path/normalize";
|
|
15
|
+
|
|
16
|
+
//#region src/helpers/resolve-command.ts
|
|
17
|
+
/**
|
|
18
|
+
* Resolves the description for a command option based on its reflection.
|
|
19
|
+
*
|
|
20
|
+
* @param propertyReflection - The reflection property of the command option.
|
|
21
|
+
* @returns The resolved description for the command option.
|
|
22
|
+
*/
|
|
23
|
+
function resolveCommandDescription(propertyReflection) {
|
|
24
|
+
return propertyReflection.getDescription()?.trim() || `A${propertyReflection.isOptional() && !propertyReflection.getDefaultValue() ? "n optional" : ""} ${propertyReflection.getType().kind === ReflectionKind.boolean ? "flag provided via the command-line" : "command-line option"} that allows the user to ${propertyReflection.getType().kind === ReflectionKind.boolean ? "set the" : propertyReflection.getType().kind === ReflectionKind.array ? "specify custom" : "specify a custom"} ${propertyReflection.getTags().title?.trim() || titleCase(propertyReflection.getNameAsString())} ${propertyReflection.getType().kind === ReflectionKind.boolean ? "indicator" : `${propertyReflection.getType().kind === ReflectionKind.number ? "numeric" : "string"} value${propertyReflection.getType().kind === ReflectionKind.array ? "s" : ""}`} that will be used in the application.`;
|
|
25
|
+
}
|
|
26
|
+
function resolveCommandId(context, file) {
|
|
27
|
+
return replacePath(findFilePath(file), context.commandsPath).split("/").filter((p) => Boolean(p) && !isVariableCommandPath(p)).join("/").replaceAll(/^\/+/g, "").replaceAll(/\/+$/g, "").replaceAll("/", "-");
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Finds the command name from the given file path.
|
|
31
|
+
*
|
|
32
|
+
* @param file - The file path to extract the command name from.
|
|
33
|
+
* @returns The command name.
|
|
34
|
+
*/
|
|
35
|
+
function resolveCommandName(file) {
|
|
36
|
+
let path = findFilePath(file);
|
|
37
|
+
let name = findFolderName(file, { requireExtension: true });
|
|
38
|
+
while (isVariableCommandPath(name)) {
|
|
39
|
+
path = resolveParentPath(path);
|
|
40
|
+
name = findFolderName(path, { requireExtension: true });
|
|
41
|
+
}
|
|
42
|
+
return name;
|
|
43
|
+
}
|
|
44
|
+
function resolveCommandPath(context, file) {
|
|
45
|
+
return replacePath(findFilePath(file), context.commandsPath).replaceAll(/^\/+/g, "").replaceAll(/\/+$/g, "");
|
|
46
|
+
}
|
|
47
|
+
function findCommandsRoot(context) {
|
|
48
|
+
if (isSetString(context.config.entry)) return appendPath(appendPath(stripStars(context.config.entry), context.config.projectRoot), context.workspaceConfig.workspaceRoot);
|
|
49
|
+
else if (isSetObject(context.config.entry) && "file" in context.config.entry) return appendPath(appendPath(stripStars(context.config.entry.file), context.config.projectRoot), context.workspaceConfig.workspaceRoot);
|
|
50
|
+
else if (Array.isArray(context.config.entry) && context.config.entry.length > 0) return commonPath(context.config.entry.map((entry) => appendPath(appendPath(stripStars(isSetString(entry) ? entry : entry.file), context.config.projectRoot), context.workspaceConfig.workspaceRoot)));
|
|
51
|
+
return appendPath(context.config.sourceRoot || context.config.projectRoot, context.workspaceConfig.workspaceRoot);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Extracts command parameter information from a type parameter reflection.
|
|
55
|
+
*
|
|
56
|
+
* @param param - The type parameter reflection to extract information from.
|
|
57
|
+
* @returns The extracted command parameter information.
|
|
58
|
+
*/
|
|
59
|
+
function extractCommandParameters(param) {
|
|
60
|
+
return {
|
|
61
|
+
name: param.name,
|
|
62
|
+
description: param.description,
|
|
63
|
+
optional: !!param.optional,
|
|
64
|
+
variadic: param.type.kind === ReflectionKind.array,
|
|
65
|
+
default: param.default
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Reflects the command tree for a given command input.
|
|
70
|
+
*
|
|
71
|
+
* @param context - The context in which the command is being reflected.
|
|
72
|
+
* @param command - The command input to reflect.
|
|
73
|
+
* @param parent - The parent command tree, if any.
|
|
74
|
+
* @returns The reflected command tree.
|
|
75
|
+
*/
|
|
76
|
+
async function reflectCommandTree(context, command, parent) {
|
|
77
|
+
const title = command.title || `${parent?.title ? `${parent.title} - ` : ""}${titleCase(command.name)}${command.isVirtual ? " Commands" : ""}`;
|
|
78
|
+
const tree = {
|
|
79
|
+
...command,
|
|
80
|
+
title,
|
|
81
|
+
description: command.description || (command.isVirtual ? `A collection of available ${command.title || titleCase(command.name)} commands that are included in the ${getAppTitle(context)} command-line application.` : `The ${title} executable command-line interface.`),
|
|
82
|
+
path: {
|
|
83
|
+
...command.path,
|
|
84
|
+
variables: {}
|
|
85
|
+
},
|
|
86
|
+
options: getDefaultOptions(context, command),
|
|
87
|
+
params: [],
|
|
88
|
+
parent: parent ?? null,
|
|
89
|
+
children: {}
|
|
90
|
+
};
|
|
91
|
+
if (!command.isVirtual) {
|
|
92
|
+
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}"`);
|
|
93
|
+
context.debug(`Adding reflection for user-defined command: ${command.id} (file: ${command.entry.input.file})`);
|
|
94
|
+
const type = await reflectType(context, command.entry.input);
|
|
95
|
+
if (type.kind !== ReflectionKind.function) throw new Error(`The command entry file "${command.entry.input.file}" does not export a valid function.`);
|
|
96
|
+
if (type.parameters.length > 0 && type.parameters[0]) {
|
|
97
|
+
const firstParam = type.parameters[0];
|
|
98
|
+
if (firstParam.type.kind === ReflectionKind.objectLiteral || firstParam.type.kind === ReflectionKind.class) {
|
|
99
|
+
const optionsReflection = ReflectionClass.from(firstParam.type);
|
|
100
|
+
for (const propertyReflection of optionsReflection.getProperties()) {
|
|
101
|
+
const propertyType = propertyReflection.getType();
|
|
102
|
+
tree.options[propertyReflection.getNameAsString()] = {
|
|
103
|
+
name: propertyReflection.getNameAsString(),
|
|
104
|
+
alias: propertyReflection.getTags().alias ?? [],
|
|
105
|
+
title: propertyReflection.getTags().title?.trim() || titleCase(propertyReflection.getNameAsString()),
|
|
106
|
+
description: resolveCommandDescription(propertyReflection),
|
|
107
|
+
env: constantCase(propertyReflection.getNameAsString()),
|
|
108
|
+
kind: propertyType.kind,
|
|
109
|
+
optional: propertyReflection.isOptional(),
|
|
110
|
+
default: propertyReflection.getDefaultValue(),
|
|
111
|
+
variadic: false
|
|
112
|
+
};
|
|
113
|
+
if (propertyType.kind === ReflectionKind.array) if (propertyType.type.kind === ReflectionKind.string || propertyType.type.kind === ReflectionKind.number) {
|
|
114
|
+
tree.options[propertyReflection.getNameAsString()].variadic = true;
|
|
115
|
+
tree.options[propertyReflection.getNameAsString()].kind = propertyType.type.kind;
|
|
116
|
+
} else throw new Error(`Unsupported array type for option "${propertyReflection.getNameAsString()}" in command "${command.name}". Only string[] and number[] are supported.`);
|
|
117
|
+
else if (propertyType.kind !== ReflectionKind.boolean && propertyType.kind !== ReflectionKind.string && propertyType.kind !== ReflectionKind.number) throw new Error(`Unsupported type for option "${propertyReflection.getNameAsString()}" in command "${command.name}". Only string, number, boolean, string[] and number[] are supported, received ${stringifyType(propertyType).trim().replaceAll(" | ", ", or ")}.`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
tree.path.variables = tree.path.segments.filter((segment) => isVariableCommandPath(segment)).reduce((obj, segment, index) => {
|
|
121
|
+
if (type.parameters.length < index + 2 || !type.parameters[index + 1]) return obj;
|
|
122
|
+
const paramName = getVariableCommandPathName(segment);
|
|
123
|
+
obj[paramName] = extractCommandParameters(type.parameters[index + 1]);
|
|
124
|
+
obj[paramName].description = obj[paramName].description || `The ${paramName} variable for the ${command.name} command.`;
|
|
125
|
+
return obj;
|
|
126
|
+
}, {});
|
|
127
|
+
if (type.parameters.length > 1) type.parameters.slice(tree.path.segments.filter((segment) => isVariableCommandPath(segment)).length + 1).forEach((param) => {
|
|
128
|
+
tree.params.push(extractCommandParameters(param));
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (context.env) {
|
|
133
|
+
if (isSetObject(tree.options)) Object.values(tree.options).filter((option) => option.env !== false).forEach((option) => {
|
|
134
|
+
context.env.types.env.addProperty({
|
|
135
|
+
name: option.env,
|
|
136
|
+
optional: option.optional ? true : void 0,
|
|
137
|
+
description: option.description,
|
|
138
|
+
visibility: ReflectionVisibility.public,
|
|
139
|
+
type: option.kind === ReflectionKind.string || option.kind === ReflectionKind.number ? option.variadic ? {
|
|
140
|
+
kind: ReflectionKind.array,
|
|
141
|
+
type: { kind: option.kind }
|
|
142
|
+
} : { kind: option.kind } : { kind: ReflectionKind.boolean },
|
|
143
|
+
default: option.default,
|
|
144
|
+
tags: {
|
|
145
|
+
title: option.title,
|
|
146
|
+
alias: option.alias.filter((alias) => alias.length > 0).map((alias) => constantCase(alias)),
|
|
147
|
+
domain: "cli"
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
if (tree.params) tree.params.forEach((param) => {
|
|
152
|
+
context.env.types.env.addProperty({
|
|
153
|
+
name: constantCase(param.name),
|
|
154
|
+
optional: param.optional ? true : void 0,
|
|
155
|
+
description: param.description,
|
|
156
|
+
visibility: ReflectionVisibility.public,
|
|
157
|
+
type: param.variadic ? {
|
|
158
|
+
kind: ReflectionKind.array,
|
|
159
|
+
type: { kind: ReflectionKind.string }
|
|
160
|
+
} : { kind: ReflectionKind.string },
|
|
161
|
+
default: param.default,
|
|
162
|
+
tags: { domain: "cli" }
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
for (const input of context.inputs.filter((input$1) => input$1.path.segments.filter((segment) => !isVariableCommandPath(segment)).length === command.path.segments.filter((segment) => !isVariableCommandPath(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);
|
|
167
|
+
return tree;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
//#endregion
|
|
171
|
+
export { findCommandsRoot, reflectCommandTree, resolveCommandId, resolveCommandName, resolveCommandPath };
|
|
172
|
+
//# sourceMappingURL=resolve-command.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-command.mjs","names":["reflectType","ReflectionClass","ReflectionKind","ReflectionVisibility","stringifyType","appendPath","commonPath","findFilePath","findFolderName","stripStars","replacePath","resolveParentPath","constantCase","titleCase","isSetObject","isSetString","getAppTitle","getVariableCommandPathName","isVariableCommandPath","getDefaultOptions","resolveCommandDescription","propertyReflection","getDescription","trim","isOptional","getDefaultValue","getType","kind","boolean","array","getTags","title","getNameAsString","number","resolveCommandId","context","file","commandsPath","split","filter","p","Boolean","join","replaceAll","resolveCommandName","path","name","requireExtension","resolveCommandPath","resolveCommandParams","map","findCommandsRoot","config","entry","projectRoot","workspaceConfig","workspaceRoot","Array","isArray","length","sourceRoot","extractCommandParameters","param","description","optional","variadic","type","default","reflectCommandTree","command","parent","isVirtual","tree","variables","options","params","children","input","fs","existsSync","Error","debug","id","function","parameters","firstParam","objectLiteral","class","optionsReflection","from","getProperties","propertyType","alias","env","string","segments","segment","reduce","obj","index","paramName","slice","forEach","push","Object","values","option","types","addProperty","undefined","visibility","public","tags","domain","inputs","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 { reflectType } from \"@powerlines/deepkit/reflect-type\";\nimport type {\n ReflectionProperty,\n TypeParameter\n} from \"@powerlines/deepkit/vendor/type\";\nimport {\n ReflectionClass,\n ReflectionKind,\n ReflectionVisibility,\n stringifyType\n} from \"@powerlines/deepkit/vendor/type\";\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 {\n getAppTitle,\n getVariableCommandPathName,\n isVariableCommandPath\n} from \"../plugin-utils/context-helpers\";\nimport type {\n CommandInput,\n CommandParam,\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 propertyReflection - The reflection property of the command option.\n * @returns The resolved description for the command option.\n */\nexport function resolveCommandDescription(\n propertyReflection: ReflectionProperty\n): string {\n return (\n propertyReflection.getDescription()?.trim() ||\n `A${\n propertyReflection.isOptional() && !propertyReflection.getDefaultValue()\n ? \"n optional\"\n : \"\"\n } ${\n propertyReflection.getType().kind === ReflectionKind.boolean\n ? \"flag provided via the command-line\"\n : \"command-line option\"\n } that allows the user to ${\n propertyReflection.getType().kind === ReflectionKind.boolean\n ? \"set the\"\n : propertyReflection.getType().kind === ReflectionKind.array\n ? \"specify custom\"\n : \"specify a custom\"\n } ${\n propertyReflection.getTags().title?.trim() ||\n titleCase(propertyReflection.getNameAsString())\n } ${\n propertyReflection.getType().kind === ReflectionKind.boolean\n ? \"indicator\"\n : `${propertyReflection.getType().kind === ReflectionKind.number ? \"numeric\" : \"string\"} value${\n propertyReflection.getType().kind === ReflectionKind.array\n ? \"s\"\n : \"\"\n }`\n } that will be used in the application.`\n );\n}\n\nexport function resolveCommandId(context: Context, file: string): string {\n return replacePath(findFilePath(file), context.commandsPath)\n .split(\"/\")\n .filter(p => Boolean(p) && !isVariableCommandPath(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 (isVariableCommandPath(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}\n\nexport function resolveCommandParams(context: Context, file: string): string[] {\n return replacePath(findFilePath(file), context.commandsPath)\n .split(\"/\")\n .filter(p => Boolean(p) && isVariableCommandPath(p))\n .map(p => p.replaceAll(/^\\[+/g, \"\").replaceAll(/\\]+$/g, \"\"));\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 param - The type parameter reflection to extract information from.\n * @returns The extracted command parameter information.\n */\nexport function extractCommandParameters(param: TypeParameter): CommandParam {\n return {\n name: param.name,\n description: param.description,\n optional: !!param.optional,\n variadic: param.type.kind === ReflectionKind.array,\n default: param.default\n } as CommandParam;\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.title} - ` : \"\"}${titleCase(command.name)}${\n command.isVirtual ? \" Commands\" : \"\"\n }`;\n\n const tree = {\n ...command,\n title,\n description:\n command.description ||\n (command.isVirtual\n ? `A collection of available ${command.title || titleCase(command.name)} commands that are included in the ${getAppTitle(\n context\n )} command-line application.`\n : `The ${title} executable command-line interface.`),\n path: {\n ...command.path,\n variables: {}\n },\n options: getDefaultOptions(context, command),\n params: [],\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 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 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 const propertyType = propertyReflection.getType();\n\n tree.options[propertyReflection.getNameAsString()] = {\n name: propertyReflection.getNameAsString(),\n alias: propertyReflection.getTags().alias ?? [],\n title:\n propertyReflection.getTags().title?.trim() ||\n titleCase(propertyReflection.getNameAsString()),\n description: resolveCommandDescription(propertyReflection),\n env: constantCase(propertyReflection.getNameAsString()),\n kind: propertyType.kind as\n | ReflectionKind.string\n | ReflectionKind.number\n | ReflectionKind.boolean,\n optional: propertyReflection.isOptional(),\n default: propertyReflection.getDefaultValue(),\n variadic: false\n };\n if (propertyType.kind === ReflectionKind.array) {\n if (\n propertyType.type.kind === ReflectionKind.string ||\n propertyType.type.kind === ReflectionKind.number\n ) {\n (\n tree.options[propertyReflection.getNameAsString()] as\n | StringCommandOption\n | NumberCommandOption\n ).variadic = true;\n (\n tree.options[propertyReflection.getNameAsString()] as\n | StringCommandOption\n | NumberCommandOption\n ).kind = propertyType.type.kind;\n } else {\n throw new Error(\n `Unsupported array type for option \"${propertyReflection.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 \"${propertyReflection.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 }\n\n tree.path.variables = tree.path.segments\n .filter(segment => isVariableCommandPath(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 = getVariableCommandPathName(segment);\n obj[paramName] = extractCommandParameters(\n type.parameters[index + 1]!\n );\n obj[paramName].description =\n obj[paramName].description ||\n `The ${paramName} variable for the ${command.name} command.`;\n\n return obj;\n },\n {} as Record<string, CommandParam>\n );\n\n if (type.parameters.length > 1) {\n type.parameters\n .slice(\n tree.path.segments.filter(segment => isVariableCommandPath(segment))\n .length + 1\n )\n .forEach(param => {\n tree.params.push(extractCommandParameters(param));\n });\n }\n }\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 (tree.params) {\n tree.params.forEach(param => {\n context.env.types.env.addProperty({\n name: constantCase(param.name),\n optional: param.optional ? true : undefined,\n description: param.description,\n visibility: ReflectionVisibility.public,\n type: param.variadic\n ? {\n kind: ReflectionKind.array,\n type: { kind: ReflectionKind.string }\n }\n : { kind: ReflectionKind.string },\n default: param.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 => !isVariableCommandPath(segment))\n .length ===\n command.path.segments.filter(segment => !isVariableCommandPath(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":";;;;;;;;;;;;;;;;;;;;;;AA4DA,SAAgBoB,0BACdC,oBACQ;AACR,QACEA,mBAAmBC,gBAAgB,EAAEC,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2B,SAAQxC,UAAS;AAC3B3B,WAAQ0D,IAAIc,MAAMd,IAAIe,YAAY;IAChC9D,MAAMlC,aAAakD,MAAMhB,KAAK;IAC9BkB,UAAUF,MAAME,WAAW,OAAO6C;IAClC9C,aAAaD,MAAMC;IACnB+C,YAAY3G,qBAAqB4G;IACjC7C,MAAMJ,MAAMG,WACR;KACEtC,MAAMzB,eAAe2B;KACrBqC,MAAM,EAAEvC,MAAMzB,eAAe4F,QAAO;KACrC,GACD,EAAEnE,MAAMzB,eAAe4F,QAAQ;IACnC3B,SAASL,MAAMK;IACf6C,MAAM,EACJC,QAAQ,OACV;IACD,CAAC;IACF;;AAIN,MAAK,MAAMpC,SAAS1C,QAAQ+E,OAAO3E,QACjCsC,YACEA,QAAMhC,KAAKkD,SAASxD,QAAOyD,YAAW,CAAC9E,sBAAsB8E,QAAQ,CAAC,CACnErC,WACDU,QAAQxB,KAAKkD,SAASxD,QAAOyD,YAAW,CAAC9E,sBAAsB8E,QAAQ,CAAC,CACrErC,SACD,KACJkB,QAAMhC,KAAKkD,SACRM,MAAM,GAAGhC,QAAQxB,KAAKkD,SAASpC,OAAO,CACtCwD,OAAOC,OAAOjB,UAAUiB,UAAU/C,QAAQxB,KAAKkD,SAASI,OAC/D,CAAC,CACC3B,MAAKI,SAASC,MAAM/B,QAAQ,MAAMsB,mBAAmBjC,SAAS0C,OAAOL,KAAK;AAG5E,QAAOA"}
|
|
@@ -1 +1,36 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_plugin_utils_context_helpers = require('../plugin-utils/context-helpers.cjs');
|
|
3
|
+
let __stryke_convert_to_array = require("@stryke/convert/to-array");
|
|
4
|
+
let __stryke_path_join_paths = require("@stryke/path/join-paths");
|
|
5
|
+
let __stryke_string_format_kebab_case = require("@stryke/string-format/kebab-case");
|
|
6
|
+
let __stryke_type_checks_is_set_object = require("@stryke/type-checks/is-set-object");
|
|
7
|
+
let __stryke_helpers_get_unique = require("@stryke/helpers/get-unique");
|
|
8
|
+
let __stryke_json_storm_json = require("@stryke/json/storm-json");
|
|
9
|
+
|
|
10
|
+
//#region src/helpers/update-package-json.ts
|
|
11
|
+
function formatBinaryPath(format) {
|
|
12
|
+
return `./dist/bin.${format === "cjs" || Array.isArray(format) && format.includes("cjs") ? "cjs" : "mjs"}`;
|
|
13
|
+
}
|
|
14
|
+
async function updatePackageJsonBinary(context) {
|
|
15
|
+
const packageJsonPath = (0, __stryke_path_join_paths.joinPaths)(context.workspaceConfig.workspaceRoot, context.config.projectRoot, "package.json");
|
|
16
|
+
if (context.config.bin && Array.isArray(context.config.bin) && context.config.bin.length > 0) {
|
|
17
|
+
context.packageJson.bin = Object.fromEntries((0, __stryke_helpers_get_unique.getUnique)((0, __stryke_convert_to_array.toArray)(context.config.bin)).map((bin) => [bin, formatBinaryPath(context.config.output.format)]));
|
|
18
|
+
await context.fs.write(packageJsonPath, __stryke_json_storm_json.StormJSON.stringify(context.packageJson));
|
|
19
|
+
} else {
|
|
20
|
+
if (Array.isArray(context.config.output.format) && context.config.output.format.length > 1) {
|
|
21
|
+
context.packageJson.bin = { [(0, __stryke_string_format_kebab_case.kebabCase)(require_plugin_utils_context_helpers.getAppName(context))]: formatBinaryPath((0, __stryke_convert_to_array.toArray)(context.config.output.format)[0]) };
|
|
22
|
+
context.packageJson.bin = (0, __stryke_convert_to_array.toArray)(context.config.output.format).reduce((ret, format) => {
|
|
23
|
+
ret[`${(0, __stryke_string_format_kebab_case.kebabCase)(require_plugin_utils_context_helpers.getAppName(context))}-${format}`] = formatBinaryPath(format);
|
|
24
|
+
return ret;
|
|
25
|
+
}, context.packageJson.bin);
|
|
26
|
+
} else context.packageJson.bin = { [(0, __stryke_string_format_kebab_case.kebabCase)(require_plugin_utils_context_helpers.getAppName(context))]: formatBinaryPath(context.config.output.format) };
|
|
27
|
+
await context.fs.write(packageJsonPath, __stryke_json_storm_json.StormJSON.stringify(context.packageJson));
|
|
28
|
+
}
|
|
29
|
+
if (!(0, __stryke_type_checks_is_set_object.isSetObject)(context.packageJson.bin)) throw new Error("Unable to determine the CLI binary name. Please specify the `bin` option in your Shell Shock configuration or ensure that the `name` field is set in your package.json.");
|
|
30
|
+
context.config.bin = Object.keys(context.packageJson.bin);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.formatBinaryPath = formatBinaryPath;
|
|
35
|
+
exports.updatePackageJsonBinary = updatePackageJsonBinary;
|
|
36
|
+
//# sourceMappingURL=update-package-json.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"update-package-json.cjs","names":["toArray","getUnique","StormJSON","joinPaths","kebabCase","isSetObject","getAppName","formatBinaryPath","format","Array","isArray","includes","updatePackageJsonBinary","context","packageJsonPath","workspaceConfig","workspaceRoot","config","projectRoot","bin","length","packageJson","Object","fromEntries","map","output","fs","write","stringify","reduce","ret","Error","keys"],"sources":["../../src/helpers/update-package-json.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 { toArray } from \"@stryke/convert/to-array\";\nimport { getUnique } from \"@stryke/helpers/get-unique\";\nimport { StormJSON } from \"@stryke/json/storm-json\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport { kebabCase } from \"@stryke/string-format/kebab-case\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { getAppName } from \"../plugin-utils/context-helpers\";\nimport type { UnresolvedContext } from \"../types/context\";\n\nexport function formatBinaryPath(\n format: string | string[] | undefined\n): string {\n return `./dist/bin.${\n format === \"cjs\" || (Array.isArray(format) && format.includes(\"cjs\"))\n ? \"cjs\"\n : \"mjs\"\n }`;\n}\n\nexport async function updatePackageJsonBinary(\n context: UnresolvedContext\n): Promise<void> {\n const packageJsonPath = joinPaths(\n context.workspaceConfig.workspaceRoot,\n context.config.projectRoot,\n \"package.json\"\n );\n if (\n context.config.bin &&\n Array.isArray(context.config.bin) &&\n context.config.bin.length > 0\n ) {\n context.packageJson.bin = Object.fromEntries(\n getUnique(toArray(context.config.bin)).map(bin => [\n bin,\n formatBinaryPath(context.config.output.format)\n ])\n );\n\n await context.fs.write(\n packageJsonPath,\n StormJSON.stringify(context.packageJson)\n );\n } else {\n if (\n Array.isArray(context.config.output.format) &&\n context.config.output.format.length > 1\n ) {\n context.packageJson.bin = {\n [kebabCase(getAppName(context))]: formatBinaryPath(\n toArray(context.config.output.format)[0]\n )\n };\n context.packageJson.bin = toArray(context.config.output.format).reduce(\n (ret, format) => {\n ret[`${kebabCase(getAppName(context))}-${format}`] =\n formatBinaryPath(format);\n\n return ret;\n },\n context.packageJson.bin\n );\n } else {\n context.packageJson.bin = {\n [kebabCase(getAppName(context))]: formatBinaryPath(\n context.config.output.format\n )\n };\n }\n\n await context.fs.write(\n packageJsonPath,\n StormJSON.stringify(context.packageJson)\n );\n }\n\n if (!isSetObject(context.packageJson.bin)) {\n throw new Error(\n \"Unable to determine the CLI binary name. Please specify the `bin` option in your Shell Shock configuration or ensure that the `name` field is set in your package.json.\"\n );\n }\n\n context.config.bin = Object.keys(context.packageJson.bin);\n}\n"],"mappings":";;;;;;;;;;AA2BA,SAAgBO,iBACdC,QACQ;AACR,QAAO,cACLA,WAAW,SAAUC,MAAMC,QAAQF,OAAO,IAAIA,OAAOG,SAAS,MAAO,GACjE,QACA;;AAIR,eAAsBC,wBACpBC,SACe;CACf,MAAMC,0DACJD,QAAQE,gBAAgBC,eACxBH,QAAQI,OAAOC,a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+
import { toArray } from "@stryke/convert/to-array";
|
|
3
|
+
import { joinPaths } from "@stryke/path/join-paths";
|
|
4
|
+
import { kebabCase } from "@stryke/string-format/kebab-case";
|
|
5
|
+
import { isSetObject } from "@stryke/type-checks/is-set-object";
|
|
6
|
+
import { getUnique } from "@stryke/helpers/get-unique";
|
|
7
|
+
import { StormJSON } from "@stryke/json/storm-json";
|
|
8
|
+
|
|
9
|
+
//#region src/helpers/update-package-json.ts
|
|
10
|
+
function formatBinaryPath(format) {
|
|
11
|
+
return `./dist/bin.${format === "cjs" || Array.isArray(format) && format.includes("cjs") ? "cjs" : "mjs"}`;
|
|
12
|
+
}
|
|
13
|
+
async function updatePackageJsonBinary(context) {
|
|
14
|
+
const packageJsonPath = joinPaths(context.workspaceConfig.workspaceRoot, context.config.projectRoot, "package.json");
|
|
15
|
+
if (context.config.bin && Array.isArray(context.config.bin) && context.config.bin.length > 0) {
|
|
16
|
+
context.packageJson.bin = Object.fromEntries(getUnique(toArray(context.config.bin)).map((bin) => [bin, formatBinaryPath(context.config.output.format)]));
|
|
17
|
+
await context.fs.write(packageJsonPath, StormJSON.stringify(context.packageJson));
|
|
18
|
+
} else {
|
|
19
|
+
if (Array.isArray(context.config.output.format) && context.config.output.format.length > 1) {
|
|
20
|
+
context.packageJson.bin = { [kebabCase(getAppName(context))]: formatBinaryPath(toArray(context.config.output.format)[0]) };
|
|
21
|
+
context.packageJson.bin = toArray(context.config.output.format).reduce((ret, format) => {
|
|
22
|
+
ret[`${kebabCase(getAppName(context))}-${format}`] = formatBinaryPath(format);
|
|
23
|
+
return ret;
|
|
24
|
+
}, context.packageJson.bin);
|
|
25
|
+
} else context.packageJson.bin = { [kebabCase(getAppName(context))]: formatBinaryPath(context.config.output.format) };
|
|
26
|
+
await context.fs.write(packageJsonPath, StormJSON.stringify(context.packageJson));
|
|
27
|
+
}
|
|
28
|
+
if (!isSetObject(context.packageJson.bin)) throw new Error("Unable to determine the CLI binary name. Please specify the `bin` option in your Shell Shock configuration or ensure that the `name` field is set in your package.json.");
|
|
29
|
+
context.config.bin = Object.keys(context.packageJson.bin);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
export { formatBinaryPath, updatePackageJsonBinary };
|
|
34
|
+
//# sourceMappingURL=update-package-json.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"update-package-json.mjs","names":["toArray","getUnique","StormJSON","joinPaths","kebabCase","isSetObject","getAppName","formatBinaryPath","format","Array","isArray","includes","updatePackageJsonBinary","context","packageJsonPath","workspaceConfig","workspaceRoot","config","projectRoot","bin","length","packageJson","Object","fromEntries","map","output","fs","write","stringify","reduce","ret","Error","keys"],"sources":["../../src/helpers/update-package-json.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 { toArray } from \"@stryke/convert/to-array\";\nimport { getUnique } from \"@stryke/helpers/get-unique\";\nimport { StormJSON } from \"@stryke/json/storm-json\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport { kebabCase } from \"@stryke/string-format/kebab-case\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { getAppName } from \"../plugin-utils/context-helpers\";\nimport type { UnresolvedContext } from \"../types/context\";\n\nexport function formatBinaryPath(\n format: string | string[] | undefined\n): string {\n return `./dist/bin.${\n format === \"cjs\" || (Array.isArray(format) && format.includes(\"cjs\"))\n ? \"cjs\"\n : \"mjs\"\n }`;\n}\n\nexport async function updatePackageJsonBinary(\n context: UnresolvedContext\n): Promise<void> {\n const packageJsonPath = joinPaths(\n context.workspaceConfig.workspaceRoot,\n context.config.projectRoot,\n \"package.json\"\n );\n if (\n context.config.bin &&\n Array.isArray(context.config.bin) &&\n context.config.bin.length > 0\n ) {\n context.packageJson.bin = Object.fromEntries(\n getUnique(toArray(context.config.bin)).map(bin => [\n bin,\n formatBinaryPath(context.config.output.format)\n ])\n );\n\n await context.fs.write(\n packageJsonPath,\n StormJSON.stringify(context.packageJson)\n );\n } else {\n if (\n Array.isArray(context.config.output.format) &&\n context.config.output.format.length > 1\n ) {\n context.packageJson.bin = {\n [kebabCase(getAppName(context))]: formatBinaryPath(\n toArray(context.config.output.format)[0]\n )\n };\n context.packageJson.bin = toArray(context.config.output.format).reduce(\n (ret, format) => {\n ret[`${kebabCase(getAppName(context))}-${format}`] =\n formatBinaryPath(format);\n\n return ret;\n },\n context.packageJson.bin\n );\n } else {\n context.packageJson.bin = {\n [kebabCase(getAppName(context))]: formatBinaryPath(\n context.config.output.format\n )\n };\n }\n\n await context.fs.write(\n packageJsonPath,\n StormJSON.stringify(context.packageJson)\n );\n }\n\n if (!isSetObject(context.packageJson.bin)) {\n throw new Error(\n \"Unable to determine the CLI binary name. Please specify the `bin` option in your Shell Shock configuration or ensure that the `name` field is set in your package.json.\"\n );\n }\n\n context.config.bin = Object.keys(context.packageJson.bin);\n}\n"],"mappings":";;;;;;;;;AA2BA,SAAgBO,iBACdC,QACQ;AACR,QAAO,cACLA,WAAW,SAAUC,MAAMC,QAAQF,OAAO,IAAIA,OAAOG,SAAS,MAAO,GACjE,QACA;;AAIR,eAAsBC,wBACpBC,SACe;CACf,MAAMC,kBAAkBX,UACtBU,QAAQE,gBAAgBC,eACxBH,QAAQI,OAAOC,aACf,eACD;AACD,KACEL,QAAQI,OAAOE,OACfV,MAAMC,QAAQG,QAAQI,OAAOE,IAAI,IACjCN,QAAQI,OAAOE,IAAIC,SAAS,GAC5B;AACAP,UAAQQ,YAAYF,MAAMG,OAAOC,YAC/B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