@contember/cli-common 2.2.0-alpha.2 → 2.2.0-alpha.4

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,37 +1,155 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const chalk = require("chalk");
3
+ const CliError = require("./CliError.cjs");
4
+ const CommandConfiguration = require("./CommandConfiguration.cjs");
5
+ var __defProp = Object.defineProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
+ const splitTokens = (name) => name.split(/[\s:]+/).filter((it) => it !== "");
4
9
  class CommandManager {
5
10
  constructor(commands) {
6
11
  this.commands = commands;
12
+ __publicField(this, "entries");
13
+ __publicField(this, "lookup", /* @__PURE__ */ new Map());
14
+ __publicField(this, "groups", /* @__PURE__ */ new Map());
15
+ __publicField(this, "maxTokens");
16
+ this.entries = createCatalog(commands);
17
+ for (const entry of this.entries) {
18
+ for (const name of [entry.name, ...entry.aliases]) {
19
+ const tokens = splitTokens(name);
20
+ const normalizedName = tokens.join(" ");
21
+ const existing = this.lookup.get(normalizedName);
22
+ if (existing !== void 0 && existing.create !== entry.create) {
23
+ throw new CommandConfiguration.InvalidConfigurationError(
24
+ `Command names "${existing.name}" and "${name}" both normalize to "${normalizedName}" but use different factories.`
25
+ );
26
+ }
27
+ this.lookup.set(normalizedName, entry);
28
+ for (let i = 1; i < tokens.length; i++) {
29
+ const prefix = tokens.slice(0, i).join(" ");
30
+ const group = this.groups.get(prefix) ?? [];
31
+ if (!group.includes(entry)) {
32
+ group.push(entry);
33
+ }
34
+ this.groups.set(prefix, group);
35
+ }
36
+ }
37
+ }
38
+ this.maxTokens = Math.max(1, ...[...this.lookup.keys()].map((it) => splitTokens(it).length));
39
+ this.validateNoPrefixCollision();
7
40
  }
8
- createCommand(name) {
9
- const fullName = this.findCommand(name);
10
- const factory = this.commands[fullName];
11
- const originalCommand = Object.entries(this.commands).find(([, it]) => it === factory);
12
- if (!originalCommand) {
13
- throw new Error("impl error");
41
+ getCommands() {
42
+ return [...this.entries].sort((a, b) => a.name.localeCompare(b.name));
43
+ }
44
+ /**
45
+ * Resolves the longest command name matching the leading argv tokens. `migrations diff`,
46
+ * `migrations:diff` and the `m:d` shorthand all resolve to the same command; the tokens that were
47
+ * not consumed are returned untouched as the command arguments.
48
+ */
49
+ resolve(args) {
50
+ const tokens = [];
51
+ const checkpoints = [];
52
+ for (let i = 0; i < args.length; i++) {
53
+ if (args[i].startsWith("-")) {
54
+ break;
55
+ }
56
+ const parts = splitTokens(args[i]);
57
+ if (parts.length === 0 || tokens.length + parts.length > this.maxTokens) {
58
+ break;
59
+ }
60
+ tokens.push(...parts);
61
+ checkpoints.push({ tokens: [...tokens], argvIndex: i + 1 });
62
+ }
63
+ for (let i = checkpoints.length - 1; i >= 0; i--) {
64
+ const entry = this.lookup.get(checkpoints[i].tokens.join(" "));
65
+ if (entry) {
66
+ return { type: "command", entry, args: args.slice(checkpoints[i].argvIndex) };
67
+ }
68
+ }
69
+ const group = this.groups.get(tokens.join(" "));
70
+ if (group !== void 0 && group.length > 0) {
71
+ const argvIndex = checkpoints[checkpoints.length - 1]?.argvIndex ?? 0;
72
+ return { type: "group", name: tokens.join(" "), entries: group, args: args.slice(argvIndex) };
73
+ }
74
+ for (let i = checkpoints.length - 1; i >= 0; i--) {
75
+ if (i < checkpoints.length - 1 && this.groups.has(checkpoints[i].tokens.join(" "))) {
76
+ continue;
77
+ }
78
+ const matched = this.matchShorthand(checkpoints[i].tokens);
79
+ if (matched.length === 1) {
80
+ return { type: "command", entry: matched[0], args: args.slice(checkpoints[i].argvIndex) };
81
+ }
82
+ if (matched.length > 1) {
83
+ throw new CliError.CliError(
84
+ `Command "${checkpoints[i].tokens.join(" ")}" is ambiguous. Did you mean one of these?
85
+ - ${matched.map((it) => it.name).join("\n - ")}`,
86
+ { code: "COMMAND_AMBIGUOUS", exitCode: CliError.ExitCode.InputError }
87
+ );
88
+ }
14
89
  }
15
- if (originalCommand[0] !== fullName) {
16
- console.warn(chalk.yellow(`Command name "${fullName}" is deprecated, use "${originalCommand[0]}" instead.`));
90
+ for (let i = tokens.length - 1; i >= 1; i--) {
91
+ const known = this.groups.get(tokens.slice(0, i).join(" "));
92
+ if (known !== void 0 && known.length > 0) {
93
+ throw new CliError.CliError(
94
+ `Command "${tokens.join(" ")}" not found. Available commands in "${tokens.slice(0, i).join(" ")}":
95
+ - ${known.map((it) => it.name).sort((a, b) => a.localeCompare(b)).join("\n - ")}`,
96
+ { code: "COMMAND_NOT_FOUND", exitCode: CliError.ExitCode.InputError }
97
+ );
98
+ }
17
99
  }
18
- return [fullName, factory()];
100
+ throw new CliError.CliError(`Command "${args[0] ?? ""}" not found.`, { code: "COMMAND_NOT_FOUND", exitCode: CliError.ExitCode.InputError });
19
101
  }
20
- findCommand(name) {
21
- if (this.commands[name]) {
22
- return name;
102
+ createCommand(name) {
103
+ const resolution = this.resolve([name]);
104
+ if (resolution.type === "group") {
105
+ throw new CliError.CliError(
106
+ `Command "${resolution.name}" is a command group. Available commands:
107
+ - ${resolution.entries.map((it) => it.name).join("\n - ")}`,
108
+ { code: "COMMAND_AMBIGUOUS", exitCode: CliError.ExitCode.InputError }
109
+ );
23
110
  }
24
- const pattern = new RegExp("^" + name.replace(/:/g, "\\w*\\:"));
25
- const matchedCommands = Object.keys(this.commands).filter((it) => it.match(pattern));
26
- if (matchedCommands.length === 1) {
27
- return matchedCommands[0];
111
+ return [resolution.entry.name, resolution.entry.create()];
112
+ }
113
+ /** Prefix matching of the abbreviated form: `m:d` => `migrations diff`, `w:u` => `workspace update api`. */
114
+ matchShorthand(tokens) {
115
+ if (tokens.length === 0) {
116
+ return [];
28
117
  }
29
- if (matchedCommands.length === 0) {
30
- throw `Command ${name} not found.`;
118
+ return this.entries.filter((entry) => entry.tokens.length >= tokens.length && tokens.every((token, index) => entry.tokens[index].startsWith(token)));
119
+ }
120
+ validateNoPrefixCollision() {
121
+ const names = [...this.lookup.keys()];
122
+ for (const name of names) {
123
+ for (const other of names) {
124
+ if (other !== name && other.startsWith(name + " ")) {
125
+ throw new CommandConfiguration.InvalidConfigurationError(
126
+ `Command name "${name}" is a prefix of "${other}". A command name must not be a prefix of another command name, otherwise it could never be resolved.`
127
+ );
128
+ }
129
+ }
31
130
  }
32
- throw `Command ${name} is ambiguous. Did you mean one of these?
33
- - ` + matchedCommands.join("\n - ") + "\n";
34
131
  }
35
132
  }
133
+ const createCatalog = (commands) => {
134
+ const byFactory = /* @__PURE__ */ new Map();
135
+ for (const [name, factory] of Object.entries(commands)) {
136
+ const registered = byFactory.get(factory);
137
+ if (registered) {
138
+ registered.aliases.push(name);
139
+ } else {
140
+ byFactory.set(factory, { name, aliases: [] });
141
+ }
142
+ }
143
+ return [...byFactory].map(([factory, { name, aliases }]) => {
144
+ const tokens = splitTokens(name);
145
+ return {
146
+ name: tokens.join(" "),
147
+ tokens,
148
+ group: tokens.length > 1 ? tokens[0] : void 0,
149
+ aliases,
150
+ create: factory
151
+ };
152
+ });
153
+ };
36
154
  exports.CommandManager = CommandManager;
37
155
  //# sourceMappingURL=CommandManager.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"CommandManager.cjs","sources":["../../../../../packages/cli-common/src/application/CommandManager.ts"],"sourcesContent":["import { Command } from './Command.js'\nimport chalk from 'chalk'\n\nexport type CommandFactoryList = { [command: string]: () => Command<any, any> }\n\nexport class CommandManager {\n\tconstructor(public readonly commands: CommandFactoryList) {}\n\n\tpublic createCommand(name: string): [string, Command<any, any>] {\n\t\tconst fullName = this.findCommand(name)\n\t\tconst factory = this.commands[fullName]\n\t\tconst originalCommand = Object.entries(this.commands).find(([, it]) => it === factory)\n\t\tif (!originalCommand) {\n\t\t\tthrow new Error('impl error')\n\t\t}\n\t\tif (originalCommand[0] !== fullName) {\n\t\t\tconsole.warn(chalk.yellow(`Command name \"${fullName}\" is deprecated, use \"${originalCommand[0]}\" instead.`))\n\t\t}\n\t\treturn [fullName, factory()]\n\t}\n\n\tprivate findCommand(name: string): string {\n\t\tif (this.commands[name]) {\n\t\t\treturn name\n\t\t}\n\t\tconst pattern = new RegExp('^' + name.replace(/:/g, '\\\\w*\\\\:'))\n\t\tconst matchedCommands = Object.keys(this.commands).filter(it => it.match(pattern))\n\t\tif (matchedCommands.length === 1) {\n\t\t\treturn matchedCommands[0]\n\t\t}\n\t\tif (matchedCommands.length === 0) {\n\t\t\tthrow `Command ${name} not found.`\n\t\t}\n\t\tthrow `Command ${name} is ambiguous. Did you mean one of these?\\n - ` + matchedCommands.join('\\n - ') + '\\n'\n\t}\n}\n"],"names":[],"mappings":";;;AAKO,MAAM,eAAe;AAAA,EAC3B,YAA4B,UAA8B;AAA9B,SAAA,WAAA;AAAA,EAA+B;AAAA,EAEpD,cAAc,MAA2C;AAC/D,UAAM,WAAW,KAAK,YAAY,IAAI;AACtC,UAAM,UAAU,KAAK,SAAS,QAAQ;AACtC,UAAM,kBAAkB,OAAO,QAAQ,KAAK,QAAQ,EAAE,KAAK,CAAC,CAAA,EAAG,EAAE,MAAM,OAAO,OAAO;AACrF,QAAI,CAAC,iBAAiB;AACrB,YAAM,IAAI,MAAM,YAAY;AAAA,IAC7B;AACA,QAAI,gBAAgB,CAAC,MAAM,UAAU;AACpC,cAAQ,KAAK,MAAM,OAAO,iBAAiB,QAAQ,yBAAyB,gBAAgB,CAAC,CAAC,YAAY,CAAC;AAAA,IAC5G;AACA,WAAO,CAAC,UAAU,SAAS;AAAA,EAC5B;AAAA,EAEQ,YAAY,MAAsB;AACzC,QAAI,KAAK,SAAS,IAAI,GAAG;AACxB,aAAO;AAAA,IACR;AACA,UAAM,UAAU,IAAI,OAAO,MAAM,KAAK,QAAQ,MAAM,SAAS,CAAC;AAC9D,UAAM,kBAAkB,OAAO,KAAK,KAAK,QAAQ,EAAE,OAAO,CAAA,OAAM,GAAG,MAAM,OAAO,CAAC;AACjF,QAAI,gBAAgB,WAAW,GAAG;AACjC,aAAO,gBAAgB,CAAC;AAAA,IACzB;AACA,QAAI,gBAAgB,WAAW,GAAG;AACjC,YAAM,WAAW,IAAI;AAAA,IACtB;AACA,UAAM,WAAW,IAAI;AAAA,OAAmD,gBAAgB,KAAK,OAAO,IAAI;AAAA,EACzG;AACD;;"}
1
+ {"version":3,"file":"CommandManager.cjs","sources":["../../../../../packages/cli-common/src/application/CommandManager.ts"],"sourcesContent":["import { Command } from './Command.js'\nimport { CliError, ExitCode } from './CliError.js'\nimport { InvalidConfigurationError } from './CommandConfiguration.js'\n\nexport type CommandFactory = () => Command<any, any>\n\n/**\n * Keys sharing the very same factory are aliases of one command — the first one registered is the\n * canonical name. Both `migrations diff` and `migrations:diff` resolve to the same command.\n */\nexport type CommandFactoryList = { [command: string]: CommandFactory }\n\nexport interface CommandCatalogEntry {\n\t/** canonical name, space separated */\n\treadonly name: string\n\treadonly tokens: readonly string[]\n\treadonly group: string | undefined\n\treadonly aliases: readonly string[]\n\treadonly create: CommandFactory\n}\n\nexport type CommandResolution =\n\t| { readonly type: 'command'; readonly entry: CommandCatalogEntry; readonly args: string[] }\n\t| { readonly type: 'group'; readonly name: string; readonly entries: readonly CommandCatalogEntry[]; readonly args: string[] }\n\nconst splitTokens = (name: string): string[] => name.split(/[\\s:]+/).filter(it => it !== '')\n\nexport class CommandManager {\n\tprivate readonly entries: CommandCatalogEntry[]\n\t/** normalized name (canonical or alias) => entry */\n\tprivate readonly lookup = new Map<string, CommandCatalogEntry>()\n\t/** every strict token prefix of a command name => commands in that group */\n\tprivate readonly groups = new Map<string, CommandCatalogEntry[]>()\n\tprivate readonly maxTokens: number\n\n\tconstructor(public readonly commands: CommandFactoryList) {\n\t\tthis.entries = createCatalog(commands)\n\t\tfor (const entry of this.entries) {\n\t\t\tfor (const name of [entry.name, ...entry.aliases]) {\n\t\t\t\tconst tokens = splitTokens(name)\n\t\t\t\tconst normalizedName = tokens.join(' ')\n\t\t\t\tconst existing = this.lookup.get(normalizedName)\n\t\t\t\tif (existing !== undefined && existing.create !== entry.create) {\n\t\t\t\t\tthrow new InvalidConfigurationError(\n\t\t\t\t\t\t`Command names \"${existing.name}\" and \"${name}\" both normalize to \"${normalizedName}\" but use different factories.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tthis.lookup.set(normalizedName, entry)\n\t\t\t\tfor (let i = 1; i < tokens.length; i++) {\n\t\t\t\t\tconst prefix = tokens.slice(0, i).join(' ')\n\t\t\t\t\tconst group = this.groups.get(prefix) ?? []\n\t\t\t\t\tif (!group.includes(entry)) {\n\t\t\t\t\t\tgroup.push(entry)\n\t\t\t\t\t}\n\t\t\t\t\tthis.groups.set(prefix, group)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tthis.maxTokens = Math.max(1, ...[...this.lookup.keys()].map(it => splitTokens(it).length))\n\t\tthis.validateNoPrefixCollision()\n\t}\n\n\tpublic getCommands(): readonly CommandCatalogEntry[] {\n\t\treturn [...this.entries].sort((a, b) => a.name.localeCompare(b.name))\n\t}\n\n\t/**\n\t * Resolves the longest command name matching the leading argv tokens. `migrations diff`,\n\t * `migrations:diff` and the `m:d` shorthand all resolve to the same command; the tokens that were\n\t * not consumed are returned untouched as the command arguments.\n\t */\n\tpublic resolve(args: string[]): CommandResolution {\n\t\tconst tokens: string[] = []\n\t\tconst checkpoints: { tokens: string[]; argvIndex: number }[] = []\n\t\tfor (let i = 0; i < args.length; i++) {\n\t\t\tif (args[i].startsWith('-')) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tconst parts = splitTokens(args[i])\n\t\t\tif (parts.length === 0 || tokens.length + parts.length > this.maxTokens) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\ttokens.push(...parts)\n\t\t\tcheckpoints.push({ tokens: [...tokens], argvIndex: i + 1 })\n\t\t}\n\n\t\tfor (let i = checkpoints.length - 1; i >= 0; i--) {\n\t\t\tconst entry = this.lookup.get(checkpoints[i].tokens.join(' '))\n\t\t\tif (entry) {\n\t\t\t\treturn { type: 'command', entry, args: args.slice(checkpoints[i].argvIndex) }\n\t\t\t}\n\t\t}\n\n\t\tconst group = this.groups.get(tokens.join(' '))\n\t\tif (group !== undefined && group.length > 0) {\n\t\t\tconst argvIndex = checkpoints[checkpoints.length - 1]?.argvIndex ?? 0\n\t\t\treturn { type: 'group', name: tokens.join(' '), entries: group, args: args.slice(argvIndex) }\n\t\t}\n\n\t\tfor (let i = checkpoints.length - 1; i >= 0; i--) {\n\t\t\t// a group named in full must be followed by a verb — never let it swallow the remaining tokens as arguments\n\t\t\tif (i < checkpoints.length - 1 && this.groups.has(checkpoints[i].tokens.join(' '))) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst matched = this.matchShorthand(checkpoints[i].tokens)\n\t\t\tif (matched.length === 1) {\n\t\t\t\treturn { type: 'command', entry: matched[0], args: args.slice(checkpoints[i].argvIndex) }\n\t\t\t}\n\t\t\tif (matched.length > 1) {\n\t\t\t\tthrow new CliError(\n\t\t\t\t\t`Command \"${checkpoints[i].tokens.join(' ')}\" is ambiguous. Did you mean one of these?\\n - ${matched.map(it => it.name).join('\\n - ')}`,\n\t\t\t\t\t{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\t// the leading tokens name a known group, but the verb does not exist in it\n\t\tfor (let i = tokens.length - 1; i >= 1; i--) {\n\t\t\tconst known = this.groups.get(tokens.slice(0, i).join(' '))\n\t\t\tif (known !== undefined && known.length > 0) {\n\t\t\t\tthrow new CliError(\n\t\t\t\t\t`Command \"${tokens.join(' ')}\" not found. Available commands in \"${tokens.slice(0, i).join(' ')}\":\\n - ${\n\t\t\t\t\t\tknown.map(it => it.name).sort((a, b) => a.localeCompare(b)).join('\\n - ')\n\t\t\t\t\t}`,\n\t\t\t\t\t{ code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\tthrow new CliError(`Command \"${args[0] ?? ''}\" not found.`, { code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError })\n\t}\n\n\tpublic createCommand(name: string): [string, Command<any, any>] {\n\t\tconst resolution = this.resolve([name])\n\t\tif (resolution.type === 'group') {\n\t\t\tthrow new CliError(\n\t\t\t\t`Command \"${resolution.name}\" is a command group. Available commands:\\n - ${resolution.entries.map(it => it.name).join('\\n - ')}`,\n\t\t\t\t{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },\n\t\t\t)\n\t\t}\n\t\treturn [resolution.entry.name, resolution.entry.create()]\n\t}\n\n\t/** Prefix matching of the abbreviated form: `m:d` => `migrations diff`, `w:u` => `workspace update api`. */\n\tprivate matchShorthand(tokens: string[]): CommandCatalogEntry[] {\n\t\tif (tokens.length === 0) {\n\t\t\treturn []\n\t\t}\n\t\treturn this.entries.filter(entry => entry.tokens.length >= tokens.length && tokens.every((token, index) => entry.tokens[index].startsWith(token)))\n\t}\n\n\tprivate validateNoPrefixCollision(): void {\n\t\tconst names = [...this.lookup.keys()]\n\t\tfor (const name of names) {\n\t\t\tfor (const other of names) {\n\t\t\t\tif (other !== name && other.startsWith(name + ' ')) {\n\t\t\t\t\tthrow new InvalidConfigurationError(\n\t\t\t\t\t\t`Command name \"${name}\" is a prefix of \"${other}\". A command name must not be a prefix of another command name, otherwise it could never be resolved.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nconst createCatalog = (commands: CommandFactoryList): CommandCatalogEntry[] => {\n\tconst byFactory = new Map<CommandFactory, { name: string; aliases: string[] }>()\n\tfor (const [name, factory] of Object.entries(commands)) {\n\t\tconst registered = byFactory.get(factory)\n\t\tif (registered) {\n\t\t\tregistered.aliases.push(name)\n\t\t} else {\n\t\t\tbyFactory.set(factory, { name, aliases: [] })\n\t\t}\n\t}\n\treturn [...byFactory].map(([factory, { name, aliases }]) => {\n\t\tconst tokens = splitTokens(name)\n\t\treturn {\n\t\t\tname: tokens.join(' '),\n\t\t\ttokens,\n\t\t\tgroup: tokens.length > 1 ? tokens[0] : undefined,\n\t\t\taliases,\n\t\t\tcreate: factory,\n\t\t}\n\t})\n}\n"],"names":["InvalidConfigurationError","CliError","ExitCode"],"mappings":";;;;;;;AAyBA,MAAM,cAAc,CAAC,SAA2B,KAAK,MAAM,QAAQ,EAAE,OAAO,CAAA,OAAM,OAAO,EAAE;AAEpF,MAAM,eAAe;AAAA,EAQ3B,YAA4B,UAA8B;AAA9B,SAAA,WAAA;AAP5B,kBAAA,MAAiB,SAAA;AAEjB,kBAAA,MAAiB,8BAAa,IAAA,CAAiC;AAE/D,kBAAA,MAAiB,8BAAa,IAAA,CAAmC;AACjE,kBAAA,MAAiB,WAAA;AAGhB,SAAK,UAAU,cAAc,QAAQ;AACrC,eAAW,SAAS,KAAK,SAAS;AACjC,iBAAW,QAAQ,CAAC,MAAM,MAAM,GAAG,MAAM,OAAO,GAAG;AAClD,cAAM,SAAS,YAAY,IAAI;AAC/B,cAAM,iBAAiB,OAAO,KAAK,GAAG;AACtC,cAAM,WAAW,KAAK,OAAO,IAAI,cAAc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,OAAO,MAAM,CAAC,OAAO,UAAU,MAAM,OAAO,KAAK,EAAE,WAAW,KAAK,CAAC,CAAC;AAAA,EAClJ;AAAA,EAEQ,4BAAkC;AACzC,UAAM,QAAQ,CAAC,GAAG,KAAK,OAAO,MAAM;AACpC,eAAW,QAAQ,OAAO;AACzB,iBAAW,SAAS,OAAO;AAC1B,YAAI,UAAU,QAAQ,MAAM,WAAW,OAAO,GAAG,GAAG;AACnD,gBAAM,IAAIF,qBAAAA;AAAAA,YACT,iBAAiB,IAAI,qBAAqB,KAAK;AAAA,UAAA;AAAA,QAEjD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;AAEA,MAAM,gBAAgB,CAAC,aAAwD;AAC9E,QAAM,gCAAgB,IAAA;AACtB,aAAW,CAAC,MAAM,OAAO,KAAK,OAAO,QAAQ,QAAQ,GAAG;AACvD,UAAM,aAAa,UAAU,IAAI,OAAO;AACxC,QAAI,YAAY;AACf,iBAAW,QAAQ,KAAK,IAAI;AAAA,IAC7B,OAAO;AACN,gBAAU,IAAI,SAAS,EAAE,MAAM,SAAS,CAAA,GAAI;AAAA,IAC7C;AAAA,EACD;AACA,SAAO,CAAC,GAAG,SAAS,EAAE,IAAI,CAAC,CAAC,SAAS,EAAE,MAAM,QAAA,CAAS,MAAM;AAC3D,UAAM,SAAS,YAAY,IAAI;AAC/B,WAAO;AAAA,MACN,MAAM,OAAO,KAAK,GAAG;AAAA,MACrB;AAAA,MACA,OAAO,OAAO,SAAS,IAAI,OAAO,CAAC,IAAI;AAAA,MACvC;AAAA,MACA,QAAQ;AAAA,IAAA;AAAA,EAEV,CAAC;AACF;;"}
@@ -1,36 +1,154 @@
1
- import chalk from "chalk";
1
+ import { CliError, ExitCode } from "./CliError.js";
2
+ import { InvalidConfigurationError } from "./CommandConfiguration.js";
3
+ var __defProp = Object.defineProperty;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
+ const splitTokens = (name) => name.split(/[\s:]+/).filter((it) => it !== "");
2
7
  class CommandManager {
3
8
  constructor(commands) {
4
9
  this.commands = commands;
10
+ __publicField(this, "entries");
11
+ __publicField(this, "lookup", /* @__PURE__ */ new Map());
12
+ __publicField(this, "groups", /* @__PURE__ */ new Map());
13
+ __publicField(this, "maxTokens");
14
+ this.entries = createCatalog(commands);
15
+ for (const entry of this.entries) {
16
+ for (const name of [entry.name, ...entry.aliases]) {
17
+ const tokens = splitTokens(name);
18
+ const normalizedName = tokens.join(" ");
19
+ const existing = this.lookup.get(normalizedName);
20
+ if (existing !== void 0 && existing.create !== entry.create) {
21
+ throw new InvalidConfigurationError(
22
+ `Command names "${existing.name}" and "${name}" both normalize to "${normalizedName}" but use different factories.`
23
+ );
24
+ }
25
+ this.lookup.set(normalizedName, entry);
26
+ for (let i = 1; i < tokens.length; i++) {
27
+ const prefix = tokens.slice(0, i).join(" ");
28
+ const group = this.groups.get(prefix) ?? [];
29
+ if (!group.includes(entry)) {
30
+ group.push(entry);
31
+ }
32
+ this.groups.set(prefix, group);
33
+ }
34
+ }
35
+ }
36
+ this.maxTokens = Math.max(1, ...[...this.lookup.keys()].map((it) => splitTokens(it).length));
37
+ this.validateNoPrefixCollision();
5
38
  }
6
- createCommand(name) {
7
- const fullName = this.findCommand(name);
8
- const factory = this.commands[fullName];
9
- const originalCommand = Object.entries(this.commands).find(([, it]) => it === factory);
10
- if (!originalCommand) {
11
- throw new Error("impl error");
39
+ getCommands() {
40
+ return [...this.entries].sort((a, b) => a.name.localeCompare(b.name));
41
+ }
42
+ /**
43
+ * Resolves the longest command name matching the leading argv tokens. `migrations diff`,
44
+ * `migrations:diff` and the `m:d` shorthand all resolve to the same command; the tokens that were
45
+ * not consumed are returned untouched as the command arguments.
46
+ */
47
+ resolve(args) {
48
+ const tokens = [];
49
+ const checkpoints = [];
50
+ for (let i = 0; i < args.length; i++) {
51
+ if (args[i].startsWith("-")) {
52
+ break;
53
+ }
54
+ const parts = splitTokens(args[i]);
55
+ if (parts.length === 0 || tokens.length + parts.length > this.maxTokens) {
56
+ break;
57
+ }
58
+ tokens.push(...parts);
59
+ checkpoints.push({ tokens: [...tokens], argvIndex: i + 1 });
60
+ }
61
+ for (let i = checkpoints.length - 1; i >= 0; i--) {
62
+ const entry = this.lookup.get(checkpoints[i].tokens.join(" "));
63
+ if (entry) {
64
+ return { type: "command", entry, args: args.slice(checkpoints[i].argvIndex) };
65
+ }
66
+ }
67
+ const group = this.groups.get(tokens.join(" "));
68
+ if (group !== void 0 && group.length > 0) {
69
+ const argvIndex = checkpoints[checkpoints.length - 1]?.argvIndex ?? 0;
70
+ return { type: "group", name: tokens.join(" "), entries: group, args: args.slice(argvIndex) };
71
+ }
72
+ for (let i = checkpoints.length - 1; i >= 0; i--) {
73
+ if (i < checkpoints.length - 1 && this.groups.has(checkpoints[i].tokens.join(" "))) {
74
+ continue;
75
+ }
76
+ const matched = this.matchShorthand(checkpoints[i].tokens);
77
+ if (matched.length === 1) {
78
+ return { type: "command", entry: matched[0], args: args.slice(checkpoints[i].argvIndex) };
79
+ }
80
+ if (matched.length > 1) {
81
+ throw new CliError(
82
+ `Command "${checkpoints[i].tokens.join(" ")}" is ambiguous. Did you mean one of these?
83
+ - ${matched.map((it) => it.name).join("\n - ")}`,
84
+ { code: "COMMAND_AMBIGUOUS", exitCode: ExitCode.InputError }
85
+ );
86
+ }
12
87
  }
13
- if (originalCommand[0] !== fullName) {
14
- console.warn(chalk.yellow(`Command name "${fullName}" is deprecated, use "${originalCommand[0]}" instead.`));
88
+ for (let i = tokens.length - 1; i >= 1; i--) {
89
+ const known = this.groups.get(tokens.slice(0, i).join(" "));
90
+ if (known !== void 0 && known.length > 0) {
91
+ throw new CliError(
92
+ `Command "${tokens.join(" ")}" not found. Available commands in "${tokens.slice(0, i).join(" ")}":
93
+ - ${known.map((it) => it.name).sort((a, b) => a.localeCompare(b)).join("\n - ")}`,
94
+ { code: "COMMAND_NOT_FOUND", exitCode: ExitCode.InputError }
95
+ );
96
+ }
15
97
  }
16
- return [fullName, factory()];
98
+ throw new CliError(`Command "${args[0] ?? ""}" not found.`, { code: "COMMAND_NOT_FOUND", exitCode: ExitCode.InputError });
17
99
  }
18
- findCommand(name) {
19
- if (this.commands[name]) {
20
- return name;
100
+ createCommand(name) {
101
+ const resolution = this.resolve([name]);
102
+ if (resolution.type === "group") {
103
+ throw new CliError(
104
+ `Command "${resolution.name}" is a command group. Available commands:
105
+ - ${resolution.entries.map((it) => it.name).join("\n - ")}`,
106
+ { code: "COMMAND_AMBIGUOUS", exitCode: ExitCode.InputError }
107
+ );
21
108
  }
22
- const pattern = new RegExp("^" + name.replace(/:/g, "\\w*\\:"));
23
- const matchedCommands = Object.keys(this.commands).filter((it) => it.match(pattern));
24
- if (matchedCommands.length === 1) {
25
- return matchedCommands[0];
109
+ return [resolution.entry.name, resolution.entry.create()];
110
+ }
111
+ /** Prefix matching of the abbreviated form: `m:d` => `migrations diff`, `w:u` => `workspace update api`. */
112
+ matchShorthand(tokens) {
113
+ if (tokens.length === 0) {
114
+ return [];
26
115
  }
27
- if (matchedCommands.length === 0) {
28
- throw `Command ${name} not found.`;
116
+ return this.entries.filter((entry) => entry.tokens.length >= tokens.length && tokens.every((token, index) => entry.tokens[index].startsWith(token)));
117
+ }
118
+ validateNoPrefixCollision() {
119
+ const names = [...this.lookup.keys()];
120
+ for (const name of names) {
121
+ for (const other of names) {
122
+ if (other !== name && other.startsWith(name + " ")) {
123
+ throw new InvalidConfigurationError(
124
+ `Command name "${name}" is a prefix of "${other}". A command name must not be a prefix of another command name, otherwise it could never be resolved.`
125
+ );
126
+ }
127
+ }
29
128
  }
30
- throw `Command ${name} is ambiguous. Did you mean one of these?
31
- - ` + matchedCommands.join("\n - ") + "\n";
32
129
  }
33
130
  }
131
+ const createCatalog = (commands) => {
132
+ const byFactory = /* @__PURE__ */ new Map();
133
+ for (const [name, factory] of Object.entries(commands)) {
134
+ const registered = byFactory.get(factory);
135
+ if (registered) {
136
+ registered.aliases.push(name);
137
+ } else {
138
+ byFactory.set(factory, { name, aliases: [] });
139
+ }
140
+ }
141
+ return [...byFactory].map(([factory, { name, aliases }]) => {
142
+ const tokens = splitTokens(name);
143
+ return {
144
+ name: tokens.join(" "),
145
+ tokens,
146
+ group: tokens.length > 1 ? tokens[0] : void 0,
147
+ aliases,
148
+ create: factory
149
+ };
150
+ });
151
+ };
34
152
  export {
35
153
  CommandManager
36
154
  };
@@ -1 +1 @@
1
- {"version":3,"file":"CommandManager.js","sources":["../../../../../packages/cli-common/src/application/CommandManager.ts"],"sourcesContent":["import { Command } from './Command.js'\nimport chalk from 'chalk'\n\nexport type CommandFactoryList = { [command: string]: () => Command<any, any> }\n\nexport class CommandManager {\n\tconstructor(public readonly commands: CommandFactoryList) {}\n\n\tpublic createCommand(name: string): [string, Command<any, any>] {\n\t\tconst fullName = this.findCommand(name)\n\t\tconst factory = this.commands[fullName]\n\t\tconst originalCommand = Object.entries(this.commands).find(([, it]) => it === factory)\n\t\tif (!originalCommand) {\n\t\t\tthrow new Error('impl error')\n\t\t}\n\t\tif (originalCommand[0] !== fullName) {\n\t\t\tconsole.warn(chalk.yellow(`Command name \"${fullName}\" is deprecated, use \"${originalCommand[0]}\" instead.`))\n\t\t}\n\t\treturn [fullName, factory()]\n\t}\n\n\tprivate findCommand(name: string): string {\n\t\tif (this.commands[name]) {\n\t\t\treturn name\n\t\t}\n\t\tconst pattern = new RegExp('^' + name.replace(/:/g, '\\\\w*\\\\:'))\n\t\tconst matchedCommands = Object.keys(this.commands).filter(it => it.match(pattern))\n\t\tif (matchedCommands.length === 1) {\n\t\t\treturn matchedCommands[0]\n\t\t}\n\t\tif (matchedCommands.length === 0) {\n\t\t\tthrow `Command ${name} not found.`\n\t\t}\n\t\tthrow `Command ${name} is ambiguous. Did you mean one of these?\\n - ` + matchedCommands.join('\\n - ') + '\\n'\n\t}\n}\n"],"names":[],"mappings":";AAKO,MAAM,eAAe;AAAA,EAC3B,YAA4B,UAA8B;AAA9B,SAAA,WAAA;AAAA,EAA+B;AAAA,EAEpD,cAAc,MAA2C;AAC/D,UAAM,WAAW,KAAK,YAAY,IAAI;AACtC,UAAM,UAAU,KAAK,SAAS,QAAQ;AACtC,UAAM,kBAAkB,OAAO,QAAQ,KAAK,QAAQ,EAAE,KAAK,CAAC,CAAA,EAAG,EAAE,MAAM,OAAO,OAAO;AACrF,QAAI,CAAC,iBAAiB;AACrB,YAAM,IAAI,MAAM,YAAY;AAAA,IAC7B;AACA,QAAI,gBAAgB,CAAC,MAAM,UAAU;AACpC,cAAQ,KAAK,MAAM,OAAO,iBAAiB,QAAQ,yBAAyB,gBAAgB,CAAC,CAAC,YAAY,CAAC;AAAA,IAC5G;AACA,WAAO,CAAC,UAAU,SAAS;AAAA,EAC5B;AAAA,EAEQ,YAAY,MAAsB;AACzC,QAAI,KAAK,SAAS,IAAI,GAAG;AACxB,aAAO;AAAA,IACR;AACA,UAAM,UAAU,IAAI,OAAO,MAAM,KAAK,QAAQ,MAAM,SAAS,CAAC;AAC9D,UAAM,kBAAkB,OAAO,KAAK,KAAK,QAAQ,EAAE,OAAO,CAAA,OAAM,GAAG,MAAM,OAAO,CAAC;AACjF,QAAI,gBAAgB,WAAW,GAAG;AACjC,aAAO,gBAAgB,CAAC;AAAA,IACzB;AACA,QAAI,gBAAgB,WAAW,GAAG;AACjC,YAAM,WAAW,IAAI;AAAA,IACtB;AACA,UAAM,WAAW,IAAI;AAAA,OAAmD,gBAAgB,KAAK,OAAO,IAAI;AAAA,EACzG;AACD;"}
1
+ {"version":3,"file":"CommandManager.js","sources":["../../../../../packages/cli-common/src/application/CommandManager.ts"],"sourcesContent":["import { Command } from './Command.js'\nimport { CliError, ExitCode } from './CliError.js'\nimport { InvalidConfigurationError } from './CommandConfiguration.js'\n\nexport type CommandFactory = () => Command<any, any>\n\n/**\n * Keys sharing the very same factory are aliases of one command — the first one registered is the\n * canonical name. Both `migrations diff` and `migrations:diff` resolve to the same command.\n */\nexport type CommandFactoryList = { [command: string]: CommandFactory }\n\nexport interface CommandCatalogEntry {\n\t/** canonical name, space separated */\n\treadonly name: string\n\treadonly tokens: readonly string[]\n\treadonly group: string | undefined\n\treadonly aliases: readonly string[]\n\treadonly create: CommandFactory\n}\n\nexport type CommandResolution =\n\t| { readonly type: 'command'; readonly entry: CommandCatalogEntry; readonly args: string[] }\n\t| { readonly type: 'group'; readonly name: string; readonly entries: readonly CommandCatalogEntry[]; readonly args: string[] }\n\nconst splitTokens = (name: string): string[] => name.split(/[\\s:]+/).filter(it => it !== '')\n\nexport class CommandManager {\n\tprivate readonly entries: CommandCatalogEntry[]\n\t/** normalized name (canonical or alias) => entry */\n\tprivate readonly lookup = new Map<string, CommandCatalogEntry>()\n\t/** every strict token prefix of a command name => commands in that group */\n\tprivate readonly groups = new Map<string, CommandCatalogEntry[]>()\n\tprivate readonly maxTokens: number\n\n\tconstructor(public readonly commands: CommandFactoryList) {\n\t\tthis.entries = createCatalog(commands)\n\t\tfor (const entry of this.entries) {\n\t\t\tfor (const name of [entry.name, ...entry.aliases]) {\n\t\t\t\tconst tokens = splitTokens(name)\n\t\t\t\tconst normalizedName = tokens.join(' ')\n\t\t\t\tconst existing = this.lookup.get(normalizedName)\n\t\t\t\tif (existing !== undefined && existing.create !== entry.create) {\n\t\t\t\t\tthrow new InvalidConfigurationError(\n\t\t\t\t\t\t`Command names \"${existing.name}\" and \"${name}\" both normalize to \"${normalizedName}\" but use different factories.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tthis.lookup.set(normalizedName, entry)\n\t\t\t\tfor (let i = 1; i < tokens.length; i++) {\n\t\t\t\t\tconst prefix = tokens.slice(0, i).join(' ')\n\t\t\t\t\tconst group = this.groups.get(prefix) ?? []\n\t\t\t\t\tif (!group.includes(entry)) {\n\t\t\t\t\t\tgroup.push(entry)\n\t\t\t\t\t}\n\t\t\t\t\tthis.groups.set(prefix, group)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tthis.maxTokens = Math.max(1, ...[...this.lookup.keys()].map(it => splitTokens(it).length))\n\t\tthis.validateNoPrefixCollision()\n\t}\n\n\tpublic getCommands(): readonly CommandCatalogEntry[] {\n\t\treturn [...this.entries].sort((a, b) => a.name.localeCompare(b.name))\n\t}\n\n\t/**\n\t * Resolves the longest command name matching the leading argv tokens. `migrations diff`,\n\t * `migrations:diff` and the `m:d` shorthand all resolve to the same command; the tokens that were\n\t * not consumed are returned untouched as the command arguments.\n\t */\n\tpublic resolve(args: string[]): CommandResolution {\n\t\tconst tokens: string[] = []\n\t\tconst checkpoints: { tokens: string[]; argvIndex: number }[] = []\n\t\tfor (let i = 0; i < args.length; i++) {\n\t\t\tif (args[i].startsWith('-')) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tconst parts = splitTokens(args[i])\n\t\t\tif (parts.length === 0 || tokens.length + parts.length > this.maxTokens) {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\ttokens.push(...parts)\n\t\t\tcheckpoints.push({ tokens: [...tokens], argvIndex: i + 1 })\n\t\t}\n\n\t\tfor (let i = checkpoints.length - 1; i >= 0; i--) {\n\t\t\tconst entry = this.lookup.get(checkpoints[i].tokens.join(' '))\n\t\t\tif (entry) {\n\t\t\t\treturn { type: 'command', entry, args: args.slice(checkpoints[i].argvIndex) }\n\t\t\t}\n\t\t}\n\n\t\tconst group = this.groups.get(tokens.join(' '))\n\t\tif (group !== undefined && group.length > 0) {\n\t\t\tconst argvIndex = checkpoints[checkpoints.length - 1]?.argvIndex ?? 0\n\t\t\treturn { type: 'group', name: tokens.join(' '), entries: group, args: args.slice(argvIndex) }\n\t\t}\n\n\t\tfor (let i = checkpoints.length - 1; i >= 0; i--) {\n\t\t\t// a group named in full must be followed by a verb — never let it swallow the remaining tokens as arguments\n\t\t\tif (i < checkpoints.length - 1 && this.groups.has(checkpoints[i].tokens.join(' '))) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst matched = this.matchShorthand(checkpoints[i].tokens)\n\t\t\tif (matched.length === 1) {\n\t\t\t\treturn { type: 'command', entry: matched[0], args: args.slice(checkpoints[i].argvIndex) }\n\t\t\t}\n\t\t\tif (matched.length > 1) {\n\t\t\t\tthrow new CliError(\n\t\t\t\t\t`Command \"${checkpoints[i].tokens.join(' ')}\" is ambiguous. Did you mean one of these?\\n - ${matched.map(it => it.name).join('\\n - ')}`,\n\t\t\t\t\t{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\t// the leading tokens name a known group, but the verb does not exist in it\n\t\tfor (let i = tokens.length - 1; i >= 1; i--) {\n\t\t\tconst known = this.groups.get(tokens.slice(0, i).join(' '))\n\t\t\tif (known !== undefined && known.length > 0) {\n\t\t\t\tthrow new CliError(\n\t\t\t\t\t`Command \"${tokens.join(' ')}\" not found. Available commands in \"${tokens.slice(0, i).join(' ')}\":\\n - ${\n\t\t\t\t\t\tknown.map(it => it.name).sort((a, b) => a.localeCompare(b)).join('\\n - ')\n\t\t\t\t\t}`,\n\t\t\t\t\t{ code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\tthrow new CliError(`Command \"${args[0] ?? ''}\" not found.`, { code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError })\n\t}\n\n\tpublic createCommand(name: string): [string, Command<any, any>] {\n\t\tconst resolution = this.resolve([name])\n\t\tif (resolution.type === 'group') {\n\t\t\tthrow new CliError(\n\t\t\t\t`Command \"${resolution.name}\" is a command group. Available commands:\\n - ${resolution.entries.map(it => it.name).join('\\n - ')}`,\n\t\t\t\t{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },\n\t\t\t)\n\t\t}\n\t\treturn [resolution.entry.name, resolution.entry.create()]\n\t}\n\n\t/** Prefix matching of the abbreviated form: `m:d` => `migrations diff`, `w:u` => `workspace update api`. */\n\tprivate matchShorthand(tokens: string[]): CommandCatalogEntry[] {\n\t\tif (tokens.length === 0) {\n\t\t\treturn []\n\t\t}\n\t\treturn this.entries.filter(entry => entry.tokens.length >= tokens.length && tokens.every((token, index) => entry.tokens[index].startsWith(token)))\n\t}\n\n\tprivate validateNoPrefixCollision(): void {\n\t\tconst names = [...this.lookup.keys()]\n\t\tfor (const name of names) {\n\t\t\tfor (const other of names) {\n\t\t\t\tif (other !== name && other.startsWith(name + ' ')) {\n\t\t\t\t\tthrow new InvalidConfigurationError(\n\t\t\t\t\t\t`Command name \"${name}\" is a prefix of \"${other}\". A command name must not be a prefix of another command name, otherwise it could never be resolved.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nconst createCatalog = (commands: CommandFactoryList): CommandCatalogEntry[] => {\n\tconst byFactory = new Map<CommandFactory, { name: string; aliases: string[] }>()\n\tfor (const [name, factory] of Object.entries(commands)) {\n\t\tconst registered = byFactory.get(factory)\n\t\tif (registered) {\n\t\t\tregistered.aliases.push(name)\n\t\t} else {\n\t\t\tbyFactory.set(factory, { name, aliases: [] })\n\t\t}\n\t}\n\treturn [...byFactory].map(([factory, { name, aliases }]) => {\n\t\tconst tokens = splitTokens(name)\n\t\treturn {\n\t\t\tname: tokens.join(' '),\n\t\t\ttokens,\n\t\t\tgroup: tokens.length > 1 ? tokens[0] : undefined,\n\t\t\taliases,\n\t\t\tcreate: factory,\n\t\t}\n\t})\n}\n"],"names":[],"mappings":";;;;;AAyBA,MAAM,cAAc,CAAC,SAA2B,KAAK,MAAM,QAAQ,EAAE,OAAO,CAAA,OAAM,OAAO,EAAE;AAEpF,MAAM,eAAe;AAAA,EAQ3B,YAA4B,UAA8B;AAA9B,SAAA,WAAA;AAP5B,kBAAA,MAAiB,SAAA;AAEjB,kBAAA,MAAiB,8BAAa,IAAA,CAAiC;AAE/D,kBAAA,MAAiB,8BAAa,IAAA,CAAmC;AACjE,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bol.toStringTag, { value: "Module" });
3
+ const chalk = require("chalk");
4
+ const CliError = require("./CliError.cjs");
5
+ const Output = require("./Output.cjs");
6
+ const renderCliError = (error, output) => {
7
+ const cliError = CliError.toCliError(error);
8
+ if (output.isJson) {
9
+ output.writeStderr(serializeError(cliError, CliError.isUnexpectedCliError(cliError)));
10
+ return cliError.exitCode;
11
+ }
12
+ output.writeStderr(chalk.red(`Error [${Output.escapeTerminalText(cliError.code)}]: ${Output.escapeTerminalText(cliError.message)}`));
13
+ if (cliError.exitCode === CliError.ExitCode.InternalError && typeof cliError.details === "string") {
14
+ output.writeStderr(chalk.gray(Output.escapeTerminalText(cliError.details)));
15
+ }
16
+ return cliError.exitCode;
17
+ };
18
+ const serializeError = (error, unexpected) => {
19
+ try {
20
+ return JSON.stringify(
21
+ {
22
+ ok: false,
23
+ error: {
24
+ code: error.code,
25
+ message: unexpected ? "An unexpected error occurred." : error.message,
26
+ retryable: error.retryable,
27
+ details: unexpected ? null : error.details ?? null
28
+ }
29
+ },
30
+ null,
31
+ 2
32
+ );
33
+ } catch {
34
+ return '{"ok":false,"error":{"code":"UNKNOWN","message":"Unable to serialize error","retryable":false,"details":null}}';
35
+ }
36
+ };
37
+ exports.renderCliError = renderCliError;
38
+ //# sourceMappingURL=ErrorRenderer.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorRenderer.cjs","sources":["../../../../../packages/cli-common/src/application/ErrorRenderer.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { ExitCode, isUnexpectedCliError, toCliError } from './CliError.js'\nimport { escapeTerminalText, Output } from './Output.js'\n\n/**\n * The single place where an error becomes user visible. Always writes to stderr — in json mode as the\n * `{ ok: false, error }` envelope, otherwise as a colored one-liner. Returns the process exit code.\n */\nexport const renderCliError = (error: unknown, output: Output): ExitCode => {\n\tconst cliError = toCliError(error)\n\tif (output.isJson) {\n\t\toutput.writeStderr(serializeError(cliError, isUnexpectedCliError(cliError)))\n\t\treturn cliError.exitCode\n\t}\n\toutput.writeStderr(chalk.red(`Error [${escapeTerminalText(cliError.code)}]: ${escapeTerminalText(cliError.message)}`))\n\tif (cliError.exitCode === ExitCode.InternalError && typeof cliError.details === 'string') {\n\t\toutput.writeStderr(chalk.gray(escapeTerminalText(cliError.details)))\n\t}\n\treturn cliError.exitCode\n}\n\nconst serializeError = (error: ReturnType<typeof toCliError>, unexpected: boolean): string => {\n\ttry {\n\t\treturn JSON.stringify(\n\t\t\t{\n\t\t\t\tok: false,\n\t\t\t\terror: {\n\t\t\t\t\tcode: error.code,\n\t\t\t\t\tmessage: unexpected ? 'An unexpected error occurred.' : error.message,\n\t\t\t\t\tretryable: error.retryable,\n\t\t\t\t\tdetails: unexpected ? null : error.details ?? null,\n\t\t\t\t},\n\t\t\t},\n\t\t\tnull,\n\t\t\t2,\n\t\t)\n\t} catch {\n\t\treturn '{\"ok\":false,\"error\":{\"code\":\"UNKNOWN\",\"message\":\"Unable to serialize error\",\"retryable\":false,\"details\":null}}'\n\t}\n}\n"],"names":["toCliError","isUnexpectedCliError","escapeTerminalText","ExitCode"],"mappings":";;;;;AAQO,MAAM,iBAAiB,CAAC,OAAgB,WAA6B;AAC3E,QAAM,WAAWA,SAAAA,WAAW,KAAK;AACjC,MAAI,OAAO,QAAQ;AAClB,WAAO,YAAY,eAAe,UAAUC,SAAAA,qBAAqB,QAAQ,CAAC,CAAC;AAC3E,WAAO,SAAS;AAAA,EACjB;AACA,SAAO,YAAY,MAAM,IAAI,UAAUC,OAAAA,mBAAmB,SAAS,IAAI,CAAC,MAAMA,OAAAA,mBAAmB,SAAS,OAAO,CAAC,EAAE,CAAC;AACrH,MAAI,SAAS,aAAaC,SAAAA,SAAS,iBAAiB,OAAO,SAAS,YAAY,UAAU;AACzF,WAAO,YAAY,MAAM,KAAKD,OAAAA,mBAAmB,SAAS,OAAO,CAAC,CAAC;AAAA,EACpE;AACA,SAAO,SAAS;AACjB;AAEA,MAAM,iBAAiB,CAAC,OAAsC,eAAgC;AAC7F,MAAI;AACH,WAAO,KAAK;AAAA,MACX;AAAA,QACC,IAAI;AAAA,QACJ,OAAO;AAAA,UACN,MAAM,MAAM;AAAA,UACZ,SAAS,aAAa,kCAAkC,MAAM;AAAA,UAC9D,WAAW,MAAM;AAAA,UACjB,SAAS,aAAa,OAAO,MAAM,WAAW;AAAA,QAAA;AAAA,MAC/C;AAAA,MAED;AAAA,MACA;AAAA,IAAA;AAAA,EAEF,QAAQ;AACP,WAAO;AAAA,EACR;AACD;;"}
@@ -0,0 +1,38 @@
1
+ import chalk from "chalk";
2
+ import { toCliError, isUnexpectedCliError, ExitCode } from "./CliError.js";
3
+ import { escapeTerminalText } from "./Output.js";
4
+ const renderCliError = (error, output) => {
5
+ const cliError = toCliError(error);
6
+ if (output.isJson) {
7
+ output.writeStderr(serializeError(cliError, isUnexpectedCliError(cliError)));
8
+ return cliError.exitCode;
9
+ }
10
+ output.writeStderr(chalk.red(`Error [${escapeTerminalText(cliError.code)}]: ${escapeTerminalText(cliError.message)}`));
11
+ if (cliError.exitCode === ExitCode.InternalError && typeof cliError.details === "string") {
12
+ output.writeStderr(chalk.gray(escapeTerminalText(cliError.details)));
13
+ }
14
+ return cliError.exitCode;
15
+ };
16
+ const serializeError = (error, unexpected) => {
17
+ try {
18
+ return JSON.stringify(
19
+ {
20
+ ok: false,
21
+ error: {
22
+ code: error.code,
23
+ message: unexpected ? "An unexpected error occurred." : error.message,
24
+ retryable: error.retryable,
25
+ details: unexpected ? null : error.details ?? null
26
+ }
27
+ },
28
+ null,
29
+ 2
30
+ );
31
+ } catch {
32
+ return '{"ok":false,"error":{"code":"UNKNOWN","message":"Unable to serialize error","retryable":false,"details":null}}';
33
+ }
34
+ };
35
+ export {
36
+ renderCliError
37
+ };
38
+ //# sourceMappingURL=ErrorRenderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorRenderer.js","sources":["../../../../../packages/cli-common/src/application/ErrorRenderer.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { ExitCode, isUnexpectedCliError, toCliError } from './CliError.js'\nimport { escapeTerminalText, Output } from './Output.js'\n\n/**\n * The single place where an error becomes user visible. Always writes to stderr — in json mode as the\n * `{ ok: false, error }` envelope, otherwise as a colored one-liner. Returns the process exit code.\n */\nexport const renderCliError = (error: unknown, output: Output): ExitCode => {\n\tconst cliError = toCliError(error)\n\tif (output.isJson) {\n\t\toutput.writeStderr(serializeError(cliError, isUnexpectedCliError(cliError)))\n\t\treturn cliError.exitCode\n\t}\n\toutput.writeStderr(chalk.red(`Error [${escapeTerminalText(cliError.code)}]: ${escapeTerminalText(cliError.message)}`))\n\tif (cliError.exitCode === ExitCode.InternalError && typeof cliError.details === 'string') {\n\t\toutput.writeStderr(chalk.gray(escapeTerminalText(cliError.details)))\n\t}\n\treturn cliError.exitCode\n}\n\nconst serializeError = (error: ReturnType<typeof toCliError>, unexpected: boolean): string => {\n\ttry {\n\t\treturn JSON.stringify(\n\t\t\t{\n\t\t\t\tok: false,\n\t\t\t\terror: {\n\t\t\t\t\tcode: error.code,\n\t\t\t\t\tmessage: unexpected ? 'An unexpected error occurred.' : error.message,\n\t\t\t\t\tretryable: error.retryable,\n\t\t\t\t\tdetails: unexpected ? null : error.details ?? null,\n\t\t\t\t},\n\t\t\t},\n\t\t\tnull,\n\t\t\t2,\n\t\t)\n\t} catch {\n\t\treturn '{\"ok\":false,\"error\":{\"code\":\"UNKNOWN\",\"message\":\"Unable to serialize error\",\"retryable\":false,\"details\":null}}'\n\t}\n}\n"],"names":[],"mappings":";;;AAQO,MAAM,iBAAiB,CAAC,OAAgB,WAA6B;AAC3E,QAAM,WAAW,WAAW,KAAK;AACjC,MAAI,OAAO,QAAQ;AAClB,WAAO,YAAY,eAAe,UAAU,qBAAqB,QAAQ,CAAC,CAAC;AAC3E,WAAO,SAAS;AAAA,EACjB;AACA,SAAO,YAAY,MAAM,IAAI,UAAU,mBAAmB,SAAS,IAAI,CAAC,MAAM,mBAAmB,SAAS,OAAO,CAAC,EAAE,CAAC;AACrH,MAAI,SAAS,aAAa,SAAS,iBAAiB,OAAO,SAAS,YAAY,UAAU;AACzF,WAAO,YAAY,MAAM,KAAK,mBAAmB,SAAS,OAAO,CAAC,CAAC;AAAA,EACpE;AACA,SAAO,SAAS;AACjB;AAEA,MAAM,iBAAiB,CAAC,OAAsC,eAAgC;AAC7F,MAAI;AACH,WAAO,KAAK;AAAA,MACX;AAAA,QACC,IAAI;AAAA,QACJ,OAAO;AAAA,UACN,MAAM,MAAM;AAAA,UACZ,SAAS,aAAa,kCAAkC,MAAM;AAAA,UAC9D,WAAW,MAAM;AAAA,UACjB,SAAS,aAAa,OAAO,MAAM,WAAW;AAAA,QAAA;AAAA,MAC/C;AAAA,MAED;AAAA,MACA;AAAA,IAAA;AAAA,EAEF,QAAQ;AACP,WAAO;AAAA,EACR;AACD;"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const Option = require("./Option.cjs");
4
+ const globalOptions = [
5
+ { name: "json", required: false, deprecated: false, mode: Option.OptionMode.VALUE_NONE, description: "Print machine readable JSON to stdout" },
6
+ {
7
+ name: "quiet",
8
+ shortcut: "q",
9
+ required: false,
10
+ deprecated: false,
11
+ mode: Option.OptionMode.VALUE_NONE,
12
+ description: "Print only the resulting data to stdout"
13
+ },
14
+ { name: "no-color", required: false, deprecated: false, mode: Option.OptionMode.VALUE_NONE, description: "Disable colored output" },
15
+ { name: "help", shortcut: "h", required: false, deprecated: false, mode: Option.OptionMode.VALUE_NONE, description: "Print help of the command" }
16
+ ];
17
+ const readGlobalOptions = (input) => ({
18
+ json: input.getRawOption("json") === true,
19
+ quiet: input.getRawOption("quiet") === true,
20
+ color: input.getRawOption("no-color") !== true,
21
+ help: input.getRawOption("help") === true
22
+ });
23
+ const readGlobalOptionsFromArgs = (args) => {
24
+ let json = false;
25
+ let quiet = false;
26
+ let color = true;
27
+ let help = false;
28
+ for (const arg of args) {
29
+ if (!arg.startsWith("-")) {
30
+ continue;
31
+ }
32
+ const token = arg.split("=", 2)[0];
33
+ if (isGlobalOptionToken(token, "json")) {
34
+ json = true;
35
+ } else if (isGlobalOptionToken(token, "quiet")) {
36
+ quiet = true;
37
+ } else if (isGlobalOptionToken(token, "no-color")) {
38
+ color = false;
39
+ } else if (isGlobalOptionToken(token, "help")) {
40
+ help = true;
41
+ }
42
+ }
43
+ return { json, quiet, color, help };
44
+ };
45
+ const isGlobalOptionToken = (token, name) => {
46
+ const option = globalOptions.find((item) => item.name === name);
47
+ return option !== void 0 && (token === `--${option.name}` || option.shortcut !== void 0 && token === `-${option.shortcut}`);
48
+ };
49
+ const hasHelpFlag = (args) => readGlobalOptionsFromArgs(args).help;
50
+ exports.globalOptions = globalOptions;
51
+ exports.hasHelpFlag = hasHelpFlag;
52
+ exports.readGlobalOptions = readGlobalOptions;
53
+ exports.readGlobalOptionsFromArgs = readGlobalOptionsFromArgs;
54
+ //# sourceMappingURL=GlobalOptions.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GlobalOptions.cjs","sources":["../../../../../packages/cli-common/src/application/GlobalOptions.ts"],"sourcesContent":["import { Option, OptionMode } from './Option.js'\n\nexport interface GlobalOptionValues {\n\treadonly json: boolean\n\treadonly quiet: boolean\n\t/** false when --no-color was passed */\n\treadonly color: boolean\n\treadonly help: boolean\n}\n\n/**\n * Options available on every command. Their names and shortcuts are reserved for the global parser.\n */\nexport const globalOptions: readonly Option[] = [\n\t{ name: 'json', required: false, deprecated: false, mode: OptionMode.VALUE_NONE, description: 'Print machine readable JSON to stdout' },\n\t{\n\t\tname: 'quiet',\n\t\tshortcut: 'q',\n\t\trequired: false,\n\t\tdeprecated: false,\n\t\tmode: OptionMode.VALUE_NONE,\n\t\tdescription: 'Print only the resulting data to stdout',\n\t},\n\t{ name: 'no-color', required: false, deprecated: false, mode: OptionMode.VALUE_NONE, description: 'Disable colored output' },\n\t{ name: 'help', shortcut: 'h', required: false, deprecated: false, mode: OptionMode.VALUE_NONE, description: 'Print help of the command' },\n]\n\nexport interface RawOptionReader {\n\tgetRawOption(name: string): string | boolean | string[] | undefined\n}\n\n/**\n * Reads the global options from a parsed input.\n */\nexport const readGlobalOptions = (input: RawOptionReader): GlobalOptionValues => ({\n\tjson: input.getRawOption('json') === true,\n\tquiet: input.getRawOption('quiet') === true,\n\tcolor: input.getRawOption('no-color') !== true,\n\thelp: input.getRawOption('help') === true,\n})\n\n/**\n * Reads the global options straight from argv, before the command parser runs. Only used to pick the\n * rendering mode early (so that a parse error is reported in the requested format) and to detect\n * --help. Nothing is removed from argv, so parsing semantics stay untouched. A token starting with a\n * dash can never be consumed as a value by {@link InputParser}, so this scan cannot misread a value.\n */\nexport const readGlobalOptionsFromArgs = (args: string[]): GlobalOptionValues => {\n\tlet json = false\n\tlet quiet = false\n\tlet color = true\n\tlet help = false\n\tfor (const arg of args) {\n\t\tif (!arg.startsWith('-')) {\n\t\t\tcontinue\n\t\t}\n\t\tconst token = arg.split('=', 2)[0]\n\t\tif (isGlobalOptionToken(token, 'json')) {\n\t\t\tjson = true\n\t\t} else if (isGlobalOptionToken(token, 'quiet')) {\n\t\t\tquiet = true\n\t\t} else if (isGlobalOptionToken(token, 'no-color')) {\n\t\t\tcolor = false\n\t\t} else if (isGlobalOptionToken(token, 'help')) {\n\t\t\thelp = true\n\t\t}\n\t}\n\treturn { json, quiet, color, help }\n}\n\nconst isGlobalOptionToken = (token: string, name: string): boolean => {\n\tconst option = globalOptions.find(item => item.name === name)\n\treturn option !== undefined && (token === `--${option.name}` || (option.shortcut !== undefined && token === `-${option.shortcut}`))\n}\n\nexport const hasHelpFlag = (args: string[]): boolean => readGlobalOptionsFromArgs(args).help\n"],"names":["OptionMode"],"mappings":";;;AAaO,MAAM,gBAAmC;AAAA,EAC/C,EAAE,MAAM,QAAQ,UAAU,OAAO,YAAY,OAAO,MAAMA,OAAAA,WAAW,YAAY,aAAa,wCAAA;AAAA,EAC9F;AAAA,IACC,MAAM;AAAA,IACN,U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IAAI,OAAO,QAAQ;AAChI;AAEO,MAAM,cAAc,CAAC,SAA4B,0BAA0B,IAAI,EAAE;;;;;"}
@@ -0,0 +1,54 @@
1
+ import { OptionMode } from "./Option.js";
2
+ const globalOptions = [
3
+ { name: "json", required: false, deprecated: false, mode: OptionMode.VALUE_NONE, description: "Print machine readable JSON to stdout" },
4
+ {
5
+ name: "quiet",
6
+ shortcut: "q",
7
+ required: false,
8
+ deprecated: false,
9
+ mode: OptionMode.VALUE_NONE,
10
+ description: "Print only the resulting data to stdout"
11
+ },
12
+ { name: "no-color", required: false, deprecated: false, mode: OptionMode.VALUE_NONE, description: "Disable colored output" },
13
+ { name: "help", shortcut: "h", required: false, deprecated: false, mode: OptionMode.VALUE_NONE, description: "Print help of the command" }
14
+ ];
15
+ const readGlobalOptions = (input) => ({
16
+ json: input.getRawOption("json") === true,
17
+ quiet: input.getRawOption("quiet") === true,
18
+ color: input.getRawOption("no-color") !== true,
19
+ help: input.getRawOption("help") === true
20
+ });
21
+ const readGlobalOptionsFromArgs = (args) => {
22
+ let json = false;
23
+ let quiet = false;
24
+ let color = true;
25
+ let help = false;
26
+ for (const arg of args) {
27
+ if (!arg.startsWith("-")) {
28
+ continue;
29
+ }
30
+ const token = arg.split("=", 2)[0];
31
+ if (isGlobalOptionToken(token, "json")) {
32
+ json = true;
33
+ } else if (isGlobalOptionToken(token, "quiet")) {
34
+ quiet = true;
35
+ } else if (isGlobalOptionToken(token, "no-color")) {
36
+ color = false;
37
+ } else if (isGlobalOptionToken(token, "help")) {
38
+ help = true;
39
+ }
40
+ }
41
+ return { json, quiet, color, help };
42
+ };
43
+ const isGlobalOptionToken = (token, name) => {
44
+ const option = globalOptions.find((item) => item.name === name);
45
+ return option !== void 0 && (token === `--${option.name}` || option.shortcut !== void 0 && token === `-${option.shortcut}`);
46
+ };
47
+ const hasHelpFlag = (args) => readGlobalOptionsFromArgs(args).help;
48
+ export {
49
+ globalOptions,
50
+ hasHelpFlag,
51
+ readGlobalOptions,
52
+ readGlobalOptionsFromArgs
53
+ };
54
+ //# sourceMappingURL=GlobalOptions.js.map