@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.
- 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
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
2
|
+
import { ExitCode, isUnexpectedCliError, toCliError } from './CliError.js'
|
|
3
|
+
import { escapeTerminalText, Output } from './Output.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The single place where an error becomes user visible. Always writes to stderr — in json mode as the
|
|
7
|
+
* `{ ok: false, error }` envelope, otherwise as a colored one-liner. Returns the process exit code.
|
|
8
|
+
*/
|
|
9
|
+
export const renderCliError = (error: unknown, output: Output): ExitCode => {
|
|
10
|
+
const cliError = toCliError(error)
|
|
11
|
+
if (output.isJson) {
|
|
12
|
+
output.writeStderr(serializeError(cliError, isUnexpectedCliError(cliError)))
|
|
13
|
+
return cliError.exitCode
|
|
14
|
+
}
|
|
15
|
+
output.writeStderr(chalk.red(`Error [${escapeTerminalText(cliError.code)}]: ${escapeTerminalText(cliError.message)}`))
|
|
16
|
+
if (cliError.exitCode === ExitCode.InternalError && typeof cliError.details === 'string') {
|
|
17
|
+
output.writeStderr(chalk.gray(escapeTerminalText(cliError.details)))
|
|
18
|
+
}
|
|
19
|
+
return cliError.exitCode
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const serializeError = (error: ReturnType<typeof toCliError>, unexpected: boolean): string => {
|
|
23
|
+
try {
|
|
24
|
+
return JSON.stringify(
|
|
25
|
+
{
|
|
26
|
+
ok: false,
|
|
27
|
+
error: {
|
|
28
|
+
code: error.code,
|
|
29
|
+
message: unexpected ? 'An unexpected error occurred.' : error.message,
|
|
30
|
+
retryable: error.retryable,
|
|
31
|
+
details: unexpected ? null : error.details ?? null,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
null,
|
|
35
|
+
2,
|
|
36
|
+
)
|
|
37
|
+
} catch {
|
|
38
|
+
return '{"ok":false,"error":{"code":"UNKNOWN","message":"Unable to serialize error","retryable":false,"details":null}}'
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Option, OptionMode } from './Option.js'
|
|
2
|
+
|
|
3
|
+
export interface GlobalOptionValues {
|
|
4
|
+
readonly json: boolean
|
|
5
|
+
readonly quiet: boolean
|
|
6
|
+
/** false when --no-color was passed */
|
|
7
|
+
readonly color: boolean
|
|
8
|
+
readonly help: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Options available on every command. Their names and shortcuts are reserved for the global parser.
|
|
13
|
+
*/
|
|
14
|
+
export const globalOptions: readonly Option[] = [
|
|
15
|
+
{ name: 'json', required: false, deprecated: false, mode: OptionMode.VALUE_NONE, description: 'Print machine readable JSON to stdout' },
|
|
16
|
+
{
|
|
17
|
+
name: 'quiet',
|
|
18
|
+
shortcut: 'q',
|
|
19
|
+
required: false,
|
|
20
|
+
deprecated: false,
|
|
21
|
+
mode: OptionMode.VALUE_NONE,
|
|
22
|
+
description: 'Print only the resulting data to stdout',
|
|
23
|
+
},
|
|
24
|
+
{ name: 'no-color', required: false, deprecated: false, mode: OptionMode.VALUE_NONE, description: 'Disable colored output' },
|
|
25
|
+
{ name: 'help', shortcut: 'h', required: false, deprecated: false, mode: OptionMode.VALUE_NONE, description: 'Print help of the command' },
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
export interface RawOptionReader {
|
|
29
|
+
getRawOption(name: string): string | boolean | string[] | undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Reads the global options from a parsed input.
|
|
34
|
+
*/
|
|
35
|
+
export const readGlobalOptions = (input: RawOptionReader): GlobalOptionValues => ({
|
|
36
|
+
json: input.getRawOption('json') === true,
|
|
37
|
+
quiet: input.getRawOption('quiet') === true,
|
|
38
|
+
color: input.getRawOption('no-color') !== true,
|
|
39
|
+
help: input.getRawOption('help') === true,
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Reads the global options straight from argv, before the command parser runs. Only used to pick the
|
|
44
|
+
* rendering mode early (so that a parse error is reported in the requested format) and to detect
|
|
45
|
+
* --help. Nothing is removed from argv, so parsing semantics stay untouched. A token starting with a
|
|
46
|
+
* dash can never be consumed as a value by {@link InputParser}, so this scan cannot misread a value.
|
|
47
|
+
*/
|
|
48
|
+
export const readGlobalOptionsFromArgs = (args: string[]): GlobalOptionValues => {
|
|
49
|
+
let json = false
|
|
50
|
+
let quiet = false
|
|
51
|
+
let color = true
|
|
52
|
+
let help = false
|
|
53
|
+
for (const arg of args) {
|
|
54
|
+
if (!arg.startsWith('-')) {
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
const token = arg.split('=', 2)[0]
|
|
58
|
+
if (isGlobalOptionToken(token, 'json')) {
|
|
59
|
+
json = true
|
|
60
|
+
} else if (isGlobalOptionToken(token, 'quiet')) {
|
|
61
|
+
quiet = true
|
|
62
|
+
} else if (isGlobalOptionToken(token, 'no-color')) {
|
|
63
|
+
color = false
|
|
64
|
+
} else if (isGlobalOptionToken(token, 'help')) {
|
|
65
|
+
help = true
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { json, quiet, color, help }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const isGlobalOptionToken = (token: string, name: string): boolean => {
|
|
72
|
+
const option = globalOptions.find(item => item.name === name)
|
|
73
|
+
return option !== undefined && (token === `--${option.name}` || (option.shortcut !== undefined && token === `-${option.shortcut}`))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const hasHelpFlag = (args: string[]): boolean => readGlobalOptionsFromArgs(args).help
|
package/src/application/Input.ts
CHANGED
|
@@ -8,6 +8,12 @@ export class Input<Args extends Arguments = Arguments, Opts extends Options = Op
|
|
|
8
8
|
return this.options[name]
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
/** Reads an option that is not part of the command signature — the global ones parsed in the same pass. */
|
|
12
|
+
getRawOption(name: string): string | boolean | string[] | undefined {
|
|
13
|
+
const options: Options = this.options
|
|
14
|
+
return options[name]
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
getArgument<Name extends keyof Args>(name: Name): Args[Name] {
|
|
12
18
|
return this.args[name]
|
|
13
19
|
}
|
|
@@ -2,13 +2,22 @@ import { Argument } from './Argument.js'
|
|
|
2
2
|
import { Option, OptionMode } from './Option.js'
|
|
3
3
|
import { Arguments, Input, Options } from './Input.js'
|
|
4
4
|
|
|
5
|
+
export interface InputParserResult<Args extends Arguments, Opts extends Options> {
|
|
6
|
+
readonly input: Input<Args, Opts>
|
|
7
|
+
readonly diagnostics: readonly string[]
|
|
8
|
+
}
|
|
9
|
+
|
|
5
10
|
export class InputParser {
|
|
6
11
|
constructor(private _arguments: Argument[], private options: Option[]) {}
|
|
7
12
|
|
|
8
|
-
parse<Args extends Arguments, Opts extends Options>(args: string[]):
|
|
9
|
-
args = args.
|
|
13
|
+
parse<Args extends Arguments, Opts extends Options>(args: string[]): InputParserResult<Args, Opts> {
|
|
14
|
+
args = args.flatMap(arg => {
|
|
15
|
+
const separator = arg.startsWith('-') ? arg.indexOf('=') : -1
|
|
16
|
+
return separator === -1 ? [arg] : [arg.slice(0, separator), arg.slice(separator + 1)]
|
|
17
|
+
})
|
|
10
18
|
let options: Options = {}
|
|
11
19
|
let argumentValues: Arguments = {}
|
|
20
|
+
const diagnostics: string[] = []
|
|
12
21
|
|
|
13
22
|
let i = 0
|
|
14
23
|
let argumentNumber = 0
|
|
@@ -61,7 +70,7 @@ export class InputParser {
|
|
|
61
70
|
}
|
|
62
71
|
if (option) {
|
|
63
72
|
if (option.deprecated) {
|
|
64
|
-
|
|
73
|
+
diagnostics.push(`Option --${option.name} is deprecated.`)
|
|
65
74
|
}
|
|
66
75
|
if (option.mode === OptionMode.VALUE_NONE) {
|
|
67
76
|
options[option.name] = true
|
|
@@ -99,7 +108,7 @@ export class InputParser {
|
|
|
99
108
|
}
|
|
100
109
|
}
|
|
101
110
|
|
|
102
|
-
return new Input<Args, Opts>(argumentValues as Args, options as Opts)
|
|
111
|
+
return { input: new Input<Args, Opts>(argumentValues as Args, options as Opts), diagnostics }
|
|
103
112
|
}
|
|
104
113
|
|
|
105
114
|
private tryParseValue(arg: string | undefined): string | undefined {
|
|
@@ -7,6 +7,18 @@ export type Option = {
|
|
|
7
7
|
mode: OptionMode
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
/** Combines option sets while omitting duplicate definitions. */
|
|
11
|
+
export const mergeOptions = (options: readonly Option[], extra: readonly Option[]): Option[] => {
|
|
12
|
+
const result = [...options]
|
|
13
|
+
for (const option of extra) {
|
|
14
|
+
const shadowed = result.some(it => it.name === option.name || (option.shortcut !== undefined && it.shortcut === option.shortcut))
|
|
15
|
+
if (!shadowed) {
|
|
16
|
+
result.push(option)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return result
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
export enum OptionMode {
|
|
11
23
|
VALUE_NONE = 'value_none',
|
|
12
24
|
VALUE_OPTIONAL = 'value_optional',
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/// <reference path="../../types/chalk-table/index.d.ts" />
|
|
2
|
+
|
|
3
|
+
import chalk from 'chalk'
|
|
4
|
+
import chalkTable from 'chalk-table'
|
|
5
|
+
import { Writable } from 'node:stream'
|
|
6
|
+
import type { GlobalOptionValues } from './GlobalOptions.js'
|
|
7
|
+
|
|
8
|
+
export type OutputMode = 'human' | 'json' | 'quiet'
|
|
9
|
+
|
|
10
|
+
let supportedChalkLevel = chalk.level
|
|
11
|
+
|
|
12
|
+
export interface OutputStream {
|
|
13
|
+
write(text: string): void
|
|
14
|
+
readonly isTty: boolean
|
|
15
|
+
readonly columns?: number
|
|
16
|
+
onError?(listener: (error: unknown) => void): void
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface OutputOptions {
|
|
20
|
+
stdout?: OutputStream
|
|
21
|
+
stderr?: OutputStream
|
|
22
|
+
isStdinTty?: () => boolean
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PromptOutputStream extends Writable {
|
|
26
|
+
readonly isTTY: boolean
|
|
27
|
+
readonly columns: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type OutputScalar = string | number | boolean | bigint | null | undefined
|
|
31
|
+
|
|
32
|
+
export interface OutputDataOptions<T> {
|
|
33
|
+
human?: (value: T) => string
|
|
34
|
+
quiet?: (value: T) => OutputScalar | readonly OutputScalar[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Per field column specs, so that each `format` sees the exact type of its own field. Indexed by the
|
|
39
|
+
* field name only to let {@link Output.table} render a cell without widening the union.
|
|
40
|
+
*/
|
|
41
|
+
export type OutputTableColumns<T> = {
|
|
42
|
+
[K in keyof T & string]: {
|
|
43
|
+
field: K
|
|
44
|
+
name: string
|
|
45
|
+
/** Human mode only — `--json` still emits the raw row and `--quiet` the raw value. */
|
|
46
|
+
format?: (value: T[K], row: T) => string
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type OutputTableColumn<T> = OutputTableColumns<T>[keyof T & string]
|
|
51
|
+
|
|
52
|
+
export const resolveOutputMode = (globals: GlobalOptionValues): OutputMode => {
|
|
53
|
+
if (globals.json) {
|
|
54
|
+
return 'json'
|
|
55
|
+
}
|
|
56
|
+
if (globals.quiet) {
|
|
57
|
+
return 'quiet'
|
|
58
|
+
}
|
|
59
|
+
return 'human'
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The single output channel of the CLI: stdout carries data, stderr carries everything a human reads.
|
|
64
|
+
*
|
|
65
|
+
* The instance is created before argv is parsed, so the mode is mutable and set by `Application` once
|
|
66
|
+
* the global options are known. Every consumer holds the same instance.
|
|
67
|
+
*/
|
|
68
|
+
export class Output {
|
|
69
|
+
private currentMode: OutputMode = 'human'
|
|
70
|
+
private progressVisible = false
|
|
71
|
+
private readonly stdout: OutputStream
|
|
72
|
+
private readonly stderr: OutputStream
|
|
73
|
+
private readonly stdinIsTty: () => boolean
|
|
74
|
+
private readonly promptStderr: PromptOutputStream
|
|
75
|
+
|
|
76
|
+
constructor(options: OutputOptions = {}) {
|
|
77
|
+
this.stdout = options.stdout ?? createProcessStream(process.stdout)
|
|
78
|
+
this.stderr = options.stderr ?? createProcessStream(process.stderr)
|
|
79
|
+
this.stdinIsTty = options.isStdinTty ?? (() => process.stdin.isTTY === true)
|
|
80
|
+
this.promptStderr = options.stderr === undefined ? process.stderr : createPromptStream(this.stderr)
|
|
81
|
+
this.stdout.onError?.(handleStreamError)
|
|
82
|
+
this.stderr.onError?.(handleStreamError)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public get mode(): OutputMode {
|
|
86
|
+
return this.currentMode
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public get isJson(): boolean {
|
|
90
|
+
return this.currentMode === 'json'
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public get isQuiet(): boolean {
|
|
94
|
+
return this.currentMode === 'quiet'
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Set by `Application` once the global options are parsed. */
|
|
98
|
+
public setMode(mode: OutputMode): void {
|
|
99
|
+
this.currentMode = mode
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public applyGlobalOptions(globals: GlobalOptionValues): void {
|
|
103
|
+
this.setMode(resolveOutputMode(globals))
|
|
104
|
+
if (globals.color && this.currentMode === 'human') {
|
|
105
|
+
if (chalk.level === 0) {
|
|
106
|
+
chalk.level = supportedChalkLevel
|
|
107
|
+
} else {
|
|
108
|
+
supportedChalkLevel = chalk.level
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
if (chalk.level !== 0) {
|
|
112
|
+
supportedChalkLevel = chalk.level
|
|
113
|
+
}
|
|
114
|
+
chalk.level = 0
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// DATA → stdout
|
|
119
|
+
|
|
120
|
+
/** Prints command data with optional mode-specific projections. JSON always receives the raw value. */
|
|
121
|
+
public data<T>(value: T, human?: (value: T) => string): void
|
|
122
|
+
public data<T>(value: T, options?: OutputDataOptions<T>): void
|
|
123
|
+
public data<T>(value: T, rendering?: ((value: T) => string) | OutputDataOptions<T>): void {
|
|
124
|
+
const human = typeof rendering === 'function' ? rendering : rendering?.human
|
|
125
|
+
if (this.currentMode === 'json') {
|
|
126
|
+
this.writeStdout(JSON.stringify(value ?? null, null, 2))
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
if (this.currentMode === 'quiet') {
|
|
130
|
+
const quietValue = typeof rendering === 'object' && rendering.quiet ? rendering.quiet(value) : value
|
|
131
|
+
for (const line of toScalarLines(quietValue)) {
|
|
132
|
+
this.writeStdout(line)
|
|
133
|
+
}
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
this.writeStdout(human ? human(value) : JSON.stringify(value ?? null, null, 2))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public table<T>(columns: OutputTableColumn<T>[], rows: T[], quietField?: keyof T & string): void {
|
|
140
|
+
if (this.currentMode === 'json') {
|
|
141
|
+
this.writeStdout(JSON.stringify(rows, null, 2))
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
if (this.currentMode === 'quiet') {
|
|
145
|
+
const field = quietField ?? columns[0]?.field
|
|
146
|
+
if (field === undefined) {
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
for (const row of rows) {
|
|
150
|
+
this.writeStdout(formatCell(row[field]))
|
|
151
|
+
}
|
|
152
|
+
return
|
|
153
|
+
}
|
|
154
|
+
if (rows.length === 0) {
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
const data: Record<string, string>[] = rows.map(row => {
|
|
158
|
+
const cells: Record<string, string> = {}
|
|
159
|
+
for (const column of columns) {
|
|
160
|
+
cells[column.field] = renderTableCell(column, row)
|
|
161
|
+
}
|
|
162
|
+
return cells
|
|
163
|
+
})
|
|
164
|
+
this.writeStdout(chalkTable<string>({ columns: columns.map(it => ({ field: it.field, name: it.name })) }, data))
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// DIAGNOSTICS → stderr
|
|
168
|
+
|
|
169
|
+
public info(message: string): void {
|
|
170
|
+
this.writeDiagnostic(message)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
public warn(message: string): void {
|
|
174
|
+
this.writeDiagnostic(message, text => chalk.yellow(text))
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public error(message: string): void {
|
|
178
|
+
this.writeDiagnostic(message, text => chalk.red(text))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// PROGRESS → stderr
|
|
182
|
+
|
|
183
|
+
public progress(message: string): void {
|
|
184
|
+
if (this.currentMode !== 'human' || !this.stderr.isTty) {
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
this.clearProgress()
|
|
188
|
+
writeStream(this.stderr, escapeTerminalText(message))
|
|
189
|
+
this.progressVisible = true
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
public clearProgress(): void {
|
|
193
|
+
if (!this.progressVisible) {
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
this.progressVisible = false
|
|
197
|
+
writeStream(this.stderr, '\r\u001b[K')
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** False unless both sides of an interactive prompt are TTYs in human mode. */
|
|
201
|
+
public canPrompt(): boolean {
|
|
202
|
+
return this.currentMode === 'human' && this.stdinIsTty() && this.stderr.isTty
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** A real Writable suitable for the `stdout` option of interactive prompt libraries. */
|
|
206
|
+
public get promptOutput(): PromptOutputStream {
|
|
207
|
+
return this.promptStderr
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Writes a line to stderr regardless of the mode. Reserved for content the user explicitly asked for
|
|
212
|
+
* (help) or must always see (a fatal error) — everything else belongs to the methods above.
|
|
213
|
+
*/
|
|
214
|
+
public writeStderr(text: string): void {
|
|
215
|
+
this.clearProgress()
|
|
216
|
+
writeStream(this.stderr, text + '\n')
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private writeStdout(text: string): void {
|
|
220
|
+
this.clearProgress()
|
|
221
|
+
writeStream(this.stdout, text + '\n')
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private writeDiagnostic(message: string, format: (text: string) => string = text => text): void {
|
|
225
|
+
if (this.currentMode !== 'human') {
|
|
226
|
+
return
|
|
227
|
+
}
|
|
228
|
+
this.clearProgress()
|
|
229
|
+
writeStream(this.stderr, format(escapeTerminalText(message)) + '\n')
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const createProcessStream = (stream: NodeJS.WriteStream): OutputStream => ({
|
|
234
|
+
write: text => {
|
|
235
|
+
stream.write(text)
|
|
236
|
+
},
|
|
237
|
+
get isTty() {
|
|
238
|
+
return stream.isTTY === true
|
|
239
|
+
},
|
|
240
|
+
get columns() {
|
|
241
|
+
return stream.columns
|
|
242
|
+
},
|
|
243
|
+
onError: listener => {
|
|
244
|
+
stream.on('error', error => listener(error))
|
|
245
|
+
},
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
class PromptWritable extends Writable {
|
|
249
|
+
public readonly isTTY: boolean
|
|
250
|
+
|
|
251
|
+
constructor(private readonly stream: OutputStream) {
|
|
252
|
+
super({ decodeStrings: false })
|
|
253
|
+
this.isTTY = stream.isTty
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
public get columns(): number {
|
|
257
|
+
return this.stream.columns ?? 80
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
public override _write(chunk: string | Uint8Array, encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
|
|
261
|
+
try {
|
|
262
|
+
writeStream(this.stream, typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString(encoding))
|
|
263
|
+
callback()
|
|
264
|
+
} catch (error) {
|
|
265
|
+
callback(error instanceof Error ? error : new Error(String(error)))
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const createPromptStream = (stream: OutputStream): PromptOutputStream => new PromptWritable(stream)
|
|
271
|
+
|
|
272
|
+
const writeStream = (stream: OutputStream, text: string): void => {
|
|
273
|
+
try {
|
|
274
|
+
stream.write(text)
|
|
275
|
+
} catch (error) {
|
|
276
|
+
handleStreamError(error)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const handleStreamError = (error: unknown): void => {
|
|
281
|
+
if (typeof error === 'object' && error !== null && 'code' in error && error.code === 'EPIPE') {
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
throw error
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Removes terminal control characters from untrusted human-readable text. */
|
|
288
|
+
export const escapeTerminalText = (value: string): string => {
|
|
289
|
+
let escaped = ''
|
|
290
|
+
for (const character of value) {
|
|
291
|
+
if (!isTerminalControl(character)) {
|
|
292
|
+
escaped += character
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return escaped
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const isTerminalControl = (character: string): boolean => {
|
|
299
|
+
const codePoint = character.codePointAt(0)
|
|
300
|
+
return codePoint !== undefined && (codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f))
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// generic in the field so that `format` is a single signature here, not a union of them
|
|
304
|
+
const renderTableCell = <T, K extends keyof T & string>(column: OutputTableColumns<T>[K], row: T): string =>
|
|
305
|
+
column.format ? column.format(row[column.field], row) : formatCell(row[column.field])
|
|
306
|
+
|
|
307
|
+
const formatCell = (value: unknown): string => {
|
|
308
|
+
if (value === undefined || value === null) {
|
|
309
|
+
return ''
|
|
310
|
+
}
|
|
311
|
+
if (typeof value === 'string') {
|
|
312
|
+
return escapeTerminalText(value)
|
|
313
|
+
}
|
|
314
|
+
if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') {
|
|
315
|
+
return String(value)
|
|
316
|
+
}
|
|
317
|
+
return JSON.stringify(value) ?? ''
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const toScalarLines = (value: unknown): string[] => {
|
|
321
|
+
const values: unknown[] = Array.isArray(value) ? value : [value]
|
|
322
|
+
if (!values.every(isOutputScalar)) {
|
|
323
|
+
throw new TypeError('Quiet output requires a scalar value or an array of scalar values. Pass an explicit quiet projection for structured data.')
|
|
324
|
+
}
|
|
325
|
+
return values.filter(it => it !== undefined && it !== null).map(formatCell)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const isOutputScalar = (value: unknown): value is OutputScalar =>
|
|
329
|
+
value === null || value === undefined || ['string', 'number', 'boolean', 'bigint'].includes(typeof value)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ends the process with the given exit code without losing buffered output.
|
|
3
|
+
*
|
|
4
|
+
* `process.exit()` terminates immediately and discards whatever `process.stdout` has not yet handed
|
|
5
|
+
* to the OS. Writes to a pipe are asynchronous, so anything past the 64 KB pipe buffer was silently
|
|
6
|
+
* dropped — `contember commands --json | jq` got a truncated document while the same command
|
|
7
|
+
* redirected to a file was complete. Assigning `exitCode` instead lets the runtime exit on its own
|
|
8
|
+
* once the streams have drained, which also covers output written directly through `console.log`.
|
|
9
|
+
*/
|
|
10
|
+
export const exitProcess = (code: number): void => {
|
|
11
|
+
process.exitCode = code
|
|
12
|
+
}
|
package/src/application/index.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
export * from './Application.js'
|
|
2
2
|
export * from './Argument.js'
|
|
3
3
|
export * from './assertNever.js'
|
|
4
|
+
export * from './CliError.js'
|
|
4
5
|
export * from './Command.js'
|
|
5
6
|
export * from './CommandManager.js'
|
|
6
7
|
export * from './CommandConfiguration.js'
|
|
8
|
+
export * from './ErrorRenderer.js'
|
|
9
|
+
export * from './exitProcess.js'
|
|
10
|
+
export * from './GlobalOptions.js'
|
|
7
11
|
export * from './Input.js'
|
|
8
12
|
export * from './InputParser.js'
|
|
9
13
|
export * from './Option.js'
|
|
14
|
+
export * from './Output.js'
|
|
10
15
|
export * from './UsageFormatter.js'
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,42 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
|
|
1
|
+
export type {
|
|
2
|
+
Argument,
|
|
3
|
+
CommandCatalogEntry,
|
|
4
|
+
CommandFactory,
|
|
5
|
+
CommandFactoryList,
|
|
6
|
+
CommandResolution,
|
|
7
|
+
GlobalOptionValues,
|
|
8
|
+
InputParserResult,
|
|
9
|
+
JsonValue,
|
|
10
|
+
Option,
|
|
11
|
+
OutputDataOptions,
|
|
12
|
+
OutputMode,
|
|
13
|
+
OutputOptions,
|
|
14
|
+
OutputScalar,
|
|
15
|
+
OutputStream,
|
|
16
|
+
OutputTableColumn,
|
|
17
|
+
OutputTableColumns,
|
|
18
|
+
ParsedCommandInput,
|
|
19
|
+
PromptOutputStream,
|
|
20
|
+
} from './application/index.js'
|
|
21
|
+
export {
|
|
22
|
+
Application,
|
|
23
|
+
CliError,
|
|
24
|
+
Command,
|
|
25
|
+
CommandConfiguration,
|
|
26
|
+
CommandManager,
|
|
27
|
+
escapeTerminalText,
|
|
28
|
+
ExitCode,
|
|
29
|
+
exitProcess,
|
|
30
|
+
globalOptions,
|
|
31
|
+
Input,
|
|
32
|
+
InvalidConfigurationError,
|
|
33
|
+
InvalidInputError,
|
|
34
|
+
mergeOptions,
|
|
35
|
+
OptionMode,
|
|
36
|
+
Output,
|
|
37
|
+
readGlobalOptionsFromArgs,
|
|
38
|
+
renderCliError,
|
|
39
|
+
toCliError,
|
|
40
|
+
} from './application/index.js'
|
|
3
41
|
export * from './npm/index.js'
|
|
4
42
|
export * from './utils/index.js'
|