@shell-shock/core 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/components/docs.cjs +6 -28
- package/dist/components/docs.cjs.map +1 -1
- package/dist/components/docs.d.cts +5 -5
- package/dist/components/docs.d.cts.map +1 -1
- package/dist/components/docs.d.mts +5 -5
- package/dist/components/docs.d.mts.map +1 -1
- package/dist/components/docs.mjs +8 -30
- package/dist/components/docs.mjs.map +1 -1
- package/dist/components/index.cjs +10 -1
- package/dist/components/index.d.cts +3 -1
- package/dist/components/index.d.mts +3 -1
- package/dist/components/index.mjs +3 -1
- package/dist/components/options-parser-logic.cjs +504 -0
- package/dist/components/options-parser-logic.cjs.map +1 -0
- package/dist/components/options-parser-logic.d.cts +86 -0
- package/dist/components/options-parser-logic.d.cts.map +1 -0
- package/dist/components/options-parser-logic.d.mts +86 -0
- package/dist/components/options-parser-logic.d.mts.map +1 -0
- package/dist/components/options-parser-logic.mjs +498 -0
- package/dist/components/options-parser-logic.mjs.map +1 -0
- package/dist/components/usage.cjs +41 -0
- package/dist/components/usage.cjs.map +1 -0
- package/dist/components/usage.d.cts +30 -0
- package/dist/components/usage.d.cts.map +1 -0
- package/dist/components/usage.d.mts +30 -0
- package/dist/components/usage.d.mts.map +1 -0
- package/dist/components/usage.mjs +40 -0
- package/dist/components/usage.mjs.map +1 -0
- package/dist/contexts/index.cjs +5 -1
- package/dist/contexts/index.d.cts +2 -1
- package/dist/contexts/index.d.mts +2 -1
- package/dist/contexts/index.mjs +2 -1
- package/dist/contexts/options.cjs +43 -0
- package/dist/contexts/options.cjs.map +1 -0
- package/dist/contexts/options.d.cts +22 -0
- package/dist/contexts/options.d.cts.map +1 -0
- package/dist/contexts/options.d.mts +22 -0
- package/dist/contexts/options.d.mts.map +1 -0
- package/dist/contexts/options.mjs +39 -0
- package/dist/contexts/options.mjs.map +1 -0
- package/dist/helpers/resolve-command.cjs +91 -61
- package/dist/helpers/resolve-command.cjs.map +1 -1
- package/dist/helpers/resolve-command.mjs +93 -63
- package/dist/helpers/resolve-command.mjs.map +1 -1
- package/dist/helpers/validations.cjs +20 -48
- package/dist/helpers/validations.cjs.map +1 -1
- package/dist/helpers/validations.mjs +21 -49
- package/dist/helpers/validations.mjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.mts +3 -3
- package/dist/plugin-utils/context-helpers.cjs +5 -5
- package/dist/plugin-utils/context-helpers.cjs.map +1 -1
- package/dist/plugin-utils/context-helpers.d.cts +3 -3
- package/dist/plugin-utils/context-helpers.d.cts.map +1 -1
- package/dist/plugin-utils/context-helpers.d.mts +3 -3
- package/dist/plugin-utils/context-helpers.d.mts.map +1 -1
- package/dist/plugin-utils/context-helpers.mjs +4 -4
- package/dist/plugin-utils/context-helpers.mjs.map +1 -1
- package/dist/plugin-utils/get-command-tree.cjs +1 -1
- package/dist/plugin-utils/get-command-tree.cjs.map +1 -1
- package/dist/plugin-utils/get-command-tree.mjs +2 -2
- package/dist/plugin-utils/get-command-tree.mjs.map +1 -1
- package/dist/plugin-utils/index.cjs +2 -2
- package/dist/plugin-utils/index.d.cts +2 -2
- package/dist/plugin-utils/index.d.mts +2 -2
- package/dist/plugin-utils/index.mjs +2 -2
- package/dist/plugin.cjs +1 -1
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.mjs +2 -2
- package/dist/plugin.mjs.map +1 -1
- package/dist/types/command.d.cts +14 -10
- package/dist/types/command.d.cts.map +1 -1
- package/dist/types/command.d.mts +14 -10
- package/dist/types/command.d.mts.map +1 -1
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.mts +2 -2
- package/package.json +57 -28
- package/dist/types/internal.cjs +0 -0
- package/dist/types/internal.d.cts +0 -24
- package/dist/types/internal.d.cts.map +0 -1
- package/dist/types/internal.d.mts +0 -24
- package/dist/types/internal.d.mts.map +0 -1
- package/dist/types/internal.mjs +0 -1
|
@@ -1 +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","replace","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.isVirtual ? parent.title.replace(/ Commands$/, \"\") : 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,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Ae6F,QAAQ;IACnC5B,SAASL,MAAMK;IACf8C,MAAM,EACJC,QAAQ,OACV;IACD,CAAC;IACF;;AAIN,MAAK,MAAMpC,SAAS3C,QAAQgF,OAAO5E,QACjCuC,YACEA,QAAMjC,KAAKmD,SAASzD,QAAO0D,YAAW,CAAC/E,2DAAsB+E,QAAQ,CAAC,CACnEtC,WACDU,QAAQxB,KAAKmD,SAASzD,QAAO0D,YAAW,CAAC/E,2DAAsB+E,QAAQ,CAAC,CACrEtC,SACD,KACJmB,QAAMjC,KAAKmD,SACRM,MAAM,GAAGjC,QAAQxB,KAAKmD,SAASrC,OAAO,CACtCyD,OAAOC,OAAOjB,UAAUiB,UAAUhD,QAAQxB,KAAKmD,SAASI,OAC/D,CAAC,CACC3B,MAAKI,SAASC,MAAMhC,QAAQ,MAAMsB,mBAAmBjC,SAAS2C,OAAOL,KAAK;AAG5E,QAAOA"}
|
|
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","getPositionalCommandOptionName","isPositionalCommandOption","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","resolveCommandParams","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","extractCommandPositionalOption","segment","reflectCommandTree","parent","isVirtual","replace","tree","positional","options","children","input","fs","existsSync","debug","id","resolved","plugins","reflectionLevel","metadata","function","parameters","firstParam","objectLiteral","class","optionsReflection","from","propertyReflection","getProperties","segments","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 getPositionalCommandOptionName,\n isPositionalCommandOption\n} from \"../plugin-utils/context-helpers\";\nimport type {\n CommandInput,\n CommandModule,\n CommandOption,\n CommandPositionalOption,\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) && !isPositionalCommandOption(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 (isPositionalCommandOption(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) && isPositionalCommandOption(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 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 extractCommandPositionalOption(\n command: CommandInput,\n segment: string,\n reflection: TypeParameter\n): CommandPositionalOption {\n if (\n reflection.type.kind !== ReflectionKind.string &&\n reflection.type.kind !== ReflectionKind.number &&\n !(\n reflection.type.kind === ReflectionKind.array &&\n (reflection.type.type.kind === ReflectionKind.string ||\n reflection.type.type.kind === ReflectionKind.number)\n )\n ) {\n throw new Error(\n `Unsupported type for positional option \"${segment}\" in command \"${\n command.name\n }\". Only string and number types (or string[] and number[]) are supported, received ${stringifyType(\n reflection.type\n )\n .trim()\n .replaceAll(\" | \", \", or \")}.`\n );\n }\n\n const option = {\n name: segment,\n alias: [],\n title: titleCase(segment),\n description:\n reflection.description ||\n resolveCommandOptionDescription(\n reflection.type.kind,\n !!reflection.optional,\n segment,\n titleCase(segment),\n reflection.default\n ),\n env: constantCase(segment),\n kind: reflection.type.kind,\n optional: reflection.optional,\n default: reflection.default,\n variadic: false\n } as CommandPositionalOption;\n\n if (reflection.type.kind === ReflectionKind.array) {\n if (\n reflection.type.type.kind === ReflectionKind.string ||\n reflection.type.type.kind === ReflectionKind.number\n ) {\n (option as StringCommandOption | NumberCommandOption).variadic = true;\n (option as StringCommandOption | NumberCommandOption).kind =\n reflection.type.type.kind;\n } else {\n throw new Error(\n `Unsupported array type for option \"${segment}\" in command \"${\n command.name\n }\". Only string[] and number[] are supported.`\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 positional: {}\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.positional = tree.path.segments\n .filter(segment => isPositionalCommandOption(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 = getPositionalCommandOptionName(segment);\n obj[paramName] = extractCommandPositionalOption(\n command,\n paramName,\n type.parameters[index + 1]!\n );\n\n obj[paramName].description =\n obj[paramName].description ||\n `The ${paramName} positional option for the ${command.name} command.`;\n\n return obj;\n },\n {} as Record<string, CommandPositionalOption>\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.positional).filter(option => option.env !== false)\n .length > 0\n ) {\n Object.values(tree.path.positional)\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 => !isPositionalCommandOption(segment))\n .length ===\n command.path.segments.filter(\n segment => !isPositionalCommandOption(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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqEA,SAAgBuB,gCACdC,MACAC,UACAC,MACAC,OACAC,cACQ;AACR,QAAO,IAAIH,YAAY,CAACG,eAAe,eAAe,GAAE,GACtDJ,SAASrB,gDAAe0B,UACpB,uCACA,sBAAqB,2BAEzBL,SAASrB,gDAAe0B,UACpB,YACAL,SAASrB,gDAAe2B,QACtB,mBACA,mBAAkB,GACtBH,OAAOI,MAAM,qDAAcL,KAAK,CAAA,GAClCF,SAASrB,gDAAe0B,UACpB,cACA,GAAGL,SAASrB,gDAAe6B,SAAS,YAAY,SAAQ,QACtDR,SAASrB,gDAAe2B,QAAQ,MAAM,KACtC;;AAIV,SAAgBG,iBAAiBC,SAAkBC,MAAsB;AACvE,6FAAgCA,KAAK,EAAED,QAAQE,aAAa,CACzDC,MAAM,IAAI,CACVC,QAAOC,MAAKC,QAAQD,EAAE,IAAI,CAAClB,+DAA0BkB,EAAE,CAAC,CACxDE,KAAK,IAAI,CACTC,WAAW,SAAS,GAAG,CACvBA,WAAW,SAAS,GAAG,CACvBA,WAAW,KAAK,IAAI;;;;;;;;AASzB,SAAgBC,mBAAmBR,MA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,MAAMuD,mBAAmB/C,SAASuD,OAAOJ,KAAK;AAG5E,QAAOA"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { getAppTitle,
|
|
1
|
+
import { getAppTitle, getPositionalCommandOptionName, isPositionalCommandOption } from "../plugin-utils/context-helpers.mjs";
|
|
2
2
|
import { getDefaultOptions } from "./utilities.mjs";
|
|
3
|
+
import { toArray } from "@stryke/convert/to-array";
|
|
3
4
|
import { appendPath } from "@stryke/path/append";
|
|
4
5
|
import { findFilePath, findFolderName } from "@stryke/path/file-path-fns";
|
|
5
6
|
import { replacePath } from "@stryke/path/replace";
|
|
@@ -8,23 +9,28 @@ import { constantCase } from "@stryke/string-format/constant-case";
|
|
|
8
9
|
import { isSetObject } from "@stryke/type-checks/is-set-object";
|
|
9
10
|
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
10
11
|
import { titleCase } from "@stryke/string-format/title-case";
|
|
11
|
-
import { ReflectionClass, ReflectionKind, ReflectionVisibility, stringifyType } from "@powerlines/deepkit/vendor/type";
|
|
12
|
-
import {
|
|
12
|
+
import { ReflectionClass, ReflectionKind, ReflectionVisibility, reflect, stringifyType } from "@powerlines/deepkit/vendor/type";
|
|
13
|
+
import { esbuildPlugin } from "@powerlines/deepkit/esbuild-plugin";
|
|
13
14
|
import { commonPath } from "@stryke/path/common";
|
|
14
15
|
import { stripStars } from "@stryke/path/normalize";
|
|
16
|
+
import { resolveModule } from "powerlines/lib/utilities/resolve";
|
|
15
17
|
|
|
16
18
|
//#region src/helpers/resolve-command.ts
|
|
17
19
|
/**
|
|
18
20
|
* Resolves the description for a command option based on its reflection.
|
|
19
21
|
*
|
|
20
|
-
* @param
|
|
22
|
+
* @param kind - The reflection kind of the command option.
|
|
23
|
+
* @param optional - Whether the command option is optional.
|
|
24
|
+
* @param name - The name of the command option.
|
|
25
|
+
* @param title - The title of the command option, if any.
|
|
26
|
+
* @param defaultValue - The default value of the command option, if any.
|
|
21
27
|
* @returns The resolved description for the command option.
|
|
22
28
|
*/
|
|
23
|
-
function
|
|
24
|
-
return
|
|
29
|
+
function resolveCommandOptionDescription(kind, optional, name, title, defaultValue) {
|
|
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.`;
|
|
25
31
|
}
|
|
26
32
|
function resolveCommandId(context, file) {
|
|
27
|
-
return replacePath(findFilePath(file), context.commandsPath).split("/").filter((p) => Boolean(p) && !
|
|
33
|
+
return replacePath(findFilePath(file), context.commandsPath).split("/").filter((p) => Boolean(p) && !isPositionalCommandOption(p)).join("/").replaceAll(/^\/+/g, "").replaceAll(/\/+$/g, "").replaceAll("/", "-");
|
|
28
34
|
}
|
|
29
35
|
/**
|
|
30
36
|
* Finds the command name from the given file path.
|
|
@@ -35,7 +41,7 @@ function resolveCommandId(context, file) {
|
|
|
35
41
|
function resolveCommandName(file) {
|
|
36
42
|
let path = findFilePath(file);
|
|
37
43
|
let name = findFolderName(file, { requireExtension: true });
|
|
38
|
-
while (
|
|
44
|
+
while (isPositionalCommandOption(name)) {
|
|
39
45
|
path = resolveParentPath(path);
|
|
40
46
|
name = findFolderName(path, { requireExtension: true });
|
|
41
47
|
}
|
|
@@ -53,17 +59,56 @@ function findCommandsRoot(context) {
|
|
|
53
59
|
/**
|
|
54
60
|
* Extracts command parameter information from a type parameter reflection.
|
|
55
61
|
*
|
|
56
|
-
* @param
|
|
57
|
-
* @
|
|
62
|
+
* @param command - The command tree to which the parameter belongs.
|
|
63
|
+
* @param reflection - The type parameter reflection to extract information from.
|
|
64
|
+
* @returns The extracted command option information.
|
|
58
65
|
*/
|
|
59
|
-
function
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
function extractCommandOption(command, reflection) {
|
|
67
|
+
const propertyType = reflection.getType();
|
|
68
|
+
const option = {
|
|
69
|
+
name: reflection.getNameAsString(),
|
|
70
|
+
alias: reflection.getTags().alias ?? [],
|
|
71
|
+
title: reflection.getTags().title?.trim() || titleCase(reflection.getNameAsString()),
|
|
72
|
+
description: reflection.getDescription() || resolveCommandOptionDescription(reflection.getKind(), reflection.isOptional(), reflection.getNameAsString(), reflection.getTags().title, reflection.getDefaultValue()),
|
|
73
|
+
env: constantCase(reflection.getNameAsString()),
|
|
74
|
+
kind: propertyType.kind,
|
|
75
|
+
optional: reflection.isOptional(),
|
|
76
|
+
default: reflection.getDefaultValue(),
|
|
77
|
+
variadic: false
|
|
66
78
|
};
|
|
79
|
+
if (propertyType.kind === ReflectionKind.array) if (propertyType.type.kind === ReflectionKind.string || propertyType.type.kind === ReflectionKind.number) {
|
|
80
|
+
option.variadic = true;
|
|
81
|
+
option.kind = propertyType.type.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 (propertyType.kind !== ReflectionKind.boolean && propertyType.kind !== ReflectionKind.string && propertyType.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(propertyType).trim().replaceAll(" | ", ", or ")}.`);
|
|
84
|
+
return option;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Extracts command parameter information from a type parameter reflection.
|
|
88
|
+
*
|
|
89
|
+
* @param command - The command tree to which the parameter belongs.
|
|
90
|
+
* @param segment - The command path segment corresponding to the parameter.
|
|
91
|
+
* @param reflection - The type parameter reflection to extract information from.
|
|
92
|
+
* @returns The extracted command option information.
|
|
93
|
+
*/
|
|
94
|
+
function extractCommandPositionalOption(command, segment, reflection) {
|
|
95
|
+
if (reflection.type.kind !== ReflectionKind.string && reflection.type.kind !== ReflectionKind.number && !(reflection.type.kind === ReflectionKind.array && (reflection.type.type.kind === ReflectionKind.string || reflection.type.type.kind === ReflectionKind.number))) throw new Error(`Unsupported type for positional option "${segment}" in command "${command.name}". Only string and number types (or string[] and number[]) are supported, received ${stringifyType(reflection.type).trim().replaceAll(" | ", ", or ")}.`);
|
|
96
|
+
const option = {
|
|
97
|
+
name: segment,
|
|
98
|
+
alias: [],
|
|
99
|
+
title: titleCase(segment),
|
|
100
|
+
description: reflection.description || resolveCommandOptionDescription(reflection.type.kind, !!reflection.optional, segment, titleCase(segment), reflection.default),
|
|
101
|
+
env: constantCase(segment),
|
|
102
|
+
kind: reflection.type.kind,
|
|
103
|
+
optional: reflection.optional,
|
|
104
|
+
default: reflection.default,
|
|
105
|
+
variadic: false
|
|
106
|
+
};
|
|
107
|
+
if (reflection.type.kind === ReflectionKind.array) if (reflection.type.type.kind === ReflectionKind.string || reflection.type.type.kind === ReflectionKind.number) {
|
|
108
|
+
option.variadic = true;
|
|
109
|
+
option.kind = reflection.type.type.kind;
|
|
110
|
+
} else throw new Error(`Unsupported array type for option "${segment}" in command "${command.name}". Only string[] and number[] are supported.`);
|
|
111
|
+
return option;
|
|
67
112
|
}
|
|
68
113
|
/**
|
|
69
114
|
* Reflects the command tree for a given command input.
|
|
@@ -76,59 +121,46 @@ function extractCommandParameters(param) {
|
|
|
76
121
|
async function reflectCommandTree(context, command, parent) {
|
|
77
122
|
const title = command.title || `${parent?.title ? `${parent.isVirtual ? parent.title.replace(/ Commands$/, "") : parent.title} - ` : ""}${titleCase(command.name)}${command.isVirtual ? " Commands" : ""}`;
|
|
78
123
|
const tree = {
|
|
124
|
+
alias: [],
|
|
79
125
|
...command,
|
|
80
126
|
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
127
|
path: {
|
|
83
128
|
...command.path,
|
|
84
|
-
|
|
129
|
+
positional: {}
|
|
85
130
|
},
|
|
86
131
|
options: getDefaultOptions(context, command),
|
|
87
|
-
params: [],
|
|
88
132
|
parent: parent ?? null,
|
|
89
133
|
children: {}
|
|
90
134
|
};
|
|
91
135
|
if (!command.isVirtual) {
|
|
92
136
|
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
137
|
context.debug(`Adding reflection for user-defined command: ${command.id} (file: ${command.entry.input.file})`);
|
|
94
|
-
const
|
|
138
|
+
const resolved = await resolveModule(context, command.entry.input, { plugins: [esbuildPlugin(context, {
|
|
139
|
+
reflection: "default",
|
|
140
|
+
reflectionLevel: "verbose"
|
|
141
|
+
})] });
|
|
142
|
+
const metadata = resolved.metadata ?? {};
|
|
143
|
+
if (isSetString(metadata.title)) tree.title = metadata.title;
|
|
144
|
+
if (isSetString(metadata.description)) tree.description = metadata.description;
|
|
145
|
+
if (isSetString(metadata.alias) || Array.isArray(metadata.alias) && metadata.alias.length > 0) tree.alias = toArray(metadata.alias);
|
|
146
|
+
const type = reflect(resolved);
|
|
95
147
|
if (type.kind !== ReflectionKind.function) throw new Error(`The command entry file "${command.entry.input.file}" does not export a valid function.`);
|
|
148
|
+
tree.description ??= command.description || type.description || `The ${tree.title} executable command-line interface.`;
|
|
96
149
|
if (type.parameters.length > 0 && type.parameters[0]) {
|
|
97
150
|
const firstParam = type.parameters[0];
|
|
98
151
|
if (firstParam.type.kind === ReflectionKind.objectLiteral || firstParam.type.kind === ReflectionKind.class) {
|
|
99
152
|
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
|
-
}
|
|
153
|
+
for (const propertyReflection of optionsReflection.getProperties()) tree.options[propertyReflection.getNameAsString()] = extractCommandOption(command, propertyReflection);
|
|
119
154
|
}
|
|
120
|
-
tree.path.
|
|
155
|
+
tree.path.positional = tree.path.segments.filter((segment) => isPositionalCommandOption(segment)).reduce((obj, segment, index) => {
|
|
121
156
|
if (type.parameters.length < index + 2 || !type.parameters[index + 1]) return obj;
|
|
122
|
-
const paramName =
|
|
123
|
-
obj[paramName] =
|
|
124
|
-
obj[paramName].description = obj[paramName].description || `The ${paramName}
|
|
157
|
+
const paramName = getPositionalCommandOptionName(segment);
|
|
158
|
+
obj[paramName] = extractCommandPositionalOption(command, paramName, type.parameters[index + 1]);
|
|
159
|
+
obj[paramName].description = obj[paramName].description || `The ${paramName} positional option for the ${command.name} command.`;
|
|
125
160
|
return obj;
|
|
126
161
|
}, {});
|
|
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
162
|
}
|
|
131
|
-
}
|
|
163
|
+
} else tree.description ??= `A collection of available ${tree.title || titleCase(tree.name)} commands that are included in the ${getAppTitle(context)} command-line application.`;
|
|
132
164
|
if (context.env) {
|
|
133
165
|
if (isSetObject(tree.options)) Object.values(tree.options).filter((option) => option.env !== false).forEach((option) => {
|
|
134
166
|
context.env.types.env.addProperty({
|
|
@@ -148,22 +180,20 @@ async function reflectCommandTree(context, command, parent) {
|
|
|
148
180
|
}
|
|
149
181
|
});
|
|
150
182
|
});
|
|
151
|
-
if (tree.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
});
|
|
164
|
-
});
|
|
183
|
+
if (Object.values(tree.path.positional).filter((option) => option.env !== false).length > 0) Object.values(tree.path.positional).filter((option) => option.env !== false).forEach((option) => context.env.types.env.addProperty({
|
|
184
|
+
name: constantCase(option.name),
|
|
185
|
+
optional: option.optional ? true : void 0,
|
|
186
|
+
description: option.description,
|
|
187
|
+
visibility: ReflectionVisibility.public,
|
|
188
|
+
type: option.variadic ? {
|
|
189
|
+
kind: ReflectionKind.array,
|
|
190
|
+
type: { kind: ReflectionKind.string }
|
|
191
|
+
} : { kind: ReflectionKind.string },
|
|
192
|
+
default: option.default,
|
|
193
|
+
tags: { domain: "cli" }
|
|
194
|
+
}));
|
|
165
195
|
}
|
|
166
|
-
for (const input of context.inputs.filter((input$1) => input$1.path.segments.filter((segment) => !
|
|
196
|
+
for (const input of context.inputs.filter((input$1) => input$1.path.segments.filter((segment) => !isPositionalCommandOption(segment)).length === command.path.segments.filter((segment) => !isPositionalCommandOption(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
197
|
return tree;
|
|
168
198
|
}
|
|
169
199
|
|
|
@@ -1 +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","replace","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.isVirtual ? parent.title.replace(/ Commands$/, \"\") : 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,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AE,CACjCT,KAAI2C,UAASjF,aAAaiF,MAAM,CAAC;KACpCqB,QAAQ;KACV;IACD,CAAC;IACF;AAGN,MAAIzC,KAAKG,OACPH,MAAKG,OAAO2B,SAAQzC,UAAS;AAC3B3B,WAAQ2D,IAAIc,MAAMd,IAAIe,YAAY;IAChC/D,MAAMlC,aAAakD,MAAMhB,KAAK;IAC9BkB,UAAUF,MAAME,WAAW,OAAO8C;IAClC/C,aAAaD,MAAMC;IACnBgD,YAAY5G,qBAAqB6G;IACjC9C,MAAMJ,MAAMG,WACR;KACEtC,MAAMzB,eAAe2B;KACrBqC,MAAM,EAAEvC,MAAMzB,eAAe6F,QAAO;KACrC,GACD,EAAEpE,MAAMzB,eAAe6F,QAAQ;IACnC5B,SAASL,MAAMK;IACf8C,MAAM,EACJC,QAAQ,OACV;IACD,CAAC;IACF;;AAIN,MAAK,MAAMpC,SAAS3C,QAAQgF,OAAO5E,QACjCuC,YACEA,QAAMjC,KAAKmD,SAASzD,QAAO0D,YAAW,CAAC/E,sBAAsB+E,QAAQ,CAAC,CACnEtC,WACDU,QAAQxB,KAAKmD,SAASzD,QAAO0D,YAAW,CAAC/E,sBAAsB+E,QAAQ,CAAC,CACrEtC,SACD,KACJmB,QAAMjC,KAAKmD,SACRM,MAAM,GAAGjC,QAAQxB,KAAKmD,SAASrC,OAAO,CACtCyD,OAAOC,OAAOjB,UAAUiB,UAAUhD,QAAQxB,KAAKmD,SAASI,OAC/D,CAAC,CACC3B,MAAKI,SAASC,MAAMhC,QAAQ,MAAMsB,mBAAmBjC,SAAS2C,OAAOL,KAAK;AAG5E,QAAOA"}
|
|
1
|
+
{"version":3,"file":"resolve-command.mjs","names":["esbuildPlugin","reflect","ReflectionClass","ReflectionKind","ReflectionVisibility","stringifyType","toArray","appendPath","commonPath","findFilePath","findFolderName","stripStars","replacePath","resolveParentPath","constantCase","titleCase","isSetObject","isSetString","resolveModule","getAppTitle","getPositionalCommandOptionName","isPositionalCommandOption","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","resolveCommandParams","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","extractCommandPositionalOption","segment","reflectCommandTree","parent","isVirtual","replace","tree","positional","options","children","input","fs","existsSync","debug","id","resolved","plugins","reflectionLevel","metadata","function","parameters","firstParam","objectLiteral","class","optionsReflection","from","propertyReflection","getProperties","segments","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 getPositionalCommandOptionName,\n isPositionalCommandOption\n} from \"../plugin-utils/context-helpers\";\nimport type {\n CommandInput,\n CommandModule,\n CommandOption,\n CommandPositionalOption,\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) && !isPositionalCommandOption(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 (isPositionalCommandOption(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) && isPositionalCommandOption(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 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 extractCommandPositionalOption(\n command: CommandInput,\n segment: string,\n reflection: TypeParameter\n): CommandPositionalOption {\n if (\n reflection.type.kind !== ReflectionKind.string &&\n reflection.type.kind !== ReflectionKind.number &&\n !(\n reflection.type.kind === ReflectionKind.array &&\n (reflection.type.type.kind === ReflectionKind.string ||\n reflection.type.type.kind === ReflectionKind.number)\n )\n ) {\n throw new Error(\n `Unsupported type for positional option \"${segment}\" in command \"${\n command.name\n }\". Only string and number types (or string[] and number[]) are supported, received ${stringifyType(\n reflection.type\n )\n .trim()\n .replaceAll(\" | \", \", or \")}.`\n );\n }\n\n const option = {\n name: segment,\n alias: [],\n title: titleCase(segment),\n description:\n reflection.description ||\n resolveCommandOptionDescription(\n reflection.type.kind,\n !!reflection.optional,\n segment,\n titleCase(segment),\n reflection.default\n ),\n env: constantCase(segment),\n kind: reflection.type.kind,\n optional: reflection.optional,\n default: reflection.default,\n variadic: false\n } as CommandPositionalOption;\n\n if (reflection.type.kind === ReflectionKind.array) {\n if (\n reflection.type.type.kind === ReflectionKind.string ||\n reflection.type.type.kind === ReflectionKind.number\n ) {\n (option as StringCommandOption | NumberCommandOption).variadic = true;\n (option as StringCommandOption | NumberCommandOption).kind =\n reflection.type.type.kind;\n } else {\n throw new Error(\n `Unsupported array type for option \"${segment}\" in command \"${\n command.name\n }\". Only string[] and number[] are supported.`\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 positional: {}\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.positional = tree.path.segments\n .filter(segment => isPositionalCommandOption(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 = getPositionalCommandOptionName(segment);\n obj[paramName] = extractCommandPositionalOption(\n command,\n paramName,\n type.parameters[index + 1]!\n );\n\n obj[paramName].description =\n obj[paramName].description ||\n `The ${paramName} positional option for the ${command.name} command.`;\n\n return obj;\n },\n {} as Record<string, CommandPositionalOption>\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.positional).filter(option => option.env !== false)\n .length > 0\n ) {\n Object.values(tree.path.positional)\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 => !isPositionalCommandOption(segment))\n .length ===\n command.path.segments.filter(\n segment => !isPositionalCommandOption(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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqEA,SAAgBuB,gCACdC,MACAC,UACAC,MACAC,OACAC,cACQ;AACR,QAAO,IAAIH,YAAY,CAACG,eAAe,eAAe,GAAE,GACtDJ,SAASrB,eAAe0B,UACpB,uCACA,sBAAqB,2BAEzBL,SAASrB,eAAe0B,UACpB,YACAL,SAASrB,eAAe2B,QACtB,mBACA,mBAAkB,GACtBH,OAAOI,MAAM,IAA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AAOvC,WAAW,OAAO4F;GACnCjD,aAAaJ,OAAOI;GACpBkD,YAAYlH,qBAAqBmH;GACjC3C,MAAMZ,OAAOW,WACT;IACEnD,MAAMrB,eAAe2B;IACrB8C,MAAM,EAAEpD,MAAMrB,eAAe0E,QAAO;IACrC,GACD,EAAErD,MAAMrB,eAAe0E,QAAQ;GACnCH,SAASV,OAAOU;GAChB8C,MAAM,EACJC,QAAQ,OACV;GACD,CACH,CAAC;;AAIP,MAAK,MAAMhC,SAASvD,QAAQwF,OAAOpF,QACjCmD,YACEA,QAAM7C,KAAK+D,SAASrE,QAAO0C,YAAW,CAAC3D,0BAA0B2D,QAAQ,CAAC,CACvEvB,WACDG,QAAQhB,KAAK+D,SAASrE,QACpB0C,YAAW,CAAC3D,0BAA0B2D,QACxC,CAAC,CAACvB,SACA,KACJgC,QAAM7C,KAAK+D,SACRgB,MAAM,GAAG/D,QAAQhB,KAAK+D,SAASlD,OAAO,CACtCmE,OAAOC,OAAOf,UAAUe,UAAUjE,QAAQhB,KAAK+D,SAASG,OAC/D,CAAC,CACCzB,MAAKG,SAASC,MAAM/D,QAAQ,MAAMuD,mBAAmB/C,SAASuD,OAAOJ,KAAK;AAG5E,QAAOA"}
|
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
const require_plugin_utils_context_helpers = require('../plugin-utils/context-helpers.cjs');
|
|
2
2
|
|
|
3
3
|
//#region src/helpers/validations.ts
|
|
4
|
-
function
|
|
4
|
+
function validateCommandPositionalOptions(command) {
|
|
5
5
|
const failures = [];
|
|
6
6
|
if (!command.isVirtual && command.path.segments.length > 0) {
|
|
7
|
-
const
|
|
8
|
-
for (const segment of command.path.segments.filter(require_plugin_utils_context_helpers.
|
|
9
|
-
if (
|
|
10
|
-
code: "
|
|
11
|
-
details: `Duplicate
|
|
7
|
+
const positionalOptionPathNames = /* @__PURE__ */ new Set();
|
|
8
|
+
for (const segment of command.path.segments.filter(require_plugin_utils_context_helpers.isPositionalCommandOption) ?? []) {
|
|
9
|
+
if (positionalOptionPathNames.has(segment)) failures.push({
|
|
10
|
+
code: "DUPLICATE_POSITIONAL_OPTION_PATH_NAME",
|
|
11
|
+
details: `Duplicate positional option path name "${require_plugin_utils_context_helpers.getPositionalCommandOptionName(segment)}" found in command.`
|
|
12
12
|
});
|
|
13
|
-
|
|
13
|
+
positionalOptionPathNames.add(require_plugin_utils_context_helpers.getPositionalCommandOptionName(segment));
|
|
14
14
|
}
|
|
15
|
-
if (command.path.segments.filter(require_plugin_utils_context_helpers.
|
|
16
|
-
code: "
|
|
17
|
-
details: `Mismatch between
|
|
15
|
+
if (command.path.segments.filter(require_plugin_utils_context_helpers.isPositionalCommandOption).length !== Object.keys(command.path.positional ?? {}).length) failures.push({
|
|
16
|
+
code: "POSITIONAL_OPTION_PATH_MISMATCH",
|
|
17
|
+
details: `Mismatch between positional options path segments and defined path positional options in command (found ${command.path.segments.filter(require_plugin_utils_context_helpers.isPositionalCommandOption).length} positional options in the command folder path "${command.path.segments.join("/")}", but ${Object.keys(command.path.positional ?? {}).length} potential positional option path(s) could be determined from the command's function signature).`
|
|
18
18
|
});
|
|
19
|
-
const missing = command.path.segments.filter(require_plugin_utils_context_helpers.
|
|
19
|
+
const missing = command.path.segments.filter(require_plugin_utils_context_helpers.isPositionalCommandOption).filter((segment) => Object.prototype.hasOwnProperty.call(command.path.positional ?? {}, require_plugin_utils_context_helpers.getPositionalCommandOptionName(segment)) === false);
|
|
20
20
|
if (missing.length > 0) failures.push({
|
|
21
|
-
code: "
|
|
22
|
-
details: `${missing.length}
|
|
21
|
+
code: "MISSING_POSITIONAL_OPTION_PATH",
|
|
22
|
+
details: `${missing.length} positional option path segment${missing.length > 1 ? "s" : ""} in the command folder path "${command.path.segments.join("/")}" do${missing.length > 1 ? "" : "es"} not have corresponding entr${missing.length > 1 ? "ies" : "y"} in the command's path positional options: \n- ${missing.map((segment) => `"${require_plugin_utils_context_helpers.getPositionalCommandOptionName(segment)}"`).join("\n- ")}`
|
|
23
23
|
});
|
|
24
24
|
else {
|
|
25
|
-
for (const varName of Object.keys(command.path.
|
|
26
|
-
code: "
|
|
27
|
-
details: `The
|
|
25
|
+
for (const varName of Object.keys(command.path.positional ?? {})) if (!command.path.segments.filter(require_plugin_utils_context_helpers.isPositionalCommandOption).find((segment) => require_plugin_utils_context_helpers.getPositionalCommandOptionName(segment) === varName)) failures.push({
|
|
26
|
+
code: "UNUSED_POSITIONAL_OPTION_PATH",
|
|
27
|
+
details: `The positional option path name "${varName}" defined in the command's path positional options is not used in the command folder path "${command.path.segments.join("/")}".`
|
|
28
28
|
});
|
|
29
29
|
command.path.segments.forEach((segment, index) => {
|
|
30
|
-
if (require_plugin_utils_context_helpers.
|
|
31
|
-
code: "
|
|
32
|
-
details: `The
|
|
30
|
+
if (require_plugin_utils_context_helpers.isPositionalCommandOption(segment) && command.path.positional[require_plugin_utils_context_helpers.getPositionalCommandOptionName(segment)]?.variadic === true && index + 1 < command.path.segments.length && command.path.segments[index + 1] && command.path.positional[require_plugin_utils_context_helpers.getPositionalCommandOptionName(command.path.segments[index + 1])]?.variadic === true) failures.push({
|
|
31
|
+
code: "MULTIPLE_VARIADIC_POSITIONAL_OPTION_PATHS",
|
|
32
|
+
details: `The positional option path segment "${require_plugin_utils_context_helpers.getPositionalCommandOptionName(segment)}" in the command at path "${command.path.segments.join("/")}" is marked as variadic, and it is followed by another variadic positional option path segment "${require_plugin_utils_context_helpers.getPositionalCommandOptionName(command.path.segments[index + 1])}". Only one variadic positional option path segment is allowed per command, and it must be the final path segment.`
|
|
33
33
|
});
|
|
34
34
|
});
|
|
35
35
|
}
|
|
@@ -55,37 +55,9 @@ function validateCommandOptions(command) {
|
|
|
55
55
|
}
|
|
56
56
|
return failures;
|
|
57
57
|
}
|
|
58
|
-
function validateCommandParams(command) {
|
|
59
|
-
const failures = [];
|
|
60
|
-
if (!command.isVirtual && command.params.length > 0) {
|
|
61
|
-
const paramNames = /* @__PURE__ */ new Set();
|
|
62
|
-
command.params.forEach((param, index) => {
|
|
63
|
-
if (paramNames.has(param.name)) failures.push({
|
|
64
|
-
code: "DUPLICATE_PARAM_NAME",
|
|
65
|
-
details: `Duplicate parameter name "${param.name}" found in command.`
|
|
66
|
-
});
|
|
67
|
-
paramNames.add(param.name);
|
|
68
|
-
if (param.optional) command.params.slice(index + 1).forEach((nextParam) => {
|
|
69
|
-
if (nextParam.optional && !nextParam.default) failures.push({
|
|
70
|
-
code: "OPTIONAL_PARAM_NOT_LAST",
|
|
71
|
-
details: `The parameter "${nextParam.name}" in the command at path "${command.path.segments.join("/")}" is required, but it follows an optional parameter "${param.name}". All required parameters must come before any optional parameters.`
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
if (param.variadic && index + 1 < command.params.length) {
|
|
75
|
-
if (command.params[index + 1]?.variadic) failures.push({
|
|
76
|
-
code: "MULTIPLE_VARIADIC_PARAMS",
|
|
77
|
-
details: `The parameter "${param.name}" in the command at path "${command.path.segments.join("/")}" is variadic, and it is followed by another variadic parameter "${command.params[index + 1]?.name}". Only one variadic parameter is allowed per command, and it must be the final parameter.`
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
return failures;
|
|
83
|
-
}
|
|
84
58
|
function validateCommand(command) {
|
|
85
59
|
const results = [];
|
|
86
|
-
let failures =
|
|
87
|
-
if (failures.length > 0) results.push(...failures);
|
|
88
|
-
failures = validateCommandParams(command);
|
|
60
|
+
let failures = validateCommandPositionalOptions(command);
|
|
89
61
|
if (failures.length > 0) results.push(...failures);
|
|
90
62
|
failures = validateCommandOptions(command);
|
|
91
63
|
if (failures.length > 0) results.push(...failures);
|