@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.
- package/dist/development/index.cjs +20 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +22 -2
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/application/Application.cjs +116 -59
- package/dist/development/src/application/Application.cjs.map +1 -1
- package/dist/development/src/application/Application.js +116 -59
- package/dist/development/src/application/Application.js.map +1 -1
- package/dist/development/src/application/CliError.cjs +104 -0
- package/dist/development/src/application/CliError.cjs.map +1 -0
- package/dist/development/src/application/CliError.js +104 -0
- package/dist/development/src/application/CliError.js.map +1 -0
- package/dist/development/src/application/Command.cjs +18 -5
- package/dist/development/src/application/Command.cjs.map +1 -1
- package/dist/development/src/application/Command.js +18 -5
- package/dist/development/src/application/Command.js.map +1 -1
- package/dist/development/src/application/CommandConfiguration.cjs +21 -3
- package/dist/development/src/application/CommandConfiguration.cjs.map +1 -1
- package/dist/development/src/application/CommandConfiguration.js +23 -5
- package/dist/development/src/application/CommandConfiguration.js.map +1 -1
- package/dist/development/src/application/CommandManager.cjs +139 -21
- package/dist/development/src/application/CommandManager.cjs.map +1 -1
- package/dist/development/src/application/CommandManager.js +139 -21
- package/dist/development/src/application/CommandManager.js.map +1 -1
- package/dist/development/src/application/ErrorRenderer.cjs +38 -0
- package/dist/development/src/application/ErrorRenderer.cjs.map +1 -0
- package/dist/development/src/application/ErrorRenderer.js +38 -0
- package/dist/development/src/application/ErrorRenderer.js.map +1 -0
- package/dist/development/src/application/GlobalOptions.cjs +54 -0
- package/dist/development/src/application/GlobalOptions.cjs.map +1 -0
- package/dist/development/src/application/GlobalOptions.js +54 -0
- package/dist/development/src/application/GlobalOptions.js.map +1 -0
- package/dist/development/src/application/Input.cjs +5 -0
- package/dist/development/src/application/Input.cjs.map +1 -1
- package/dist/development/src/application/Input.js +5 -0
- package/dist/development/src/application/Input.js.map +1 -1
- package/dist/development/src/application/InputParser.cjs +7 -3
- package/dist/development/src/application/InputParser.cjs.map +1 -1
- package/dist/development/src/application/InputParser.js +7 -3
- package/dist/development/src/application/InputParser.js.map +1 -1
- package/dist/development/src/application/Option.cjs +11 -0
- package/dist/development/src/application/Option.cjs.map +1 -1
- package/dist/development/src/application/Option.js +12 -1
- package/dist/development/src/application/Option.js.map +1 -1
- package/dist/development/src/application/Output.cjs +242 -0
- package/dist/development/src/application/Output.cjs.map +1 -0
- package/dist/development/src/application/Output.js +242 -0
- package/dist/development/src/application/Output.js.map +1 -0
- package/dist/development/src/application/exitProcess.cjs +7 -0
- package/dist/development/src/application/exitProcess.cjs.map +1 -0
- package/dist/development/src/application/exitProcess.js +7 -0
- package/dist/development/src/application/exitProcess.js.map +1 -0
- package/dist/development/src/npm/CommandRunner.cjs +126 -24
- package/dist/development/src/npm/CommandRunner.cjs.map +1 -1
- package/dist/development/src/npm/CommandRunner.js +126 -24
- package/dist/development/src/npm/CommandRunner.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Bun.cjs +1 -2
- package/dist/development/src/npm/packageManagers/Bun.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/Bun.js +1 -2
- package/dist/development/src/npm/packageManagers/Bun.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Npm.cjs +1 -2
- package/dist/development/src/npm/packageManagers/Npm.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/Npm.js +1 -2
- package/dist/development/src/npm/packageManagers/Npm.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Pnpm.cjs +1 -2
- package/dist/development/src/npm/packageManagers/Pnpm.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/Pnpm.js +1 -2
- package/dist/development/src/npm/packageManagers/Pnpm.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Yarn.cjs +1 -2
- package/dist/development/src/npm/packageManagers/Yarn.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/Yarn.js +1 -2
- package/dist/development/src/npm/packageManagers/Yarn.js.map +1 -1
- package/dist/development/src/npm/packageManagers/YarnClassic.cjs +1 -2
- package/dist/development/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/YarnClassic.js +1 -2
- package/dist/development/src/npm/packageManagers/YarnClassic.js.map +1 -1
- package/dist/production/index.cjs +20 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +22 -2
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/application/Application.cjs +116 -59
- package/dist/production/src/application/Application.cjs.map +1 -1
- package/dist/production/src/application/Application.js +116 -59
- package/dist/production/src/application/Application.js.map +1 -1
- package/dist/production/src/application/CliError.cjs +104 -0
- package/dist/production/src/application/CliError.cjs.map +1 -0
- package/dist/production/src/application/CliError.js +104 -0
- package/dist/production/src/application/CliError.js.map +1 -0
- package/dist/production/src/application/Command.cjs +18 -5
- package/dist/production/src/application/Command.cjs.map +1 -1
- package/dist/production/src/application/Command.js +18 -5
- package/dist/production/src/application/Command.js.map +1 -1
- package/dist/production/src/application/CommandConfiguration.cjs +21 -3
- package/dist/production/src/application/CommandConfiguration.cjs.map +1 -1
- package/dist/production/src/application/CommandConfiguration.js +23 -5
- package/dist/production/src/application/CommandConfiguration.js.map +1 -1
- package/dist/production/src/application/CommandManager.cjs +139 -21
- package/dist/production/src/application/CommandManager.cjs.map +1 -1
- package/dist/production/src/application/CommandManager.js +139 -21
- package/dist/production/src/application/CommandManager.js.map +1 -1
- package/dist/production/src/application/ErrorRenderer.cjs +38 -0
- package/dist/production/src/application/ErrorRenderer.cjs.map +1 -0
- package/dist/production/src/application/ErrorRenderer.js +38 -0
- package/dist/production/src/application/ErrorRenderer.js.map +1 -0
- package/dist/production/src/application/GlobalOptions.cjs +54 -0
- package/dist/production/src/application/GlobalOptions.cjs.map +1 -0
- package/dist/production/src/application/GlobalOptions.js +54 -0
- package/dist/production/src/application/GlobalOptions.js.map +1 -0
- package/dist/production/src/application/Input.cjs +5 -0
- package/dist/production/src/application/Input.cjs.map +1 -1
- package/dist/production/src/application/Input.js +5 -0
- package/dist/production/src/application/Input.js.map +1 -1
- package/dist/production/src/application/InputParser.cjs +7 -3
- package/dist/production/src/application/InputParser.cjs.map +1 -1
- package/dist/production/src/application/InputParser.js +7 -3
- package/dist/production/src/application/InputParser.js.map +1 -1
- package/dist/production/src/application/Option.cjs +11 -0
- package/dist/production/src/application/Option.cjs.map +1 -1
- package/dist/production/src/application/Option.js +12 -1
- package/dist/production/src/application/Option.js.map +1 -1
- package/dist/production/src/application/Output.cjs +242 -0
- package/dist/production/src/application/Output.cjs.map +1 -0
- package/dist/production/src/application/Output.js +242 -0
- package/dist/production/src/application/Output.js.map +1 -0
- package/dist/production/src/application/exitProcess.cjs +7 -0
- package/dist/production/src/application/exitProcess.cjs.map +1 -0
- package/dist/production/src/application/exitProcess.js +7 -0
- package/dist/production/src/application/exitProcess.js.map +1 -0
- package/dist/production/src/npm/CommandRunner.cjs +126 -24
- package/dist/production/src/npm/CommandRunner.cjs.map +1 -1
- package/dist/production/src/npm/CommandRunner.js +126 -24
- package/dist/production/src/npm/CommandRunner.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Bun.cjs +1 -2
- package/dist/production/src/npm/packageManagers/Bun.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/Bun.js +1 -2
- package/dist/production/src/npm/packageManagers/Bun.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Npm.cjs +1 -2
- package/dist/production/src/npm/packageManagers/Npm.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/Npm.js +1 -2
- package/dist/production/src/npm/packageManagers/Npm.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Pnpm.cjs +1 -2
- package/dist/production/src/npm/packageManagers/Pnpm.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/Pnpm.js +1 -2
- package/dist/production/src/npm/packageManagers/Pnpm.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Yarn.cjs +1 -2
- package/dist/production/src/npm/packageManagers/Yarn.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/Yarn.js +1 -2
- package/dist/production/src/npm/packageManagers/Yarn.js.map +1 -1
- package/dist/production/src/npm/packageManagers/YarnClassic.cjs +1 -2
- package/dist/production/src/npm/packageManagers/YarnClassic.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/YarnClassic.js +1 -2
- package/dist/production/src/npm/packageManagers/YarnClassic.js.map +1 -1
- package/dist/types/application/Application.d.ts +22 -11
- package/dist/types/application/Application.d.ts.map +1 -1
- package/dist/types/application/Argument.d.ts.map +1 -1
- package/dist/types/application/CliError.d.ts +40 -0
- package/dist/types/application/CliError.d.ts.map +1 -0
- package/dist/types/application/Command.d.ts +12 -2
- package/dist/types/application/Command.d.ts.map +1 -1
- package/dist/types/application/CommandConfiguration.d.ts +7 -4
- package/dist/types/application/CommandConfiguration.d.ts.map +1 -1
- package/dist/types/application/CommandManager.d.ts +40 -2
- package/dist/types/application/CommandManager.d.ts.map +1 -1
- package/dist/types/application/ErrorRenderer.d.ts +8 -0
- package/dist/types/application/ErrorRenderer.d.ts.map +1 -0
- package/dist/types/application/GlobalOptions.d.ts +28 -0
- package/dist/types/application/GlobalOptions.d.ts.map +1 -0
- package/dist/types/application/Input.d.ts +2 -0
- package/dist/types/application/Input.d.ts.map +1 -1
- package/dist/types/application/InputParser.d.ts +5 -1
- package/dist/types/application/InputParser.d.ts.map +1 -1
- package/dist/types/application/Option.d.ts +2 -0
- package/dist/types/application/Option.d.ts.map +1 -1
- package/dist/types/application/Output.d.ts +81 -0
- package/dist/types/application/Output.d.ts.map +1 -0
- package/dist/types/application/UsageFormatter.d.ts.map +1 -1
- package/dist/types/application/exitProcess.d.ts +11 -0
- package/dist/types/application/exitProcess.d.ts.map +1 -0
- package/dist/types/application/index.d.ts +5 -0
- package/dist/types/application/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/npm/CommandRunner.d.ts +13 -0
- package/dist/types/npm/CommandRunner.d.ts.map +1 -1
- package/dist/types/npm/Package.d.ts.map +1 -1
- package/dist/types/npm/PackageWorkspace.d.ts.map +1 -1
- package/dist/types/npm/PackageWorkspaceResolver.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Bun.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Npm.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/PackageManagerHelpers.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Pnpm.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Yarn.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/YarnClassic.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/utils/findPackageRoot.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/application/Application.ts +137 -75
- package/src/application/CliError.ts +117 -0
- package/src/application/Command.ts +27 -6
- package/src/application/CommandConfiguration.ts +22 -4
- package/src/application/CommandManager.ts +172 -22
- package/src/application/ErrorRenderer.ts +40 -0
- package/src/application/GlobalOptions.ts +76 -0
- package/src/application/Input.ts +6 -0
- package/src/application/InputParser.ts +13 -4
- package/src/application/Option.ts +12 -0
- package/src/application/Output.ts +329 -0
- package/src/application/exitProcess.ts +12 -0
- package/src/application/index.ts +5 -0
- package/src/index.ts +40 -2
- package/src/npm/CommandRunner.ts +166 -23
- package/src/npm/packageManagers/Bun.ts +1 -2
- package/src/npm/packageManagers/Npm.ts +1 -2
- package/src/npm/packageManagers/Pnpm.ts +1 -2
- package/src/npm/packageManagers/Yarn.ts +1 -2
- package/src/npm/packageManagers/YarnClassic.ts +1 -2
- package/tests/cases/unit/application.test.ts +426 -0
- package/tests/cases/unit/cliError.test.ts +127 -0
- package/tests/cases/unit/commandManager.test.ts +270 -0
- package/tests/cases/unit/commandRunner.test.ts +192 -0
- package/tests/cases/unit/inputParser.test.ts +21 -0
- package/tests/cases/unit/output.test.ts +393 -0
- package/tests/lib/testOutput.ts +35 -0
- package/types/chalk-table/index.d.ts +9 -0
|
@@ -1,99 +1,161 @@
|
|
|
1
|
-
import { InvalidInputError } from './InputParser.js'
|
|
2
|
-
import { CommandManager } from './CommandManager.js'
|
|
3
1
|
import chalk from 'chalk'
|
|
2
|
+
import { CommandCatalogEntry, CommandFactory, CommandManager } from './CommandManager.js'
|
|
4
3
|
import { Command } from './Command.js'
|
|
4
|
+
import { InvalidInputError } from './InputParser.js'
|
|
5
|
+
import { Output } from './Output.js'
|
|
6
|
+
import { ExitCode } from './CliError.js'
|
|
7
|
+
import { renderCliError } from './ErrorRenderer.js'
|
|
8
|
+
import { exitProcess } from './exitProcess.js'
|
|
9
|
+
import { globalOptions, hasHelpFlag, readGlobalOptionsFromArgs } from './GlobalOptions.js'
|
|
10
|
+
import { UsageFormatter } from './UsageFormatter.js'
|
|
5
11
|
|
|
6
12
|
export class Application {
|
|
13
|
+
private readonly commandManager: CommandManager
|
|
14
|
+
private readonly applicationDescription: string
|
|
15
|
+
private readonly output: Output
|
|
16
|
+
private readonly options: ApplicationOptions
|
|
17
|
+
|
|
18
|
+
constructor(commandManager: CommandManager, applicationDescription: string, options?: ApplicationOptions)
|
|
19
|
+
constructor(commandManager: CommandManager, applicationDescription: string, output: Output, options?: ApplicationOptions)
|
|
7
20
|
constructor(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
21
|
+
commandManager: CommandManager,
|
|
22
|
+
applicationDescription: string,
|
|
23
|
+
outputOrOptions: Output | ApplicationOptions = {},
|
|
24
|
+
options: ApplicationOptions = {},
|
|
25
|
+
) {
|
|
26
|
+
this.commandManager = commandManager
|
|
27
|
+
this.applicationDescription = applicationDescription
|
|
28
|
+
this.output = outputOrOptions instanceof Output ? outputOrOptions : new Output()
|
|
29
|
+
this.options = outputOrOptions instanceof Output ? options : outputOrOptions
|
|
30
|
+
}
|
|
14
31
|
|
|
15
32
|
async run(args: string[]): Promise<void> {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
console.error(`Usage: <command> <command args>`)
|
|
20
|
-
const commands = Object.entries(this.commandManager.commands)
|
|
21
|
-
.filter(([name, factory], index, commands) => commands.findIndex(it => it[1] === factory) === index)
|
|
22
|
-
.map(([name]) => name)
|
|
23
|
-
.sort((a, b) => a.localeCompare(b))
|
|
24
|
-
|
|
25
|
-
const maxCommandNameLength = Math.max(...commands.map(it => it.length))
|
|
26
|
-
|
|
27
|
-
for (let commandName of commands) {
|
|
28
|
-
const [, command] = this.commandManager.createCommand(commandName)
|
|
29
|
-
const configuration = command.getConfiguration()
|
|
30
|
-
const indent = ' '
|
|
31
|
-
console.error(
|
|
32
|
-
`${indent}${
|
|
33
|
-
chalk.greenBright(
|
|
34
|
-
commandName.padEnd(maxCommandNameLength),
|
|
35
|
-
)
|
|
36
|
-
}${indent}${configuration.getDescription()}`,
|
|
37
|
-
)
|
|
38
|
-
if (name === '--help') {
|
|
39
|
-
const helpIndent = `${indent}${' '.repeat(maxCommandNameLength)}${indent}`
|
|
40
|
-
const usage = configuration.getUsage({ format: 'multiline', indent: helpIndent })
|
|
41
|
-
if (usage) {
|
|
42
|
-
console.error(chalk.green(usage))
|
|
43
|
-
} else {
|
|
44
|
-
console.error(`${helpIndent}${chalk.gray('No options')}`)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return process.exit(0)
|
|
33
|
+
if (args.length === 0 || readGlobalOptionsFromArgs(args.slice(0, 1)).help) {
|
|
34
|
+
exitProcess(await this.execute(args))
|
|
35
|
+
return
|
|
50
36
|
}
|
|
51
|
-
|
|
37
|
+
const [name, ...commandArgs] = args
|
|
38
|
+
await this.runCommandInternal(name, commandArgs, true)
|
|
52
39
|
}
|
|
53
40
|
|
|
54
|
-
public async runCommand(name: string, args: string[]) {
|
|
55
|
-
|
|
41
|
+
public async runCommand(name: string, args: string[]): Promise<void> {
|
|
42
|
+
await this.runCommandInternal(name, args, false)
|
|
56
43
|
}
|
|
57
44
|
|
|
58
|
-
public async runCommandInternal(name: string, args: string[], showCommandName: boolean) {
|
|
59
|
-
|
|
45
|
+
public async runCommandInternal(name: string, args: string[], showCommandName: boolean): Promise<void> {
|
|
46
|
+
exitProcess(await this.executeArgs([name, ...args], showCommandName))
|
|
47
|
+
}
|
|
60
48
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
49
|
+
/** Runs the given argv and returns the exit code instead of terminating the process. */
|
|
50
|
+
public async execute(args: string[]): Promise<number> {
|
|
51
|
+
const help = readGlobalOptionsFromArgs(args.slice(0, 1)).help
|
|
52
|
+
if (args.length === 0 || help) {
|
|
53
|
+
this.printApplicationHelp(help)
|
|
54
|
+
return ExitCode.Success
|
|
55
|
+
}
|
|
56
|
+
return this.executeArgs(args, true)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public async executeCommand(name: string, args: string[]): Promise<number> {
|
|
60
|
+
return this.executeArgs([name, ...args], false)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async executeArgs(args: string[], showCommandName: boolean): Promise<number> {
|
|
64
|
+
let entry: CommandCatalogEntry | undefined
|
|
65
|
+
let command: ReturnType<CommandFactory> | undefined
|
|
66
|
+
// the mode has to be known before anything can fail, so that every error is reported in the requested format
|
|
67
|
+
this.output.applyGlobalOptions(readGlobalOptionsFromArgs(args))
|
|
68
|
+
try {
|
|
69
|
+
const resolution = this.commandManager.resolve(args)
|
|
70
|
+
if (resolution.type === 'group') {
|
|
71
|
+
const groupHelp = resolution.args.length === 1 && (resolution.args[0] === '--help' || resolution.args[0] === '-h')
|
|
72
|
+
if (resolution.args.length > 0 && !groupHelp) {
|
|
73
|
+
throw new InvalidInputError(`Unexpected input after command group "${resolution.name}": ${resolution.args.join(' ')}`)
|
|
74
|
+
}
|
|
75
|
+
this.printGroupHelp(resolution.name, resolution.entries)
|
|
76
|
+
return ExitCode.Success
|
|
64
77
|
}
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
entry = resolution.entry
|
|
79
|
+
const commandArgs = resolution.args
|
|
80
|
+
command = entry.create()
|
|
81
|
+
|
|
82
|
+
if (hasHelpFlag(commandArgs)) {
|
|
83
|
+
this.printCommandHelp(entry.name, command, showCommandName)
|
|
84
|
+
return ExitCode.Success
|
|
69
85
|
}
|
|
70
|
-
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
console.error('\nArguments and options:')
|
|
75
|
-
console.error(configuration.getUsage({ format: 'multiline' }))
|
|
86
|
+
|
|
87
|
+
if (command.run !== Command.prototype.run) {
|
|
88
|
+
await this.options.beforeRun?.({ command, args: commandArgs, name: entry.name })
|
|
89
|
+
return await command.run(commandArgs, this.output)
|
|
76
90
|
}
|
|
77
91
|
|
|
78
|
-
|
|
79
|
-
|
|
92
|
+
const parsed = command.parse(commandArgs)
|
|
93
|
+
this.output.applyGlobalOptions(parsed.globals)
|
|
80
94
|
|
|
81
|
-
|
|
82
|
-
await this.options.beforeRun?.({ command, args, name: fullName })
|
|
95
|
+
await this.options.beforeRun?.({ command, args: commandArgs, name: entry.name })
|
|
83
96
|
|
|
84
|
-
|
|
85
|
-
return process.exit(result)
|
|
97
|
+
return await command.runParsed(parsed, this.output)
|
|
86
98
|
} catch (e) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return process.exit(1)
|
|
93
|
-
} else {
|
|
94
|
-
console.error(e)
|
|
95
|
-
return process.exit(2)
|
|
99
|
+
const exitCode = renderCliError(e, this.output)
|
|
100
|
+
if (e instanceof InvalidInputError && command !== undefined && this.output.mode === 'human') {
|
|
101
|
+
const usage = command.getConfiguration().getUsage({ format: 'line' })
|
|
102
|
+
this.output.writeStderr(`${showCommandName && entry ? chalk.greenBright(entry.name) + ' ' : ''}${chalk.green(usage)}`)
|
|
96
103
|
}
|
|
104
|
+
return exitCode
|
|
97
105
|
}
|
|
98
106
|
}
|
|
107
|
+
|
|
108
|
+
private printApplicationHelp(verbose: boolean): void {
|
|
109
|
+
this.output.writeStderr(this.applicationDescription)
|
|
110
|
+
this.output.writeStderr(`Usage: <command> <command args>`)
|
|
111
|
+
const commands = this.commandManager.getCommands()
|
|
112
|
+
if (commands.length === 0) {
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
const maxCommandNameLength = Math.max(...commands.map(it => it.name.length))
|
|
116
|
+
const indent = ' '
|
|
117
|
+
for (const entry of commands) {
|
|
118
|
+
const configuration = entry.create().getConfiguration()
|
|
119
|
+
this.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`)
|
|
120
|
+
if (verbose) {
|
|
121
|
+
const helpIndent = `${indent}${' '.repeat(maxCommandNameLength)}${indent}`
|
|
122
|
+
const usage = configuration.getUsage({ format: 'multiline', indent: helpIndent })
|
|
123
|
+
this.output.writeStderr(usage ? chalk.green(usage) : `${helpIndent}${chalk.gray('No options')}`)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private printGroupHelp(name: string, entries: readonly CommandCatalogEntry[]): void {
|
|
129
|
+
this.output.writeStderr(`Usage: ${name} <command> <command args>`)
|
|
130
|
+
const maxCommandNameLength = Math.max(...entries.map(it => it.name.length))
|
|
131
|
+
const indent = ' '
|
|
132
|
+
for (const entry of [...entries].sort((a, b) => a.name.localeCompare(b.name))) {
|
|
133
|
+
const configuration = entry.create().getConfiguration()
|
|
134
|
+
this.output.writeStderr(`${indent}${chalk.greenBright(entry.name.padEnd(maxCommandNameLength))}${indent}${configuration.getDescription()}`)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private printCommandHelp(name: string, command: ReturnType<CommandFactory>, showCommandName: boolean): void {
|
|
139
|
+
if (showCommandName) {
|
|
140
|
+
this.output.writeStderr(chalk.greenBright(name))
|
|
141
|
+
}
|
|
142
|
+
const configuration = command.getConfiguration()
|
|
143
|
+
const commandDescription = configuration.getDescription()
|
|
144
|
+
if (commandDescription) {
|
|
145
|
+
this.output.writeStderr(commandDescription)
|
|
146
|
+
}
|
|
147
|
+
const shortUsage = configuration.getUsage({ format: 'short' })
|
|
148
|
+
if (shortUsage) {
|
|
149
|
+
this.output.writeStderr('\nUsage:')
|
|
150
|
+
this.output.writeStderr(chalk.green(shortUsage))
|
|
151
|
+
this.output.writeStderr('\nArguments and options:')
|
|
152
|
+
this.output.writeStderr(configuration.getUsage({ format: 'multiline' }))
|
|
153
|
+
}
|
|
154
|
+
this.output.writeStderr('\nGlobal options:')
|
|
155
|
+
this.output.writeStderr(UsageFormatter.format([], [...globalOptions], { format: 'multiline' }))
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
interface ApplicationOptions {
|
|
160
|
+
beforeRun?: (args: { command: ReturnType<CommandFactory>; name: string; args: string[] }) => void | Promise<void>
|
|
99
161
|
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { InvalidInputError } from './InputParser.js'
|
|
2
|
+
|
|
3
|
+
export enum ExitCode {
|
|
4
|
+
Success = 0,
|
|
5
|
+
/** invalid input / validation */
|
|
6
|
+
InputError = 1,
|
|
7
|
+
/** unexpected, a bug */
|
|
8
|
+
InternalError = 2,
|
|
9
|
+
NotFound = 3,
|
|
10
|
+
/** network, timeout, 5xx — the caller SHOULD retry */
|
|
11
|
+
Transient = 4,
|
|
12
|
+
/** already exists */
|
|
13
|
+
Conflict = 5,
|
|
14
|
+
/** auth / permission denied */
|
|
15
|
+
Forbidden = 6,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CliErrorOptions {
|
|
19
|
+
/** stable machine-readable slug, e.g. 'PROJECT_NOT_DEFINED' */
|
|
20
|
+
code: string
|
|
21
|
+
exitCode?: ExitCode
|
|
22
|
+
retryable?: boolean
|
|
23
|
+
details?: unknown
|
|
24
|
+
cause?: unknown
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue }
|
|
28
|
+
|
|
29
|
+
export class CliError extends Error {
|
|
30
|
+
public readonly code: string
|
|
31
|
+
public readonly exitCode: ExitCode
|
|
32
|
+
public readonly retryable: boolean
|
|
33
|
+
public readonly details?: JsonValue
|
|
34
|
+
|
|
35
|
+
constructor(message: string, options: CliErrorOptions) {
|
|
36
|
+
super(message, { cause: options.cause })
|
|
37
|
+
this.name = 'CliError'
|
|
38
|
+
this.code = options.code
|
|
39
|
+
this.exitCode = options.exitCode ?? ExitCode.InputError
|
|
40
|
+
this.retryable = options.retryable ?? this.exitCode === ExitCode.Transient
|
|
41
|
+
this.details = normalizeJsonValue(options.details)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const unexpectedCliErrors = new WeakSet<CliError>()
|
|
46
|
+
|
|
47
|
+
export const isUnexpectedCliError = (error: CliError): boolean => unexpectedCliErrors.has(error)
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Normalizes anything thrown by a command into a {@link CliError}. Legacy `throw 'some message'` is
|
|
51
|
+
* treated as an input error, everything else unexpected as an internal one.
|
|
52
|
+
*/
|
|
53
|
+
export const toCliError = (error: unknown): CliError => {
|
|
54
|
+
if (error instanceof CliError) {
|
|
55
|
+
return error
|
|
56
|
+
}
|
|
57
|
+
if (error instanceof InvalidInputError) {
|
|
58
|
+
return new CliError(error.message, { code: 'INVALID_INPUT', exitCode: ExitCode.InputError, cause: error })
|
|
59
|
+
}
|
|
60
|
+
if (typeof error === 'string') {
|
|
61
|
+
return new CliError(error, { code: 'ERROR', exitCode: ExitCode.InputError })
|
|
62
|
+
}
|
|
63
|
+
if (error instanceof Error) {
|
|
64
|
+
const cliError = new CliError(error.message, { code: 'UNKNOWN', exitCode: ExitCode.InternalError, details: error.stack, cause: error })
|
|
65
|
+
unexpectedCliErrors.add(cliError)
|
|
66
|
+
return cliError
|
|
67
|
+
}
|
|
68
|
+
const cliError = new CliError(safeString(error), { code: 'UNKNOWN', exitCode: ExitCode.InternalError, details: error })
|
|
69
|
+
unexpectedCliErrors.add(cliError)
|
|
70
|
+
return cliError
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const safeString = (value: unknown): string => {
|
|
74
|
+
try {
|
|
75
|
+
return String(value)
|
|
76
|
+
} catch {
|
|
77
|
+
return 'Unknown error'
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const normalizeJsonValue = (value: unknown): JsonValue | undefined => normalizeValue(value, new WeakSet())
|
|
82
|
+
|
|
83
|
+
const normalizeValue = (value: unknown, ancestors: WeakSet<object>): JsonValue | undefined => {
|
|
84
|
+
if (value === undefined) {
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean') {
|
|
88
|
+
return value
|
|
89
|
+
}
|
|
90
|
+
if (typeof value === 'number') {
|
|
91
|
+
return Number.isFinite(value) ? value : String(value)
|
|
92
|
+
}
|
|
93
|
+
if (typeof value === 'bigint' || typeof value === 'symbol' || typeof value === 'function') {
|
|
94
|
+
return safeString(value)
|
|
95
|
+
}
|
|
96
|
+
if (ancestors.has(value)) {
|
|
97
|
+
return '[Circular]'
|
|
98
|
+
}
|
|
99
|
+
ancestors.add(value)
|
|
100
|
+
try {
|
|
101
|
+
if (Array.isArray(value)) {
|
|
102
|
+
return value.map(item => normalizeValue(item, ancestors) ?? null)
|
|
103
|
+
}
|
|
104
|
+
const result: { [key: string]: JsonValue } = {}
|
|
105
|
+
for (const [key, item] of Object.entries(value)) {
|
|
106
|
+
const normalized = normalizeValue(item, ancestors)
|
|
107
|
+
if (normalized !== undefined) {
|
|
108
|
+
result[key] = normalized
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return result
|
|
112
|
+
} catch {
|
|
113
|
+
return safeString(value)
|
|
114
|
+
} finally {
|
|
115
|
+
ancestors.delete(value)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { CommandConfiguration } from './CommandConfiguration.js'
|
|
2
2
|
import { Arguments, Input, Options } from './Input.js'
|
|
3
|
+
import { globalOptions, GlobalOptionValues, readGlobalOptions } from './GlobalOptions.js'
|
|
4
|
+
import { Output } from './Output.js'
|
|
5
|
+
|
|
6
|
+
export interface ParsedCommandInput<Args extends Arguments, TOptions extends Options> {
|
|
7
|
+
readonly input: Input<Args, TOptions>
|
|
8
|
+
readonly globals: GlobalOptionValues
|
|
9
|
+
readonly diagnostics: readonly string[]
|
|
10
|
+
}
|
|
3
11
|
|
|
4
12
|
export abstract class Command<Args extends Arguments, TOptions extends Options> {
|
|
5
13
|
private configuration: CommandConfiguration<Args, TOptions> | undefined
|
|
@@ -10,18 +18,31 @@ export abstract class Command<Args extends Arguments, TOptions extends Options>
|
|
|
10
18
|
if (this.configuration === undefined) {
|
|
11
19
|
const configuration = new CommandConfiguration()
|
|
12
20
|
this.configure(configuration)
|
|
13
|
-
configuration.validate()
|
|
21
|
+
configuration.validate(globalOptions)
|
|
14
22
|
this.configuration = configuration
|
|
15
23
|
}
|
|
16
24
|
return this.configuration
|
|
17
25
|
}
|
|
18
26
|
|
|
19
|
-
protected abstract execute(input: Input<Args, TOptions
|
|
27
|
+
protected abstract execute(input: Input<Args, TOptions>, output: Output): Promise<void | number>
|
|
28
|
+
|
|
29
|
+
/** Parses argv in a single pass over the command options merged with the global ones. */
|
|
30
|
+
public parse(args: string[]): ParsedCommandInput<Args, TOptions> {
|
|
31
|
+
const parsed = this.getConfiguration().createParser(globalOptions).parse<Args, TOptions>(args)
|
|
32
|
+
return { input: parsed.input, globals: readGlobalOptions(parsed.input), diagnostics: parsed.diagnostics }
|
|
33
|
+
}
|
|
20
34
|
|
|
21
|
-
public async
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
public async runParsed(parsed: ParsedCommandInput<Args, TOptions>, output: Output): Promise<number> {
|
|
36
|
+
for (const diagnostic of parsed.diagnostics) {
|
|
37
|
+
output.warn(diagnostic)
|
|
38
|
+
}
|
|
39
|
+
const result = await this.execute(parsed.input, output)
|
|
25
40
|
return result || 0
|
|
26
41
|
}
|
|
42
|
+
|
|
43
|
+
public async run(args: string[], output: Output = new Output()): Promise<number> {
|
|
44
|
+
const parsed = this.parse(args)
|
|
45
|
+
output.applyGlobalOptions(parsed.globals)
|
|
46
|
+
return this.runParsed(parsed, output)
|
|
47
|
+
}
|
|
27
48
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Argument, ArgumentConfiguration } from './Argument.js'
|
|
2
|
-
import { Option, OptionConfiguration, OptionMode } from './Option.js'
|
|
2
|
+
import { mergeOptions, Option, OptionConfiguration, OptionMode } from './Option.js'
|
|
3
3
|
import { InputParser } from './InputParser.js'
|
|
4
4
|
import { UsageFormat, UsageFormatter } from './UsageFormatter.js'
|
|
5
5
|
import { Arguments, Options } from './Input.js'
|
|
@@ -26,7 +26,7 @@ export class CommandConfiguration<Args extends Arguments, TOptions extends Optio
|
|
|
26
26
|
return new OptionConfiguration(option)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
public validate() {
|
|
29
|
+
public validate(reservedOptions: readonly Option[] = []) {
|
|
30
30
|
let hasVariadic = false
|
|
31
31
|
let hasOptional = false
|
|
32
32
|
for (let argument of this.arguments) {
|
|
@@ -37,14 +37,32 @@ export class CommandConfiguration<Args extends Arguments, TOptions extends Optio
|
|
|
37
37
|
throw new InvalidConfigurationError(`A variadic argument must be the last`)
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
+
for (const option of this.options) {
|
|
41
|
+
const collision = reservedOptions.find(reserved =>
|
|
42
|
+
reserved.name === option.name
|
|
43
|
+
|| (option.shortcut !== undefined && reserved.shortcut === option.shortcut)
|
|
44
|
+
)
|
|
45
|
+
if (collision !== undefined) {
|
|
46
|
+
throw new InvalidConfigurationError(`Option --${option.name} conflicts with reserved global option --${collision.name}`)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
40
49
|
}
|
|
41
50
|
|
|
42
51
|
public getDescription(): string {
|
|
43
52
|
return this.descriptionValue
|
|
44
53
|
}
|
|
45
54
|
|
|
46
|
-
public
|
|
47
|
-
return
|
|
55
|
+
public getArguments(): readonly Argument[] {
|
|
56
|
+
return [...this.arguments]
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public getOptions(): readonly Option[] {
|
|
60
|
+
return [...this.options]
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Appends shared options for a single parser pass after configuration validation. */
|
|
64
|
+
public createParser(extraOptions: readonly Option[] = []): InputParser {
|
|
65
|
+
return new InputParser(this.arguments, mergeOptions(this.options, extraOptions))
|
|
48
66
|
}
|
|
49
67
|
|
|
50
68
|
public getUsage(args: { format?: UsageFormat; indent?: string } = {}): string {
|
|
@@ -1,36 +1,186 @@
|
|
|
1
1
|
import { Command } from './Command.js'
|
|
2
|
-
import
|
|
2
|
+
import { CliError, ExitCode } from './CliError.js'
|
|
3
|
+
import { InvalidConfigurationError } from './CommandConfiguration.js'
|
|
3
4
|
|
|
4
|
-
export type
|
|
5
|
+
export type CommandFactory = () => Command<any, any>
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Keys sharing the very same factory are aliases of one command — the first one registered is the
|
|
9
|
+
* canonical name. Both `migrations diff` and `migrations:diff` resolve to the same command.
|
|
10
|
+
*/
|
|
11
|
+
export type CommandFactoryList = { [command: string]: CommandFactory }
|
|
12
|
+
|
|
13
|
+
export interface CommandCatalogEntry {
|
|
14
|
+
/** canonical name, space separated */
|
|
15
|
+
readonly name: string
|
|
16
|
+
readonly tokens: readonly string[]
|
|
17
|
+
readonly group: string | undefined
|
|
18
|
+
readonly aliases: readonly string[]
|
|
19
|
+
readonly create: CommandFactory
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type CommandResolution =
|
|
23
|
+
| { readonly type: 'command'; readonly entry: CommandCatalogEntry; readonly args: string[] }
|
|
24
|
+
| { readonly type: 'group'; readonly name: string; readonly entries: readonly CommandCatalogEntry[]; readonly args: string[] }
|
|
25
|
+
|
|
26
|
+
const splitTokens = (name: string): string[] => name.split(/[\s:]+/).filter(it => it !== '')
|
|
5
27
|
|
|
6
28
|
export class CommandManager {
|
|
7
|
-
|
|
29
|
+
private readonly entries: CommandCatalogEntry[]
|
|
30
|
+
/** normalized name (canonical or alias) => entry */
|
|
31
|
+
private readonly lookup = new Map<string, CommandCatalogEntry>()
|
|
32
|
+
/** every strict token prefix of a command name => commands in that group */
|
|
33
|
+
private readonly groups = new Map<string, CommandCatalogEntry[]>()
|
|
34
|
+
private readonly maxTokens: number
|
|
8
35
|
|
|
9
|
-
public
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
36
|
+
constructor(public readonly commands: CommandFactoryList) {
|
|
37
|
+
this.entries = createCatalog(commands)
|
|
38
|
+
for (const entry of this.entries) {
|
|
39
|
+
for (const name of [entry.name, ...entry.aliases]) {
|
|
40
|
+
const tokens = splitTokens(name)
|
|
41
|
+
const normalizedName = tokens.join(' ')
|
|
42
|
+
const existing = this.lookup.get(normalizedName)
|
|
43
|
+
if (existing !== undefined && existing.create !== entry.create) {
|
|
44
|
+
throw new InvalidConfigurationError(
|
|
45
|
+
`Command names "${existing.name}" and "${name}" both normalize to "${normalizedName}" but use different factories.`,
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
this.lookup.set(normalizedName, entry)
|
|
49
|
+
for (let i = 1; i < tokens.length; i++) {
|
|
50
|
+
const prefix = tokens.slice(0, i).join(' ')
|
|
51
|
+
const group = this.groups.get(prefix) ?? []
|
|
52
|
+
if (!group.includes(entry)) {
|
|
53
|
+
group.push(entry)
|
|
54
|
+
}
|
|
55
|
+
this.groups.set(prefix, group)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
15
58
|
}
|
|
16
|
-
|
|
17
|
-
|
|
59
|
+
this.maxTokens = Math.max(1, ...[...this.lookup.keys()].map(it => splitTokens(it).length))
|
|
60
|
+
this.validateNoPrefixCollision()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public getCommands(): readonly CommandCatalogEntry[] {
|
|
64
|
+
return [...this.entries].sort((a, b) => a.name.localeCompare(b.name))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Resolves the longest command name matching the leading argv tokens. `migrations diff`,
|
|
69
|
+
* `migrations:diff` and the `m:d` shorthand all resolve to the same command; the tokens that were
|
|
70
|
+
* not consumed are returned untouched as the command arguments.
|
|
71
|
+
*/
|
|
72
|
+
public resolve(args: string[]): CommandResolution {
|
|
73
|
+
const tokens: string[] = []
|
|
74
|
+
const checkpoints: { tokens: string[]; argvIndex: number }[] = []
|
|
75
|
+
for (let i = 0; i < args.length; i++) {
|
|
76
|
+
if (args[i].startsWith('-')) {
|
|
77
|
+
break
|
|
78
|
+
}
|
|
79
|
+
const parts = splitTokens(args[i])
|
|
80
|
+
if (parts.length === 0 || tokens.length + parts.length > this.maxTokens) {
|
|
81
|
+
break
|
|
82
|
+
}
|
|
83
|
+
tokens.push(...parts)
|
|
84
|
+
checkpoints.push({ tokens: [...tokens], argvIndex: i + 1 })
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (let i = checkpoints.length - 1; i >= 0; i--) {
|
|
88
|
+
const entry = this.lookup.get(checkpoints[i].tokens.join(' '))
|
|
89
|
+
if (entry) {
|
|
90
|
+
return { type: 'command', entry, args: args.slice(checkpoints[i].argvIndex) }
|
|
91
|
+
}
|
|
18
92
|
}
|
|
19
|
-
|
|
93
|
+
|
|
94
|
+
const group = this.groups.get(tokens.join(' '))
|
|
95
|
+
if (group !== undefined && group.length > 0) {
|
|
96
|
+
const argvIndex = checkpoints[checkpoints.length - 1]?.argvIndex ?? 0
|
|
97
|
+
return { type: 'group', name: tokens.join(' '), entries: group, args: args.slice(argvIndex) }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (let i = checkpoints.length - 1; i >= 0; i--) {
|
|
101
|
+
// a group named in full must be followed by a verb — never let it swallow the remaining tokens as arguments
|
|
102
|
+
if (i < checkpoints.length - 1 && this.groups.has(checkpoints[i].tokens.join(' '))) {
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
const matched = this.matchShorthand(checkpoints[i].tokens)
|
|
106
|
+
if (matched.length === 1) {
|
|
107
|
+
return { type: 'command', entry: matched[0], args: args.slice(checkpoints[i].argvIndex) }
|
|
108
|
+
}
|
|
109
|
+
if (matched.length > 1) {
|
|
110
|
+
throw new CliError(
|
|
111
|
+
`Command "${checkpoints[i].tokens.join(' ')}" is ambiguous. Did you mean one of these?\n - ${matched.map(it => it.name).join('\n - ')}`,
|
|
112
|
+
{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// the leading tokens name a known group, but the verb does not exist in it
|
|
118
|
+
for (let i = tokens.length - 1; i >= 1; i--) {
|
|
119
|
+
const known = this.groups.get(tokens.slice(0, i).join(' '))
|
|
120
|
+
if (known !== undefined && known.length > 0) {
|
|
121
|
+
throw new CliError(
|
|
122
|
+
`Command "${tokens.join(' ')}" not found. Available commands in "${tokens.slice(0, i).join(' ')}":\n - ${
|
|
123
|
+
known.map(it => it.name).sort((a, b) => a.localeCompare(b)).join('\n - ')
|
|
124
|
+
}`,
|
|
125
|
+
{ code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError },
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
throw new CliError(`Command "${args[0] ?? ''}" not found.`, { code: 'COMMAND_NOT_FOUND', exitCode: ExitCode.InputError })
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public createCommand(name: string): [string, Command<any, any>] {
|
|
134
|
+
const resolution = this.resolve([name])
|
|
135
|
+
if (resolution.type === 'group') {
|
|
136
|
+
throw new CliError(
|
|
137
|
+
`Command "${resolution.name}" is a command group. Available commands:\n - ${resolution.entries.map(it => it.name).join('\n - ')}`,
|
|
138
|
+
{ code: 'COMMAND_AMBIGUOUS', exitCode: ExitCode.InputError },
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
return [resolution.entry.name, resolution.entry.create()]
|
|
20
142
|
}
|
|
21
143
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
144
|
+
/** Prefix matching of the abbreviated form: `m:d` => `migrations diff`, `w:u` => `workspace update api`. */
|
|
145
|
+
private matchShorthand(tokens: string[]): CommandCatalogEntry[] {
|
|
146
|
+
if (tokens.length === 0) {
|
|
147
|
+
return []
|
|
25
148
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
149
|
+
return this.entries.filter(entry => entry.tokens.length >= tokens.length && tokens.every((token, index) => entry.tokens[index].startsWith(token)))
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private validateNoPrefixCollision(): void {
|
|
153
|
+
const names = [...this.lookup.keys()]
|
|
154
|
+
for (const name of names) {
|
|
155
|
+
for (const other of names) {
|
|
156
|
+
if (other !== name && other.startsWith(name + ' ')) {
|
|
157
|
+
throw new InvalidConfigurationError(
|
|
158
|
+
`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.`,
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
30
162
|
}
|
|
31
|
-
|
|
32
|
-
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const createCatalog = (commands: CommandFactoryList): CommandCatalogEntry[] => {
|
|
167
|
+
const byFactory = new Map<CommandFactory, { name: string; aliases: string[] }>()
|
|
168
|
+
for (const [name, factory] of Object.entries(commands)) {
|
|
169
|
+
const registered = byFactory.get(factory)
|
|
170
|
+
if (registered) {
|
|
171
|
+
registered.aliases.push(name)
|
|
172
|
+
} else {
|
|
173
|
+
byFactory.set(factory, { name, aliases: [] })
|
|
33
174
|
}
|
|
34
|
-
throw `Command ${name} is ambiguous. Did you mean one of these?\n - ` + matchedCommands.join('\n - ') + '\n'
|
|
35
175
|
}
|
|
176
|
+
return [...byFactory].map(([factory, { name, aliases }]) => {
|
|
177
|
+
const tokens = splitTokens(name)
|
|
178
|
+
return {
|
|
179
|
+
name: tokens.join(' '),
|
|
180
|
+
tokens,
|
|
181
|
+
group: tokens.length > 1 ? tokens[0] : undefined,
|
|
182
|
+
aliases,
|
|
183
|
+
create: factory,
|
|
184
|
+
}
|
|
185
|
+
})
|
|
36
186
|
}
|