@contember/cli-common 2.1.0-rc.3 → 2.2.0-alpha.10
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/UsageFormatter.cjs.map +1 -1
- package/dist/development/src/application/UsageFormatter.js.map +1 -1
- 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/Package.cjs.map +1 -1
- package/dist/development/src/npm/Package.js.map +1 -1
- package/dist/development/src/npm/PackageWorkspace.cjs +3 -3
- package/dist/development/src/npm/PackageWorkspace.cjs.map +1 -1
- package/dist/development/src/npm/PackageWorkspace.js.map +1 -1
- package/dist/development/src/npm/PackageWorkspaceResolver.cjs +5 -5
- package/dist/development/src/npm/PackageWorkspaceResolver.cjs.map +1 -1
- package/dist/development/src/npm/PackageWorkspaceResolver.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Bun.cjs +3 -4
- 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 +3 -4
- 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/PackageManagerHelpers.cjs +3 -3
- package/dist/development/src/npm/packageManagers/PackageManagerHelpers.cjs.map +1 -1
- package/dist/development/src/npm/packageManagers/PackageManagerHelpers.js.map +1 -1
- package/dist/development/src/npm/packageManagers/Pnpm.cjs +5 -6
- 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 +3 -4
- 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 +3 -4
- 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/development/src/utils/findPackageRoot.cjs +3 -3
- package/dist/development/src/utils/findPackageRoot.cjs.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/UsageFormatter.cjs.map +1 -1
- package/dist/production/src/application/UsageFormatter.js.map +1 -1
- 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/Package.cjs.map +1 -1
- package/dist/production/src/npm/Package.js.map +1 -1
- package/dist/production/src/npm/PackageWorkspace.cjs +3 -3
- package/dist/production/src/npm/PackageWorkspace.cjs.map +1 -1
- package/dist/production/src/npm/PackageWorkspace.js.map +1 -1
- package/dist/production/src/npm/PackageWorkspaceResolver.cjs +5 -5
- package/dist/production/src/npm/PackageWorkspaceResolver.cjs.map +1 -1
- package/dist/production/src/npm/PackageWorkspaceResolver.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Bun.cjs +3 -4
- 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 +3 -4
- 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/PackageManagerHelpers.cjs +3 -3
- package/dist/production/src/npm/packageManagers/PackageManagerHelpers.cjs.map +1 -1
- package/dist/production/src/npm/packageManagers/PackageManagerHelpers.js.map +1 -1
- package/dist/production/src/npm/packageManagers/Pnpm.cjs +5 -6
- 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 +3 -4
- 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 +3 -4
- 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/production/src/utils/findPackageRoot.cjs +3 -3
- package/dist/production/src/utils/findPackageRoot.cjs.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 +14 -4
- package/dist/types/application/Command.d.ts.map +1 -1
- package/dist/types/application/CommandConfiguration.d.ts +10 -7
- package/dist/types/application/CommandConfiguration.d.ts.map +1 -1
- package/dist/types/application/CommandManager.d.ts +41 -3
- 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 +8 -4
- 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 +2 -2
- 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 +15 -10
- package/dist/types/application/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +4 -4
- 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 +1 -1
- package/dist/types/npm/Package.d.ts.map +1 -1
- package/dist/types/npm/PackageWorkspace.d.ts +3 -3
- package/dist/types/npm/PackageWorkspace.d.ts.map +1 -1
- package/dist/types/npm/PackageWorkspaceResolver.d.ts +3 -3
- package/dist/types/npm/PackageWorkspaceResolver.d.ts.map +1 -1
- package/dist/types/npm/index.d.ts +8 -8
- package/dist/types/npm/index.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Bun.d.ts +5 -5
- package/dist/types/npm/packageManagers/Bun.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Npm.d.ts +5 -5
- package/dist/types/npm/packageManagers/Npm.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/PackageManager.d.ts +2 -2
- package/dist/types/npm/packageManagers/PackageManager.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/PackageManagerHelpers.d.ts +3 -3
- package/dist/types/npm/packageManagers/PackageManagerHelpers.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Pnpm.d.ts +5 -5
- package/dist/types/npm/packageManagers/Pnpm.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/Yarn.d.ts +5 -5
- package/dist/types/npm/packageManagers/Yarn.d.ts.map +1 -1
- package/dist/types/npm/packageManagers/YarnClassic.d.ts +5 -5
- 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/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/index.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/application/Application.ts +138 -76
- package/src/application/CliError.ts +117 -0
- package/src/application/Command.ts +29 -8
- package/src/application/CommandConfiguration.ts +26 -8
- package/src/application/CommandManager.ts +173 -23
- 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 +16 -7
- package/src/application/Option.ts +12 -0
- package/src/application/Output.ts +329 -0
- package/src/application/UsageFormatter.ts +3 -3
- package/src/application/exitProcess.ts +12 -0
- package/src/application/index.ts +15 -10
- package/src/index.ts +42 -4
- package/src/npm/CommandRunner.ts +166 -23
- package/src/npm/Package.ts +1 -1
- package/src/npm/PackageWorkspace.ts +4 -4
- package/src/npm/PackageWorkspaceResolver.ts +9 -9
- package/src/npm/index.ts +8 -8
- package/src/npm/packageManagers/Bun.ts +7 -8
- package/src/npm/packageManagers/Npm.ts +7 -8
- package/src/npm/packageManagers/PackageManager.ts +2 -2
- package/src/npm/packageManagers/PackageManagerHelpers.ts +3 -3
- package/src/npm/packageManagers/Pnpm.ts +7 -8
- package/src/npm/packageManagers/Yarn.ts +7 -8
- package/src/npm/packageManagers/YarnClassic.ts +7 -8
- package/src/utils/index.ts +1 -1
- 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,393 @@
|
|
|
1
|
+
import { beforeAll, describe, expect, test } from 'bun:test'
|
|
2
|
+
import chalk from 'chalk'
|
|
3
|
+
import { escapeTerminalText, Output, OutputStream, OutputTableColumn } from '../../../src/index.js'
|
|
4
|
+
import { CapturingStream, createTestOutput } from '../../lib/testOutput.js'
|
|
5
|
+
|
|
6
|
+
beforeAll(() => {
|
|
7
|
+
// deterministic assertions regardless of the terminal the suite runs in
|
|
8
|
+
chalk.level = 0
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
describe('Output.data', () => {
|
|
12
|
+
test('human mode uses the formatter', () => {
|
|
13
|
+
const { output, stdout, stderr } = createTestOutput()
|
|
14
|
+
output.setMode('human')
|
|
15
|
+
output.data('1.2.3', it => it)
|
|
16
|
+
expect(stdout.text).toBe('1.2.3\n')
|
|
17
|
+
expect(stderr.text).toBe('')
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('human mode falls back to JSON when no formatter is given', () => {
|
|
21
|
+
const { output, stdout } = createTestOutput()
|
|
22
|
+
output.setMode('human')
|
|
23
|
+
output.data({ a: 1 })
|
|
24
|
+
expect(stdout.text).toBe('{\n "a": 1\n}\n')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('json mode ignores the formatter', () => {
|
|
28
|
+
const { output, stdout } = createTestOutput()
|
|
29
|
+
output.setMode('json')
|
|
30
|
+
output.data('1.2.3', () => 'human form')
|
|
31
|
+
expect(stdout.text).toBe('"1.2.3"\n')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('quiet mode prints bare scalars, one per line', () => {
|
|
35
|
+
const { output, stdout } = createTestOutput()
|
|
36
|
+
output.setMode('quiet')
|
|
37
|
+
output.data(['a', 'b', 'c'], () => 'human form')
|
|
38
|
+
expect(stdout.text).toBe('a\nb\nc\n')
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('quiet mode prints a bare scalar', () => {
|
|
42
|
+
const { output, stdout } = createTestOutput()
|
|
43
|
+
output.setMode('quiet')
|
|
44
|
+
output.data('1.2.3', it => it)
|
|
45
|
+
expect(stdout.text).toBe('1.2.3\n')
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('quiet mode uses an explicit typed projection', () => {
|
|
49
|
+
const { output, stdout } = createTestOutput()
|
|
50
|
+
output.setMode('quiet')
|
|
51
|
+
output.data({ id: 'project-1', name: 'Blog' }, {
|
|
52
|
+
human: value => value.name,
|
|
53
|
+
quiet: value => value.id,
|
|
54
|
+
})
|
|
55
|
+
expect(stdout.text).toBe('project-1\n')
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('quiet mode rejects an object before writing stdout', () => {
|
|
59
|
+
const { output, stdout } = createTestOutput()
|
|
60
|
+
output.setMode('quiet')
|
|
61
|
+
expect(() => output.data({ id: 'project-1' })).toThrow('Quiet output requires a scalar value')
|
|
62
|
+
expect(stdout.text).toBe('')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('quiet mode rejects an object array before writing any stdout', () => {
|
|
66
|
+
const { output, stdout } = createTestOutput()
|
|
67
|
+
output.setMode('quiet')
|
|
68
|
+
expect(() => output.data(['first', { id: 'project-1' }])).toThrow('Quiet output requires a scalar value')
|
|
69
|
+
expect(stdout.text).toBe('')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('json mode ignores all projections', () => {
|
|
73
|
+
const { output, stdout } = createTestOutput()
|
|
74
|
+
const value = { id: 'project-1', name: 'Blog' }
|
|
75
|
+
output.setMode('json')
|
|
76
|
+
output.data(value, { human: item => item.name, quiet: item => item.id })
|
|
77
|
+
expect(JSON.parse(stdout.text)).toStrictEqual(value)
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
describe('Output.table', () => {
|
|
82
|
+
interface Row {
|
|
83
|
+
id: string
|
|
84
|
+
name: string
|
|
85
|
+
}
|
|
86
|
+
const columns: { field: keyof Row & string; name: string }[] = [{ field: 'id', name: 'ID' }, { field: 'name', name: 'Name' }]
|
|
87
|
+
const rows: Row[] = [{ id: '1', name: 'first' }, { id: '2', name: 'second' }]
|
|
88
|
+
|
|
89
|
+
test('human mode renders a table on stdout', () => {
|
|
90
|
+
const { output, stdout, stderr } = createTestOutput()
|
|
91
|
+
output.setMode('human')
|
|
92
|
+
output.table(columns, rows)
|
|
93
|
+
expect(stdout.text).toContain('first')
|
|
94
|
+
expect(stdout.text).toContain('Name')
|
|
95
|
+
expect(stderr.text).toBe('')
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('json mode prints the bare row array', () => {
|
|
99
|
+
const { output, stdout } = createTestOutput()
|
|
100
|
+
output.setMode('json')
|
|
101
|
+
output.table(columns, rows)
|
|
102
|
+
expect(JSON.parse(stdout.text)).toStrictEqual(rows)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('quiet mode prints the quiet column only', () => {
|
|
106
|
+
const { output, stdout } = createTestOutput()
|
|
107
|
+
output.setMode('quiet')
|
|
108
|
+
output.table(columns, rows, 'name')
|
|
109
|
+
expect(stdout.text).toBe('first\nsecond\n')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('quiet mode falls back to the first column', () => {
|
|
113
|
+
const { output, stdout } = createTestOutput()
|
|
114
|
+
output.setMode('quiet')
|
|
115
|
+
output.table(columns, rows)
|
|
116
|
+
expect(stdout.text).toBe('1\n2\n')
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
describe('Output.table column formatter', () => {
|
|
121
|
+
interface EventRow {
|
|
122
|
+
id: string
|
|
123
|
+
numRetries: number
|
|
124
|
+
log: { message: string }[]
|
|
125
|
+
}
|
|
126
|
+
// each formatter sees the exact type of its own field — no narrowing, no cast
|
|
127
|
+
const columns: OutputTableColumn<EventRow>[] = [
|
|
128
|
+
{ field: 'id', name: 'ID', format: value => `#${value}` },
|
|
129
|
+
{ field: 'numRetries', name: 'Retries' },
|
|
130
|
+
{ field: 'log', name: 'Log', format: (value, row) => `${row.id}:${value[value.length - 1]?.message ?? '(none)'}` },
|
|
131
|
+
]
|
|
132
|
+
const rows: EventRow[] = [
|
|
133
|
+
{ id: '1', numRetries: 2, log: [{ message: 'first' }, { message: 'last' }] },
|
|
134
|
+
{ id: '2', numRetries: 0, log: [] },
|
|
135
|
+
]
|
|
136
|
+
|
|
137
|
+
test('human mode applies the formatter and leaves unformatted columns raw', () => {
|
|
138
|
+
const { output, stdout } = createTestOutput()
|
|
139
|
+
output.setMode('human')
|
|
140
|
+
output.table(columns, rows)
|
|
141
|
+
expect(stdout.text).toContain('#1')
|
|
142
|
+
expect(stdout.text).toContain('1:last')
|
|
143
|
+
expect(stdout.text).toContain('2:(none)')
|
|
144
|
+
// the summarised column must not leak the whole structure into the cell
|
|
145
|
+
expect(stdout.text).not.toContain('first')
|
|
146
|
+
// a column without a formatter still renders the raw value (numRetries of the second row)
|
|
147
|
+
expect(stdout.text).toContain('0')
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('json mode keeps the raw rows, ignoring the formatter', () => {
|
|
151
|
+
const { output, stdout } = createTestOutput()
|
|
152
|
+
output.setMode('json')
|
|
153
|
+
output.table(columns, rows)
|
|
154
|
+
expect(JSON.parse(stdout.text)).toStrictEqual(rows)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('quiet mode keeps the raw value of the quiet column, ignoring the formatter', () => {
|
|
158
|
+
const { output, stdout } = createTestOutput()
|
|
159
|
+
output.setMode('quiet')
|
|
160
|
+
output.table(columns, rows, 'id')
|
|
161
|
+
expect(stdout.text).toBe('1\n2\n')
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('the v1 column shape without a formatter still compiles and renders', () => {
|
|
165
|
+
const legacyColumns: { field: keyof EventRow & string; name: string }[] = [{ field: 'id', name: 'ID' }]
|
|
166
|
+
const { output, stdout } = createTestOutput()
|
|
167
|
+
output.setMode('human')
|
|
168
|
+
output.table(legacyColumns, rows)
|
|
169
|
+
expect(stdout.text).toContain('ID')
|
|
170
|
+
expect(stdout.text).not.toContain('#1')
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
describe('Output diagnostics', () => {
|
|
175
|
+
test('go to stderr in human mode', () => {
|
|
176
|
+
const { output, stdout, stderr } = createTestOutput()
|
|
177
|
+
output.setMode('human')
|
|
178
|
+
output.info('info message')
|
|
179
|
+
output.warn('warn message')
|
|
180
|
+
output.error('error message')
|
|
181
|
+
expect(stdout.text).toBe('')
|
|
182
|
+
expect(stderr.lines).toStrictEqual(['info message', 'warn message', 'error message'])
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
test('are suppressed in json mode', () => {
|
|
186
|
+
const { output, stdout, stderr } = createTestOutput()
|
|
187
|
+
output.setMode('json')
|
|
188
|
+
output.info('info message')
|
|
189
|
+
output.data({ a: 1 })
|
|
190
|
+
expect(JSON.parse(stdout.text)).toStrictEqual({ a: 1 })
|
|
191
|
+
expect(stderr.text).toBe('')
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('are suppressed in quiet mode', () => {
|
|
195
|
+
const { output, stderr } = createTestOutput()
|
|
196
|
+
output.setMode('quiet')
|
|
197
|
+
output.info('info message')
|
|
198
|
+
output.warn('warn message')
|
|
199
|
+
output.error('error message')
|
|
200
|
+
expect(stderr.text).toBe('')
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
test('writeStderr is never suppressed', () => {
|
|
204
|
+
const { output, stdout, stderr } = createTestOutput()
|
|
205
|
+
output.setMode('quiet')
|
|
206
|
+
output.writeStderr('always visible')
|
|
207
|
+
expect(stdout.text).toBe('')
|
|
208
|
+
expect(stderr.text).toBe('always visible\n')
|
|
209
|
+
})
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
describe('terminal escaping', () => {
|
|
213
|
+
test('removes C0, C1, and escape controls', () => {
|
|
214
|
+
expect(escapeTerminalText('a\u0000b\u001bc\u0085d')).toBe('abcd')
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
test('preserves printable boundary, Unicode, and astral characters', () => {
|
|
218
|
+
expect(escapeTerminalText('\u001f \u007e\u007f\u0080\u009f\u00a0Příliš žluťoučký 🐘')).toBe(' \u007e\u00a0Příliš žluťoučký 🐘')
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('sanitizes untrusted table and diagnostic values in human mode', () => {
|
|
222
|
+
const { output, stdout, stderr } = createTestOutput()
|
|
223
|
+
output.setMode('human')
|
|
224
|
+
output.table([{ field: 'value', name: 'Value' }], [{ value: 'cell\u0007\u001b]8;;https://example.test' }])
|
|
225
|
+
output.warn('warning\u001b[2J')
|
|
226
|
+
expect(stdout.text).not.toContain('\u001b')
|
|
227
|
+
expect(stdout.text).not.toContain('\u0007')
|
|
228
|
+
expect(stderr.text).not.toContain('\u001b')
|
|
229
|
+
expect(stdout.text).toContain('cell]8;;https://example.test')
|
|
230
|
+
expect(stderr.text).toContain('warning[2J')
|
|
231
|
+
})
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
class StreamWriteError extends Error {
|
|
235
|
+
constructor(public readonly code: string) {
|
|
236
|
+
super(code)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
class FailingStream implements OutputStream {
|
|
241
|
+
public readonly isTty = false
|
|
242
|
+
|
|
243
|
+
constructor(private readonly error: Error) {}
|
|
244
|
+
|
|
245
|
+
public write(): void {
|
|
246
|
+
throw this.error
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
class ErrorEmittingStream implements OutputStream {
|
|
251
|
+
public readonly isTty = false
|
|
252
|
+
private readonly listeners: ((error: unknown) => void)[] = []
|
|
253
|
+
|
|
254
|
+
public write(): void {
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
public onError(listener: (error: unknown) => void): void {
|
|
258
|
+
this.listeners.push(listener)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
public emitError(error: unknown): void {
|
|
262
|
+
for (const listener of this.listeners) {
|
|
263
|
+
listener(error)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
describe('stream errors', () => {
|
|
269
|
+
test('treats an injected EPIPE as a closed consumer', () => {
|
|
270
|
+
const output = new Output({ stdout: new FailingStream(new StreamWriteError('EPIPE')) })
|
|
271
|
+
expect(() => output.data('ignored', value => value)).not.toThrow()
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
test('does not hide unrelated injected stream errors', () => {
|
|
275
|
+
const output = new Output({ stdout: new FailingStream(new StreamWriteError('EIO')) })
|
|
276
|
+
expect(() => output.data('broken', value => value)).toThrow('EIO')
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
test('handles asynchronous stream errors through the injected error channel', () => {
|
|
280
|
+
const stream = new ErrorEmittingStream()
|
|
281
|
+
new Output({ stdout: stream })
|
|
282
|
+
expect(() => stream.emitError(new StreamWriteError('EPIPE'))).not.toThrow()
|
|
283
|
+
expect(() => stream.emitError(new StreamWriteError('EIO'))).toThrow('EIO')
|
|
284
|
+
})
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
describe('Output.progress', () => {
|
|
288
|
+
test('is written only on a TTY in human mode', () => {
|
|
289
|
+
const { output, stderr } = createTestOutput({ stderrTty: true })
|
|
290
|
+
output.setMode('human')
|
|
291
|
+
output.progress('working')
|
|
292
|
+
expect(stderr.text).toBe('working')
|
|
293
|
+
output.clearProgress()
|
|
294
|
+
expect(stderr.text).toBe('working\r\u001b[K')
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
test('is skipped when stderr is not a TTY', () => {
|
|
298
|
+
const { output, stderr } = createTestOutput({ stderrTty: false })
|
|
299
|
+
output.setMode('human')
|
|
300
|
+
output.progress('working')
|
|
301
|
+
expect(stderr.text).toBe('')
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
test('is skipped in json mode even on a TTY', () => {
|
|
305
|
+
const { output, stderr } = createTestOutput({ stderrTty: true })
|
|
306
|
+
output.setMode('json')
|
|
307
|
+
output.progress('working')
|
|
308
|
+
expect(stderr.text).toBe('')
|
|
309
|
+
})
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
describe('Output.canPrompt', () => {
|
|
313
|
+
test('is true only in human mode when stdin and diagnostic output are TTYs', () => {
|
|
314
|
+
const { output } = createTestOutput({ stdinTty: true, stderrTty: true })
|
|
315
|
+
output.setMode('human')
|
|
316
|
+
expect(output.canPrompt()).toBe(true)
|
|
317
|
+
output.setMode('json')
|
|
318
|
+
expect(output.canPrompt()).toBe(false)
|
|
319
|
+
output.setMode('quiet')
|
|
320
|
+
expect(output.canPrompt()).toBe(false)
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
test('is false when stdin is not a TTY', () => {
|
|
324
|
+
const { output } = createTestOutput({ stdinTty: false, stderrTty: true })
|
|
325
|
+
output.setMode('human')
|
|
326
|
+
expect(output.canPrompt()).toBe(false)
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
test('is false when diagnostic output is not a TTY', () => {
|
|
330
|
+
const { output } = createTestOutput({ stdinTty: true, stderrTty: false })
|
|
331
|
+
output.setMode('human')
|
|
332
|
+
expect(output.canPrompt()).toBe(false)
|
|
333
|
+
})
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
describe('Output.promptOutput', () => {
|
|
337
|
+
test('uses the real process stderr by default', () => {
|
|
338
|
+
const output = new Output()
|
|
339
|
+
expect(output.promptOutput).toBe(process.stderr)
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
test('adapts an injected diagnostic stream to a Writable with TTY metadata', async () => {
|
|
343
|
+
const stderr = new CapturingStream(true, 132)
|
|
344
|
+
const output = new Output({ stderr })
|
|
345
|
+
|
|
346
|
+
expect(output.promptOutput.writable).toBe(true)
|
|
347
|
+
expect(output.promptOutput.isTTY).toBe(true)
|
|
348
|
+
expect(output.promptOutput.columns).toBe(132)
|
|
349
|
+
|
|
350
|
+
await new Promise<void>((resolve, reject) => {
|
|
351
|
+
output.promptOutput.write('prompt text', error => error ? reject(error) : resolve())
|
|
352
|
+
})
|
|
353
|
+
expect(stderr.text).toBe('prompt text')
|
|
354
|
+
})
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
describe('Output.applyGlobalOptions', () => {
|
|
358
|
+
test('picks json over quiet', () => {
|
|
359
|
+
const { output } = createTestOutput()
|
|
360
|
+
output.applyGlobalOptions({ json: true, quiet: true, color: true, help: false })
|
|
361
|
+
expect(output.mode).toBe('json')
|
|
362
|
+
expect(output.isJson).toBe(true)
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
test('picks quiet', () => {
|
|
366
|
+
const { output } = createTestOutput()
|
|
367
|
+
output.applyGlobalOptions({ json: false, quiet: true, color: true, help: false })
|
|
368
|
+
expect(output.mode).toBe('quiet')
|
|
369
|
+
expect(output.isQuiet).toBe(true)
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
test('defaults to human', () => {
|
|
373
|
+
const { output } = createTestOutput()
|
|
374
|
+
output.applyGlobalOptions({ json: false, quiet: false, color: true, help: false })
|
|
375
|
+
expect(output.mode).toBe('human')
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
test('--no-color turns chalk off', () => {
|
|
379
|
+
const { output } = createTestOutput()
|
|
380
|
+
chalk.level = 3
|
|
381
|
+
output.applyGlobalOptions({ json: false, quiet: false, color: false, help: false })
|
|
382
|
+
expect(chalk.level).toBe(0)
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
test('restores supported color after a machine execution', () => {
|
|
386
|
+
const { output } = createTestOutput()
|
|
387
|
+
chalk.level = 2
|
|
388
|
+
output.applyGlobalOptions({ json: true, quiet: false, color: true, help: false })
|
|
389
|
+
expect(chalk.level).toBe(0)
|
|
390
|
+
output.applyGlobalOptions({ json: false, quiet: false, color: true, help: false })
|
|
391
|
+
expect(chalk.level).toBe(2)
|
|
392
|
+
})
|
|
393
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Output, OutputStream } from '../../src/index.js'
|
|
2
|
+
|
|
3
|
+
export class CapturingStream implements OutputStream {
|
|
4
|
+
public chunks: string[] = []
|
|
5
|
+
|
|
6
|
+
constructor(public readonly isTty: boolean = false, public readonly columns: number = 80) {}
|
|
7
|
+
|
|
8
|
+
public write(text: string): void {
|
|
9
|
+
this.chunks.push(text)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public get text(): string {
|
|
13
|
+
return this.chunks.join('')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public get lines(): string[] {
|
|
17
|
+
return this.text.split('\n').filter(it => it !== '')
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface TestOutput {
|
|
22
|
+
output: Output
|
|
23
|
+
stdout: CapturingStream
|
|
24
|
+
stderr: CapturingStream
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const createTestOutput = ({ stderrTty = false, stdinTty = false }: { stderrTty?: boolean; stdinTty?: boolean } = {}): TestOutput => {
|
|
28
|
+
const stdout = new CapturingStream()
|
|
29
|
+
const stderr = new CapturingStream(stderrTty)
|
|
30
|
+
return {
|
|
31
|
+
stdout,
|
|
32
|
+
stderr,
|
|
33
|
+
output: new Output({ stdout, stderr, isStdinTty: () => stdinTty }),
|
|
34
|
+
}
|
|
35
|
+
}
|