@forwardimpact/libcli 0.1.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/LICENSE +201 -0
- package/cli.js +69 -0
- package/color.js +39 -0
- package/format.js +149 -0
- package/help.js +98 -0
- package/index.js +17 -0
- package/package.json +27 -0
- package/summary.js +19 -0
- package/test/cli.test.js +138 -0
- package/test/color.test.js +45 -0
- package/test/format.test.js +90 -0
- package/test/help.test.js +98 -0
- package/test/summary.test.js +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Dick Olsson
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/cli.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { HelpRenderer } from "./help.js";
|
|
3
|
+
|
|
4
|
+
export class Cli {
|
|
5
|
+
#definition;
|
|
6
|
+
#proc;
|
|
7
|
+
#helpRenderer;
|
|
8
|
+
|
|
9
|
+
constructor(definition, { process, helpRenderer }) {
|
|
10
|
+
this.#definition = definition;
|
|
11
|
+
this.#proc = process;
|
|
12
|
+
this.#helpRenderer = helpRenderer;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get name() {
|
|
16
|
+
return this.#definition.name;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
parse(argv) {
|
|
20
|
+
const options = {};
|
|
21
|
+
for (const [name, opt] of Object.entries(this.#definition.options || {})) {
|
|
22
|
+
options[name] = { type: opt.type };
|
|
23
|
+
if (opt.short) options[name].short = opt.short;
|
|
24
|
+
if (opt.default !== undefined) options[name].default = opt.default;
|
|
25
|
+
if (opt.multiple) options[name].multiple = opt.multiple;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { values, positionals } = parseArgs({
|
|
29
|
+
options,
|
|
30
|
+
allowPositionals: true,
|
|
31
|
+
args: argv,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (values.help) {
|
|
35
|
+
if (values.json) {
|
|
36
|
+
this.#helpRenderer.renderJson(this.#definition, this.#proc.stdout);
|
|
37
|
+
} else {
|
|
38
|
+
this.#helpRenderer.render(this.#definition, this.#proc.stdout);
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (values.version && this.#definition.version) {
|
|
44
|
+
this.#proc.stdout.write(this.#definition.version + "\n");
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return { values, positionals };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
showHelp() {
|
|
52
|
+
this.#helpRenderer.render(this.#definition, this.#proc.stdout);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
error(message) {
|
|
56
|
+
this.#proc.stderr.write(`${this.#definition.name}: error: ${message}\n`);
|
|
57
|
+
this.#proc.exitCode = 1;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
usageError(message) {
|
|
61
|
+
this.#proc.stderr.write(`${this.#definition.name}: error: ${message}\n`);
|
|
62
|
+
this.#proc.exitCode = 2;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function createCli(definition) {
|
|
67
|
+
const helpRenderer = new HelpRenderer({ process });
|
|
68
|
+
return new Cli(definition, { process, helpRenderer });
|
|
69
|
+
}
|
package/color.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// ANSI color constants
|
|
2
|
+
export const colors = {
|
|
3
|
+
reset: "\x1b[0m",
|
|
4
|
+
bold: "\x1b[1m",
|
|
5
|
+
dim: "\x1b[2m",
|
|
6
|
+
italic: "\x1b[3m",
|
|
7
|
+
underline: "\x1b[4m",
|
|
8
|
+
red: "\x1b[31m",
|
|
9
|
+
green: "\x1b[32m",
|
|
10
|
+
yellow: "\x1b[33m",
|
|
11
|
+
blue: "\x1b[34m",
|
|
12
|
+
magenta: "\x1b[35m",
|
|
13
|
+
cyan: "\x1b[36m",
|
|
14
|
+
white: "\x1b[37m",
|
|
15
|
+
gray: "\x1b[90m",
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Check if output supports colors.
|
|
20
|
+
* @param {object} proc - process-like object with env and stdout
|
|
21
|
+
* @returns {boolean}
|
|
22
|
+
*/
|
|
23
|
+
export function supportsColor(proc = process) {
|
|
24
|
+
if (proc.env.NO_COLOR) return false;
|
|
25
|
+
if (proc.env.FORCE_COLOR) return true;
|
|
26
|
+
return proc.stdout?.isTTY ?? false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Wrap text with ANSI color if supported.
|
|
31
|
+
* @param {string} text
|
|
32
|
+
* @param {string} color
|
|
33
|
+
* @param {object} proc - process-like object
|
|
34
|
+
* @returns {string}
|
|
35
|
+
*/
|
|
36
|
+
export function colorize(text, color, proc = process) {
|
|
37
|
+
if (!supportsColor(proc)) return text;
|
|
38
|
+
return `${color}${text}${colors.reset}`;
|
|
39
|
+
}
|
package/format.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { colorize, colors } from "./color.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Format a header (bold + cyan)
|
|
5
|
+
* @param {string} text
|
|
6
|
+
* @param {object} proc
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
export function formatHeader(text, proc = process) {
|
|
10
|
+
return colorize(text, colors.bold + colors.cyan, proc);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Format a subheader (bold)
|
|
15
|
+
* @param {string} text
|
|
16
|
+
* @param {object} proc
|
|
17
|
+
* @returns {string}
|
|
18
|
+
*/
|
|
19
|
+
export function formatSubheader(text, proc = process) {
|
|
20
|
+
return colorize(text, colors.bold, proc);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Format a list item with label and value
|
|
25
|
+
* @param {string} label
|
|
26
|
+
* @param {string} value
|
|
27
|
+
* @param {number} indent
|
|
28
|
+
* @param {object} proc
|
|
29
|
+
* @returns {string}
|
|
30
|
+
*/
|
|
31
|
+
export function formatListItem(label, value, indent = 0, proc = process) {
|
|
32
|
+
const padding = " ".repeat(indent);
|
|
33
|
+
const bullet = colorize("\u2022", colors.dim, proc);
|
|
34
|
+
return `${padding}${bullet} ${label}: ${value}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Format a bullet item (no label)
|
|
39
|
+
* @param {string} text
|
|
40
|
+
* @param {number} indent
|
|
41
|
+
* @param {object} proc
|
|
42
|
+
* @returns {string}
|
|
43
|
+
*/
|
|
44
|
+
export function formatBullet(text, indent = 0, proc = process) {
|
|
45
|
+
const padding = " ".repeat(indent);
|
|
46
|
+
const bullet = colorize("\u2022", colors.dim, proc);
|
|
47
|
+
return `${padding}${bullet} ${text}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Format a table with aligned columns
|
|
52
|
+
* @param {string[]} headers
|
|
53
|
+
* @param {Array<Array<string|number>>} rows
|
|
54
|
+
* @param {Object} options
|
|
55
|
+
* @param {boolean} [options.compact=false]
|
|
56
|
+
* @param {object} proc
|
|
57
|
+
* @returns {string}
|
|
58
|
+
*/
|
|
59
|
+
export function formatTable(headers, rows, options = {}, proc = process) {
|
|
60
|
+
const { compact = false } = options;
|
|
61
|
+
|
|
62
|
+
const widths = headers.map((h, i) =>
|
|
63
|
+
Math.max(String(h).length, ...rows.map((r) => String(r[i] || "").length)),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const lines = [];
|
|
67
|
+
|
|
68
|
+
const headerLine = headers
|
|
69
|
+
.map((h, i) => String(h).padEnd(widths[i]))
|
|
70
|
+
.join(" ");
|
|
71
|
+
lines.push(colorize(headerLine, colors.bold, proc));
|
|
72
|
+
|
|
73
|
+
if (!compact) {
|
|
74
|
+
lines.push(widths.map((w) => "\u2500".repeat(w)).join("\u2500\u2500"));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
for (const row of rows) {
|
|
78
|
+
lines.push(
|
|
79
|
+
row.map((cell, i) => String(cell || "").padEnd(widths[i])).join(" "),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return lines.join("\n");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Format an error message
|
|
88
|
+
* @param {string} message
|
|
89
|
+
* @param {object} proc
|
|
90
|
+
* @returns {string}
|
|
91
|
+
*/
|
|
92
|
+
export function formatError(message, proc = process) {
|
|
93
|
+
return colorize(`Error: ${message}`, colors.red, proc);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Format a success message
|
|
98
|
+
* @param {string} message
|
|
99
|
+
* @param {object} proc
|
|
100
|
+
* @returns {string}
|
|
101
|
+
*/
|
|
102
|
+
export function formatSuccess(message, proc = process) {
|
|
103
|
+
return colorize(message, colors.green, proc);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Format a warning message
|
|
108
|
+
* @param {string} message
|
|
109
|
+
* @param {object} proc
|
|
110
|
+
* @returns {string}
|
|
111
|
+
*/
|
|
112
|
+
export function formatWarning(message, proc = process) {
|
|
113
|
+
return colorize(`Warning: ${message}`, colors.yellow, proc);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Create a horizontal rule
|
|
118
|
+
* @param {number} width
|
|
119
|
+
* @param {object} proc
|
|
120
|
+
* @returns {string}
|
|
121
|
+
*/
|
|
122
|
+
export function horizontalRule(width = 60, proc = process) {
|
|
123
|
+
return colorize("\u2500".repeat(width), colors.dim, proc);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Format a section with title and content
|
|
128
|
+
* @param {string} title
|
|
129
|
+
* @param {string} content
|
|
130
|
+
* @param {object} proc
|
|
131
|
+
* @returns {string}
|
|
132
|
+
*/
|
|
133
|
+
export function formatSection(title, content, proc = process) {
|
|
134
|
+
return `${formatHeader(title, proc)}\n\n${content}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Indent all lines of text
|
|
139
|
+
* @param {string} text
|
|
140
|
+
* @param {number} spaces
|
|
141
|
+
* @returns {string}
|
|
142
|
+
*/
|
|
143
|
+
export function indent(text, spaces = 2) {
|
|
144
|
+
const padding = " ".repeat(spaces);
|
|
145
|
+
return text
|
|
146
|
+
.split("\n")
|
|
147
|
+
.map((line) => padding + line)
|
|
148
|
+
.join("\n");
|
|
149
|
+
}
|
package/help.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { supportsColor } from "./color.js";
|
|
2
|
+
import { formatHeader, formatSubheader } from "./format.js";
|
|
3
|
+
|
|
4
|
+
export class HelpRenderer {
|
|
5
|
+
#proc;
|
|
6
|
+
|
|
7
|
+
constructor({ process }) {
|
|
8
|
+
this.#proc = process;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
#sectionHeader(text) {
|
|
12
|
+
return supportsColor(this.#proc) ? formatSubheader(text, this.#proc) : text;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#renderHeader(definition) {
|
|
16
|
+
let header = definition.name;
|
|
17
|
+
if (definition.version) header += ` ${definition.version}`;
|
|
18
|
+
if (definition.description) header += ` \u2014 ${definition.description}`;
|
|
19
|
+
const formatted = supportsColor(this.#proc)
|
|
20
|
+
? formatHeader(header, this.#proc)
|
|
21
|
+
: header;
|
|
22
|
+
return [formatted, ""];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#renderUsage(definition) {
|
|
26
|
+
if (definition.usage) {
|
|
27
|
+
return [`Usage: ${definition.usage}`, ""];
|
|
28
|
+
}
|
|
29
|
+
if (definition.commands && definition.commands.length > 0) {
|
|
30
|
+
return [`Usage: ${definition.name} <command> [options]`, ""];
|
|
31
|
+
}
|
|
32
|
+
return [`Usage: ${definition.name} [options]`, ""];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#renderCommands(definition) {
|
|
36
|
+
if (!definition.commands || definition.commands.length === 0) return [];
|
|
37
|
+
const lines = [this.#sectionHeader("Commands:")];
|
|
38
|
+
const maxWidth = Math.max(
|
|
39
|
+
...definition.commands.map(
|
|
40
|
+
(c) => c.name.length + (c.args ? c.args.length + 1 : 0),
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
for (const cmd of definition.commands) {
|
|
44
|
+
const left = cmd.args ? `${cmd.name} ${cmd.args}` : cmd.name;
|
|
45
|
+
lines.push(` ${left.padEnd(maxWidth)} ${cmd.description}`);
|
|
46
|
+
}
|
|
47
|
+
lines.push("");
|
|
48
|
+
return lines;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#renderOptions(definition) {
|
|
52
|
+
if (!definition.options) return [];
|
|
53
|
+
const entries = Object.entries(definition.options);
|
|
54
|
+
if (entries.length === 0) return [];
|
|
55
|
+
const lines = [this.#sectionHeader("Options:")];
|
|
56
|
+
const optStrings = entries.map(([name, opt]) => {
|
|
57
|
+
let s = `--${name}`;
|
|
58
|
+
if (opt.type === "string") s += `=<${opt.type}>`;
|
|
59
|
+
if (opt.short) s += `, -${opt.short}`;
|
|
60
|
+
return s;
|
|
61
|
+
});
|
|
62
|
+
const maxOptWidth = Math.max(...optStrings.map((s) => s.length));
|
|
63
|
+
for (let i = 0; i < entries.length; i++) {
|
|
64
|
+
lines.push(
|
|
65
|
+
` ${optStrings[i].padEnd(maxOptWidth)} ${entries[i][1].description}`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
lines.push("");
|
|
69
|
+
return lines;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
#renderExamples(definition) {
|
|
73
|
+
if (!definition.examples || definition.examples.length === 0) return [];
|
|
74
|
+
const lines = [this.#sectionHeader("Examples:")];
|
|
75
|
+
for (const ex of definition.examples) {
|
|
76
|
+
lines.push(` ${ex}`);
|
|
77
|
+
}
|
|
78
|
+
lines.push("");
|
|
79
|
+
return lines;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
render(definition, stream) {
|
|
83
|
+
const out = stream || this.#proc.stdout;
|
|
84
|
+
const lines = [
|
|
85
|
+
...this.#renderHeader(definition),
|
|
86
|
+
...this.#renderUsage(definition),
|
|
87
|
+
...this.#renderCommands(definition),
|
|
88
|
+
...this.#renderOptions(definition),
|
|
89
|
+
...this.#renderExamples(definition),
|
|
90
|
+
];
|
|
91
|
+
out.write(lines.join("\n"));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
renderJson(definition, stream) {
|
|
95
|
+
const out = stream || this.#proc.stdout;
|
|
96
|
+
out.write(JSON.stringify(definition, null, 2) + "\n");
|
|
97
|
+
}
|
|
98
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { Cli, createCli } from "./cli.js";
|
|
2
|
+
export { HelpRenderer } from "./help.js";
|
|
3
|
+
export { SummaryRenderer } from "./summary.js";
|
|
4
|
+
export { colors, supportsColor, colorize } from "./color.js";
|
|
5
|
+
export {
|
|
6
|
+
formatHeader,
|
|
7
|
+
formatSubheader,
|
|
8
|
+
formatListItem,
|
|
9
|
+
formatBullet,
|
|
10
|
+
formatTable,
|
|
11
|
+
formatError,
|
|
12
|
+
formatSuccess,
|
|
13
|
+
formatWarning,
|
|
14
|
+
horizontalRule,
|
|
15
|
+
formatSection,
|
|
16
|
+
indent,
|
|
17
|
+
} from "./format.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@forwardimpact/libcli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared CLI infrastructure for the Forward Impact monorepo",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "D. Olsson <hi@senzilla.io>",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"engines": {
|
|
10
|
+
"bun": ">=1.2.0",
|
|
11
|
+
"node": ">=18.0.0"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "bun run node --test test/*.test.js"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/forwardimpact/monorepo.git",
|
|
19
|
+
"directory": "libraries/libcli"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@forwardimpact/libharness": "^0.1.5"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/summary.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class SummaryRenderer {
|
|
2
|
+
#proc;
|
|
3
|
+
|
|
4
|
+
constructor({ process }) {
|
|
5
|
+
this.#proc = process;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
render({ title, items }, stream = this.#proc.stdout) {
|
|
9
|
+
stream.write(title + "\n");
|
|
10
|
+
if (!items || items.length === 0) return;
|
|
11
|
+
|
|
12
|
+
const maxLabel = Math.max(...items.map((item) => item.label.length));
|
|
13
|
+
for (const item of items) {
|
|
14
|
+
stream.write(
|
|
15
|
+
` ${item.label.padEnd(maxLabel)} \u2014 ${item.description}\n`,
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
package/test/cli.test.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { test, describe } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
|
|
4
|
+
import { Cli } from "../cli.js";
|
|
5
|
+
import { HelpRenderer } from "../help.js";
|
|
6
|
+
|
|
7
|
+
function createProc() {
|
|
8
|
+
return {
|
|
9
|
+
env: {},
|
|
10
|
+
stdout: {
|
|
11
|
+
isTTY: false,
|
|
12
|
+
output: "",
|
|
13
|
+
write(data) {
|
|
14
|
+
this.output += data;
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
stderr: {
|
|
18
|
+
output: "",
|
|
19
|
+
write(data) {
|
|
20
|
+
this.output += data;
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
exitCode: 0,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const definition = {
|
|
28
|
+
name: "fit-test",
|
|
29
|
+
version: "1.0.0",
|
|
30
|
+
description: "Test CLI",
|
|
31
|
+
options: {
|
|
32
|
+
output: { type: "string", description: "Output path" },
|
|
33
|
+
json: { type: "boolean", description: "JSON output" },
|
|
34
|
+
help: { type: "boolean", short: "h", description: "Show help" },
|
|
35
|
+
version: { type: "boolean", description: "Show version" },
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function createCli(proc) {
|
|
40
|
+
const helpRenderer = new HelpRenderer({ process: proc });
|
|
41
|
+
return new Cli(definition, { process: proc, helpRenderer });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe("Cli", () => {
|
|
45
|
+
describe("parse", () => {
|
|
46
|
+
test("returns values and positionals for normal input", () => {
|
|
47
|
+
const proc = createProc();
|
|
48
|
+
const cli = createCli(proc);
|
|
49
|
+
const result = cli.parse(["run", "--output=out.txt"]);
|
|
50
|
+
assert.deepStrictEqual(result.positionals, ["run"]);
|
|
51
|
+
assert.strictEqual(result.values.output, "out.txt");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("returns null and writes help when --help is passed", () => {
|
|
55
|
+
const proc = createProc();
|
|
56
|
+
const cli = createCli(proc);
|
|
57
|
+
const result = cli.parse(["--help"]);
|
|
58
|
+
assert.strictEqual(result, null);
|
|
59
|
+
assert.ok(proc.stdout.output.includes("fit-test"));
|
|
60
|
+
assert.ok(proc.stdout.output.includes("Test CLI"));
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("returns null and writes JSON when --help --json is passed", () => {
|
|
64
|
+
const proc = createProc();
|
|
65
|
+
const cli = createCli(proc);
|
|
66
|
+
const result = cli.parse(["--help", "--json"]);
|
|
67
|
+
assert.strictEqual(result, null);
|
|
68
|
+
const parsed = JSON.parse(proc.stdout.output);
|
|
69
|
+
assert.strictEqual(parsed.name, "fit-test");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("returns null and writes version when --version is passed", () => {
|
|
73
|
+
const proc = createProc();
|
|
74
|
+
const cli = createCli(proc);
|
|
75
|
+
const result = cli.parse(["--version"]);
|
|
76
|
+
assert.strictEqual(result, null);
|
|
77
|
+
assert.strictEqual(proc.stdout.output.trim(), "1.0.0");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("throws on unknown flags", () => {
|
|
81
|
+
const proc = createProc();
|
|
82
|
+
const cli = createCli(proc);
|
|
83
|
+
assert.throws(() => cli.parse(["--unknown"]), {
|
|
84
|
+
code: "ERR_PARSE_ARGS_UNKNOWN_OPTION",
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("showHelp", () => {
|
|
90
|
+
test("writes help to stdout without re-parsing", () => {
|
|
91
|
+
const proc = createProc();
|
|
92
|
+
const cli = createCli(proc);
|
|
93
|
+
cli.showHelp();
|
|
94
|
+
assert.ok(proc.stdout.output.includes("fit-test"));
|
|
95
|
+
assert.ok(proc.stdout.output.includes("Test CLI"));
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("parse with multiple option", () => {
|
|
100
|
+
test("collects repeated flags into an array", () => {
|
|
101
|
+
const proc = createProc();
|
|
102
|
+
const multiDef = {
|
|
103
|
+
name: "fit-multi",
|
|
104
|
+
options: {
|
|
105
|
+
tag: { type: "string", multiple: true, description: "Tags" },
|
|
106
|
+
help: { type: "boolean", short: "h", description: "Show help" },
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
const helpRenderer = new HelpRenderer({ process: proc });
|
|
110
|
+
const cli = new Cli(multiDef, { process: proc, helpRenderer });
|
|
111
|
+
const result = cli.parse(["--tag=a", "--tag=b"]);
|
|
112
|
+
assert.deepStrictEqual(result.values.tag, ["a", "b"]);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("error", () => {
|
|
117
|
+
test("writes prefixed message to stderr and sets exitCode to 1", () => {
|
|
118
|
+
const proc = createProc();
|
|
119
|
+
const cli = createCli(proc);
|
|
120
|
+
cli.error("something broke");
|
|
121
|
+
assert.strictEqual(
|
|
122
|
+
proc.stderr.output,
|
|
123
|
+
"fit-test: error: something broke\n",
|
|
124
|
+
);
|
|
125
|
+
assert.strictEqual(proc.exitCode, 1);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe("usageError", () => {
|
|
130
|
+
test("writes prefixed message to stderr and sets exitCode to 2", () => {
|
|
131
|
+
const proc = createProc();
|
|
132
|
+
const cli = createCli(proc);
|
|
133
|
+
cli.usageError("bad argument");
|
|
134
|
+
assert.strictEqual(proc.stderr.output, "fit-test: error: bad argument\n");
|
|
135
|
+
assert.strictEqual(proc.exitCode, 2);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { test, describe } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
|
|
4
|
+
import { colors, supportsColor, colorize } from "../color.js";
|
|
5
|
+
|
|
6
|
+
describe("supportsColor", () => {
|
|
7
|
+
test("returns false when NO_COLOR is set", () => {
|
|
8
|
+
const proc = { env: { NO_COLOR: "1" }, stdout: { isTTY: true } };
|
|
9
|
+
assert.strictEqual(supportsColor(proc), false);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("returns true when FORCE_COLOR is set", () => {
|
|
13
|
+
const proc = { env: { FORCE_COLOR: "1" }, stdout: { isTTY: false } };
|
|
14
|
+
assert.strictEqual(supportsColor(proc), true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("returns true when stdout is a TTY", () => {
|
|
18
|
+
const proc = { env: {}, stdout: { isTTY: true } };
|
|
19
|
+
assert.strictEqual(supportsColor(proc), true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("returns false when stdout is not a TTY", () => {
|
|
23
|
+
const proc = { env: {}, stdout: { isTTY: false } };
|
|
24
|
+
assert.strictEqual(supportsColor(proc), false);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("returns false when stdout is undefined", () => {
|
|
28
|
+
const proc = { env: {} };
|
|
29
|
+
assert.strictEqual(supportsColor(proc), false);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("colorize", () => {
|
|
34
|
+
test("wraps text with ANSI codes when color is supported", () => {
|
|
35
|
+
const proc = { env: { FORCE_COLOR: "1" }, stdout: { isTTY: true } };
|
|
36
|
+
const result = colorize("hello", colors.red, proc);
|
|
37
|
+
assert.strictEqual(result, `${colors.red}hello${colors.reset}`);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("returns plain text when color is not supported", () => {
|
|
41
|
+
const proc = { env: {}, stdout: { isTTY: false } };
|
|
42
|
+
const result = colorize("hello", colors.red, proc);
|
|
43
|
+
assert.strictEqual(result, "hello");
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { test, describe } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
formatTable,
|
|
6
|
+
formatHeader,
|
|
7
|
+
formatError,
|
|
8
|
+
indent,
|
|
9
|
+
horizontalRule,
|
|
10
|
+
} from "../format.js";
|
|
11
|
+
import { colors } from "../color.js";
|
|
12
|
+
|
|
13
|
+
const noColor = { env: {}, stdout: { isTTY: false } };
|
|
14
|
+
const withColor = { env: { FORCE_COLOR: "1" }, stdout: { isTTY: true } };
|
|
15
|
+
|
|
16
|
+
describe("formatTable", () => {
|
|
17
|
+
test("aligns columns correctly", () => {
|
|
18
|
+
const result = formatTable(
|
|
19
|
+
["Name", "Value"],
|
|
20
|
+
[
|
|
21
|
+
["short", "1"],
|
|
22
|
+
["longer name", "2"],
|
|
23
|
+
],
|
|
24
|
+
{},
|
|
25
|
+
noColor,
|
|
26
|
+
);
|
|
27
|
+
const lines = result.split("\n");
|
|
28
|
+
assert.strictEqual(lines.length, 4); // header, separator, 2 rows
|
|
29
|
+
assert.ok(lines[0].startsWith("Name"));
|
|
30
|
+
assert.ok(lines[2].startsWith("short"));
|
|
31
|
+
assert.ok(lines[3].startsWith("longer name"));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("compact option omits separator", () => {
|
|
35
|
+
const result = formatTable(
|
|
36
|
+
["A", "B"],
|
|
37
|
+
[["1", "2"]],
|
|
38
|
+
{ compact: true },
|
|
39
|
+
noColor,
|
|
40
|
+
);
|
|
41
|
+
const lines = result.split("\n");
|
|
42
|
+
assert.strictEqual(lines.length, 2); // header + 1 row, no separator
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("formatHeader", () => {
|
|
47
|
+
test("returns bold+cyan text when color supported", () => {
|
|
48
|
+
const result = formatHeader("Title", withColor);
|
|
49
|
+
assert.ok(result.includes(colors.bold));
|
|
50
|
+
assert.ok(result.includes(colors.cyan));
|
|
51
|
+
assert.ok(result.includes("Title"));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("returns plain text when color not supported", () => {
|
|
55
|
+
const result = formatHeader("Title", noColor);
|
|
56
|
+
assert.strictEqual(result, "Title");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("formatError", () => {
|
|
61
|
+
test("returns red Error text when color supported", () => {
|
|
62
|
+
const result = formatError("bad thing", withColor);
|
|
63
|
+
assert.ok(result.includes(colors.red));
|
|
64
|
+
assert.ok(result.includes("Error: bad thing"));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("returns plain text when color not supported", () => {
|
|
68
|
+
const result = formatError("bad thing", noColor);
|
|
69
|
+
assert.strictEqual(result, "Error: bad thing");
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("indent", () => {
|
|
74
|
+
test("adds correct padding to all lines", () => {
|
|
75
|
+
const result = indent("line1\nline2", 4);
|
|
76
|
+
assert.strictEqual(result, " line1\n line2");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("defaults to 2 spaces", () => {
|
|
80
|
+
const result = indent("a\nb");
|
|
81
|
+
assert.strictEqual(result, " a\n b");
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe("horizontalRule", () => {
|
|
86
|
+
test("returns correct-width dashed line", () => {
|
|
87
|
+
const result = horizontalRule(10, noColor);
|
|
88
|
+
assert.strictEqual(result, "\u2500".repeat(10));
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { test, describe } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
|
|
4
|
+
import { HelpRenderer } from "../help.js";
|
|
5
|
+
|
|
6
|
+
function createStream() {
|
|
7
|
+
return {
|
|
8
|
+
output: "",
|
|
9
|
+
write(data) {
|
|
10
|
+
this.output += data;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const proc = { env: {}, stdout: { isTTY: false, write() {} } };
|
|
16
|
+
|
|
17
|
+
function createRenderer() {
|
|
18
|
+
return new HelpRenderer({ process: proc });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const fullDefinition = {
|
|
22
|
+
name: "fit-test",
|
|
23
|
+
version: "1.0.0",
|
|
24
|
+
description: "A test CLI",
|
|
25
|
+
commands: [
|
|
26
|
+
{ name: "run", args: "<file>", description: "Run a file" },
|
|
27
|
+
{ name: "check", description: "Check syntax" },
|
|
28
|
+
],
|
|
29
|
+
options: {
|
|
30
|
+
output: { type: "string", description: "Output path" },
|
|
31
|
+
verbose: { type: "boolean", short: "v", description: "Verbose output" },
|
|
32
|
+
help: { type: "boolean", short: "h", description: "Show this help" },
|
|
33
|
+
},
|
|
34
|
+
examples: ["fit-test run main.js", "fit-test check --verbose"],
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
describe("HelpRenderer", () => {
|
|
38
|
+
describe("render", () => {
|
|
39
|
+
test("includes header line with name, version, description", () => {
|
|
40
|
+
const stream = createStream();
|
|
41
|
+
createRenderer().render(fullDefinition, stream);
|
|
42
|
+
assert.ok(stream.output.includes("fit-test 1.0.0"));
|
|
43
|
+
assert.ok(stream.output.includes("A test CLI"));
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("includes one-line-per-command with aligned descriptions", () => {
|
|
47
|
+
const stream = createStream();
|
|
48
|
+
createRenderer().render(fullDefinition, stream);
|
|
49
|
+
assert.ok(stream.output.includes("run <file>"));
|
|
50
|
+
assert.ok(stream.output.includes("Run a file"));
|
|
51
|
+
assert.ok(stream.output.includes("check"));
|
|
52
|
+
assert.ok(stream.output.includes("Check syntax"));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("includes options with type hints and descriptions", () => {
|
|
56
|
+
const stream = createStream();
|
|
57
|
+
createRenderer().render(fullDefinition, stream);
|
|
58
|
+
assert.ok(stream.output.includes("--output=<string>"));
|
|
59
|
+
assert.ok(stream.output.includes("--verbose, -v"));
|
|
60
|
+
assert.ok(stream.output.includes("--help, -h"));
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("includes examples section", () => {
|
|
64
|
+
const stream = createStream();
|
|
65
|
+
createRenderer().render(fullDefinition, stream);
|
|
66
|
+
assert.ok(stream.output.includes("fit-test run main.js"));
|
|
67
|
+
assert.ok(stream.output.includes("fit-test check --verbose"));
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("omits commands section when definition has no commands", () => {
|
|
71
|
+
const stream = createStream();
|
|
72
|
+
const def = {
|
|
73
|
+
name: "fit-simple",
|
|
74
|
+
options: { help: { type: "boolean", description: "Help" } },
|
|
75
|
+
};
|
|
76
|
+
createRenderer().render(def, stream);
|
|
77
|
+
assert.ok(!stream.output.includes("Commands:"));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("uses custom usage string when provided", () => {
|
|
81
|
+
const stream = createStream();
|
|
82
|
+
const def = { name: "fit-query", usage: "fit-query <s> <p> <o>" };
|
|
83
|
+
createRenderer().render(def, stream);
|
|
84
|
+
assert.ok(stream.output.includes("Usage: fit-query <s> <p> <o>"));
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("renderJson", () => {
|
|
89
|
+
test("produces valid JSON matching the definition", () => {
|
|
90
|
+
const stream = createStream();
|
|
91
|
+
createRenderer().renderJson(fullDefinition, stream);
|
|
92
|
+
const parsed = JSON.parse(stream.output);
|
|
93
|
+
assert.strictEqual(parsed.name, "fit-test");
|
|
94
|
+
assert.strictEqual(parsed.commands.length, 2);
|
|
95
|
+
assert.strictEqual(parsed.commands[0].name, "run");
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { test, describe } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
|
|
4
|
+
import { SummaryRenderer } from "../summary.js";
|
|
5
|
+
|
|
6
|
+
function createStream() {
|
|
7
|
+
return {
|
|
8
|
+
output: "",
|
|
9
|
+
write(data) {
|
|
10
|
+
this.output += data;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const proc = { stdout: { write() {} } };
|
|
16
|
+
|
|
17
|
+
describe("SummaryRenderer", () => {
|
|
18
|
+
test("writes title and aligned items", () => {
|
|
19
|
+
const stream = createStream();
|
|
20
|
+
const renderer = new SummaryRenderer({ process: proc });
|
|
21
|
+
renderer.render(
|
|
22
|
+
{
|
|
23
|
+
title: "Generated 38 files",
|
|
24
|
+
items: [
|
|
25
|
+
{ label: "definitions/", description: "Service definitions" },
|
|
26
|
+
{ label: "proto/", description: "Proto source files" },
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
stream,
|
|
30
|
+
);
|
|
31
|
+
assert.ok(stream.output.includes("Generated 38 files\n"));
|
|
32
|
+
assert.ok(stream.output.includes("definitions/"));
|
|
33
|
+
assert.ok(stream.output.includes("proto/"));
|
|
34
|
+
// Labels should be aligned
|
|
35
|
+
const lines = stream.output.split("\n").filter((l) => l.startsWith(" "));
|
|
36
|
+
const firstDash = lines[0].indexOf("\u2014");
|
|
37
|
+
const secondDash = lines[1].indexOf("\u2014");
|
|
38
|
+
assert.strictEqual(firstDash, secondDash);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("writes only title when items is empty", () => {
|
|
42
|
+
const stream = createStream();
|
|
43
|
+
const renderer = new SummaryRenderer({ process: proc });
|
|
44
|
+
renderer.render({ title: "Done", items: [] }, stream);
|
|
45
|
+
assert.strictEqual(stream.output, "Done\n");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("right-pads labels to the longest label width", () => {
|
|
49
|
+
const stream = createStream();
|
|
50
|
+
const renderer = new SummaryRenderer({ process: proc });
|
|
51
|
+
renderer.render(
|
|
52
|
+
{
|
|
53
|
+
title: "Title",
|
|
54
|
+
items: [
|
|
55
|
+
{ label: "ab", description: "short" },
|
|
56
|
+
{ label: "abcdef", description: "long" },
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
stream,
|
|
60
|
+
);
|
|
61
|
+
const lines = stream.output.split("\n").filter((l) => l.startsWith(" "));
|
|
62
|
+
// "ab" should be padded to 6 chars to match "abcdef"
|
|
63
|
+
assert.ok(lines[0].includes("ab "));
|
|
64
|
+
});
|
|
65
|
+
});
|