@contember/cli-common 2.2.0-alpha.1 → 2.2.0-alpha.11

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.
Files changed (224) hide show
  1. package/dist/development/index.cjs +20 -0
  2. package/dist/development/index.cjs.map +1 -1
  3. package/dist/development/index.js +22 -2
  4. package/dist/development/index.js.map +1 -1
  5. package/dist/development/src/application/Application.cjs +116 -59
  6. package/dist/development/src/application/Application.cjs.map +1 -1
  7. package/dist/development/src/application/Application.js +116 -59
  8. package/dist/development/src/application/Application.js.map +1 -1
  9. package/dist/development/src/application/CliError.cjs +104 -0
  10. package/dist/development/src/application/CliError.cjs.map +1 -0
  11. package/dist/development/src/application/CliError.js +104 -0
  12. package/dist/development/src/application/CliError.js.map +1 -0
  13. package/dist/development/src/application/Command.cjs +18 -5
  14. package/dist/development/src/application/Command.cjs.map +1 -1
  15. package/dist/development/src/application/Command.js +18 -5
  16. package/dist/development/src/application/Command.js.map +1 -1
  17. package/dist/development/src/application/CommandConfiguration.cjs +21 -3
  18. package/dist/development/src/application/CommandConfiguration.cjs.map +1 -1
  19. package/dist/development/src/application/CommandConfiguration.js +23 -5
  20. package/dist/development/src/application/CommandConfiguration.js.map +1 -1
  21. package/dist/development/src/application/CommandManager.cjs +139 -21
  22. package/dist/development/src/application/CommandManager.cjs.map +1 -1
  23. package/dist/development/src/application/CommandManager.js +139 -21
  24. package/dist/development/src/application/CommandManager.js.map +1 -1
  25. package/dist/development/src/application/ErrorRenderer.cjs +38 -0
  26. package/dist/development/src/application/ErrorRenderer.cjs.map +1 -0
  27. package/dist/development/src/application/ErrorRenderer.js +38 -0
  28. package/dist/development/src/application/ErrorRenderer.js.map +1 -0
  29. package/dist/development/src/application/GlobalOptions.cjs +54 -0
  30. package/dist/development/src/application/GlobalOptions.cjs.map +1 -0
  31. package/dist/development/src/application/GlobalOptions.js +54 -0
  32. package/dist/development/src/application/GlobalOptions.js.map +1 -0
  33. package/dist/development/src/application/Input.cjs +5 -0
  34. package/dist/development/src/application/Input.cjs.map +1 -1
  35. package/dist/development/src/application/Input.js +5 -0
  36. package/dist/development/src/application/Input.js.map +1 -1
  37. package/dist/development/src/application/InputParser.cjs +7 -3
  38. package/dist/development/src/application/InputParser.cjs.map +1 -1
  39. package/dist/development/src/application/InputParser.js +7 -3
  40. package/dist/development/src/application/InputParser.js.map +1 -1
  41. package/dist/development/src/application/Option.cjs +11 -0
  42. package/dist/development/src/application/Option.cjs.map +1 -1
  43. package/dist/development/src/application/Option.js +12 -1
  44. package/dist/development/src/application/Option.js.map +1 -1
  45. package/dist/development/src/application/Output.cjs +242 -0
  46. package/dist/development/src/application/Output.cjs.map +1 -0
  47. package/dist/development/src/application/Output.js +242 -0
  48. package/dist/development/src/application/Output.js.map +1 -0
  49. package/dist/development/src/application/exitProcess.cjs +7 -0
  50. package/dist/development/src/application/exitProcess.cjs.map +1 -0
  51. package/dist/development/src/application/exitProcess.js +7 -0
  52. package/dist/development/src/application/exitProcess.js.map +1 -0
  53. package/dist/development/src/npm/CommandRunner.cjs +126 -24
  54. package/dist/development/src/npm/CommandRunner.cjs.map +1 -1
  55. package/dist/development/src/npm/CommandRunner.js +126 -24
  56. package/dist/development/src/npm/CommandRunner.js.map +1 -1
  57. package/dist/development/src/npm/packageManagers/Bun.cjs +1 -2
  58. package/dist/development/src/npm/packageManagers/Bun.cjs.map +1 -1
  59. package/dist/development/src/npm/packageManagers/Bun.js +1 -2
  60. package/dist/development/src/npm/packageManagers/Bun.js.map +1 -1
  61. package/dist/development/src/npm/packageManagers/Npm.cjs +1 -2
  62. package/dist/development/src/npm/packageManagers/Npm.cjs.map +1 -1
  63. package/dist/development/src/npm/packageManagers/Npm.js +1 -2
  64. package/dist/development/src/npm/packageManagers/Npm.js.map +1 -1
  65. package/dist/development/src/npm/packageManagers/Pnpm.cjs +1 -2
  66. package/dist/development/src/npm/packageManagers/Pnpm.cjs.map +1 -1
  67. package/dist/development/src/npm/packageManagers/Pnpm.js +1 -2
  68. package/dist/development/src/npm/packageManagers/Pnpm.js.map +1 -1
  69. package/dist/development/src/npm/packageManagers/Yarn.cjs +1 -2
  70. package/dist/development/src/npm/packageManagers/Yarn.cjs.map +1 -1
  71. package/dist/development/src/npm/packageManagers/Yarn.js +1 -2
  72. package/dist/development/src/npm/packageManagers/Yarn.js.map +1 -1
  73. package/dist/development/src/npm/packageManagers/YarnClassic.cjs +1 -2
  74. package/dist/development/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
  75. package/dist/development/src/npm/packageManagers/YarnClassic.js +1 -2
  76. package/dist/development/src/npm/packageManagers/YarnClassic.js.map +1 -1
  77. package/dist/production/index.cjs +20 -0
  78. package/dist/production/index.cjs.map +1 -1
  79. package/dist/production/index.js +22 -2
  80. package/dist/production/index.js.map +1 -1
  81. package/dist/production/src/application/Application.cjs +116 -59
  82. package/dist/production/src/application/Application.cjs.map +1 -1
  83. package/dist/production/src/application/Application.js +116 -59
  84. package/dist/production/src/application/Application.js.map +1 -1
  85. package/dist/production/src/application/CliError.cjs +104 -0
  86. package/dist/production/src/application/CliError.cjs.map +1 -0
  87. package/dist/production/src/application/CliError.js +104 -0
  88. package/dist/production/src/application/CliError.js.map +1 -0
  89. package/dist/production/src/application/Command.cjs +18 -5
  90. package/dist/production/src/application/Command.cjs.map +1 -1
  91. package/dist/production/src/application/Command.js +18 -5
  92. package/dist/production/src/application/Command.js.map +1 -1
  93. package/dist/production/src/application/CommandConfiguration.cjs +21 -3
  94. package/dist/production/src/application/CommandConfiguration.cjs.map +1 -1
  95. package/dist/production/src/application/CommandConfiguration.js +23 -5
  96. package/dist/production/src/application/CommandConfiguration.js.map +1 -1
  97. package/dist/production/src/application/CommandManager.cjs +139 -21
  98. package/dist/production/src/application/CommandManager.cjs.map +1 -1
  99. package/dist/production/src/application/CommandManager.js +139 -21
  100. package/dist/production/src/application/CommandManager.js.map +1 -1
  101. package/dist/production/src/application/ErrorRenderer.cjs +38 -0
  102. package/dist/production/src/application/ErrorRenderer.cjs.map +1 -0
  103. package/dist/production/src/application/ErrorRenderer.js +38 -0
  104. package/dist/production/src/application/ErrorRenderer.js.map +1 -0
  105. package/dist/production/src/application/GlobalOptions.cjs +54 -0
  106. package/dist/production/src/application/GlobalOptions.cjs.map +1 -0
  107. package/dist/production/src/application/GlobalOptions.js +54 -0
  108. package/dist/production/src/application/GlobalOptions.js.map +1 -0
  109. package/dist/production/src/application/Input.cjs +5 -0
  110. package/dist/production/src/application/Input.cjs.map +1 -1
  111. package/dist/production/src/application/Input.js +5 -0
  112. package/dist/production/src/application/Input.js.map +1 -1
  113. package/dist/production/src/application/InputParser.cjs +7 -3
  114. package/dist/production/src/application/InputParser.cjs.map +1 -1
  115. package/dist/production/src/application/InputParser.js +7 -3
  116. package/dist/production/src/application/InputParser.js.map +1 -1
  117. package/dist/production/src/application/Option.cjs +11 -0
  118. package/dist/production/src/application/Option.cjs.map +1 -1
  119. package/dist/production/src/application/Option.js +12 -1
  120. package/dist/production/src/application/Option.js.map +1 -1
  121. package/dist/production/src/application/Output.cjs +242 -0
  122. package/dist/production/src/application/Output.cjs.map +1 -0
  123. package/dist/production/src/application/Output.js +242 -0
  124. package/dist/production/src/application/Output.js.map +1 -0
  125. package/dist/production/src/application/exitProcess.cjs +7 -0
  126. package/dist/production/src/application/exitProcess.cjs.map +1 -0
  127. package/dist/production/src/application/exitProcess.js +7 -0
  128. package/dist/production/src/application/exitProcess.js.map +1 -0
  129. package/dist/production/src/npm/CommandRunner.cjs +126 -24
  130. package/dist/production/src/npm/CommandRunner.cjs.map +1 -1
  131. package/dist/production/src/npm/CommandRunner.js +126 -24
  132. package/dist/production/src/npm/CommandRunner.js.map +1 -1
  133. package/dist/production/src/npm/packageManagers/Bun.cjs +1 -2
  134. package/dist/production/src/npm/packageManagers/Bun.cjs.map +1 -1
  135. package/dist/production/src/npm/packageManagers/Bun.js +1 -2
  136. package/dist/production/src/npm/packageManagers/Bun.js.map +1 -1
  137. package/dist/production/src/npm/packageManagers/Npm.cjs +1 -2
  138. package/dist/production/src/npm/packageManagers/Npm.cjs.map +1 -1
  139. package/dist/production/src/npm/packageManagers/Npm.js +1 -2
  140. package/dist/production/src/npm/packageManagers/Npm.js.map +1 -1
  141. package/dist/production/src/npm/packageManagers/Pnpm.cjs +1 -2
  142. package/dist/production/src/npm/packageManagers/Pnpm.cjs.map +1 -1
  143. package/dist/production/src/npm/packageManagers/Pnpm.js +1 -2
  144. package/dist/production/src/npm/packageManagers/Pnpm.js.map +1 -1
  145. package/dist/production/src/npm/packageManagers/Yarn.cjs +1 -2
  146. package/dist/production/src/npm/packageManagers/Yarn.cjs.map +1 -1
  147. package/dist/production/src/npm/packageManagers/Yarn.js +1 -2
  148. package/dist/production/src/npm/packageManagers/Yarn.js.map +1 -1
  149. package/dist/production/src/npm/packageManagers/YarnClassic.cjs +1 -2
  150. package/dist/production/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
  151. package/dist/production/src/npm/packageManagers/YarnClassic.js +1 -2
  152. package/dist/production/src/npm/packageManagers/YarnClassic.js.map +1 -1
  153. package/dist/types/application/Application.d.ts +22 -11
  154. package/dist/types/application/Application.d.ts.map +1 -1
  155. package/dist/types/application/Argument.d.ts.map +1 -1
  156. package/dist/types/application/CliError.d.ts +40 -0
  157. package/dist/types/application/CliError.d.ts.map +1 -0
  158. package/dist/types/application/Command.d.ts +12 -2
  159. package/dist/types/application/Command.d.ts.map +1 -1
  160. package/dist/types/application/CommandConfiguration.d.ts +7 -4
  161. package/dist/types/application/CommandConfiguration.d.ts.map +1 -1
  162. package/dist/types/application/CommandManager.d.ts +40 -2
  163. package/dist/types/application/CommandManager.d.ts.map +1 -1
  164. package/dist/types/application/ErrorRenderer.d.ts +8 -0
  165. package/dist/types/application/ErrorRenderer.d.ts.map +1 -0
  166. package/dist/types/application/GlobalOptions.d.ts +28 -0
  167. package/dist/types/application/GlobalOptions.d.ts.map +1 -0
  168. package/dist/types/application/Input.d.ts +2 -0
  169. package/dist/types/application/Input.d.ts.map +1 -1
  170. package/dist/types/application/InputParser.d.ts +5 -1
  171. package/dist/types/application/InputParser.d.ts.map +1 -1
  172. package/dist/types/application/Option.d.ts +2 -0
  173. package/dist/types/application/Option.d.ts.map +1 -1
  174. package/dist/types/application/Output.d.ts +81 -0
  175. package/dist/types/application/Output.d.ts.map +1 -0
  176. package/dist/types/application/UsageFormatter.d.ts.map +1 -1
  177. package/dist/types/application/exitProcess.d.ts +11 -0
  178. package/dist/types/application/exitProcess.d.ts.map +1 -0
  179. package/dist/types/application/index.d.ts +5 -0
  180. package/dist/types/application/index.d.ts.map +1 -1
  181. package/dist/types/index.d.ts +2 -2
  182. package/dist/types/index.d.ts.map +1 -1
  183. package/dist/types/npm/CommandRunner.d.ts +13 -0
  184. package/dist/types/npm/CommandRunner.d.ts.map +1 -1
  185. package/dist/types/npm/Package.d.ts.map +1 -1
  186. package/dist/types/npm/PackageWorkspace.d.ts.map +1 -1
  187. package/dist/types/npm/PackageWorkspaceResolver.d.ts.map +1 -1
  188. package/dist/types/npm/packageManagers/Bun.d.ts.map +1 -1
  189. package/dist/types/npm/packageManagers/Npm.d.ts.map +1 -1
  190. package/dist/types/npm/packageManagers/PackageManagerHelpers.d.ts.map +1 -1
  191. package/dist/types/npm/packageManagers/Pnpm.d.ts.map +1 -1
  192. package/dist/types/npm/packageManagers/Yarn.d.ts.map +1 -1
  193. package/dist/types/npm/packageManagers/YarnClassic.d.ts.map +1 -1
  194. package/dist/types/tsconfig.tsbuildinfo +1 -1
  195. package/dist/types/utils/findPackageRoot.d.ts.map +1 -1
  196. package/package.json +3 -2
  197. package/src/application/Application.ts +137 -75
  198. package/src/application/CliError.ts +117 -0
  199. package/src/application/Command.ts +27 -6
  200. package/src/application/CommandConfiguration.ts +22 -4
  201. package/src/application/CommandManager.ts +172 -22
  202. package/src/application/ErrorRenderer.ts +40 -0
  203. package/src/application/GlobalOptions.ts +76 -0
  204. package/src/application/Input.ts +6 -0
  205. package/src/application/InputParser.ts +13 -4
  206. package/src/application/Option.ts +12 -0
  207. package/src/application/Output.ts +329 -0
  208. package/src/application/exitProcess.ts +12 -0
  209. package/src/application/index.ts +5 -0
  210. package/src/index.ts +40 -2
  211. package/src/npm/CommandRunner.ts +166 -23
  212. package/src/npm/packageManagers/Bun.ts +1 -2
  213. package/src/npm/packageManagers/Npm.ts +1 -2
  214. package/src/npm/packageManagers/Pnpm.ts +1 -2
  215. package/src/npm/packageManagers/Yarn.ts +1 -2
  216. package/src/npm/packageManagers/YarnClassic.ts +1 -2
  217. package/tests/cases/unit/application.test.ts +426 -0
  218. package/tests/cases/unit/cliError.test.ts +127 -0
  219. package/tests/cases/unit/commandManager.test.ts +270 -0
  220. package/tests/cases/unit/commandRunner.test.ts +192 -0
  221. package/tests/cases/unit/inputParser.test.ts +21 -0
  222. package/tests/cases/unit/output.test.ts +393 -0
  223. package/tests/lib/testOutput.ts +35 -0
  224. package/types/chalk-table/index.d.ts +9 -0
