@auis/cli 0.3.0 → 0.4.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/README.md +28 -5
- package/package.json +1 -1
- package/src/cli.mjs +6 -4
- package/src/doctor.mjs +22 -3
- package/src/install.mjs +42 -10
- package/src/modules.mjs +143 -0
- package/src/ui.mjs +61 -0
package/README.md
CHANGED
|
@@ -49,20 +49,41 @@ npx @auis/cli@latest doctor
|
|
|
49
49
|
capability with a reason, and an alternative when the answer is no. It writes
|
|
50
50
|
nothing.
|
|
51
51
|
|
|
52
|
-
In a **Next.js App Router** app, `init`
|
|
53
|
-
the drawer and the agent mentions — and nothing else:
|
|
52
|
+
In a **Next.js App Router** app, `init` asks what to install:
|
|
54
53
|
|
|
55
54
|
```bash
|
|
56
|
-
npx @auis/cli@latest init --dry-run # print what it would touch
|
|
55
|
+
npx @auis/cli@latest init --dry-run # pick, then print what it would touch
|
|
57
56
|
npx @auis/cli@latest init
|
|
58
57
|
```
|
|
59
58
|
|
|
59
|
+
```
|
|
60
|
+
What should auis install? ↑↓ move · space toggle · a all · enter install · esc cancel
|
|
61
|
+
|
|
62
|
+
❯ ◉ Review pins, comments, the drawer and agent mentions
|
|
63
|
+
◯ State Mode switch a screen's states in place, and ?ge= deep links that replay clicks
|
|
64
|
+
it is not packaged for an existing app yet, and it needs a registry of your screens
|
|
65
|
+
◯ Edit Mode visual edits an agent turns into code
|
|
66
|
+
it is not packaged yet, and it needs a map of your components and tokens
|
|
67
|
+
◯ Styleguide a living page of your own components and tokens
|
|
68
|
+
it has to document your components, not Auis's, and that scanner does not exist yet
|
|
69
|
+
◯ UX Flows navigable flow diagrams that are pages in your app
|
|
70
|
+
it is not packaged for an existing app yet
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Today **Review** is the one that installs — pins, comments, the drawer and the
|
|
74
|
+
agent mentions. The other modules stay on the menu, dimmed, with the reason they
|
|
75
|
+
cannot be picked yet; they light up as each one is packaged. In CI or a script,
|
|
76
|
+
skip the menu with `--only review` (or `--yes` for every module that installs
|
|
77
|
+
here). `--only` with a module that cannot install says why and writes nothing.
|
|
78
|
+
|
|
79
|
+
With Review picked, `init` prints what it touches, and touches nothing else:
|
|
80
|
+
|
|
60
81
|
```
|
|
61
82
|
What init touches 7 places
|
|
62
83
|
|
|
63
84
|
create app/api/review-bridge/[...path]/route.ts 15 lines — the bridge's catch-all route
|
|
64
85
|
edit app/layout.tsx +2 lines — one import, one <AuisReview /> before </body>
|
|
65
|
-
edit package.json +1 dependency — @auis/next@^0.
|
|
86
|
+
edit package.json +1 dependency — @auis/next@^0.2.0
|
|
66
87
|
edit package-lock.json npm install
|
|
67
88
|
append .gitignore 4 lines, in a delimited block
|
|
68
89
|
create .auis/manifest.json what auis wrote, so uninstall can reverse it
|
|
@@ -108,7 +129,8 @@ npx @auis/cli@latest init [directory] install Review into an existing N
|
|
|
108
129
|
npx @auis/cli@latest uninstall [directory] take it back out, exactly
|
|
109
130
|
|
|
110
131
|
--json doctor only: the full report as JSON
|
|
111
|
-
--only <
|
|
132
|
+
--only <a,b> init only: install these and skip the menu
|
|
133
|
+
(review, states, edit, styleguide, flows; review installs today)
|
|
112
134
|
--dry-run init only: print what it would touch, write nothing
|
|
113
135
|
--purge uninstall only: delete the comments instead of moving them to the backups
|
|
114
136
|
-r, --ref <ref> branch, tag or commit of the template (default: main)
|
|
@@ -118,6 +140,7 @@ npx @auis/cli@latest uninstall [directory] take it back out, exactly
|
|
|
118
140
|
--force create: scaffold into a directory that is not empty
|
|
119
141
|
uninstall: also remove files you changed since init
|
|
120
142
|
-y, --yes take the defaults, never prompt
|
|
143
|
+
(init: every module that installs in this repository)
|
|
121
144
|
-h, --help show usage
|
|
122
145
|
-v, --version print the CLI version
|
|
123
146
|
```
|
package/package.json
CHANGED
package/src/cli.mjs
CHANGED
|
@@ -39,7 +39,7 @@ export function parseArgs(argv) {
|
|
|
39
39
|
force: false,
|
|
40
40
|
yes: false,
|
|
41
41
|
dryRun: false,
|
|
42
|
-
only:
|
|
42
|
+
only: undefined,
|
|
43
43
|
purge: false,
|
|
44
44
|
help: false,
|
|
45
45
|
version: false,
|
|
@@ -142,12 +142,12 @@ export function help() {
|
|
|
142
142
|
write(` ${c.bold("Usage")}`);
|
|
143
143
|
write(` npx @auis/cli@latest ${c.dim("[directory] [options]")} ${c.dim("scaffold a new project")}`);
|
|
144
144
|
write(` npx @auis/cli@latest doctor ${c.dim("[directory]")} ${c.dim("read an existing repo, write nothing")}`);
|
|
145
|
-
write(` npx @auis/cli@latest init ${c.dim("[directory]")} ${c.dim("install
|
|
145
|
+
write(` npx @auis/cli@latest init ${c.dim("[directory]")} ${c.dim("pick what to install into an existing Next.js app")}`);
|
|
146
146
|
write(` npx @auis/cli@latest uninstall ${c.dim("[directory]")} ${c.dim("take it back out, exactly")}`);
|
|
147
147
|
write();
|
|
148
148
|
write(` ${c.bold("Options")}`);
|
|
149
149
|
write(` --json doctor only: the full report as JSON`);
|
|
150
|
-
write(` --only ${c.dim("<
|
|
150
|
+
write(` --only ${c.dim("<a,b>")} init only: install these, skip the menu ${c.dim(`(${ONLY.join(", ")}; review installs today)`)}`);
|
|
151
151
|
write(` --dry-run init only: print what it would touch, write nothing`);
|
|
152
152
|
write(` --purge uninstall only: delete the comments instead of moving them to the backups`);
|
|
153
153
|
write(` -r, --ref ${c.dim("<ref>")} branch, tag or commit of the template ${c.dim(`(default: ${DEFAULT_REF})`)}`);
|
|
@@ -156,7 +156,7 @@ export function help() {
|
|
|
156
156
|
write(` --no-git skip git init and the first commit`);
|
|
157
157
|
write(` --force create: scaffold into a directory that is not empty`);
|
|
158
158
|
write(` uninstall: also remove files you changed since init`);
|
|
159
|
-
write(` -y, --yes take the defaults, never prompt`);
|
|
159
|
+
write(` -y, --yes take the defaults, never prompt ${c.dim("(init: every module that installs here)")}`);
|
|
160
160
|
write(` -h, --help show this`);
|
|
161
161
|
write(` -v, --version print the CLI version`);
|
|
162
162
|
write();
|
|
@@ -165,6 +165,7 @@ export function help() {
|
|
|
165
165
|
write(` npx @auis/cli@latest . --no-install`);
|
|
166
166
|
write(` npx @auis/cli@latest doctor`);
|
|
167
167
|
write(` npx @auis/cli@latest init --dry-run`);
|
|
168
|
+
write(` npx @auis/cli@latest init --only review --yes`);
|
|
168
169
|
write();
|
|
169
170
|
write(` ${c.dim(REPO_URL)}`);
|
|
170
171
|
write();
|
|
@@ -208,6 +209,7 @@ export async function run(argv = []) {
|
|
|
208
209
|
install: options.install,
|
|
209
210
|
packageManager: options.packageManager,
|
|
210
211
|
only: options.only,
|
|
212
|
+
yes: options.yes,
|
|
211
213
|
});
|
|
212
214
|
}
|
|
213
215
|
if (options.command === "uninstall") {
|
package/src/doctor.mjs
CHANGED
|
@@ -17,6 +17,7 @@ import path from "node:path";
|
|
|
17
17
|
import { color, write } from "./ui.mjs";
|
|
18
18
|
import { detect, detectAgentFiles } from "./detect.mjs";
|
|
19
19
|
import { planInstall } from "./install.mjs";
|
|
20
|
+
import { moduleChoices } from "./modules.mjs";
|
|
20
21
|
|
|
21
22
|
export const DOCTOR_VERSION = 1;
|
|
22
23
|
|
|
@@ -195,6 +196,16 @@ function installable(plan, caps) {
|
|
|
195
196
|
};
|
|
196
197
|
}
|
|
197
198
|
|
|
199
|
+
/** The menu `auis init` would show here: every module, and whether it can go in. */
|
|
200
|
+
function modulesFor(plan) {
|
|
201
|
+
return moduleChoices({ planOk: plan.ok, planReason: plan.reason }).map(({ id, label, selectable, note }) => ({
|
|
202
|
+
id,
|
|
203
|
+
label,
|
|
204
|
+
installable: selectable,
|
|
205
|
+
reason: note,
|
|
206
|
+
}));
|
|
207
|
+
}
|
|
208
|
+
|
|
198
209
|
export function buildReport(root, cliVersion) {
|
|
199
210
|
const d = detect(root);
|
|
200
211
|
const caps = capabilities(d);
|
|
@@ -210,6 +221,7 @@ export function buildReport(root, cliVersion) {
|
|
|
210
221
|
capabilities: caps,
|
|
211
222
|
plan: installPlan(d, root),
|
|
212
223
|
installable: installable(planInstall(d, root), caps),
|
|
224
|
+
modules: modulesFor(planInstall(d, root)),
|
|
213
225
|
summary,
|
|
214
226
|
};
|
|
215
227
|
}
|
|
@@ -272,6 +284,14 @@ export function report(doc) {
|
|
|
272
284
|
}
|
|
273
285
|
}
|
|
274
286
|
|
|
287
|
+
write();
|
|
288
|
+
write(` ${color.bold("Modules")} ${color.dim("what `auis init` offers in this repository")}`);
|
|
289
|
+
write();
|
|
290
|
+
for (const m of doc.modules) {
|
|
291
|
+
const mark = m.installable ? color.green("✓") : color.dim("·");
|
|
292
|
+
write(` ${mark} ${(m.installable ? color.bold : color.dim)(m.label.padEnd(14))}${m.installable ? color.dim("installs with `auis init`") : color.dim(m.reason)}`);
|
|
293
|
+
}
|
|
294
|
+
|
|
275
295
|
write();
|
|
276
296
|
if (doc.installable.ok) {
|
|
277
297
|
write(` ${color.bold("What `auis init` would touch")} ${color.dim(`${doc.plan.length} places`)}`);
|
|
@@ -280,9 +300,8 @@ export function report(doc) {
|
|
|
280
300
|
write(` ${color.dim(step.action.padEnd(8))}${step.path.padEnd(44)}${color.dim(step.detail)}`);
|
|
281
301
|
}
|
|
282
302
|
write();
|
|
283
|
-
write(` ${color.dim("
|
|
284
|
-
write(` ${color.dim("
|
|
285
|
-
write(` ${color.dim("created with `npx @auis/cli <dir>`; they are not installable into an existing app yet.")}`);
|
|
303
|
+
write(` ${color.dim("Pick what to install with `npx @auis/cli init`; `npx @auis/cli uninstall` reverses it.")}`);
|
|
304
|
+
write(` ${color.dim("Every module runs today in a project created with `npx @auis/cli <dir>`.")}`);
|
|
286
305
|
} else {
|
|
287
306
|
write(` ${color.bold("auis init")} ${color.dim(doc.installable.reason)}`);
|
|
288
307
|
}
|
package/src/install.mjs
CHANGED
|
@@ -30,12 +30,20 @@ import crypto from "node:crypto";
|
|
|
30
30
|
import fs from "node:fs";
|
|
31
31
|
import os from "node:os";
|
|
32
32
|
import path from "node:path";
|
|
33
|
-
import { color, fail, step, warn, write } from "./ui.mjs";
|
|
33
|
+
import { color, fail, pickMany, step, warn, write } from "./ui.mjs";
|
|
34
34
|
import { detect } from "./detect.mjs";
|
|
35
35
|
import { exec } from "./template.mjs";
|
|
36
|
+
import {
|
|
37
|
+
MODULE_IDS,
|
|
38
|
+
initialPicker,
|
|
39
|
+
moduleChoices,
|
|
40
|
+
pickedIds,
|
|
41
|
+
pickerReducer,
|
|
42
|
+
resolveOnly,
|
|
43
|
+
} from "./modules.mjs";
|
|
36
44
|
|
|
37
45
|
/** The kit version `init` writes. Kept equal to kit/next/package.json by a test. */
|
|
38
|
-
export const KIT_VERSION = "0.
|
|
46
|
+
export const KIT_VERSION = "0.2.0";
|
|
39
47
|
export const KIT_PACKAGE = "@auis/next";
|
|
40
48
|
export const MANIFEST_VERSION = 1;
|
|
41
49
|
|
|
@@ -55,7 +63,8 @@ const LOCKFILES = {
|
|
|
55
63
|
bun: ["bun.lock", "bun.lockb"],
|
|
56
64
|
};
|
|
57
65
|
|
|
58
|
-
|
|
66
|
+
/** Every module `--only` names; which of them install today is in modules.mjs. */
|
|
67
|
+
export const ONLY = MODULE_IDS;
|
|
59
68
|
|
|
60
69
|
const sha = (buf) => crypto.createHash("sha256").update(buf).digest("hex");
|
|
61
70
|
const abs = (root, rel) => path.join(root, rel);
|
|
@@ -275,8 +284,10 @@ export async function init({
|
|
|
275
284
|
dryRun = false,
|
|
276
285
|
install = true,
|
|
277
286
|
packageManager,
|
|
278
|
-
only
|
|
287
|
+
only,
|
|
288
|
+
yes = false,
|
|
279
289
|
kitRange = `^${KIT_VERSION}`,
|
|
290
|
+
pick = pickMany,
|
|
280
291
|
} = {}) {
|
|
281
292
|
const root = path.resolve(directory);
|
|
282
293
|
if (!fs.existsSync(abs(root, "package.json"))) {
|
|
@@ -286,11 +297,6 @@ export async function init({
|
|
|
286
297
|
);
|
|
287
298
|
return 1;
|
|
288
299
|
}
|
|
289
|
-
if (!ONLY.includes(only)) {
|
|
290
|
-
fail(`--only ${only}: only ${ONLY.join(", ")} can be installed into an existing app today.`);
|
|
291
|
-
return 1;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
300
|
const existing = readManifest(root);
|
|
295
301
|
if (existing) {
|
|
296
302
|
write(` ${color.green("✓")} Already installed ${color.dim(`(${MANIFEST}, ${existing.createdAt})`)} — nothing to do.`);
|
|
@@ -301,6 +307,32 @@ export async function init({
|
|
|
301
307
|
|
|
302
308
|
const d = detect(root);
|
|
303
309
|
const plan = planInstall(d, root, { packageManager });
|
|
310
|
+
|
|
311
|
+
// What to install: `--only`, else the menu (a TTY without --yes), else every
|
|
312
|
+
// module that can go in here. Today that is Review alone; the menu still
|
|
313
|
+
// lists the rest, each with the reason it cannot be picked yet.
|
|
314
|
+
const choices = moduleChoices({ planOk: plan.ok, planReason: plan.reason });
|
|
315
|
+
let modules;
|
|
316
|
+
if (only !== undefined) {
|
|
317
|
+
const resolved = resolveOnly(only, choices);
|
|
318
|
+
if (!resolved.ok) {
|
|
319
|
+
fail(`${resolved.message}\n Run ${color.bold("npx @auis/cli doctor")} for what Auis can do here.`);
|
|
320
|
+
return 1;
|
|
321
|
+
}
|
|
322
|
+
modules = resolved.ids;
|
|
323
|
+
} else {
|
|
324
|
+
const canPick = choices.some((c) => c.selectable);
|
|
325
|
+
const picked = canPick && !yes ? await pick("What should auis install?", choices, { initialPicker, pickerReducer }) : null;
|
|
326
|
+
if (picked?.cancelled) {
|
|
327
|
+
write();
|
|
328
|
+
write(` ${color.dim("Nothing was installed.")}`);
|
|
329
|
+
write();
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
332
|
+
modules = picked ? pickedIds(picked) : choices.filter((c) => c.selectable).map((c) => c.id);
|
|
333
|
+
if (picked) write();
|
|
334
|
+
}
|
|
335
|
+
|
|
304
336
|
if (!plan.ok) {
|
|
305
337
|
fail(`${plan.reason}\n Run ${color.bold("npx @auis/cli doctor")} for what Auis can still do here.`);
|
|
306
338
|
return 1;
|
|
@@ -409,7 +441,7 @@ export async function init({
|
|
|
409
441
|
const manifest = {
|
|
410
442
|
auisManifestVersion: MANIFEST_VERSION,
|
|
411
443
|
createdAt: new Date().toISOString(),
|
|
412
|
-
only:
|
|
444
|
+
only: modules,
|
|
413
445
|
kit: { package: KIT_PACKAGE, range: kitRange },
|
|
414
446
|
packageManager: plan.packageManager,
|
|
415
447
|
artifacts,
|
package/src/modules.mjs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `auis init` can install into an existing app, one module per thing a
|
|
3
|
+
* person would pick. Only `installable` modules can be chosen; the others are
|
|
4
|
+
* listed with the reason they cannot be yet, so the menu tells the truth about
|
|
5
|
+
* what Auis does today instead of hiding the rest.
|
|
6
|
+
*
|
|
7
|
+
* Whether an installable module can go into THIS repository is a second
|
|
8
|
+
* question, answered by the install plan (`planInstall`, the same one `doctor`
|
|
9
|
+
* prints): `moduleChoices` folds that answer in.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export const MODULES = [
|
|
13
|
+
{
|
|
14
|
+
id: "review",
|
|
15
|
+
label: "Review",
|
|
16
|
+
summary: "pins, comments, the drawer and agent mentions",
|
|
17
|
+
status: "installable",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: "states",
|
|
21
|
+
label: "State Mode",
|
|
22
|
+
summary: "switch a screen's states in place, and ?ge= deep links that replay clicks",
|
|
23
|
+
status: "planned",
|
|
24
|
+
reason: "it is not packaged for an existing app yet, and it needs a registry of your screens",
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
id: "edit",
|
|
28
|
+
label: "Edit Mode",
|
|
29
|
+
summary: "visual edits an agent turns into code",
|
|
30
|
+
status: "planned",
|
|
31
|
+
reason: "it is not packaged yet, and it needs a map of your components and tokens",
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: "styleguide",
|
|
35
|
+
label: "Styleguide",
|
|
36
|
+
summary: "a living page of your own components and tokens",
|
|
37
|
+
status: "planned",
|
|
38
|
+
reason: "it has to document your components, not Auis's, and that scanner does not exist yet",
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: "flows",
|
|
42
|
+
label: "UX Flows",
|
|
43
|
+
summary: "navigable flow diagrams that are pages in your app",
|
|
44
|
+
status: "planned",
|
|
45
|
+
reason: "it is not packaged for an existing app yet",
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
export const MODULE_IDS = MODULES.map((m) => m.id);
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The menu rows for this repository. A module is selectable when it is
|
|
53
|
+
* installable AND the install plan works here; otherwise the row says why.
|
|
54
|
+
* The default selection is every selectable module.
|
|
55
|
+
*/
|
|
56
|
+
export function moduleChoices({ planOk = true, planReason = null } = {}) {
|
|
57
|
+
return MODULES.map((m) => {
|
|
58
|
+
if (m.status !== "installable") {
|
|
59
|
+
return { id: m.id, label: m.label, summary: m.summary, selectable: false, checked: false, note: m.reason };
|
|
60
|
+
}
|
|
61
|
+
if (!planOk) {
|
|
62
|
+
return { id: m.id, label: m.label, summary: m.summary, selectable: false, checked: false, note: planReason };
|
|
63
|
+
}
|
|
64
|
+
return { id: m.id, label: m.label, summary: m.summary, selectable: true, checked: true, note: null };
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* `--only review` or `--only review,states`: the ids, in catalog order, or the
|
|
70
|
+
* reason the list cannot be installed. An unknown id names the valid ones; a
|
|
71
|
+
* known one that cannot be installed says why.
|
|
72
|
+
*/
|
|
73
|
+
export function resolveOnly(value, choices) {
|
|
74
|
+
const asked = String(value ?? "")
|
|
75
|
+
.split(",")
|
|
76
|
+
.map((s) => s.trim().toLowerCase())
|
|
77
|
+
.filter(Boolean);
|
|
78
|
+
if (asked.length === 0) return { ok: false, message: `--only needs a module: ${MODULE_IDS.join(", ")}` };
|
|
79
|
+
const unknown = asked.filter((id) => !MODULE_IDS.includes(id));
|
|
80
|
+
if (unknown.length > 0) {
|
|
81
|
+
return { ok: false, message: `--only ${unknown.join(", ")}: unknown module. Modules: ${MODULE_IDS.join(", ")}` };
|
|
82
|
+
}
|
|
83
|
+
for (const id of asked) {
|
|
84
|
+
const row = choices.find((c) => c.id === id);
|
|
85
|
+
if (!row.selectable) return { ok: false, message: `${row.label} cannot be installed here: ${row.note}.` };
|
|
86
|
+
}
|
|
87
|
+
return { ok: true, ids: MODULE_IDS.filter((id) => asked.includes(id)) };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The picker's state machine, with no terminal in it so it can be tested.
|
|
92
|
+
* Keys: "up", "down", "space" (toggle), "all" (a: toggle every selectable),
|
|
93
|
+
* "enter" (confirm; refused with nothing selected), "cancel" (Esc, Ctrl-C).
|
|
94
|
+
*/
|
|
95
|
+
export function initialPicker(rows) {
|
|
96
|
+
const first = rows.findIndex((r) => r.selectable);
|
|
97
|
+
return {
|
|
98
|
+
rows,
|
|
99
|
+
cursor: first === -1 ? 0 : first,
|
|
100
|
+
checked: rows.filter((r) => r.checked).map((r) => r.id),
|
|
101
|
+
done: false,
|
|
102
|
+
cancelled: false,
|
|
103
|
+
hint: null,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function pickerReducer(state, key) {
|
|
108
|
+
if (state.done || state.cancelled) return state;
|
|
109
|
+
const { rows } = state;
|
|
110
|
+
const selectable = rows.filter((r) => r.selectable).map((r) => r.id);
|
|
111
|
+
switch (key) {
|
|
112
|
+
case "up":
|
|
113
|
+
return { ...state, cursor: (state.cursor - 1 + rows.length) % rows.length, hint: null };
|
|
114
|
+
case "down":
|
|
115
|
+
return { ...state, cursor: (state.cursor + 1) % rows.length, hint: null };
|
|
116
|
+
case "space": {
|
|
117
|
+
const row = rows[state.cursor];
|
|
118
|
+
if (!row.selectable) return { ...state, hint: `${row.label}: ${row.note}` };
|
|
119
|
+
const on = state.checked.includes(row.id);
|
|
120
|
+
return {
|
|
121
|
+
...state,
|
|
122
|
+
checked: on ? state.checked.filter((id) => id !== row.id) : [...state.checked, row.id],
|
|
123
|
+
hint: null,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
case "all": {
|
|
127
|
+
const allOn = selectable.every((id) => state.checked.includes(id));
|
|
128
|
+
return { ...state, checked: allOn ? [] : selectable, hint: null };
|
|
129
|
+
}
|
|
130
|
+
case "enter":
|
|
131
|
+
if (state.checked.length === 0) return { ...state, hint: "Pick at least one module, or press Esc to leave." };
|
|
132
|
+
return { ...state, done: true, hint: null };
|
|
133
|
+
case "cancel":
|
|
134
|
+
return { ...state, cancelled: true };
|
|
135
|
+
default:
|
|
136
|
+
return state;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The ids a finished picker chose, in catalog order. */
|
|
141
|
+
export function pickedIds(state) {
|
|
142
|
+
return MODULE_IDS.filter((id) => state.checked.includes(id));
|
|
143
|
+
}
|
package/src/ui.mjs
CHANGED
|
@@ -82,3 +82,64 @@ export async function ask(question, fallback) {
|
|
|
82
82
|
rl.close();
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A checkbox menu in the terminal: ↑/↓ to move, space to toggle, a for all,
|
|
88
|
+
* enter to confirm, Esc to leave. Rows that cannot be picked stay visible,
|
|
89
|
+
* dimmed, with their reason. Resolves to the finished picker state (see
|
|
90
|
+
* modules.mjs), or null when stdin is not a TTY.
|
|
91
|
+
*/
|
|
92
|
+
export async function pickMany(title, rows, { initialPicker, pickerReducer }) {
|
|
93
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) return null;
|
|
94
|
+
const readline = await import("node:readline");
|
|
95
|
+
let state = initialPicker(rows);
|
|
96
|
+
let drawn = 0;
|
|
97
|
+
|
|
98
|
+
const render = () => {
|
|
99
|
+
if (drawn > 0) process.stdout.write(`${ESC}[${drawn}A${ESC}[0J`);
|
|
100
|
+
const lines = [` ${color.bold(title)} ${color.dim("↑↓ move · space toggle · a all · enter install · esc cancel")}`, ""];
|
|
101
|
+
const width = Math.max(...state.rows.map((r) => r.label.length)) + 2;
|
|
102
|
+
state.rows.forEach((row, i) => {
|
|
103
|
+
const pointer = i === state.cursor ? color.cyan("❯") : " ";
|
|
104
|
+
const box = row.selectable ? (state.checked.includes(row.id) ? color.green("◉") : "◯") : color.dim("◯");
|
|
105
|
+
const label = row.selectable ? color.bold(row.label.padEnd(width)) : color.dim(row.label.padEnd(width));
|
|
106
|
+
lines.push(` ${pointer} ${box} ${label}${color.dim(row.summary)}`);
|
|
107
|
+
if (!row.selectable) lines.push(` ${" ".repeat(width)}${color.dim(row.note)}`);
|
|
108
|
+
});
|
|
109
|
+
lines.push("", state.hint ? ` ${color.yellow("!")} ${state.hint}` : "");
|
|
110
|
+
process.stdout.write(`${lines.join("\n")}\n`);
|
|
111
|
+
drawn = lines.length;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
readline.emitKeypressEvents(process.stdin);
|
|
115
|
+
process.stdin.setRawMode(true);
|
|
116
|
+
process.stdout.write(`${ESC}[?25l`);
|
|
117
|
+
render();
|
|
118
|
+
try {
|
|
119
|
+
return await new Promise((resolve) => {
|
|
120
|
+
const onKey = (_str, key = {}) => {
|
|
121
|
+
const name =
|
|
122
|
+
key.ctrl && key.name === "c" ? "cancel"
|
|
123
|
+
: key.name === "escape" ? "cancel"
|
|
124
|
+
: key.name === "return" ? "enter"
|
|
125
|
+
: key.name === "space" ? "space"
|
|
126
|
+
: key.name === "up" || key.name === "k" ? "up"
|
|
127
|
+
: key.name === "down" || key.name === "j" ? "down"
|
|
128
|
+
: key.name === "a" ? "all"
|
|
129
|
+
: null;
|
|
130
|
+
if (!name) return;
|
|
131
|
+
state = pickerReducer(state, name);
|
|
132
|
+
render();
|
|
133
|
+
if (state.done || state.cancelled) {
|
|
134
|
+
process.stdin.off("keypress", onKey);
|
|
135
|
+
resolve(state);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
process.stdin.on("keypress", onKey);
|
|
139
|
+
});
|
|
140
|
+
} finally {
|
|
141
|
+
process.stdin.setRawMode(false);
|
|
142
|
+
process.stdin.pause();
|
|
143
|
+
process.stdout.write(`${ESC}[?25h`);
|
|
144
|
+
}
|
|
145
|
+
}
|