@jinshuju/cli 0.5.0 → 0.6.0
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/cli-bin.js +5 -1
- package/dist/cli.js +5 -4
- package/dist/help.d.ts +11 -5
- package/dist/help.js +43 -25
- package/dist/local.js +1 -1
- package/dist/result.d.ts +14 -2
- package/dist/result.js +15 -5
- package/dist/style.d.ts +22 -0
- package/dist/style.js +12 -0
- package/package.json +1 -1
package/dist/cli-bin.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { runCli } from './cli.js';
|
|
3
|
-
|
|
3
|
+
import { styleFor } from './style.js';
|
|
4
|
+
const result = await runCli(process.argv.slice(2), {
|
|
5
|
+
stdout: (chunk) => void process.stdout.write(chunk),
|
|
6
|
+
terminal: { stdout: styleFor(process.stdout), stderr: styleFor(process.stderr) }
|
|
7
|
+
});
|
|
4
8
|
if (result.stdout)
|
|
5
9
|
process.stdout.write(result.stdout);
|
|
6
10
|
if (result.stderr)
|
package/dist/cli.js
CHANGED
|
@@ -19,20 +19,21 @@ export async function runCli(args = [], runtime = {}) {
|
|
|
19
19
|
const stdin = runtime.stdin ?? readStdin;
|
|
20
20
|
if (flags['--version'] || flags['-V'])
|
|
21
21
|
return ok(VERSION);
|
|
22
|
+
const terminal = runtime.terminal ?? {};
|
|
22
23
|
if (args.length === 0)
|
|
23
|
-
return ok(rootHelp());
|
|
24
|
+
return ok(rootHelp(undefined, terminal.stdout));
|
|
24
25
|
if (flags['--help'] || flags['-h'])
|
|
25
|
-
return ok(helpFor(words));
|
|
26
|
+
return ok(helpFor(words, undefined, terminal.stdout));
|
|
26
27
|
const resource = words[0];
|
|
27
28
|
try {
|
|
28
29
|
if (resource === 'auth' || resource === 'config')
|
|
29
30
|
return await runLocal(words, flags, runtime, stdin);
|
|
30
31
|
if (!command)
|
|
31
|
-
return unknown(words, outputOf(flags));
|
|
32
|
+
return unknown(words, outputOf(flags), terminal.stderr);
|
|
32
33
|
return await runRemote(command, words, flags, runtime, stdin);
|
|
33
34
|
}
|
|
34
35
|
catch (error) {
|
|
35
|
-
return fail(error, outputOf(flags));
|
|
36
|
+
return fail(error, outputOf(flags), terminal.stderr);
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
async function runRemote(command, words, flags, runtime, stdin) {
|
package/dist/help.d.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { type Command } from './commands/index.js';
|
|
2
|
+
import { type Style } from './style.js';
|
|
2
3
|
/**
|
|
3
4
|
* Help is rendered from the command table, so a command cannot be reachable
|
|
4
5
|
* without appearing here, and a flag cannot be accepted without being
|
|
5
6
|
* described. The root help lists one line per resource, then the global
|
|
6
7
|
* options.
|
|
8
|
+
*
|
|
9
|
+
* Every help opens with the version, piped or not: help is read by a person,
|
|
10
|
+
* and which version it describes is the first thing worth knowing about it.
|
|
7
11
|
*/
|
|
8
|
-
|
|
12
|
+
/** `jinshuju v0.5.0`, for the top of a help and of an error at a terminal. */
|
|
13
|
+
export declare function versionLine(style?: Style): string;
|
|
14
|
+
export declare function rootHelp(commands?: readonly Command[], style?: Style): string;
|
|
9
15
|
/** Every verb of one resource, for `jinshuju form --help`. */
|
|
10
|
-
export declare function resourceHelp(resource: string, commands?: readonly Command[]): string;
|
|
11
|
-
export declare function commandHelp(command: Command): string;
|
|
16
|
+
export declare function resourceHelp(resource: string, commands?: readonly Command[], style?: Style): string;
|
|
17
|
+
export declare function commandHelp(command: Command, style?: Style): string;
|
|
12
18
|
/** What the caller typed that matched nothing, and the nearest things that do. */
|
|
13
|
-
export declare function unknownCommandHelp(words: readonly string[], commands?: readonly Command[]): string;
|
|
19
|
+
export declare function unknownCommandHelp(words: readonly string[], commands?: readonly Command[], style?: Style): string;
|
|
14
20
|
/** Help for whatever the words point at: a command, a resource, or the root. */
|
|
15
|
-
export declare function helpFor(words: readonly string[], commands?: readonly Command[]): string;
|
|
21
|
+
export declare function helpFor(words: readonly string[], commands?: readonly Command[], style?: Style): string;
|
package/dist/help.js
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
1
|
import { COMMANDS, RESOURCES, findCommand } from './commands/index.js';
|
|
2
2
|
import { EXIT_CODES } from './errors.js';
|
|
3
3
|
import { GLOBAL_OPTIONS, optionKey } from './options.js';
|
|
4
|
+
import { PLAIN } from './style.js';
|
|
5
|
+
import { VERSION } from './version.js';
|
|
4
6
|
/**
|
|
5
7
|
* Help is rendered from the command table, so a command cannot be reachable
|
|
6
8
|
* without appearing here, and a flag cannot be accepted without being
|
|
7
9
|
* described. The root help lists one line per resource, then the global
|
|
8
10
|
* options.
|
|
11
|
+
*
|
|
12
|
+
* Every help opens with the version, piped or not: help is read by a person,
|
|
13
|
+
* and which version it describes is the first thing worth knowing about it.
|
|
9
14
|
*/
|
|
10
|
-
|
|
15
|
+
/** `jinshuju v0.5.0`, for the top of a help and of an error at a terminal. */
|
|
16
|
+
export function versionLine(style = PLAIN) {
|
|
17
|
+
return `${style.heading('jinshuju')} ${style.dim(`v${VERSION}`)}`;
|
|
18
|
+
}
|
|
19
|
+
export function rootHelp(commands = COMMANDS, style = PLAIN) {
|
|
11
20
|
const present = new Set(commands.map((command) => command.path[0]));
|
|
12
21
|
const resources = RESOURCES.filter((resource) => present.has(resource.name) || resource.name === 'auth' || resource.name === 'config');
|
|
13
22
|
const width = Math.max(...resources.map((resource) => resource.name.length)) + 4;
|
|
14
23
|
return [
|
|
15
|
-
|
|
24
|
+
versionLine(style),
|
|
25
|
+
'',
|
|
26
|
+
`${style.heading('Usage:')} jinshuju <resource> <verb> [args] [flags]`,
|
|
16
27
|
'',
|
|
17
|
-
'Commands:',
|
|
28
|
+
style.heading('Commands:'),
|
|
18
29
|
...resources.map((resource) => ` ${resource.name.padEnd(width)}${resource.summary}`),
|
|
19
30
|
'',
|
|
20
|
-
'Global Options:',
|
|
31
|
+
style.heading('Global Options:'),
|
|
21
32
|
...GLOBAL_OPTIONS.map((option) => ` ${flagLabel(option).padEnd(width + 12)}${option.description}`),
|
|
22
33
|
'',
|
|
23
34
|
'Run `jinshuju <resource> --help` to see its verbs.',
|
|
@@ -32,16 +43,18 @@ export function rootHelp(commands = COMMANDS) {
|
|
|
32
43
|
].join('\n');
|
|
33
44
|
}
|
|
34
45
|
/** Every verb of one resource, for `jinshuju form --help`. */
|
|
35
|
-
export function resourceHelp(resource, commands = COMMANDS) {
|
|
46
|
+
export function resourceHelp(resource, commands = COMMANDS, style = PLAIN) {
|
|
36
47
|
const owned = commands.filter((command) => command.path[0] === resource);
|
|
37
48
|
if (owned.length === 0)
|
|
38
|
-
return rootHelp(commands);
|
|
49
|
+
return rootHelp(commands, style);
|
|
39
50
|
const width = Math.max(...owned.map((command) => command.path.join(' ').length)) + 4;
|
|
40
51
|
const note = RESOURCES.find((entry) => entry.name === resource)?.note;
|
|
41
52
|
return [
|
|
42
|
-
|
|
53
|
+
versionLine(style),
|
|
43
54
|
'',
|
|
44
|
-
'
|
|
55
|
+
`${style.heading('Usage:')} jinshuju ${resource} <verb> [args] [flags]`,
|
|
56
|
+
'',
|
|
57
|
+
style.heading('Commands:'),
|
|
45
58
|
...owned.map((command) => ` ${command.path.join(' ').padEnd(width)}${command.summary}`),
|
|
46
59
|
...(note ? ['', note] : []),
|
|
47
60
|
'',
|
|
@@ -49,9 +62,9 @@ export function resourceHelp(resource, commands = COMMANDS) {
|
|
|
49
62
|
''
|
|
50
63
|
].join('\n');
|
|
51
64
|
}
|
|
52
|
-
export function commandHelp(command) {
|
|
65
|
+
export function commandHelp(command, style = PLAIN) {
|
|
53
66
|
const usage = [
|
|
54
|
-
'Usage: jinshuju
|
|
67
|
+
`${style.heading('Usage:')} jinshuju`,
|
|
55
68
|
...command.path,
|
|
56
69
|
...(command.args ?? []).map((arg) => {
|
|
57
70
|
const name = arg.required ? `<${arg.name}>` : `[${arg.name}]`;
|
|
@@ -59,29 +72,34 @@ export function commandHelp(command) {
|
|
|
59
72
|
}),
|
|
60
73
|
'[flags]'
|
|
61
74
|
].join(' ');
|
|
62
|
-
const parts = [usage, '', command.description ?? command.summary];
|
|
75
|
+
const parts = [versionLine(style), '', usage, '', command.description ?? command.summary];
|
|
63
76
|
if (command.args?.length) {
|
|
64
77
|
const width = Math.max(...command.args.map((arg) => arg.name.length)) + 4;
|
|
65
|
-
parts.push('', 'Arguments:', ...command.args.map((arg) => ` ${arg.name.padEnd(width)}${arg.description}`));
|
|
78
|
+
parts.push('', style.heading('Arguments:'), ...command.args.map((arg) => ` ${arg.name.padEnd(width)}${arg.description}`));
|
|
66
79
|
}
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
80
|
+
// One width across both lists, so the descriptions line up down the page.
|
|
81
|
+
const own = command.options ?? [];
|
|
82
|
+
const width = Math.max(...[...own, ...GLOBAL_OPTIONS].map((option) => flagLabel(option).length)) + 4;
|
|
83
|
+
const describe = (option) => ` ${flagLabel(option).padEnd(width)}${option.description}`;
|
|
84
|
+
if (own.length > 0)
|
|
85
|
+
parts.push('', style.heading('Flags:'), ...own.map(describe));
|
|
86
|
+
parts.push('', style.heading('Global Options:'), ...GLOBAL_OPTIONS.map(describe));
|
|
70
87
|
if (command.payload?.length)
|
|
71
|
-
parts.push('', 'Payload:', ...command.payload.map((line) => ` ${line}`));
|
|
72
|
-
if (command.examples?.length)
|
|
73
|
-
parts.push('', 'Examples:', ...command.examples.map((example) => ` ${example}`));
|
|
88
|
+
parts.push('', style.heading('Payload:'), ...command.payload.map((line) => ` ${line}`));
|
|
89
|
+
if (command.examples?.length) {
|
|
90
|
+
parts.push('', style.heading('Examples:'), ...command.examples.map((example) => ` ${example}`));
|
|
91
|
+
}
|
|
74
92
|
return `${parts.join('\n')}\n`;
|
|
75
93
|
}
|
|
76
94
|
/** What the caller typed that matched nothing, and the nearest things that do. */
|
|
77
|
-
export function unknownCommandHelp(words, commands = COMMANDS) {
|
|
95
|
+
export function unknownCommandHelp(words, commands = COMMANDS, style = PLAIN) {
|
|
78
96
|
const typed = words.join(' ');
|
|
79
97
|
const resource = words[0];
|
|
80
98
|
const siblings = commands.filter((command) => command.path[0] === resource).slice(0, 10);
|
|
81
99
|
return [
|
|
82
100
|
`Unknown command: jinshuju ${typed}`,
|
|
83
101
|
...(siblings.length > 0
|
|
84
|
-
? ['', 'Did you mean:', ...siblings.map((command) => ` jinshuju ${command.path.join(' ')}`)]
|
|
102
|
+
? ['', style.heading('Did you mean:'), ...siblings.map((command) => ` jinshuju ${command.path.join(' ')}`)]
|
|
85
103
|
: []),
|
|
86
104
|
'',
|
|
87
105
|
'Run `jinshuju --help` for the full list.',
|
|
@@ -89,16 +107,16 @@ export function unknownCommandHelp(words, commands = COMMANDS) {
|
|
|
89
107
|
].join('\n');
|
|
90
108
|
}
|
|
91
109
|
/** Help for whatever the words point at: a command, a resource, or the root. */
|
|
92
|
-
export function helpFor(words, commands = COMMANDS) {
|
|
110
|
+
export function helpFor(words, commands = COMMANDS, style = PLAIN) {
|
|
93
111
|
if (words.length === 0)
|
|
94
|
-
return rootHelp(commands);
|
|
112
|
+
return rootHelp(commands, style);
|
|
95
113
|
const command = findCommand(words, commands);
|
|
96
114
|
if (command)
|
|
97
|
-
return commandHelp(command);
|
|
115
|
+
return commandHelp(command, style);
|
|
98
116
|
const resource = words[0];
|
|
99
117
|
if (commands.some((candidate) => candidate.path[0] === resource))
|
|
100
|
-
return resourceHelp(resource, commands);
|
|
101
|
-
return rootHelp(commands);
|
|
118
|
+
return resourceHelp(resource, commands, style);
|
|
119
|
+
return rootHelp(commands, style);
|
|
102
120
|
}
|
|
103
121
|
function flagLabel(option) {
|
|
104
122
|
const flags = option.short ? `${option.short}, ${option.name}` : option.name;
|
package/dist/local.js
CHANGED
|
@@ -40,7 +40,7 @@ export async function runLocal(words, flags, runtime, stdin) {
|
|
|
40
40
|
case 'config unset':
|
|
41
41
|
return configUnset(words, options);
|
|
42
42
|
default:
|
|
43
|
-
return unknown(words, options.output);
|
|
43
|
+
return unknown(words, options.output, runtime.terminal?.stderr);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
function requireArg(value, name) {
|
package/dist/result.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { HttpClient } from './http.js';
|
|
2
2
|
import type { OutputFormat } from './options.js';
|
|
3
|
+
import type { Style } from './style.js';
|
|
3
4
|
export type CliResult = {
|
|
4
5
|
exitCode: number;
|
|
5
6
|
stdout: string;
|
|
@@ -16,6 +17,14 @@ export type CliRuntime = {
|
|
|
16
17
|
stdout?: (chunk: string) => void;
|
|
17
18
|
/** How wide a table may be. Defaults to the terminal, or 120 through a pipe. */
|
|
18
19
|
width?: number;
|
|
20
|
+
/**
|
|
21
|
+
* How each stream is styled when it is a terminal; a stream left out is a
|
|
22
|
+
* pipe, and gets neither colour nor the version line on an error.
|
|
23
|
+
*/
|
|
24
|
+
terminal?: {
|
|
25
|
+
stdout?: Style;
|
|
26
|
+
stderr?: Style;
|
|
27
|
+
};
|
|
19
28
|
};
|
|
20
29
|
export declare function ok(stdout: string): CliResult;
|
|
21
30
|
/**
|
|
@@ -23,9 +32,12 @@ export declare function ok(stdout: string): CliResult;
|
|
|
23
32
|
* anything being parsed; with `--output json` stderr carries the same in
|
|
24
33
|
* words, plus whatever status and body the server answered with, so a script
|
|
25
34
|
* that reads JSON on the way in reads JSON on the way out as well.
|
|
35
|
+
*
|
|
36
|
+
* At a terminal a text failure opens with the version, since "which version"
|
|
37
|
+
* is the first question about any error. Only there: JSON stays JSON.
|
|
26
38
|
*/
|
|
27
|
-
export declare function fail(error: unknown, output: OutputFormat): CliResult;
|
|
39
|
+
export declare function fail(error: unknown, output: OutputFormat, terminal?: Style): CliResult;
|
|
28
40
|
/** An unknown command is a usage error, and in text mode the help is the message. */
|
|
29
|
-
export declare function unknown(words: readonly string[], output: OutputFormat): CliResult;
|
|
41
|
+
export declare function unknown(words: readonly string[], output: OutputFormat, terminal?: Style): CliResult;
|
|
30
42
|
/** What `--output` asked for, read before the flags are checked so a failure can honour it. */
|
|
31
43
|
export declare function outputOf(flags: Record<string, unknown>): OutputFormat;
|
package/dist/result.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EXIT_CODES, UsageError, classify } from './errors.js';
|
|
2
|
-
import { unknownCommandHelp } from './help.js';
|
|
2
|
+
import { unknownCommandHelp, versionLine } from './help.js';
|
|
3
3
|
import { json } from './render.js';
|
|
4
4
|
export function ok(stdout) {
|
|
5
5
|
return { exitCode: 0, stdout: stdout.endsWith('\n') ? stdout : `${stdout}\n`, stderr: '' };
|
|
@@ -9,21 +9,31 @@ export function ok(stdout) {
|
|
|
9
9
|
* anything being parsed; with `--output json` stderr carries the same in
|
|
10
10
|
* words, plus whatever status and body the server answered with, so a script
|
|
11
11
|
* that reads JSON on the way in reads JSON on the way out as well.
|
|
12
|
+
*
|
|
13
|
+
* At a terminal a text failure opens with the version, since "which version"
|
|
14
|
+
* is the first question about any error. Only there: JSON stays JSON.
|
|
12
15
|
*/
|
|
13
|
-
export function fail(error, output) {
|
|
16
|
+
export function fail(error, output, terminal) {
|
|
14
17
|
const sorted = classify(error);
|
|
15
18
|
if (output !== 'text') {
|
|
16
19
|
const { exitCode, ...envelope } = sorted;
|
|
17
20
|
return { exitCode, stdout: '', stderr: `${json({ error: envelope })}\n` };
|
|
18
21
|
}
|
|
19
22
|
const wait = sorted.retry_after === undefined ? '' : ` Retry after ${sorted.retry_after}s.`;
|
|
20
|
-
return { exitCode: sorted.exitCode, stdout: '', stderr:
|
|
23
|
+
return { exitCode: sorted.exitCode, stdout: '', stderr: `${banner(terminal)}Error: ${sorted.message}${wait}\n` };
|
|
21
24
|
}
|
|
22
25
|
/** An unknown command is a usage error, and in text mode the help is the message. */
|
|
23
|
-
export function unknown(words, output) {
|
|
26
|
+
export function unknown(words, output, terminal) {
|
|
24
27
|
if (output !== 'text')
|
|
25
28
|
return fail(new UsageError(`Unknown command: jinshuju ${words.join(' ')}`), output);
|
|
26
|
-
return {
|
|
29
|
+
return {
|
|
30
|
+
exitCode: EXIT_CODES.usage,
|
|
31
|
+
stdout: '',
|
|
32
|
+
stderr: banner(terminal) + unknownCommandHelp(words, undefined, terminal)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function banner(terminal) {
|
|
36
|
+
return terminal ? `${versionLine(terminal)}\n\n` : '';
|
|
27
37
|
}
|
|
28
38
|
/** What `--output` asked for, read before the flags are checked so a failure can honour it. */
|
|
29
39
|
export function outputOf(flags) {
|
package/dist/style.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colour for the person at a terminal, and nothing for anyone else.
|
|
3
|
+
*
|
|
4
|
+
* A style exists only for a stream that is a terminal; a pipe gets none, so
|
|
5
|
+
* what a script or an agent reads is byte-for-byte what it was. NO_COLOR
|
|
6
|
+
* (https://no-color.org) keeps the terminal but drops the colour.
|
|
7
|
+
*
|
|
8
|
+
* The orange is the Jinshuju logo's, #FF8533. A terminal that does not say it
|
|
9
|
+
* takes 24-bit colour gets the nearest of the 256.
|
|
10
|
+
*/
|
|
11
|
+
export type Style = {
|
|
12
|
+
/** A section heading, and the name on the version line. */
|
|
13
|
+
heading(text: string): string;
|
|
14
|
+
/** What is there to be glanced at, not read: the version. */
|
|
15
|
+
dim(text: string): string;
|
|
16
|
+
};
|
|
17
|
+
export declare const PLAIN: Style;
|
|
18
|
+
type Env = NodeJS.ProcessEnv | Record<string, string | undefined>;
|
|
19
|
+
export declare function styleFor(stream: {
|
|
20
|
+
isTTY?: boolean;
|
|
21
|
+
}, env?: Env): Style | undefined;
|
|
22
|
+
export {};
|
package/dist/style.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const PLAIN = { heading: (text) => text, dim: (text) => text };
|
|
2
|
+
export function styleFor(stream, env = process.env) {
|
|
3
|
+
if (!stream.isTTY)
|
|
4
|
+
return undefined;
|
|
5
|
+
if (env.NO_COLOR)
|
|
6
|
+
return PLAIN;
|
|
7
|
+
const orange = /^(truecolor|24bit)$/i.test(env.COLORTERM ?? '') ? '38;2;255;133;51' : '38;5;209';
|
|
8
|
+
return {
|
|
9
|
+
heading: (text) => `\x1b[${orange}m${text}\x1b[39m`,
|
|
10
|
+
dim: (text) => `\x1b[90m${text}\x1b[39m`
|
|
11
|
+
};
|
|
12
|
+
}
|