@@ -1,82 +1,139 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const InputParser = require("./InputParser.cjs");
4
3
  const chalk = require("chalk");
4
+ const Command = require("./Command.cjs");
5
+ const InputParser = require("./InputParser.cjs");
6
+ const Output = require("./Output.cjs");
7
+ const CliError = require("./CliError.cjs");
8
+ const ErrorRenderer = require("./ErrorRenderer.cjs");
9
+ const exitProcess = require("./exitProcess.cjs");
10
+ const GlobalOptions = require("./GlobalOptions.cjs");
11
+ const UsageFormatter = require("./UsageFormatter.cjs");
12
+ var __defProp = Object.defineProperty;
13
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
15
  class Application {
6
- constructor(commandManager, applicationDescription, options = {}) {
16
+ constructor(commandManager, applicationDescription, outputOrOptions = {}, options = {}) {
17
+ __publicField(this, "commandManager");
18
+ __publicField(this, "applicationDescription");
19
+ __publicField(this, "output");
20
+ __publicField(this, "options");
7
21
  this.commandManager = commandManager;
8
22
  this.applicationDescription = applicationDescription;
9
- this.options = options;
23
+ this.output = outputOrOptions instanceof Output.Output ? outputOrOptions : new Output.Output();
24
+ this.options = outputOrOptions instanceof Output.Output ? options : outputOrOptions;
10
25
  }
11
26
  async run(args) {
12
- const [name, ...rest] = args;
13
- if (!name || name === "--help") {
14
- console.error(this.applicationDescription);
15
- console.error(`Usage: <command> <command args>`);
16
- const commands = Object.entries(this.commandManager.commands).filter(([name2, factory], index, commands2) => commands2.findIndex((it) => it[1] === factory) === index).map(([name2]) => name2).sort((a, b) => a.localeCompare(b));
17
- const maxCommandNameLength = Math.max(...commands.map((it) => it.length));
18
- for (let commandName of commands) {
19
- const [, command] = this.commandManager.createCommand(commandName);
20
- const configuration = command.getConfiguration();
21
- const indent = " ";
22
- console.error(
23
- `${indent}${chalk.greenBright(
24
- commandName.padEnd(maxCommandNameLength)
25
- )}${indent}${configuration.getDescription()}`
26
- );
27
- if (name === "--help") {
28
- const helpIndent = `${indent}${" ".repeat(maxCommandNameLength)}${indent}`;
29
- const usage = configuration.getUsage({ format: "multiline", indent: helpIndent });
30
- if (usage) {
31
- console.error(chalk.green(usage));
32
- } else {
33
- console.error(`${helpIndent}${chalk.gray("No options")}`);
34
- }
35
- }
36
- }
37
- return process.exit(0);
27
+ if (args.length === 0 || GlobalOptions.readGlobalOptionsFromArgs(args.slice(0, 1)).help) {
28
+ exitProcess.exitProcess(await this.execute(args));
29
+ return;
38
30
  }
39
- return this.runCommandInternal(name, rest, true);
31
+ const [name, ...commandArgs] = args;
32
+ await this.runCommandInternal(name, commandArgs, true);
40
33
  }
41
34
  async runCommand(name, args) {
42
- return this.runCommandInternal(name, args, false);
35
+ await this.runCommandInternal(name, args, false);
43
36
  }
44
37
  async runCommandInternal(name, args, showCommandName) {
45
- const [fullName, command] = this.commandManager.createCommand(name);
46
- if (args[0] === "--help" || args[0] === "-h") {
47
- if (showCommandName) {
48
- console.error(chalk.greenBright(fullName));
38
+ exitProcess.exitProcess(await this.executeArgs([name, ...args], showCommandName));
39
+ }
40
+ /** Runs the given argv and returns the exit code instead of terminating the process. */
41
+ async execute(args) {
42
+ const help = GlobalOptions.readGlobalOptionsFromArgs(args.slice(0, 1)).help;
43
+ if (args.length === 0 || help) {
44
+ this.printApplicationHelp(help);
45
+ return CliError.ExitCode.Success;
46
+ }
47
+ return this.executeArgs(args, true);
48
+ }
49
+ async executeCommand(name, args) {
50
+ return this.executeArgs([name, ...args], false);
51
+ }
52
+ async executeArgs(args, showCommandName) {
53
+ let entry;
54
+ let command;
55
+ this.output.applyGlobalOptions(GlobalOptions.readGlobalOptionsFromArgs(args));
56
+ try {
57
+ const resolution = this.commandManager.resolve(args);
58
+ if (resolution.type === "group") {
59
+ const groupHelp = resolution.args.length === 1 && (resolution.args[0] === "--help" || resolution.args[0] === "-h");
60
+ if (resolution.args.length > 0 && !groupHelp) {
61
+ throw new InputParser.InvalidInputError(`Unexpected input after command group "${resolution.name}": ${resolution.args.join(" ")}`);
62
+ }
63
+ this.printGroupHelp(resolution.name, resolution.entries);
64
+ return CliError.ExitCode.Success;
49
65
  }
50
- const configuration = command.getConfiguration();
51
- const commandDescription = configuration.getDescription();
52
- if (commandDescription) {
53
- console.error(commandDescription);
66
+ entry = resolution.entry;
67
+ const commandArgs = resolution.args;
68
+ command = entry.create();
69
+ if (GlobalOptions.hasHelpFlag(commandArgs)) {
70
+ this.printCommandHelp(entry.name, command, showCommandName);
71
+ return CliError.ExitCode.Success;
54
72
  }
55
- const shortUsage = configuration.getUsage({ format: "short" });
56
- if (shortUsage) {
57
- console.error("\nUsage:");
58
- console.error(chalk.green(shortUsage));
59
- console.error("\nArguments and options:");
60
- console.error(configuration.getUsage({ format: "multiline" }));
73
+ if (command.run !== Command.Command.prototype.run) {
74
+ await this.options.beforeRun?.({ command, args: commandArgs, name: entry.name });
75
+ return await command.run(commandArgs, this.output);
61
76
  }
62
- return process.exit(0);
63
- }
64
- try {
65
- await this.options.beforeRun?.({ command, args, name: fullName });
66
- const result = await command.run(args);
67
- return process.exit(result);
77
+ const parsed = command.parse(commandArgs);
78
+ this.output.applyGlobalOptions(parsed.globals);
79
+ await this.options.beforeRun?.({ command, args: commandArgs, name: entry.name });
80
+ return await command.runParsed(parsed, this.output);
68
81
  } catch (e) {
69
- if (e instanceof InputParser.InvalidInputError) {
70
- console.error(chalk.bgRedBright.white(e.message));
71
- const configuration = command.getConfiguration();
72
- console.error(`${showCommandName ? chalk.greenBright(fullName) + " " : ""}${chalk.green(configuration.getUsage({ format: "line" }))}`);
73
- return process.exit(1);
74
- } else {
75
- console.error(e);
76
- return process.exit(2);
82
+ const exitCode = ErrorRenderer.renderCliError(e, this.output);
83
+ if (e instanceof InputParser.InvalidInputError && command !== void 0 && this.output.mode === "human") {
84
+ const usage = command.getConfiguration().getUsage({ format: "line" });
85
+ this.output.writeStderr(`${showCommandName && entry ? chalk.greenBright(entry.name) + " " : ""}${chalk.green(usage)}`);
86
+ }
87
+ return exitCode;
88
+ }
89
+ }
90
+ printApplicationHelp(verbose) {
91
+ this.output.writeStderr(this.applicationDescription);
92
+ this.output.writeStderr(`Usage: <command> <command args>`);
93
+ const commands = this.commandManager.getCommands();
94
+ if (commands.length === 0) {
95
+ return;
96
+ }
97
+ const maxCommandNameLength = Math.max(...commands.map((it) => it.name.length));
98
+ const indent = " ";
99
+ for (const entry of commands) {
100
+ const configuration = entry.create().getConfiguration();
101
+ this.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`);
102
+ if (verbose) {
103
+ const helpIndent = `${indent}${" ".repeat(maxCommandNameLength)}${indent}`;
104
+ const usage = configuration.getUsage({ format: "multiline", indent: helpIndent });
105
+ this.output.writeStderr(usage ? chalk.green(usage) : `${helpIndent}${chalk.gray("No options")}`);
77
106
  }
78
107
  }
79
108
  }
109
+ printGroupHelp(name, entries) {
110
+ this.output.writeStderr(`Usage: ${name} <command> <command args>`);
111
+ const maxCommandNameLength = Math.max(...entries.map((it) => it.name.length));
112
+ const indent = " ";
113
+ for (const entry of [...entries].sort((a, b) => a.name.localeCompare(b.name))) {
114
+ const configuration = entry.create().getConfiguration();
115
+ this.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`);
116
+ }
117
+ }
118
+ printCommandHelp(name, command, showCommandName) {
119
+ if (showCommandName) {
120
+ this.output.writeStderr(chalk.greenBright(name));
121
+ }
122
+ const configuration = command.getConfiguration();
123
+ const commandDescription = configuration.getDescription();
124
+ if (commandDescription) {
125
+ this.output.writeStderr(commandDescription);
126
+ }
127
+ const shortUsage = configuration.getUsage({ format: "short" });
128
+ if (shortUsage) {
129
+ this.output.writeStderr("\nUsage:");
130
+ this.output.writeStderr(chalk.green(shortUsage));
131
+ this.output.writeStderr("\nArguments and options:");
132
+ this.output.writeStderr(configuration.getUsage({ format: "multiline" }));
133
+ }
134
+ this.output.writeStderr("\nGlobal options:");
135
+ this.output.writeStderr(UsageFormatter.UsageFormatter.format([], [...GlobalOptions.globalOptions], { format: "multiline" }));
136
+ }
80
137
  }
81
138
  exports.Application = Application;
82
139
  //# sourceMappingURL=Application.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"Application.cjs","sources":["../../../../../packages/cli-common/src/application/Application.ts"],"sourcesContent":["import { InvalidInputError } from './InputParser.js'\nimport { CommandManager } from './CommandManager.js'\nimport chalk from 'chalk'\nimport { Command } from './Command.js'\n\nexport class Application {\n\tconstructor(\n\t\tprivate readonly commandManager: CommandManager,\n\t\tprivate readonly applicationDescription: string,\n\t\tprivate readonly options: {\n\t\t\tbeforeRun?: (args: { command: Command<any, any>; name: string; args: string[] }) => void\n\t\t} = {},\n\t) {}\n\n\tasync run(args: string[]): Promise<void> {\n\t\tconst [name, ...rest] = args\n\t\tif (!name || name === '--help') {\n\t\t\tconsole.error(this.applicationDescription)\n\t\t\tconsole.error(`Usage: <command> <command args>`)\n\t\t\tconst commands = Object.entries(this.commandManager.commands)\n\t\t\t\t.filter(([name, factory], index, commands) => commands.findIndex(it => it[1] === factory) === index)\n\t\t\t\t.map(([name]) => name)\n\t\t\t\t.sort((a, b) => a.localeCompare(b))\n\n\t\t\tconst maxCommandNameLength = Math.max(...commands.map(it => it.length))\n\n\t\t\tfor (let commandName of commands) {\n\t\t\t\tconst [, command] = this.commandManager.createCommand(commandName)\n\t\t\t\tconst configuration = command.getConfiguration()\n\t\t\t\tconst indent = ' '\n\t\t\t\tconsole.error(\n\t\t\t\t\t`${indent}${\n\t\t\t\t\t\tchalk.greenBright(\n\t\t\t\t\t\t\tcommandName.padEnd(maxCommandNameLength),\n\t\t\t\t\t\t)\n\t\t\t\t\t}${indent}${configuration.getDescription()}`,\n\t\t\t\t)\n\t\t\t\tif (name === '--help') {\n\t\t\t\t\tconst helpIndent = `${indent}${' '.repeat(maxCommandNameLength)}${indent}`\n\t\t\t\t\tconst usage = configuration.getUsage({ format: 'multiline', indent: helpIndent })\n\t\t\t\t\tif (usage) {\n\t\t\t\t\t\tconsole.error(chalk.green(usage))\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconsole.error(`${helpIndent}${chalk.gray('No options')}`)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn process.exit(0)\n\t\t}\n\t\treturn this.runCommandInternal(name, rest, true)\n\t}\n\n\tpublic async runCommand(name: string, args: string[]) {\n\t\treturn this.runCommandInternal(name, args, false)\n\t}\n\n\tpublic async runCommandInternal(name: string, args: string[], showCommandName: boolean) {\n\t\tconst [fullName, command] = this.commandManager.createCommand(name)\n\n\t\tif (args[0] === '--help' || args[0] === '-h') {\n\t\t\tif (showCommandName) {\n\t\t\t\tconsole.error(chalk.greenBright(fullName))\n\t\t\t}\n\t\t\tconst configuration = command.getConfiguration()\n\t\t\tconst commandDescription = configuration.getDescription()\n\t\t\tif (commandDescription) {\n\t\t\t\tconsole.error(commandDescription)\n\t\t\t}\n\t\t\tconst shortUsage = configuration.getUsage({ format: 'short' })\n\t\t\tif (shortUsage) {\n\t\t\t\tconsole.error('\\nUsage:')\n\t\t\t\tconsole.error(chalk.green(shortUsage))\n\t\t\t\tconsole.error('\\nArguments and options:')\n\t\t\t\tconsole.error(configuration.getUsage({ format: 'multiline' }))\n\t\t\t}\n\n\t\t\treturn process.exit(0)\n\t\t}\n\n\t\ttry {\n\t\t\tawait this.options.beforeRun?.({ command, args, name: fullName })\n\n\t\t\tconst result = await command.run(args)\n\t\t\treturn process.exit(result)\n\t\t} catch (e) {\n\t\t\tif (e instanceof InvalidInputError) {\n\t\t\t\tconsole.error(chalk.bgRedBright.white(e.message))\n\t\t\t\tconst configuration = command.getConfiguration()\n\t\t\t\tconsole.error(`${showCommandName ? chalk.greenBright(fullName) + ' ' : ''}${chalk.green(configuration.getUsage({ format: 'line' }))}`)\n\n\t\t\t\treturn process.exit(1)\n\t\t\t} else {\n\t\t\t\tconsole.error(e)\n\t\t\t\treturn process.exit(2)\n\t\t\t}\n\t\t}\n\t}\n}\n"],"names":["name","commands","InvalidInputError"],"mappings":";;;;AAKO,MAAM,YAAY;AAAA,EACxB,YACkB,gBACA,wBACA,UAEb,CAAA,GACH;AALgB,SAAA,iBAAA;AACA,SAAA,yBAAA;AACA,SAAA,UAAA;AAAA,EAGf;AAAA,EAEH,MAAM,IAAI,MAA+B;AACxC,UAAM,CAAC,MAAM,GAAG,IAAI,IAAI;AACxB,QAAI,CAAC,QAAQ,SAAS,UAAU;AAC/B,cAAQ,MAAM,KAAK,sBAAsB;AACzC,cAAQ,MAAM,iCAAiC;AAC/C,YAAM,WAAW,OAAO,QAAQ,KAAK,eAAe,QAAQ,EAC1D,OAAO,CAAC,CAACA,OAAM,OAAO,GAAG,OAAOC,cAAaA,UAAS,UAAU,CAAA,OAAM,GAAG,CAAC,MAAM,OAAO,MAAM,KAAK,EAClG,IAAI,CAAC,CAACD,KAAI,MAAMA,KAAI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,iBAAA;AAC9B,YAAM,qBAAqB,cAAc,eAAA;AACzC,UAAI,oBAAoB;AACvB,gBAAQ,MAAM,kBAAkB;AAAA,MACjC;AACA,YAAM,aAAa,cAAc,SAAS,EAAE,QAAQ,SAAS;AAC7D,UAAI,YAAY;AACf,gBAAQ,MAAM,UAAU;AACxB,gBAAQ,MAAM,MAAM,MAAM,UAAU,CAAC;AACrC,gBAAQ,MAAM,0BAA0B;AACxC,gBAAQ,MAAM,cAAc,SAAS,EAAE,QAAQ,YAAA,CAAa,CAAC;AAAA,MAC9D;AAEA,aAAO,QAAQ,KAAK,CAAC;AAAA,IACtB;AAEA,QAAI;AACH,YAAM,KAAK,QAAQ,YAAY,EAAE,SAAS,MAAM,MAAM,UAAU;AAEhE,YAAM,SAAS,MAAM,QAAQ,IAAI,IAAI;AACrC,aAAO,QAAQ,KAAK,MAAM;AAAA,IAC3B,SAAS,GAAG;AACX,UAAI,aAAaE,YAAAA,mBAAmB;AACnC,gBAAQ,MAAM,MAAM,YAAY,MAAM,EAAE,OAAO,CAAC;AAChD,cAAM,gBAAgB,QAAQ,iBAAA;AAC9B,gBAAQ,MAAM,GAAG,kBAAkB,MAAM,YAAY,QAAQ,IAAI,MAAM,EAAE,GAAG,MAAM,MAAM,cAAc,SAAS,EAAE,QAAQ,QAAQ,CAAC,CAAC,EAAE;AAErI,eAAO,QAAQ,KAAK,CAAC;AAAA,MACtB,OAAO;AACN,gBAAQ,MAAM,CAAC;AACf,eAAO,QAAQ,KAAK,CAAC;AAAA,MACtB;AAAA,IACD;AAAA,EACD;AACD;;"}
1
+ {"version":3,"file":"Application.cjs","sources":["../../../../../packages/cli-common/src/application/Application.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { CommandCatalogEntry, CommandFactory, CommandManager } from './CommandManager.js'\nimport { Command } from './Command.js'\nimport { InvalidInputError } from './InputParser.js'\nimport { Output } from './Output.js'\nimport { ExitCode } from './CliError.js'\nimport { renderCliError } from './ErrorRenderer.js'\nimport { exitProcess } from './exitProcess.js'\nimport { globalOptions, hasHelpFlag, readGlobalOptionsFromArgs } from './GlobalOptions.js'\nimport { UsageFormatter } from './UsageFormatter.js'\n\nexport class Application {\n\tprivate readonly commandManager: CommandManager\n\tprivate readonly applicationDescription: string\n\tprivate readonly output: Output\n\tprivate readonly options: ApplicationOptions\n\n\tconstructor(commandManager: CommandManager, applicationDescription: string, options?: ApplicationOptions)\n\tconstructor(commandManager: CommandManager, applicationDescription: string, output: Output, options?: ApplicationOptions)\n\tconstructor(\n\t\tcommandManager: CommandManager,\n\t\tapplicationDescription: string,\n\t\toutputOrOptions: Output | ApplicationOptions = {},\n\t\toptions: ApplicationOptions = {},\n\t) {\n\t\tthis.commandManager = commandManager\n\t\tthis.applicationDescription = applicationDescription\n\t\tthis.output = outputOrOptions instanceof Output ? outputOrOptions : new Output()\n\t\tthis.options = outputOrOptions instanceof Output ? options : outputOrOptions\n\t}\n\n\tasync run(args: string[]): Promise<void> {\n\t\tif (args.length === 0 || readGlobalOptionsFromArgs(args.slice(0, 1)).help) {\n\t\t\texitProcess(await this.execute(args))\n\t\t\treturn\n\t\t}\n\t\tconst [name, ...commandArgs] = args\n\t\tawait this.runCommandInternal(name, commandArgs, true)\n\t}\n\n\tpublic async runCommand(name: string, args: string[]): Promise<void> {\n\t\tawait this.runCommandInternal(name, args, false)\n\t}\n\n\tpublic async runCommandInternal(name: string, args: string[], showCommandName: boolean): Promise<void> {\n\t\texitProcess(await this.executeArgs([name, ...args], showCommandName))\n\t}\n\n\t/** Runs the given argv and returns the exit code instead of terminating the process. */\n\tpublic async execute(args: string[]): Promise<number> {\n\t\tconst help = readGlobalOptionsFromArgs(args.slice(0, 1)).help\n\t\tif (args.length === 0 || help) {\n\t\t\tthis.printApplicationHelp(help)\n\t\t\treturn ExitCode.Success\n\t\t}\n\t\treturn this.executeArgs(args, true)\n\t}\n\n\tpublic async executeCommand(name: string, args: string[]): Promise<number> {\n\t\treturn this.executeArgs([name, ...args], false)\n\t}\n\n\tprivate async executeArgs(args: string[], showCommandName: boolean): Promise<number> {\n\t\tlet entry: CommandCatalogEntry | undefined\n\t\tlet command: ReturnType<CommandFactory> | undefined\n\t\t// the mode has to be known before anything can fail, so that every error is reported in the requested format\n\t\tthis.output.applyGlobalOptions(readGlobalOptionsFromArgs(args))\n\t\ttry {\n\t\t\tconst resolution = this.commandManager.resolve(args)\n\t\t\tif (resolution.type === 'group') {\n\t\t\t\tconst groupHelp = resolution.args.length === 1 && (resolution.args[0] === '--help' || resolution.args[0] === '-h')\n\t\t\t\tif (resolution.args.length > 0 && !groupHelp) {\n\t\t\t\t\tthrow new InvalidInputError(`Unexpected input after command group \"${resolution.name}\": ${resolution.args.join(' ')}`)\n\t\t\t\t}\n\t\t\t\tthis.printGroupHelp(resolution.name, resolution.entries)\n\t\t\t\treturn ExitCode.Success\n\t\t\t}\n\t\t\tentry = resolution.entry\n\t\t\tconst commandArgs = resolution.args\n\t\t\tcommand = entry.create()\n\n\t\t\tif (hasHelpFlag(commandArgs)) {\n\t\t\t\tthis.printCommandHelp(entry.name, command, showCommandName)\n\t\t\t\treturn ExitCode.Success\n\t\t\t}\n\n\t\t\tif (command.run !== Command.prototype.run) {\n\t\t\t\tawait this.options.beforeRun?.({ command, args: commandArgs, name: entry.name })\n\t\t\t\treturn await command.run(commandArgs, this.output)\n\t\t\t}\n\n\t\t\tconst parsed = command.parse(commandArgs)\n\t\t\tthis.output.applyGlobalOptions(parsed.globals)\n\n\t\t\tawait this.options.beforeRun?.({ command, args: commandArgs, name: entry.name })\n\n\t\t\treturn await command.runParsed(parsed, this.output)\n\t\t} catch (e) {\n\t\t\tconst exitCode = renderCliError(e, this.output)\n\t\t\tif (e instanceof InvalidInputError && command !== undefined && this.output.mode === 'human') {\n\t\t\t\tconst usage = command.getConfiguration().getUsage({ format: 'line' })\n\t\t\t\tthis.output.writeStderr(`${showCommandName && entry ? chalk.greenBright(entry.name) + ' ' : ''}${chalk.green(usage)}`)\n\t\t\t}\n\t\t\treturn exitCode\n\t\t}\n\t}\n\n\tprivate printApplicationHelp(verbose: boolean): void {\n\t\tthis.output.writeStderr(this.applicationDescription)\n\t\tthis.output.writeStderr(`Usage: <command> <command args>`)\n\t\tconst commands = this.commandManager.getCommands()\n\t\tif (commands.length === 0) {\n\t\t\treturn\n\t\t}\n\t\tconst maxCommandNameLength = Math.max(...commands.map(it => it.name.length))\n\t\tconst indent = ' '\n\t\tfor (const entry of commands) {\n\t\t\tconst configuration = entry.create().getConfiguration()\n\t\t\tthis.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`)\n\t\t\tif (verbose) {\n\t\t\t\tconst helpIndent = `${indent}${' '.repeat(maxCommandNameLength)}${indent}`\n\t\t\t\tconst usage = configuration.getUsage({ format: 'multiline', indent: helpIndent })\n\t\t\t\tthis.output.writeStderr(usage ? chalk.green(usage) : `${helpIndent}${chalk.gray('No options')}`)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate printGroupHelp(name: string, entries: readonly CommandCatalogEntry[]): void {\n\t\tthis.output.writeStderr(`Usage: ${name} <command> <command args>`)\n\t\tconst maxCommandNameLength = Math.max(...entries.map(it => it.name.length))\n\t\tconst indent = ' '\n\t\tfor (const entry of [...entries].sort((a, b) => a.name.localeCompare(b.name))) {\n\t\t\tconst configuration = entry.create().getConfiguration()\n\t\t\tthis.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`)\n\t\t}\n\t}\n\n\tprivate printCommandHelp(name: string, command: ReturnType<CommandFactory>, showCommandName: boolean): void {\n\t\tif (showCommandName) {\n\t\t\tthis.output.writeStderr(chalk.greenBright(name))\n\t\t}\n\t\tconst configuration = command.getConfiguration()\n\t\tconst commandDescription = configuration.getDescription()\n\t\tif (commandDescription) {\n\t\t\tthis.output.writeStderr(commandDescription)\n\t\t}\n\t\tconst shortUsage = configuration.getUsage({ format: 'short' })\n\t\tif (shortUsage) {\n\t\t\tthis.output.writeStderr('\\nUsage:')\n\t\t\tthis.output.writeStderr(chalk.green(shortUsage))\n\t\t\tthis.output.writeStderr('\\nArguments and options:')\n\t\t\tthis.output.writeStderr(configuration.getUsage({ format: 'multiline' }))\n\t\t}\n\t\tthis.output.writeStderr('\\nGlobal options:')\n\t\tthis.output.writeStderr(UsageFormatter.format([], [...globalOptions], { format: 'multiline' }))\n\t}\n}\n\ninterface ApplicationOptions {\n\tbeforeRun?: (args: { command: ReturnType<CommandFactory>; name: string; args: string[] }) => void | Promise<void>\n}\n"],"names":["Output","readGlobalOptionsFromArgs","exitProcess","ExitCode","InvalidInputError","hasHelpFlag","Command","renderCliError","UsageFormatter","globalOptions"],"mappings":";;;;;;;;;;;;;;AAWO,MAAM,YAAY;AAAA,EAQxB,YACC,gBACA,wBACA,kBAA+C,CAAA,GAC/C,UAA8B,IAC7B;AAZF,kBAAA,MAAiB,gBAAA;AACjB,kBAAA,MAAiB,wBAAA;AACjB,kBAAA,MAAiB,QAAA;AACjB,kBAAA,MAAiB,SAAA;AAUhB,SAAK,iBAAiB;AACtB,SAAK,yBAAyB;AAC9B,SAAK,SAAS,2BAA2BA,OAAAA,SAAS,kBAAkB,IAAIA,OAAAA,OAAA;AACxE,SAAK,UAAU,2BAA2BA,OAAAA,SAAS,UAAU;AAAA,EAC9D;AAAA,EAEA,MAAM,IAAI,MAA+B;AACxC,QAAI,KAAK,WAAW,KAAKC,wCAA0B,KAAK,MAAM,GAAG,CAAC,CAAC,EAAE,MAAM;AAC1EC,kBAAAA,YAAY,MAAM,KAAK,QAAQ,IAAI,CAAC;AACpC;AAAA,IACD;AACA,UAAM,CAAC,MAAM,GAAG,WAAW,IAAI;AAC/B,UAAM,KAAK,mBAAmB,MAAM,aAAa,IAAI;AAAA,EACtD;AAAA,EAEA,MAAa,WAAW,MAAc,MAA+B;AACpE,UAAM,KAAK,mBAAmB,MAAM,MAAM,KAAK;AAAA,EAChD;AAAA,EAEA,MAAa,mBAAmB,M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AY,0BAA0B;AAClD,WAAK,OAAO,YAAY,cAAc,SAAS,EAAE,QAAQ,YAAA,CAAa,CAAC;AAAA,IACxE;AACA,SAAK,OAAO,YAAY,mBAAmB;AAC3C,SAAK,OAAO,YAAYI,eAAAA,eAAe,OAAO,CAAA,GAAI,CAAC,GAAGC,cAAAA,aAAa,GAAG,EAAE,QAAQ,YAAA,CAAa,CAAC;AAAA,EAC/F;AACD;;"}
@@ -1,80 +1,137 @@
1
- import { InvalidInputError } from "./InputParser.js";
2
1
  import chalk from "chalk";
2
+ import { Command } from "./Command.js";
3
+ import { InvalidInputError } from "./InputParser.js";
4
+ import { Output } from "./Output.js";
5
+ import { ExitCode } from "./CliError.js";
6
+ import { renderCliError } from "./ErrorRenderer.js";
7
+ import { exitProcess } from "./exitProcess.js";
8
+ import { readGlobalOptionsFromArgs, hasHelpFlag, globalOptions } from "./GlobalOptions.js";
9
+ import { UsageFormatter } from "./UsageFormatter.js";
10
+ var __defProp = Object.defineProperty;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
3
13
  class Application {
4
- constructor(commandManager, applicationDescription, options = {}) {
14
+ constructor(commandManager, applicationDescription, outputOrOptions = {}, options = {}) {
15
+ __publicField(this, "commandManager");
16
+ __publicField(this, "applicationDescription");
17
+ __publicField(this, "output");
18
+ __publicField(this, "options");
5
19
  this.commandManager = commandManager;
6
20
  this.applicationDescription = applicationDescription;
7
- this.options = options;
21
+ this.output = outputOrOptions instanceof Output ? outputOrOptions : new Output();
22
+ this.options = outputOrOptions instanceof Output ? options : outputOrOptions;
8
23
  }
9
24
  async run(args) {
10
- const [name, ...rest] = args;
11
- if (!name || name === "--help") {
12
- console.error(this.applicationDescription);
13
- console.error(`Usage: <command> <command args>`);
14
- const commands = Object.entries(this.commandManager.commands).filter(([name2, factory], index, commands2) => commands2.findIndex((it) => it[1] === factory) === index).map(([name2]) => name2).sort((a, b) => a.localeCompare(b));
15
- const maxCommandNameLength = Math.max(...commands.map((it) => it.length));
16
- for (let commandName of commands) {
17
- const [, command] = this.commandManager.createCommand(commandName);
18
- const configuration = command.getConfiguration();
19
- const indent = " ";
20
- console.error(
21
- `${indent}${chalk.greenBright(
22
- commandName.padEnd(maxCommandNameLength)
23
- )}${indent}${configuration.getDescription()}`
24
- );
25
- if (name === "--help") {
26
- const helpIndent = `${indent}${" ".repeat(maxCommandNameLength)}${indent}`;
27
- const usage = configuration.getUsage({ format: "multiline", indent: helpIndent });
28
- if (usage) {
29
- console.error(chalk.green(usage));
30
- } else {
31
- console.error(`${helpIndent}${chalk.gray("No options")}`);
32
- }
33
- }
34
- }
35
- return process.exit(0);
25
+ if (args.length === 0 || readGlobalOptionsFromArgs(args.slice(0, 1)).help) {
26
+ exitProcess(await this.execute(args));
27
+ return;
36
28
  }
37
- return this.runCommandInternal(name, rest, true);
29
+ const [name, ...commandArgs] = args;
30
+ await this.runCommandInternal(name, commandArgs, true);
38
31
  }
39
32
  async runCommand(name, args) {
40
- return this.runCommandInternal(name, args, false);
33
+ await this.runCommandInternal(name, args, false);
41
34
  }
42
35
  async runCommandInternal(name, args, showCommandName) {
43
- const [fullName, command] = this.commandManager.createCommand(name);
44
- if (args[0] === "--help" || args[0] === "-h") {
45
- if (showCommandName) {
46
- console.error(chalk.greenBright(fullName));
36
+ exitProcess(await this.executeArgs([name, ...args], showCommandName));
37
+ }
38
+ /** Runs the given argv and returns the exit code instead of terminating the process. */
39
+ async execute(args) {
40
+ const help = readGlobalOptionsFromArgs(args.slice(0, 1)).help;
41
+ if (args.length === 0 || help) {
42
+ this.printApplicationHelp(help);
43
+ return ExitCode.Success;
44
+ }
45
+ return this.executeArgs(args, true);
46
+ }
47
+ async executeCommand(name, args) {
48
+ return this.executeArgs([name, ...args], false);
49
+ }
50
+ async executeArgs(args, showCommandName) {
51
+ let entry;
52
+ let command;
53
+ this.output.applyGlobalOptions(readGlobalOptionsFromArgs(args));
54
+ try {
55
+ const resolution = this.commandManager.resolve(args);
56
+ if (resolution.type === "group") {
57
+ const groupHelp = resolution.args.length === 1 && (resolution.args[0] === "--help" || resolution.args[0] === "-h");
58
+ if (resolution.args.length > 0 && !groupHelp) {
59
+ throw new InvalidInputError(`Unexpected input after command group "${resolution.name}": ${resolution.args.join(" ")}`);
60
+ }
61
+ this.printGroupHelp(resolution.name, resolution.entries);
62
+ return ExitCode.Success;
47
63
  }
48
- const configuration = command.getConfiguration();
49
- const commandDescription = configuration.getDescription();
50
- if (commandDescription) {
51
- console.error(commandDescription);
64
+ entry = resolution.entry;
65
+ const commandArgs = resolution.args;
66
+ command = entry.create();
67
+ if (hasHelpFlag(commandArgs)) {
68
+ this.printCommandHelp(entry.name, command, showCommandName);
69
+ return ExitCode.Success;
52
70
  }
53
- const shortUsage = configuration.getUsage({ format: "short" });
54
- if (shortUsage) {
55
- console.error("\nUsage:");
56
- console.error(chalk.green(shortUsage));
57
- console.error("\nArguments and options:");
58
- console.error(configuration.getUsage({ format: "multiline" }));
71
+ if (command.run !== Command.prototype.run) {
72
+ await this.options.beforeRun?.({ command, args: commandArgs, name: entry.name });
73
+ return await command.run(commandArgs, this.output);
59
74
  }
60
- return process.exit(0);
61
- }
62
- try {
63
- await this.options.beforeRun?.({ command, args, name: fullName });
64
- const result = await command.run(args);
65
- return process.exit(result);
75
+ const parsed = command.parse(commandArgs);
76
+ this.output.applyGlobalOptions(parsed.globals);
77
+ await this.options.beforeRun?.({ command, args: commandArgs, name: entry.name });
78
+ return await command.runParsed(parsed, this.output);
66
79
  } catch (e) {
67
- if (e instanceof InvalidInputError) {
68
- console.error(chalk.bgRedBright.white(e.message));
69
- const configuration = command.getConfiguration();
70
- console.error(`${showCommandName ? chalk.greenBright(fullName) + " " : ""}${chalk.green(configuration.getUsage({ format: "line" }))}`);
71
- return process.exit(1);
72
- } else {
73
- console.error(e);
74
- return process.exit(2);
80
+ const exitCode = renderCliError(e, this.output);
81
+ if (e instanceof InvalidInputError && command !== void 0 && this.output.mode === "human") {
82
+ const usage = command.getConfiguration().getUsage({ format: "line" });
83
+ this.output.writeStderr(`${showCommandName && entry ? chalk.greenBright(entry.name) + " " : ""}${chalk.green(usage)}`);
84
+ }
85
+ return exitCode;
86
+ }
87
+ }
88
+ printApplicationHelp(verbose) {
89
+ this.output.writeStderr(this.applicationDescription);
90
+ this.output.writeStderr(`Usage: <command> <command args>`);
91
+ const commands = this.commandManager.getCommands();
92
+ if (commands.length === 0) {
93
+ return;
94
+ }
95
+ const maxCommandNameLength = Math.max(...commands.map((it) => it.name.length));
96
+ const indent = " ";
97
+ for (const entry of commands) {
98
+ const configuration = entry.create().getConfiguration();
99
+ this.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`);
100
+ if (verbose) {
101
+ const helpIndent = `${indent}${" ".repeat(maxCommandNameLength)}${indent}`;
102
+ const usage = configuration.getUsage({ format: "multiline", indent: helpIndent });
103
+ this.output.writeStderr(usage ? chalk.green(usage) : `${helpIndent}${chalk.gray("No options")}`);
75
104
  }
76
105
  }
77
106
  }
107
+ printGroupHelp(name, entries) {
108
+ this.output.writeStderr(`Usage: ${name} <command> <command args>`);
109
+ const maxCommandNameLength = Math.max(...entries.map((it) => it.name.length));
110
+ const indent = " ";
111
+ for (const entry of [...entries].sort((a, b) => a.name.localeCompare(b.name))) {
112
+ const configuration = entry.create().getConfiguration();
113
+ this.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`);
114
+ }
115
+ }
116
+ printCommandHelp(name, command, showCommandName) {
117
+ if (showCommandName) {
118
+ this.output.writeStderr(chalk.greenBright(name));
119
+ }
120
+ const configuration = command.getConfiguration();
121
+ const commandDescription = configuration.getDescription();
122
+ if (commandDescription) {
123
+ this.output.writeStderr(commandDescription);
124
+ }
125
+ const shortUsage = configuration.getUsage({ format: "short" });
126
+ if (shortUsage) {
127
+ this.output.writeStderr("\nUsage:");
128
+ this.output.writeStderr(chalk.green(shortUsage));
129
+ this.output.writeStderr("\nArguments and options:");
130
+ this.output.writeStderr(configuration.getUsage({ format: "multiline" }));
131
+ }
132
+ this.output.writeStderr("\nGlobal options:");
133
+ this.output.writeStderr(UsageFormatter.format([], [...globalOptions], { format: "multiline" }));
134
+ }
78
135
  }
79
136
  export {
80
137
  Application
@@ -1 +1 @@
1
- {"version":3,"file":"Application.js","sources":["../../../../../packages/cli-common/src/application/Application.ts"],"sourcesContent":["import { InvalidInputError } from './InputParser.js'\nimport { CommandManager } from './CommandManager.js'\nimport chalk from 'chalk'\nimport { Command } from './Command.js'\n\nexport class Application {\n\tconstructor(\n\t\tprivate readonly commandManager: CommandManager,\n\t\tprivate readonly applicationDescription: string,\n\t\tprivate readonly options: {\n\t\t\tbeforeRun?: (args: { command: Command<any, any>; name: string; args: string[] }) => void\n\t\t} = {},\n\t) {}\n\n\tasync run(args: string[]): Promise<void> {\n\t\tconst [name, ...rest] = args\n\t\tif (!name || name === '--help') {\n\t\t\tconsole.error(this.applicationDescription)\n\t\t\tconsole.error(`Usage: <command> <command args>`)\n\t\t\tconst commands = Object.entries(this.commandManager.commands)\n\t\t\t\t.filter(([name, factory], index, commands) => commands.findIndex(it => it[1] === factory) === index)\n\t\t\t\t.map(([name]) => name)\n\t\t\t\t.sort((a, b) => a.localeCompare(b))\n\n\t\t\tconst maxCommandNameLength = Math.max(...commands.map(it => it.length))\n\n\t\t\tfor (let commandName of commands) {\n\t\t\t\tconst [, command] = this.commandManager.createCommand(commandName)\n\t\t\t\tconst configuration = command.getConfiguration()\n\t\t\t\tconst indent = ' '\n\t\t\t\tconsole.error(\n\t\t\t\t\t`${indent}${\n\t\t\t\t\t\tchalk.greenBright(\n\t\t\t\t\t\t\tcommandName.padEnd(maxCommandNameLength),\n\t\t\t\t\t\t)\n\t\t\t\t\t}${indent}${configuration.getDescription()}`,\n\t\t\t\t)\n\t\t\t\tif (name === '--help') {\n\t\t\t\t\tconst helpIndent = `${indent}${' '.repeat(maxCommandNameLength)}${indent}`\n\t\t\t\t\tconst usage = configuration.getUsage({ format: 'multiline', indent: helpIndent })\n\t\t\t\t\tif (usage) {\n\t\t\t\t\t\tconsole.error(chalk.green(usage))\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconsole.error(`${helpIndent}${chalk.gray('No options')}`)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn process.exit(0)\n\t\t}\n\t\treturn this.runCommandInternal(name, rest, true)\n\t}\n\n\tpublic async runCommand(name: string, args: string[]) {\n\t\treturn this.runCommandInternal(name, args, false)\n\t}\n\n\tpublic async runCommandInternal(name: string, args: string[], showCommandName: boolean) {\n\t\tconst [fullName, command] = this.commandManager.createCommand(name)\n\n\t\tif (args[0] === '--help' || args[0] === '-h') {\n\t\t\tif (showCommandName) {\n\t\t\t\tconsole.error(chalk.greenBright(fullName))\n\t\t\t}\n\t\t\tconst configuration = command.getConfiguration()\n\t\t\tconst commandDescription = configuration.getDescription()\n\t\t\tif (commandDescription) {\n\t\t\t\tconsole.error(commandDescription)\n\t\t\t}\n\t\t\tconst shortUsage = configuration.getUsage({ format: 'short' })\n\t\t\tif (shortUsage) {\n\t\t\t\tconsole.error('\\nUsage:')\n\t\t\t\tconsole.error(chalk.green(shortUsage))\n\t\t\t\tconsole.error('\\nArguments and options:')\n\t\t\t\tconsole.error(configuration.getUsage({ format: 'multiline' }))\n\t\t\t}\n\n\t\t\treturn process.exit(0)\n\t\t}\n\n\t\ttry {\n\t\t\tawait this.options.beforeRun?.({ command, args, name: fullName })\n\n\t\t\tconst result = await command.run(args)\n\t\t\treturn process.exit(result)\n\t\t} catch (e) {\n\t\t\tif (e instanceof InvalidInputError) {\n\t\t\t\tconsole.error(chalk.bgRedBright.white(e.message))\n\t\t\t\tconst configuration = command.getConfiguration()\n\t\t\t\tconsole.error(`${showCommandName ? chalk.greenBright(fullName) + ' ' : ''}${chalk.green(configuration.getUsage({ format: 'line' }))}`)\n\n\t\t\t\treturn process.exit(1)\n\t\t\t} else {\n\t\t\t\tconsole.error(e)\n\t\t\t\treturn process.exit(2)\n\t\t\t}\n\t\t}\n\t}\n}\n"],"names":["name","commands"],"mappings":";;AAKO,MAAM,YAAY;AAAA,EACxB,YACkB,gBACA,wBACA,UAEb,CAAA,GACH;AALgB,SAAA,iBAAA;AACA,SAAA,yBAAA;AACA,SAAA,UAAA;AAAA,EAGf;AAAA,EAEH,MAAM,IAAI,MAA+B;AACxC,UAAM,CAAC,MAAM,GAAG,IAAI,IAAI;AACxB,QAAI,CAAC,QAAQ,SAAS,UAAU;AAC/B,cAAQ,MAAM,KAAK,sBAAsB;AACzC,cAAQ,MAAM,iCAAiC;AAC/C,YAAM,WAAW,OAAO,QAAQ,KAAK,eAAe,QAAQ,EAC1D,OAAO,CAAC,CAACA,OAAM,OAAO,GAAG,OAAOC,cAAaA,UAAS,UAAU,CAAA,OAAM,GAAG,CAAC,MAAM,OAAO,MAAM,KAAK,EAClG,IAAI,CAAC,CAACD,KAAI,MAAMA,KAAI,EACpB,KAAK,CAAC,GAAG,MAAM,EAAE,cAAc,CAAC,CAAC;AAEnC,YAAM,uBAAuB,KAAK,IAAI,GAAG,SAAS,IAAI,CAAA,OAAM,GAAG,MAAM,CAAC;AAEtE,eAAS,eAAe,UAAU;AACjC,cAAM,CAAA,EAAG,OAAO,IAAI,KAAK,eAAe,cAAc,WAAW;AACjE,cAAM,gBAAgB,QAAQ,iBAAA;AAC9B,cAAM,SAAS;AACf,gBAAQ;AAAA,UACP,GAAG,MAAM,GACR,MAAM;AAAA,YACL,YAAY,OAAO,oBAAoB;AAAA,UAAA,CAEzC,GAAG,MAAM,GAAG,cAAc,gBAAgB;AAAA,QAAA;AAE3C,YAAI,SAAS,UAAU;AACtB,gBAAM,aAAa,GAAG,MAAM,GAAG,IAAI,OAAO,oBAAoB,CAAC,GAAG,MAAM;AACxE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,KAAK,MAAM;AAAA,IAC3B,SAAS,GAAG;AACX,UAAI,aAAa,mBAAmB;AACnC,gBAAQ,MAAM,MAAM,YAAY,MAAM,EAAE,OAAO,CAAC;AAChD,cAAM,gBAAgB,QAAQ,iBAAA;AAC9B,gBAAQ,MAAM,GAAG,kBAAkB,MAAM,YAAY,QAAQ,IAAI,MAAM,EAAE,GAAG,MAAM,MAAM,cAAc,SAAS,EAAE,QAAQ,QAAQ,CAAC,CAAC,EAAE;AAErI,eAAO,QAAQ,KAAK,CAAC;AAAA,MACtB,OAAO;AACN,gBAAQ,MAAM,CAAC;AACf,eAAO,QAAQ,KAAK,CAAC;AAAA,MACtB;AAAA,IACD;AAAA,EACD;AACD;"}
1
+ {"version":3,"file":"Application.js","sources":["../../../../../packages/cli-common/src/application/Application.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { CommandCatalogEntry, CommandFactory, CommandManager } from './CommandManager.js'\nimport { Command } from './Command.js'\nimport { InvalidInputError } from './InputParser.js'\nimport { Output } from './Output.js'\nimport { ExitCode } from './CliError.js'\nimport { renderCliError } from './ErrorRenderer.js'\nimport { exitProcess } from './exitProcess.js'\nimport { globalOptions, hasHelpFlag, readGlobalOptionsFromArgs } from './GlobalOptions.js'\nimport { UsageFormatter } from './UsageFormatter.js'\n\nexport class Application {\n\tprivate readonly commandManager: CommandManager\n\tprivate readonly applicationDescription: string\n\tprivate readonly output: Output\n\tprivate readonly options: ApplicationOptions\n\n\tconstructor(commandManager: CommandManager, applicationDescription: string, options?: ApplicationOptions)\n\tconstructor(commandManager: CommandManager, applicationDescription: string, output: Output, options?: ApplicationOptions)\n\tconstructor(\n\t\tcommandManager: CommandManager,\n\t\tapplicationDescription: string,\n\t\toutputOrOptions: Output | ApplicationOptions = {},\n\t\toptions: ApplicationOptions = {},\n\t) {\n\t\tthis.commandManager = commandManager\n\t\tthis.applicationDescription = applicationDescription\n\t\tthis.output = outputOrOptions instanceof Output ? outputOrOptions : new Output()\n\t\tthis.options = outputOrOptions instanceof Output ? options : outputOrOptions\n\t}\n\n\tasync run(args: string[]): Promise<void> {\n\t\tif (args.length === 0 || readGlobalOptionsFromArgs(args.slice(0, 1)).help) {\n\t\t\texitProcess(await this.execute(args))\n\t\t\treturn\n\t\t}\n\t\tconst [name, ...commandArgs] = args\n\t\tawait this.runCommandInternal(name, commandArgs, true)\n\t}\n\n\tpublic async runCommand(name: string, args: string[]): Promise<void> {\n\t\tawait this.runCommandInternal(name, args, false)\n\t}\n\n\tpublic async runCommandInternal(name: string, args: string[], showCommandName: boolean): Promise<void> {\n\t\texitProcess(await this.executeArgs([name, ...args], showCommandName))\n\t}\n\n\t/** Runs the given argv and returns the exit code instead of terminating the process. */\n\tpublic async execute(args: string[]): Promise<number> {\n\t\tconst help = readGlobalOptionsFromArgs(args.slice(0, 1)).help\n\t\tif (args.length === 0 || help) {\n\t\t\tthis.printApplicationHelp(help)\n\t\t\treturn ExitCode.Success\n\t\t}\n\t\treturn this.executeArgs(args, true)\n\t}\n\n\tpublic async executeCommand(name: string, args: string[]): Promise<number> {\n\t\treturn this.executeArgs([name, ...args], false)\n\t}\n\n\tprivate async executeArgs(args: string[], showCommandName: boolean): Promise<number> {\n\t\tlet entry: CommandCatalogEntry | undefined\n\t\tlet command: ReturnType<CommandFactory> | undefined\n\t\t// the mode has to be known before anything can fail, so that every error is reported in the requested format\n\t\tthis.output.applyGlobalOptions(readGlobalOptionsFromArgs(args))\n\t\ttry {\n\t\t\tconst resolution = this.commandManager.resolve(args)\n\t\t\tif (resolution.type === 'group') {\n\t\t\t\tconst groupHelp = resolution.args.length === 1 && (resolution.args[0] === '--help' || resolution.args[0] === '-h')\n\t\t\t\tif (resolution.args.length > 0 && !groupHelp) {\n\t\t\t\t\tthrow new InvalidInputError(`Unexpected input after command group \"${resolution.name}\": ${resolution.args.join(' ')}`)\n\t\t\t\t}\n\t\t\t\tthis.printGroupHelp(resolution.name, resolution.entries)\n\t\t\t\treturn ExitCode.Success\n\t\t\t}\n\t\t\tentry = resolution.entry\n\t\t\tconst commandArgs = resolution.args\n\t\t\tcommand = entry.create()\n\n\t\t\tif (hasHelpFlag(commandArgs)) {\n\t\t\t\tthis.printCommandHelp(entry.name, command, showCommandName)\n\t\t\t\treturn ExitCode.Success\n\t\t\t}\n\n\t\t\tif (command.run !== Command.prototype.run) {\n\t\t\t\tawait this.options.beforeRun?.({ command, args: commandArgs, name: entry.name })\n\t\t\t\treturn await command.run(commandArgs, this.output)\n\t\t\t}\n\n\t\t\tconst parsed = command.parse(commandArgs)\n\t\t\tthis.output.applyGlobalOptions(parsed.globals)\n\n\t\t\tawait this.options.beforeRun?.({ command, args: commandArgs, name: entry.name })\n\n\t\t\treturn await command.runParsed(parsed, this.output)\n\t\t} catch (e) {\n\t\t\tconst exitCode = renderCliError(e, this.output)\n\t\t\tif (e instanceof InvalidInputError && command !== undefined && this.output.mode === 'human') {\n\t\t\t\tconst usage = command.getConfiguration().getUsage({ format: 'line' })\n\t\t\t\tthis.output.writeStderr(`${showCommandName && entry ? chalk.greenBright(entry.name) + ' ' : ''}${chalk.green(usage)}`)\n\t\t\t}\n\t\t\treturn exitCode\n\t\t}\n\t}\n\n\tprivate printApplicationHelp(verbose: boolean): void {\n\t\tthis.output.writeStderr(this.applicationDescription)\n\t\tthis.output.writeStderr(`Usage: <command> <command args>`)\n\t\tconst commands = this.commandManager.getCommands()\n\t\tif (commands.length === 0) {\n\t\t\treturn\n\t\t}\n\t\tconst maxCommandNameLength = Math.max(...commands.map(it => it.name.length))\n\t\tconst indent = ' '\n\t\tfor (const entry of commands) {\n\t\t\tconst configuration = entry.create().getConfiguration()\n\t\t\tthis.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`)\n\t\t\tif (verbose) {\n\t\t\t\tconst helpIndent = `${indent}${' '.repeat(maxCommandNameLength)}${indent}`\n\t\t\t\tconst usage = configuration.getUsage({ format: 'multiline', indent: helpIndent })\n\t\t\t\tthis.output.writeStderr(usage ? chalk.green(usage) : `${helpIndent}${chalk.gray('No options')}`)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate printGroupHelp(name: string, entries: readonly CommandCatalogEntry[]): void {\n\t\tthis.output.writeStderr(`Usage: ${name} <command> <command args>`)\n\t\tconst maxCommandNameLength = Math.max(...entries.map(it => it.name.length))\n\t\tconst indent = ' '\n\t\tfor (const entry of [...entries].sort((a, b) => a.name.localeCompare(b.name))) {\n\t\t\tconst configuration = entry.create().getConfiguration()\n\t\t\tthis.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`)\n\t\t}\n\t}\n\n\tprivate printCommandHelp(name: string, command: ReturnType<CommandFactory>, showCommandName: boolean): void {\n\t\tif (showCommandName) {\n\t\t\tthis.output.writeStderr(chalk.greenBright(name))\n\t\t}\n\t\tconst configuration = command.getConfiguration()\n\t\tconst commandDescription = configuration.getDescription()\n\t\tif (commandDescription) {\n\t\t\tthis.output.writeStderr(commandDescription)\n\t\t}\n\t\tconst shortUsage = configuration.getUsage({ format: 'short' })\n\t\tif (shortUsage) {\n\t\t\tthis.output.writeStderr('\\nUsage:')\n\t\t\tthis.output.writeStderr(chalk.green(shortUsage))\n\t\t\tthis.output.writeStderr('\\nArguments and options:')\n\t\t\tthis.output.writeStderr(configuration.getUsage({ format: 'multiline' }))\n\t\t}\n\t\tthis.output.writeStderr('\\nGlobal options:')\n\t\tthis.output.writeStderr(UsageFormatter.format([], [...globalOptions], { format: 'multiline' }))\n\t}\n}\n\ninterface ApplicationOptions {\n\tbeforeRun?: (args: { command: ReturnType<CommandFactory>; name: string; args: string[] }) => void | Promise<void>\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AAWO,MAAM,YAAY;AAAA,EAQxB,YACC,gBACA,wBACA,kBAA+C,CAAA,GAC/C,UAA8B,IAC7B;AAZF,kBAAA,MAAiB,gBAAA;AACjB,kBAAA,MAAiB,wBAAA;AACjB,kBAAA,MAAiB,Q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AC,GAAG,MAAM,GAAG,cAAc,eAAA,CAAgB,EAAE;AAAA,IAC3I;AAAA,EACD;AAAA,EAEQ,iBAAiB,MAAc,SAAqC,iBAAgC;AAC3G,QAAI,iBAAiB;AACpB,WAAK,OAAO,YAAY,MAAM,YAAY,IAAI,CAAC;AAAA,IAChD;AACA,UAAM,gBAAgB,QAAQ,iBAAA;AAC9B,UAAM,qBAAqB,cAAc,eAAA;AACzC,QAAI,oBAAoB;AACvB,WAAK,OAAO,YAAY,kBAAkB;AAAA,IAC3C;AACA,UAAM,aAAa,cAAc,SAAS,EAAE,QAAQ,SAAS;AAC7D,QAAI,YAAY;AACf,WAAK,OAAO,YAAY,UAAU;AAClC,WAAK,OAAO,YAAY,MAAM,MAAM,UAAU,CAAC;AAC/C,WAAK,OAAO,YAAY,0BAA0B;AAClD,WAAK,OAAO,YAAY,cAAc,SAAS,EAAE,QAAQ,YAAA,CAAa,CAAC;AAAA,IACxE;AACA,SAAK,OAAO,YAAY,mBAAmB;AAC3C,SAAK,OAAO,YAAY,eAAe,OAAO,CAAA,GAAI,CAAC,GAAG,aAAa,GAAG,EAAE,QAAQ,YAAA,CAAa,CAAC;AAAA,EAC/F;AACD;"}
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const InputParser = require("./InputParser.cjs");
4
+ var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ var ExitCode = /* @__PURE__ */ ((ExitCode2) => {
8
+ ExitCode2[ExitCode2["Success"] = 0] = "Success";
9
+ ExitCode2[ExitCode2["InputError"] = 1] = "InputError";
10
+ ExitCode2[ExitCode2["InternalError"] = 2] = "InternalError";
11
+ ExitCode2[ExitCode2["NotFound"] = 3] = "NotFound";
12
+ ExitCode2[ExitCode2["Transient"] = 4] = "Transient";
13
+ ExitCode2[ExitCode2["Conflict"] = 5] = "Conflict";
14
+ ExitCode2[ExitCode2["Forbidden"] = 6] = "Forbidden";
15
+ return ExitCode2;
16
+ })(ExitCode || {});
17
+ class CliError extends Error {
18
+ constructor(message, options) {
19
+ super(message, { cause: options.cause });
20
+ __publicField(this, "code");
21
+ __publicField(this, "exitCode");
22
+ __publicField(this, "retryable");
23
+ __publicField(this, "details");
24
+ this.name = "CliError";
25
+ this.code = options.code;
26
+ this.exitCode = options.exitCode ?? 1;
27
+ this.retryable = options.retryable ?? this.exitCode === 4;
28
+ this.details = normalizeJsonValue(options.details);
29
+ }
30
+ }
31
+ const unexpectedCliErrors = /* @__PURE__ */ new WeakSet();
32
+ const isUnexpectedCliError = (error) => unexpectedCliErrors.has(error);
33
+ const toCliError = (error) => {
34
+ if (error instanceof CliError) {
35
+ return error;
36
+ }
37
+ if (error instanceof InputParser.InvalidInputError) {
38
+ return new CliError(error.message, { code: "INVALID_INPUT", exitCode: 1, cause: error });
39
+ }
40
+ if (typeof error === "string") {
41
+ return new CliError(error, {
42
+ code: "ERROR",
43
+ exitCode: 1
44
+ /* InputError */
45
+ });
46
+ }
47
+ if (error instanceof Error) {
48
+ const cliError2 = new CliError(error.message, { code: "UNKNOWN", exitCode: 2, details: error.stack, cause: error });
49
+ unexpectedCliErrors.add(cliError2);
50
+ return cliError2;
51
+ }
52
+ const cliError = new CliError(safeString(error), { code: "UNKNOWN", exitCode: 2, details: error });
53
+ unexpectedCliErrors.add(cliError);
54
+ return cliError;
55
+ };
56
+ const safeString = (value) => {
57
+ try {
58
+ return String(value);
59
+ } catch {
60
+ return "Unknown error";
61
+ }
62
+ };
63
+ const normalizeJsonValue = (value) => normalizeValue(value, /* @__PURE__ */ new WeakSet());
64
+ const normalizeValue = (value, ancestors) => {
65
+ if (value === void 0) {
66
+ return void 0;
67
+ }
68
+ if (value === null || typeof value === "string" || typeof value === "boolean") {
69
+ return value;
70
+ }
71
+ if (typeof value === "number") {
72
+ return Number.isFinite(value) ? value : String(value);
73
+ }
74
+ if (typeof value === "bigint" || typeof value === "symbol" || typeof value === "function") {
75
+ return safeString(value);
76
+ }
77
+ if (ancestors.has(value)) {
78
+ return "[Circular]";
79
+ }
80
+ ancestors.add(value);
81
+ try {
82
+ if (Array.isArray(value)) {
83
+ return value.map((item) => normalizeValue(item, ancestors) ?? null);
84
+ }
85
+ const result = {};
86
+ for (const [key, item] of Object.entries(value)) {
87
+ const normalized = normalizeValue(item, ancestors);
88
+ if (normalized !== void 0) {
89
+ result[key] = normalized;
90
+ }
91
+ }
92
+ return result;
93
+ } catch {
94
+ return safeString(value);
95
+ } finally {
96
+ ancestors.delete(value);
97
+ }
98
+ };
99
+ exports.CliError = CliError;
100
+ exports.ExitCode = ExitCode;
101
+ exports.isUnexpectedCliError = isUnexpectedCliError;
102
+ exports.normalizeJsonValue = normalizeJsonValue;
103
+ exports.toCliError = toCliError;
104
+ //# sourceMappingURL=CliError.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CliError.cjs","sources":["../../../../../packages/cli-common/src/application/CliError.ts"],"sourcesContent":["import { InvalidInputError } from './InputParser.js'\n\nexport enum ExitCode {\n\tSuccess = 0,\n\t/** invalid input / validation */\n\tInputError = 1,\n\t/** unexpected, a bug */\n\tInternalError = 2,\n\tNotFound = 3,\n\t/** network, timeout, 5xx — the caller SHOULD retry */\n\tTransient = 4,\n\t/** already exists */\n\tConflict = 5,\n\t/** auth / permission denied */\n\tForbidden = 6,\n}\n\nexport interface CliErrorOptions {\n\t/** stable machine-readable slug, e.g. 'PROJECT_NOT_DEFINED' */\n\tcode: string\n\texitCode?: ExitCode\n\tretryable?: boolean\n\tdetails?: unknown\n\tcause?: unknown\n}\n\nexport type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue }\n\nexport class CliError extends Error {\n\tpublic readonly code: string\n\tpublic readonly exitCode: ExitCode\n\tpublic readonly retryable: boolean\n\tpublic readonly details?: JsonValue\n\n\tconstructor(message: string, options: CliErrorOptions) {\n\t\tsuper(message, { cause: options.cause })\n\t\tthis.name = 'CliError'\n\t\tthis.code = options.code\n\t\tthis.exitCode = options.exitCode ?? ExitCode.InputError\n\t\tthis.retryable = options.retryable ?? this.exitCode === ExitCode.Transient\n\t\tthis.details = normalizeJsonValue(options.details)\n\t}\n}\n\nconst unexpectedCliErrors = new WeakSet<CliError>()\n\nexport const isUnexpectedCliError = (error: CliError): boolean => unexpectedCliErrors.has(error)\n\n/**\n * Normalizes anything thrown by a command into a {@link CliError}. Legacy `throw 'some message'` is\n * treated as an input error, everything else unexpected as an internal one.\n */\nexport const toCliError = (error: unknown): CliError => {\n\tif (error instanceof CliError) {\n\t\treturn error\n\t}\n\tif (error instanceof InvalidInputError) {\n\t\treturn new CliError(error.message, { code: 'INVALID_INPUT', exitCode: ExitCode.InputError, cause: error })\n\t}\n\tif (typeof error === 'string') {\n\t\treturn new CliError(error, { code: 'ERROR', exitCode: ExitCode.InputError })\n\t}\n\tif (error instanceof Error) {\n\t\tconst cliError = new CliError(error.message, { code: 'UNKNOWN', exitCode: ExitCode.InternalError, details: error.stack, cause: error })\n\t\tunexpectedCliErrors.add(cliError)\n\t\treturn cliError\n\t}\n\tconst cliError = new CliError(safeString(error), { code: 'UNKNOWN', exitCode: ExitCode.InternalError, details: error })\n\tunexpectedCliErrors.add(cliError)\n\treturn cliError\n}\n\nconst safeString = (value: unknown): string => {\n\ttry {\n\t\treturn String(value)\n\t} catch {\n\t\treturn 'Unknown error'\n\t}\n}\n\nexport const normalizeJsonValue = (value: unknown): JsonValue | undefined => normalizeValue(value, new WeakSet())\n\nconst normalizeValue = (value: unknown, ancestors: WeakSet<object>): JsonValue | undefined => {\n\tif (value === undefined) {\n\t\treturn undefined\n\t}\n\tif (value === null || typeof value === 'string' || typeof value === 'boolean') {\n\t\treturn value\n\t}\n\tif (typeof value === 'number') {\n\t\treturn Number.isFinite(value) ? value : String(value)\n\t}\n\tif (typeof value === 'bigint' || typeof value === 'symbol' || typeof value === 'function') {\n\t\treturn safeString(value)\n\t}\n\tif (ancestors.has(value)) {\n\t\treturn '[Circular]'\n\t}\n\tancestors.add(value)\n\ttry {\n\t\tif (Array.isArray(value)) {\n\t\t\treturn value.map(item => normalizeValue(item, ancestors) ?? null)\n\t\t}\n\t\tconst result: { [key: string]: JsonValue } = {}\n\t\tfor (const [key, item] of Object.entries(value)) {\n\t\t\tconst normalized = normalizeValue(item, ancestors)\n\t\t\tif (normalized !== undefined) {\n\t\t\t\tresult[key] = normalized\n\t\t\t}\n\t\t}\n\t\treturn result\n\t} catch {\n\t\treturn safeString(value)\n\t} finally {\n\t\tancestors.delete(value)\n\t}\n}\n"],"names":["ExitCode","InvalidInputError","cliError"],"mappings":";;;;;;AAEO,IAAK,6BAAAA,cAAL;AACNA,YAAAA,UAAA,aAAU,CAAA,IAAV;AAEAA,YAAAA,UAAA,gBAAa,CAAA,IAAb;AAEAA,YAAAA,UAAA,mBAAgB,CAAA,IAAhB;AACAA,YAAAA,UAAA,cAAW,CAAA,IAAX;AAEAA,YAAAA,UAAA,eAAY,CAAA,IAAZ;AAEAA,YAAAA,UAAA,cAAW,CAAA,IAAX;AAEAA,YAAAA,UAAA,eAAY,CAAA,IAAZ;AAZW,SAAAA;AAAA,GAAA,YAAA,CAAA,CAAA;AA0BL,MAAM,iBAAiB,MAAM;AAAA,EAMnC,YAAY,SAAiB,SAA0B;AACtD,UAAM,SAAS,EAAE,OAAO,QAAQ,OAAO;AANxC,kBAAA,MAAgB,MAAA;AAChB,kBAAA,MAAgB,UAAA;AAChB,kBAAA,MAAgB,WAAA;AAChB,kBAAA,MAAgB,SAAA;AAIf,SAAK,OAAO;AACZ,SAAK,OAAO,QAAQ;AACpB,SAAK,WAAW,QAAQ,YAAY;AACpC,SAAK,YAAY,QAAQ,aAAa,KAAK,aAAa;AACxD,SAAK,UAAU,mBAAmB,QAAQ,OAAO;AAAA,EAClD;AACD;AAEA,MAA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