repown 0.1.1 → 0.3.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/CHANGELOG.md +77 -1
- package/README.md +314 -149
- package/dist/cli.js +18 -17
- package/dist/commands/doctor.js +323 -50
- package/dist/commands/fix.js +14 -12
- package/dist/commands/guard.js +10 -2
- package/dist/commands/scan.js +14 -7
- package/dist/commands/setup.js +27 -0
- package/dist/commands/start.js +56 -0
- package/dist/commands/status.js +244 -41
- package/dist/commands/use.js +139 -10
- package/dist/core/credential/gh.js +96 -9
- package/dist/core/exec.js +55 -6
- package/dist/core/git.js +43 -1
- package/dist/core/guard/check.js +1 -1
- package/dist/core/guard/hook.js +11 -2
- package/dist/core/hosts/github.js +31 -14
- package/dist/core/identity.js +28 -2
- package/dist/core/inspect.js +3 -2
- package/dist/core/unpushed.js +110 -0
- package/dist/core/version.js +17 -0
- package/dist/program.js +13 -0
- package/dist/ui/dispatch.js +23 -5
- package/dist/ui/format.js +15 -1
- package/dist/ui/help.js +7 -2
- package/dist/wizard/clack.js +132 -0
- package/dist/wizard/engine.js +244 -0
- package/dist/wizard/home-context.js +101 -0
- package/dist/wizard/home-flow.js +90 -0
- package/dist/wizard/home-run.js +148 -0
- package/dist/wizard/home-text.js +50 -0
- package/dist/wizard/plain.js +183 -0
- package/dist/wizard/review-text.js +110 -0
- package/dist/wizard/setup-changes.js +73 -0
- package/dist/wizard/setup-context.js +256 -0
- package/dist/wizard/setup-flow.js +733 -0
- package/dist/wizard/setup-run.js +473 -0
- package/npm-shrinkwrap.json +121 -0
- package/package.json +6 -2
package/dist/ui/dispatch.js
CHANGED
|
@@ -11,10 +11,14 @@ import { renderHelpFor } from "./help.js";
|
|
|
11
11
|
import { suggest } from "./suggest.js";
|
|
12
12
|
import { ok, err } from "../core/result.js";
|
|
13
13
|
import * as out from "./format.js";
|
|
14
|
+
/** Every command the program declares, in declaration order. Help uses this, and so can a runner. */
|
|
15
|
+
export async function loadEntries(commands) {
|
|
16
|
+
const names = Object.keys(commands);
|
|
17
|
+
const loaded = await Promise.all(names.map((name) => commands[name]()));
|
|
18
|
+
return new Map(names.map((name, index) => [name, loaded[index]]));
|
|
19
|
+
}
|
|
14
20
|
async function loadAll(program) {
|
|
15
|
-
|
|
16
|
-
const entries = await Promise.all(names.map((name) => program.commands[name]()));
|
|
17
|
-
return new Map(names.map((name, index) => [name, entries[index]]));
|
|
21
|
+
return loadEntries(program.commands);
|
|
18
22
|
}
|
|
19
23
|
function reportUsageError(tag, message) {
|
|
20
24
|
const [first, ...rest] = message.split('\n');
|
|
@@ -121,15 +125,29 @@ export async function runProgram(program, argv) {
|
|
|
121
125
|
return runHelp(program, []);
|
|
122
126
|
if (argv[0] === 'help')
|
|
123
127
|
return runHelp(program, argv.slice(1));
|
|
124
|
-
|
|
128
|
+
const words = await bareArgv(program, argv);
|
|
129
|
+
if (typeof words === 'function')
|
|
130
|
+
return words();
|
|
131
|
+
if (words === 'help')
|
|
132
|
+
return runHelp(program, []);
|
|
133
|
+
if (words.length === 0 && program.defaultCommand === undefined)
|
|
125
134
|
return runHelp(program, []);
|
|
126
|
-
const top = await resolveTop(program,
|
|
135
|
+
const top = await resolveTop(program, words);
|
|
127
136
|
if (!top.ok) {
|
|
128
137
|
reportUsageError(program.name, top.error + helpPointer(program, []));
|
|
129
138
|
return 2;
|
|
130
139
|
}
|
|
131
140
|
return dispatch(program, top.value);
|
|
132
141
|
}
|
|
142
|
+
/** Empty argv with a chooser becomes that command, `'help'` for the top help, or a runner. */
|
|
143
|
+
async function bareArgv(program, argv) {
|
|
144
|
+
if (argv.length > 0 || program.chooseDefault === undefined)
|
|
145
|
+
return argv;
|
|
146
|
+
const chosen = await program.chooseDefault();
|
|
147
|
+
if (typeof chosen === 'function')
|
|
148
|
+
return chosen;
|
|
149
|
+
return chosen === 'help' ? 'help' : [chosen];
|
|
150
|
+
}
|
|
133
151
|
/** The whole life of the process: run, set the exit code, and report a crash as a failure. */
|
|
134
152
|
export function start(program) {
|
|
135
153
|
out.ignoreBrokenPipe();
|
package/dist/ui/format.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// saying which check spoke, then the message.
|
|
3
3
|
//
|
|
4
4
|
// OK identity octocat <...> push-as:octocat
|
|
5
|
-
//
|
|
5
|
+
// NOTE gh active as someone else, so `gh pr create` would act as them
|
|
6
6
|
// fix: gh auth switch -u octocat
|
|
7
7
|
const COLOURS = {
|
|
8
8
|
green: '\x1b[32m',
|
|
@@ -43,6 +43,10 @@ export function pass(tag, message) {
|
|
|
43
43
|
export function warn(tag, message) {
|
|
44
44
|
status({ stream: process.stderr, word: 'WARN', colour: 'yellow' }, tag, message);
|
|
45
45
|
}
|
|
46
|
+
/** Optional advice. Same columns as warn, on stderr. Dim when that stream has colour. */
|
|
47
|
+
export function noted(tag, message) {
|
|
48
|
+
status({ stream: process.stderr, word: 'NOTE', colour: 'dim' }, tag, message);
|
|
49
|
+
}
|
|
46
50
|
export function fail(tag, message) {
|
|
47
51
|
status({ stream: process.stderr, word: 'FAIL', colour: 'red' }, tag, message);
|
|
48
52
|
}
|
|
@@ -50,6 +54,10 @@ export function fail(tag, message) {
|
|
|
50
54
|
export function detail(message) {
|
|
51
55
|
process.stderr.write(' ' + message + '\n');
|
|
52
56
|
}
|
|
57
|
+
/** Stderr with no indent. `detail`'s seven spaces would attach a closing tally to the row above. */
|
|
58
|
+
export function note(message) {
|
|
59
|
+
process.stderr.write(message + '\n');
|
|
60
|
+
}
|
|
53
61
|
export function line(message = '') {
|
|
54
62
|
process.stdout.write(message + '\n');
|
|
55
63
|
}
|
|
@@ -67,6 +75,12 @@ export function heading(text) {
|
|
|
67
75
|
export function dim(text) {
|
|
68
76
|
return paint(process.stdout, 'dim', text);
|
|
69
77
|
}
|
|
78
|
+
/** Shown to a person. Windows uses native separators; any other platform is unchanged. */
|
|
79
|
+
export function displayPath(path) {
|
|
80
|
+
if (process.platform !== 'win32')
|
|
81
|
+
return path;
|
|
82
|
+
return path.replaceAll('/', '\\');
|
|
83
|
+
}
|
|
70
84
|
/**
|
|
71
85
|
* A reader closing early (`repown help | head -3`) delivers EPIPE on the next
|
|
72
86
|
* write. That is the reader's choice, not a fault in repown, so the write is
|
package/dist/ui/help.js
CHANGED
|
@@ -20,7 +20,10 @@ function topHeader(entries) {
|
|
|
20
20
|
const lines = ['', ' repown <command> [options]', ''];
|
|
21
21
|
for (const [name, entry] of entries)
|
|
22
22
|
lines.push(' ' + name.padEnd(10) + entry.summary);
|
|
23
|
-
lines.push('', '
|
|
23
|
+
lines.push('', ' Bare `repown`, with stdin, stdout and stderr all terminals: `repown setup` in a');
|
|
24
|
+
lines.push(' clone, and a start screen outside a clone or in a bare repository. With only');
|
|
25
|
+
lines.push(' stdout redirected: status in a clone, and this help outside one (exit 0). With');
|
|
26
|
+
lines.push(' no terminal on stdin or stderr, it is status (exit 1 outside a clone).');
|
|
24
27
|
lines.push(' Run `repown help <command>` for its options.', '');
|
|
25
28
|
return lines;
|
|
26
29
|
}
|
|
@@ -93,5 +96,7 @@ function optionHelp(option) {
|
|
|
93
96
|
const value = option.kind === 'boolean' ? '' : ' <value>';
|
|
94
97
|
const choices = option.choices ? ' (' + option.choices.join('|') + ')' : '';
|
|
95
98
|
const fallback = option.default !== undefined && option.default !== '' ? ' [default: ' + option.default + ']' : '';
|
|
96
|
-
|
|
99
|
+
const head = '--' + option.name + value;
|
|
100
|
+
// A name too long for the column still gets a gap before its help.
|
|
101
|
+
return (head.length >= 20 ? head + ' ' : head.padEnd(20)) + option.help + choices + fallback;
|
|
97
102
|
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// The wizard drawn with @clack/prompts: arrow-key choices with a hint each, defaults,
|
|
2
|
+
// a line while state is read, the review in a box. The ONLY file that imports the
|
|
3
|
+
// library, and it is loaded with a dynamic import() from choosePrompter (ADR-016),
|
|
4
|
+
// so no other command -- and never the pre-push hook -- depends on it.
|
|
5
|
+
//
|
|
6
|
+
// Everything is drawn on the stream it's given (stderr), so stdout keeps carrying only
|
|
7
|
+
// what the commands print. Back is the engine's: a "← Back" choice, or `<` typed at a
|
|
8
|
+
// text question, offered from the second question on. Esc and Ctrl-C cancel.
|
|
9
|
+
import { styleText } from 'node:util';
|
|
10
|
+
import * as p from '@clack/prompts';
|
|
11
|
+
import { BACK, CANCEL } from "./engine.js";
|
|
12
|
+
import { BACK_WORD } from "./plain.js";
|
|
13
|
+
import { BACK_TO_REVIEW, PICK_QUESTION, RUN_THIS_STEP, reviewDefault, reviewLines, reviewOptions, reviewQuestion, stepConfirmLines, stepOptions, textWidth, wrap } from "./review-text.js";
|
|
14
|
+
/** A value no real choice can have. */
|
|
15
|
+
const GO_BACK = '\u0000back';
|
|
16
|
+
export function clackPrompter(streams) {
|
|
17
|
+
const io = { input: streams.input, output: streams.output };
|
|
18
|
+
return {
|
|
19
|
+
ask: (step, asked) => askDrawn(step, asked, io),
|
|
20
|
+
review: (review) => showReview(review, io),
|
|
21
|
+
pickStep: (steps) => pickClack(steps, io),
|
|
22
|
+
choose: (message, options) => chooseClack(message, options, io),
|
|
23
|
+
note: (message) => p.log.warn(wrap(message, widthOf(io, GUTTER)).join('\n'), io),
|
|
24
|
+
show: (lines) => logLines(io, lines),
|
|
25
|
+
confirmStep: (confirm) => showStep(confirm, io),
|
|
26
|
+
close: () => { },
|
|
27
|
+
intro: (title) => p.intro(title, io),
|
|
28
|
+
// After a cancel, clack has already drawn the gutter's last line.
|
|
29
|
+
outro: (message, cancelled) => (cancelled ? p.cancel(message, io) : p.outro(message, io)),
|
|
30
|
+
// A line, not clack's spinner: the spinner takes over Ctrl-C and exits 0, where a
|
|
31
|
+
// cancel must exit 130.
|
|
32
|
+
busy: (message) => p.log.step(message, io),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function askDrawn(step, asked, io) {
|
|
36
|
+
if (asked.detail)
|
|
37
|
+
p.log.info(wrap(asked.detail, widthOf(io, GUTTER)).join('\n'), io);
|
|
38
|
+
return askStep(step, asked, io);
|
|
39
|
+
}
|
|
40
|
+
async function pickClack(steps, io) {
|
|
41
|
+
const id = await p.select({ ...io, message: PICK_QUESTION, options: pickOptions(steps) });
|
|
42
|
+
if (p.isCancel(id))
|
|
43
|
+
return CANCEL;
|
|
44
|
+
return id === GO_BACK ? BACK : id;
|
|
45
|
+
}
|
|
46
|
+
function pickOptions(steps) {
|
|
47
|
+
return [...steps.map((step) => ({ value: step.id, label: step.message })), { value: GO_BACK, label: '← ' + BACK_TO_REVIEW }];
|
|
48
|
+
}
|
|
49
|
+
async function chooseClack(message, options, io) {
|
|
50
|
+
const value = await p.select({ ...io, message, options: options.map(choiceOption) });
|
|
51
|
+
return p.isCancel(value) ? CANCEL : value;
|
|
52
|
+
}
|
|
53
|
+
function choiceOption(option) {
|
|
54
|
+
return option.hint === undefined ? { value: option.value, label: option.label } : option;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Columns to leave free. clack wraps a list question 13 columns short of the window and
|
|
58
|
+
* a box's text 6 short, but never a text question's hint or a log line; wrapping every
|
|
59
|
+
* one here, within those widths, keeps them all in the gutter and none wrapped twice.
|
|
60
|
+
*/
|
|
61
|
+
const GUTTER = 13;
|
|
62
|
+
const BOX = 6;
|
|
63
|
+
function widthOf(io, margin) {
|
|
64
|
+
return textWidth(io.output.columns, margin);
|
|
65
|
+
}
|
|
66
|
+
function askStep(step, asked, io) {
|
|
67
|
+
if (step.kind === 'select')
|
|
68
|
+
return askSelect(step, asked, io);
|
|
69
|
+
if (step.kind === 'confirm')
|
|
70
|
+
return askConfirm(step, asked, io);
|
|
71
|
+
return askText(step, asked, io);
|
|
72
|
+
}
|
|
73
|
+
/** "← Back", unless this is the first question asked and there is nowhere to go. */
|
|
74
|
+
function backOption(asked) {
|
|
75
|
+
return asked.canGoBack ? [{ value: GO_BACK, label: '← Back', hint: 'to the previous question' }] : [];
|
|
76
|
+
}
|
|
77
|
+
async function askSelect(step, asked, io) {
|
|
78
|
+
const options = [...asked.choices.map((choice) => ({ ...choice })), ...backOption(asked)];
|
|
79
|
+
const preset = typeof asked.initial === 'string' ? { initialValue: asked.initial } : {};
|
|
80
|
+
const value = await p.select({ ...io, ...preset, message: messageOf(step, io), options });
|
|
81
|
+
if (p.isCancel(value))
|
|
82
|
+
return CANCEL;
|
|
83
|
+
return value === GO_BACK ? BACK : value;
|
|
84
|
+
}
|
|
85
|
+
async function askConfirm(step, asked, io) {
|
|
86
|
+
const value = await p.select({ ...io, message: messageOf(step, io), initialValue: asked.initial === true ? 'yes' : 'no',
|
|
87
|
+
options: [{ value: 'yes', label: 'Yes' }, { value: 'no', label: 'No' }, ...backOption(asked)] });
|
|
88
|
+
if (p.isCancel(value))
|
|
89
|
+
return CANCEL;
|
|
90
|
+
return value === GO_BACK ? BACK : value === 'yes';
|
|
91
|
+
}
|
|
92
|
+
async function askText(step, asked, io) {
|
|
93
|
+
const preset = typeof asked.initial === 'string' ? { initialValue: asked.initial } : {};
|
|
94
|
+
const extra = asked.canGoBack ? 'type ' + BACK_WORD + ' to go back' : undefined;
|
|
95
|
+
const value = await p.text({ ...io, ...preset, message: messageOf(step, io, extra),
|
|
96
|
+
validate: (typed) => (isBack(typed, asked) ? undefined : step.validate?.((typed ?? '').trim()) ?? undefined) });
|
|
97
|
+
if (p.isCancel(value))
|
|
98
|
+
return CANCEL;
|
|
99
|
+
return isBack(value, asked) ? BACK : value.trim();
|
|
100
|
+
}
|
|
101
|
+
/** `<` means back only where there is somewhere to go; on the first question it is just text. */
|
|
102
|
+
function isBack(typed, asked) {
|
|
103
|
+
return asked.canGoBack && typed?.trim() === BACK_WORD;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* clack draws its gutter in front of a select's second line but not a text prompt's,
|
|
107
|
+
* so a text question draws it itself.
|
|
108
|
+
*/
|
|
109
|
+
const TEXT_GUTTER = styleText('gray', '│') + ' ';
|
|
110
|
+
/** The question, and under it the hint, wrapped in the gutter -- visible whatever the answer shows. */
|
|
111
|
+
function messageOf(step, io, extra) {
|
|
112
|
+
const hint = [step.hint, extra].filter((part) => part).join(' · ');
|
|
113
|
+
const gutter = step.kind === 'text' ? TEXT_GUTTER : '';
|
|
114
|
+
return [step.message, ...(hint ? wrap(hint, widthOf(io, GUTTER)) : [])].join('\n' + gutter);
|
|
115
|
+
}
|
|
116
|
+
async function showStep(confirm, io) {
|
|
117
|
+
logLines(io, stepConfirmLines(confirm));
|
|
118
|
+
return chosen(confirm, io);
|
|
119
|
+
}
|
|
120
|
+
function logLines(io, lines) {
|
|
121
|
+
p.log.message(lines.flatMap((text) => wrap(text, widthOf(io, GUTTER))).join('\n'), io);
|
|
122
|
+
}
|
|
123
|
+
async function chosen(confirm, io) {
|
|
124
|
+
const choice = await p.select({ ...io, message: RUN_THIS_STEP, options: stepOptions(), initialValue: confirm.initial });
|
|
125
|
+
return p.isCancel(choice) ? CANCEL : choice;
|
|
126
|
+
}
|
|
127
|
+
async function showReview(review, io) {
|
|
128
|
+
p.note(reviewLines(review, widthOf(io, BOX), (text) => styleText('dim', text)).join('\n'), review.title, io);
|
|
129
|
+
const choice = await p.select({ ...io, message: reviewQuestion(review), options: reviewOptions(review),
|
|
130
|
+
initialValue: reviewDefault(review) });
|
|
131
|
+
return p.isCancel(choice) ? CANCEL : choice;
|
|
132
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// The wizard's navigation: which step comes next, going back, skipping what was
|
|
2
|
+
// given as a flag, and the review loop. It draws nothing itself -- a Prompter does
|
|
3
|
+
// (src/wizard/plain.ts, src/wizard/clack.ts, or a scripted one in the tests) -- and
|
|
4
|
+
// it writes nothing: it only returns the answers the review accepted. What those
|
|
5
|
+
// answers turn into is the flow's job (src/wizard/setup-flow.ts).
|
|
6
|
+
//
|
|
7
|
+
// WHY THE ENGINE OWNS "BACK". No prompt library draws navigation across questions
|
|
8
|
+
// (@clack/prompts has none), so the history lives here, where the scripted tests
|
|
9
|
+
// can drive it without a terminal.
|
|
10
|
+
export const BACK = Symbol('back');
|
|
11
|
+
export const CANCEL = Symbol('cancel');
|
|
12
|
+
/** Asks every reachable step from `pass.start` on, with a history for going back. */
|
|
13
|
+
export async function runFlow(flow, context, pass, prompter) {
|
|
14
|
+
const answers = { ...pass.answers };
|
|
15
|
+
const asked = [...pass.history];
|
|
16
|
+
let index = pass.start;
|
|
17
|
+
while (index < flow.steps.length) {
|
|
18
|
+
const step = flow.steps[index];
|
|
19
|
+
if (!shouldAsk(step, { answers, context, given: pass.given, resume: index === pass.start })) {
|
|
20
|
+
index++;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const reply = await ask(step, { answers, canGoBack: asked.length > 0 || pass.toOpening }, context, prompter);
|
|
24
|
+
if (reply === CANCEL)
|
|
25
|
+
return { status: 'cancelled' };
|
|
26
|
+
if (reply === BACK) {
|
|
27
|
+
const back = retreat(asked, pass.toOpening, index);
|
|
28
|
+
if (back.walk)
|
|
29
|
+
return back.walk;
|
|
30
|
+
index = back.index;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (keep(step, reply, prompter, { answers, asked, index }))
|
|
34
|
+
index++;
|
|
35
|
+
}
|
|
36
|
+
return { status: 'answered', answers: filled(flow, answers, context, pass.given), asked };
|
|
37
|
+
}
|
|
38
|
+
function retreat(asked, toOpening, index) {
|
|
39
|
+
if (asked.length > 0)
|
|
40
|
+
return { index: asked.pop() };
|
|
41
|
+
return toOpening ? { walk: { status: 'opening' }, index } : { index };
|
|
42
|
+
}
|
|
43
|
+
function keep(step, reply, prompter, bag) {
|
|
44
|
+
const problem = step.validate?.(reply) ?? null;
|
|
45
|
+
if (problem) {
|
|
46
|
+
prompter.note(problem);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
bag.answers[step.id] = reply;
|
|
50
|
+
bag.asked.push(bag.index);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
/** The step this walk resumed on is asked even when `auto` would skip it. */
|
|
54
|
+
function shouldAsk(step, at) {
|
|
55
|
+
if (!applicable(step, at.answers, at.context, at.given))
|
|
56
|
+
return false;
|
|
57
|
+
if (at.resume)
|
|
58
|
+
return true;
|
|
59
|
+
if (step.skip?.(at.answers, at.context))
|
|
60
|
+
return false;
|
|
61
|
+
return !step.auto?.(at.answers, at.context);
|
|
62
|
+
}
|
|
63
|
+
function applicable(step, answers, context, given) {
|
|
64
|
+
return !given.has(step.id) && (step.when?.(answers, context) ?? true);
|
|
65
|
+
}
|
|
66
|
+
function filled(flow, answers, context, given) {
|
|
67
|
+
return prune(flow, flow.fill?.(answers, context) ?? answers, context, given);
|
|
68
|
+
}
|
|
69
|
+
async function ask(step, at, context, prompter) {
|
|
70
|
+
const { answers, canGoBack } = at;
|
|
71
|
+
const initial = answers[step.id] ?? await step.initial?.(answers, context);
|
|
72
|
+
const choices = step.choices?.(answers, context) ?? [];
|
|
73
|
+
const detail = step.detail?.(answers, context);
|
|
74
|
+
return prompter.ask(drawn(step, answers, context), { initial, choices, detail, canGoBack });
|
|
75
|
+
}
|
|
76
|
+
function drawn(step, answers, context) {
|
|
77
|
+
const shown = {
|
|
78
|
+
id: step.id, kind: step.kind, flag: step.flag,
|
|
79
|
+
message: wording(step.message, answers, context), hint: wording(step.hint, answers, context),
|
|
80
|
+
};
|
|
81
|
+
return step.validate ? { ...shown, validate: step.validate } : shown;
|
|
82
|
+
}
|
|
83
|
+
function wording(text, answers, context) {
|
|
84
|
+
return typeof text === 'function' ? text(answers, context) : text;
|
|
85
|
+
}
|
|
86
|
+
/** Drops answers whose step is no longer reachable, so a stale branch never reaches the plan. */
|
|
87
|
+
export function prune(flow, answers, context, given) {
|
|
88
|
+
const kept = {};
|
|
89
|
+
for (const step of flow.steps) {
|
|
90
|
+
const value = answers[step.id];
|
|
91
|
+
if (value === undefined)
|
|
92
|
+
continue;
|
|
93
|
+
if (given.has(step.id) || (step.when?.(kept, context) ?? true))
|
|
94
|
+
kept[step.id] = value;
|
|
95
|
+
}
|
|
96
|
+
return kept;
|
|
97
|
+
}
|
|
98
|
+
/** A value given as a flag passes the same check a typed answer would: one message per refusal. */
|
|
99
|
+
export function refusedGiven(flow, given) {
|
|
100
|
+
return flow.steps.flatMap((step) => {
|
|
101
|
+
const value = given[step.id];
|
|
102
|
+
const problem = value === undefined ? null : step.validate?.(value) ?? null;
|
|
103
|
+
return problem ? [step.flag + ': ' + problem] : [];
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/** Steps the user can return to from the review: reachable, and not given as a flag. */
|
|
107
|
+
export function reachable(flow, answers, context, given) {
|
|
108
|
+
return flow.steps.filter((step) => answers[step.id] !== undefined && applicable(step, answers, context, given));
|
|
109
|
+
}
|
|
110
|
+
function fresh(given, fixed) {
|
|
111
|
+
return { answers: { ...given }, start: 0, given: fixed, history: [], toOpening: false, showOpening: fixed.size === 0 };
|
|
112
|
+
}
|
|
113
|
+
/** The whole wizard: an opening review when the flow has one, then ask, review, and loop. */
|
|
114
|
+
export async function wizard(flow, context, given, prompter) {
|
|
115
|
+
const fixed = new Set(Object.keys(given));
|
|
116
|
+
let pass = fresh(given, fixed);
|
|
117
|
+
for (;;) {
|
|
118
|
+
const opened = await takeOpening(flow, context, pass, prompter);
|
|
119
|
+
if (opened.outcome)
|
|
120
|
+
return opened.outcome;
|
|
121
|
+
pass = opened.pass;
|
|
122
|
+
const walked = await takePass(flow, context, pass, prompter);
|
|
123
|
+
if (walked.outcome)
|
|
124
|
+
return walked.outcome;
|
|
125
|
+
pass = walked.pass;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function takeOpening(flow, context, pass, prompter) {
|
|
129
|
+
if (!pass.showOpening)
|
|
130
|
+
return { outcome: null, pass };
|
|
131
|
+
const opening = flow.opening?.(context) ?? null;
|
|
132
|
+
if (!opening)
|
|
133
|
+
return { outcome: null, pass: { ...pass, showOpening: false } };
|
|
134
|
+
return openingChoice({ flow, context, pass, prompter }, await prompter.review(opening));
|
|
135
|
+
}
|
|
136
|
+
function openingChoice(scene, choice) {
|
|
137
|
+
const stopped = stoppedChoice(choice);
|
|
138
|
+
if (stopped)
|
|
139
|
+
return { outcome: stopped, pass: scene.pass };
|
|
140
|
+
if (choice === 'account' || choice === 'gh')
|
|
141
|
+
return { outcome: null, pass: resumed(scene, choice, null) };
|
|
142
|
+
if (choice === 'edit')
|
|
143
|
+
scene.prompter.note('nothing has been asked yet');
|
|
144
|
+
return { outcome: null, pass: scene.pass };
|
|
145
|
+
}
|
|
146
|
+
function stoppedChoice(choice) {
|
|
147
|
+
if (choice === 'done')
|
|
148
|
+
return { status: 'done' };
|
|
149
|
+
if (choice === 'decline')
|
|
150
|
+
return { status: 'declined' };
|
|
151
|
+
if (choice === CANCEL)
|
|
152
|
+
return { status: 'cancelled' };
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
function resumed(scene, choice, walk) {
|
|
156
|
+
const spot = scene.flow.resume?.(choice, scene.context);
|
|
157
|
+
const start = spot?.start ?? 0;
|
|
158
|
+
return {
|
|
159
|
+
answers: branchAnswers(choice, spot?.answers ?? {}, walk), start, given: scene.pass.given,
|
|
160
|
+
history: walk ? walk.asked.filter((index) => index < start) : [],
|
|
161
|
+
toOpening: walk === null, showOpening: false,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function branchAnswers(choice, freshAnswers, walk) {
|
|
165
|
+
if (!walk)
|
|
166
|
+
return freshAnswers;
|
|
167
|
+
return choice === 'gh' ? { ...walk.answers, gh: true } : walk.answers;
|
|
168
|
+
}
|
|
169
|
+
async function takePass(flow, context, pass, prompter) {
|
|
170
|
+
const walk = await runFlow(flow, context, pass, prompter);
|
|
171
|
+
if (walk.status === 'cancelled')
|
|
172
|
+
return { outcome: { status: 'cancelled' }, pass };
|
|
173
|
+
if (walk.status === 'opening')
|
|
174
|
+
return { outcome: null, pass: fresh({}, pass.given) };
|
|
175
|
+
return afterReview({ flow, context, pass, prompter }, walk);
|
|
176
|
+
}
|
|
177
|
+
async function afterReview(scene, walk) {
|
|
178
|
+
const shown = scene.flow.review(walk.answers, scene.context);
|
|
179
|
+
const choice = await scene.prompter.review(shown);
|
|
180
|
+
const direct = directChoice(choice, shown, walk.answers);
|
|
181
|
+
if (direct)
|
|
182
|
+
return { outcome: direct, pass: scene.pass };
|
|
183
|
+
if (choice === 'account' || choice === 'gh')
|
|
184
|
+
return { outcome: null, pass: resumed(scene, choice, walk) };
|
|
185
|
+
return restart(scene, walk, choice);
|
|
186
|
+
}
|
|
187
|
+
/** Done promises "nothing needed changing": on a review with work left, it changes nothing either way. */
|
|
188
|
+
function directChoice(choice, shown, answers) {
|
|
189
|
+
if (choice === 'run')
|
|
190
|
+
return { status: 'run', answers };
|
|
191
|
+
if (choice === 'done')
|
|
192
|
+
return { status: shown.settled ? 'done' : 'declined' };
|
|
193
|
+
if (choice === 'decline')
|
|
194
|
+
return { status: 'declined' };
|
|
195
|
+
if (choice === CANCEL)
|
|
196
|
+
return { status: 'cancelled' };
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
async function restart(scene, walk, choice) {
|
|
200
|
+
if (choice !== 'back' && choice !== 'edit')
|
|
201
|
+
return { outcome: null, pass: scene.pass };
|
|
202
|
+
const how = { walk, given: scene.pass.given, choice, toOpening: scene.pass.toOpening };
|
|
203
|
+
const start = await restartAt(scene.flow, scene.context, how, scene.prompter);
|
|
204
|
+
if (start === CANCEL)
|
|
205
|
+
return { outcome: { status: 'cancelled' }, pass: scene.pass };
|
|
206
|
+
if (start === 'opening')
|
|
207
|
+
return { outcome: null, pass: fresh({}, scene.pass.given) };
|
|
208
|
+
return { outcome: null, pass: continued(scene.pass, walk, start) };
|
|
209
|
+
}
|
|
210
|
+
function continued(pass, walk, start) {
|
|
211
|
+
return {
|
|
212
|
+
answers: walk.answers, start, given: pass.given,
|
|
213
|
+
history: walk.asked.filter((index) => index < start),
|
|
214
|
+
toOpening: false, showOpening: false,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Where to resume: the last step asked (Back), or the one the user picks (Change an
|
|
219
|
+
* answer). With nothing asked -- every value given as a flag -- Back has nowhere to go
|
|
220
|
+
* and simply shows the review again. Back from a pass that came from the opening
|
|
221
|
+
* review returns there.
|
|
222
|
+
*/
|
|
223
|
+
async function restartAt(flow, context, how, prompter) {
|
|
224
|
+
if (how.choice === 'back')
|
|
225
|
+
return backIndex(how, flow);
|
|
226
|
+
const steps = reachable(flow, how.walk.answers, context, how.given).map((step) => drawn(step, how.walk.answers, context));
|
|
227
|
+
if (steps.length === 0) {
|
|
228
|
+
prompter.note('every answer came from a flag: there is nothing here to change');
|
|
229
|
+
return flow.steps.length;
|
|
230
|
+
}
|
|
231
|
+
const id = await prompter.pickStep(steps);
|
|
232
|
+
if (id === CANCEL)
|
|
233
|
+
return CANCEL;
|
|
234
|
+
if (id === BACK)
|
|
235
|
+
return flow.steps.length;
|
|
236
|
+
const index = flow.steps.findIndex((step) => step.id === id);
|
|
237
|
+
return index < 0 ? flow.steps.length : index;
|
|
238
|
+
}
|
|
239
|
+
function backIndex(how, flow) {
|
|
240
|
+
const last = how.walk.asked.at(-1);
|
|
241
|
+
if (last !== undefined)
|
|
242
|
+
return last;
|
|
243
|
+
return how.toOpening ? 'opening' : flow.steps.length;
|
|
244
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// What the start screen reads before it asks: the registry, whether gh is git's
|
|
2
|
+
// credential helper, and the clones below this folder. Read-only. A directory
|
|
3
|
+
// it cannot list is skipped; `repown scan` is the command that reports those.
|
|
4
|
+
import { readdir } from 'node:fs/promises';
|
|
5
|
+
import { join, relative, resolve, sep } from 'node:path';
|
|
6
|
+
import { Git } from "../core/git.js";
|
|
7
|
+
import { inspectRepo } from "../core/inspect.js";
|
|
8
|
+
import { planRepair } from "../core/credential/repair.js";
|
|
9
|
+
import { loadRegistry } from "../core/registry.js";
|
|
10
|
+
import { identityProblems } from "../commands/status.js";
|
|
11
|
+
import { CONCURRENCY, discover, mapLimited } from "../commands/scan.js";
|
|
12
|
+
/** How many levels below this folder the start screen looks. `repown scan` looks further. */
|
|
13
|
+
export const HOME_DEPTH = 2;
|
|
14
|
+
export async function readHome(cwd, deps = {}) {
|
|
15
|
+
const [registry, ghIsHelper, clones] = await Promise.all([
|
|
16
|
+
registryOf(deps), helperOf(cwd, deps), clonesOf(cwd, deps),
|
|
17
|
+
]);
|
|
18
|
+
return { cwd: resolve(cwd), registry, ghIsHelper, clones };
|
|
19
|
+
}
|
|
20
|
+
function registryOf(deps) {
|
|
21
|
+
return deps.registry ? deps.registry() : loadRegistry();
|
|
22
|
+
}
|
|
23
|
+
/** The same question `repown fix` previews: gh's helper entries are there to remove. */
|
|
24
|
+
function helperOf(cwd, deps) {
|
|
25
|
+
return deps.ghIsHelper ? deps.ghIsHelper(cwd) : ghHelper(cwd);
|
|
26
|
+
}
|
|
27
|
+
async function ghHelper(cwd) {
|
|
28
|
+
return (await planRepair(new Git(cwd))).length > 0;
|
|
29
|
+
}
|
|
30
|
+
async function clonesOf(cwd, deps) {
|
|
31
|
+
const found = await findClones(cwd, deps);
|
|
32
|
+
const described = await mapLimited(found, CONCURRENCY, (dir) => describeClone(dir, deps));
|
|
33
|
+
return described.filter(isClone);
|
|
34
|
+
}
|
|
35
|
+
async function findClones(cwd, deps) {
|
|
36
|
+
if (deps.findClones)
|
|
37
|
+
return deps.findClones(cwd);
|
|
38
|
+
if (await new Git(cwd).isBare())
|
|
39
|
+
return [];
|
|
40
|
+
return clonesWithin(cwd, HOME_DEPTH);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Repositories at most `depth` levels down, counting a repository at `dir`.
|
|
44
|
+
* Git accepting a directory stops the walk. Git refusing one is not a clone;
|
|
45
|
+
* the levels still left are searched inside it. This folder is that rule at depth 0.
|
|
46
|
+
*/
|
|
47
|
+
async function clonesWithin(dir, depth) {
|
|
48
|
+
if (depth < 0)
|
|
49
|
+
return [];
|
|
50
|
+
const found = await search(dir, depth);
|
|
51
|
+
const parts = await mapLimited(found, CONCURRENCY, (path) => keepOrEnter(dir, path, depth));
|
|
52
|
+
return parts.flat();
|
|
53
|
+
}
|
|
54
|
+
async function keepOrEnter(searched, path, budget) {
|
|
55
|
+
if (await new Git(path).isRepo())
|
|
56
|
+
return [path];
|
|
57
|
+
return enterRefused(path, budget - levelsBelow(searched, path));
|
|
58
|
+
}
|
|
59
|
+
/** One of `remaining` is spent stepping into `dir`; what is left is searched below it. */
|
|
60
|
+
async function enterRefused(dir, remaining) {
|
|
61
|
+
if (remaining <= 0)
|
|
62
|
+
return [];
|
|
63
|
+
const names = await childNames(dir);
|
|
64
|
+
const nested = await mapLimited(names, CONCURRENCY, (name) => clonesWithin(join(dir, name), remaining - 1));
|
|
65
|
+
return nested.flat();
|
|
66
|
+
}
|
|
67
|
+
function levelsBelow(root, path) {
|
|
68
|
+
const rel = relative(resolve(root), resolve(path));
|
|
69
|
+
return rel === '' ? 0 : rel.split(sep).length;
|
|
70
|
+
}
|
|
71
|
+
async function search(dir, depth) {
|
|
72
|
+
const unreadable = [];
|
|
73
|
+
const found = await discover({ root: dir, path: dir }, depth, unreadable, skipHomeEntry);
|
|
74
|
+
return found.map((entry) => entry.path);
|
|
75
|
+
}
|
|
76
|
+
/** Child directories the start screen would enter. An unlistable folder is skipped. */
|
|
77
|
+
async function childNames(cwd) {
|
|
78
|
+
try {
|
|
79
|
+
const entries = await readdir(cwd, { withFileTypes: true });
|
|
80
|
+
return entries.filter((entry) => entry.isDirectory() && !skipHomeEntry(entry.name)).map((entry) => entry.name);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async function describeClone(dir, deps) {
|
|
87
|
+
const inspect = deps.inspect ?? inspectRepo;
|
|
88
|
+
const repo = await inspect(new Git(dir));
|
|
89
|
+
if (!repo.isRepo)
|
|
90
|
+
return null;
|
|
91
|
+
return { path: resolve(dir), setUp: identityProblems(repo).length === 0 };
|
|
92
|
+
}
|
|
93
|
+
function isClone(clone) {
|
|
94
|
+
return clone !== null;
|
|
95
|
+
}
|
|
96
|
+
/** Dot-directories, node_modules, and on Windows AppData. Only the start screen passes this. */
|
|
97
|
+
function skipHomeEntry(name) {
|
|
98
|
+
if (name.startsWith('.') || name === 'node_modules')
|
|
99
|
+
return true;
|
|
100
|
+
return process.platform === 'win32' && name.toLowerCase() === 'appdata';
|
|
101
|
+
}
|