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
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Pure: the summary, the menu and the clone list, and the command an action
|
|
2
|
+
// stands for. Drawing and running live in home-run.ts.
|
|
3
|
+
import { relative } from 'node:path';
|
|
4
|
+
import { registryPath } from "../core/registry.js";
|
|
5
|
+
import { HOME_DEPTH } from "./home-context.js";
|
|
6
|
+
import { CD_NOTE, CHECK_MACHINE, HELPER_LINE, HINT_ACCOUNT, HINT_DOCTOR, HINT_FIX, NONE_RECORDED, NOT_SET_UP, QUIT, RECORD_ACCOUNT, SET_UP, SETUP_CLONE, SHOW_HELP, STOP_GH, clonesNone, clonesSome, couldNotRead, moreClones, recordedLine, setupHint, unreadableLine, } from "./home-text.js";
|
|
7
|
+
import { printable } from "./setup-flow.js";
|
|
8
|
+
/** Shown choices. `back` returns to the menu; the others run a command or leave. */
|
|
9
|
+
export const MENU = {
|
|
10
|
+
setup: 'setup', fix: 'fix', account: 'account', doctor: 'doctor', help: 'help', quit: 'quit',
|
|
11
|
+
};
|
|
12
|
+
export const BACK = 'back';
|
|
13
|
+
const LIST_CAP = 20;
|
|
14
|
+
export function summaryLines(home) {
|
|
15
|
+
const lines = [row('Accounts', accountsText(home.registry)), row('Clones', clonesText(home.clones))];
|
|
16
|
+
if (home.ghIsHelper)
|
|
17
|
+
lines.splice(1, 0, row('Helper', HELPER_LINE));
|
|
18
|
+
return lines;
|
|
19
|
+
}
|
|
20
|
+
export function homeNote(home) {
|
|
21
|
+
return home.clones.length === 0 ? CD_NOTE : null;
|
|
22
|
+
}
|
|
23
|
+
export function menuItems(home) {
|
|
24
|
+
return [...setupItem(home), ...fixItem(home), ...STANDING];
|
|
25
|
+
}
|
|
26
|
+
export function listedClones(home) {
|
|
27
|
+
const ordered = orderClones(home);
|
|
28
|
+
const shown = ordered.slice(0, LIST_CAP);
|
|
29
|
+
const rest = ordered.length - shown.length;
|
|
30
|
+
return { clones: shown.map((clone) => cloneChoice(home.cwd, clone)), more: rest > 0 ? moreClones(rest) : null };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The command the action stands for. `--cwd=<path>` is how setup's own plans spell
|
|
34
|
+
* it. `accounts add` puts `--` before the login, as that plan does, so a login
|
|
35
|
+
* that starts with a dash stays the account.
|
|
36
|
+
*/
|
|
37
|
+
export function commandFor(action) {
|
|
38
|
+
if (action.kind === 'setup')
|
|
39
|
+
return ['setup', '--cwd=' + action.path];
|
|
40
|
+
if (action.kind === 'fix')
|
|
41
|
+
return ['fix'];
|
|
42
|
+
if (action.kind === 'account')
|
|
43
|
+
return ['accounts', 'add', '--', action.login];
|
|
44
|
+
return ['doctor'];
|
|
45
|
+
}
|
|
46
|
+
function accountsText(registry) {
|
|
47
|
+
if (!registry.ok)
|
|
48
|
+
return couldNotRead(registryPath());
|
|
49
|
+
return accountsSummary(registry.value);
|
|
50
|
+
}
|
|
51
|
+
function accountsSummary(registry) {
|
|
52
|
+
const names = Object.keys(registry.accounts).sort();
|
|
53
|
+
const unread = registry.unreadable.length;
|
|
54
|
+
if (unread > 0)
|
|
55
|
+
return unreadableLine(names.length, unread, registryPath());
|
|
56
|
+
return names.length === 0 ? NONE_RECORDED : recordedLine(names.map(printable));
|
|
57
|
+
}
|
|
58
|
+
function clonesText(clones) {
|
|
59
|
+
if (clones.length === 0)
|
|
60
|
+
return clonesNone(HOME_DEPTH);
|
|
61
|
+
const pending = clones.filter((clone) => !clone.setUp).length;
|
|
62
|
+
return clonesSome(clones.length, pending, clones.length - pending);
|
|
63
|
+
}
|
|
64
|
+
function setupItem(home) {
|
|
65
|
+
if (home.clones.length === 0)
|
|
66
|
+
return [];
|
|
67
|
+
const pending = home.clones.filter((clone) => !clone.setUp).length;
|
|
68
|
+
return [{ value: MENU.setup, label: SETUP_CLONE, hint: setupHint(pending, home.clones.length) }];
|
|
69
|
+
}
|
|
70
|
+
function fixItem(home) {
|
|
71
|
+
if (!home.ghIsHelper)
|
|
72
|
+
return [];
|
|
73
|
+
return [{ value: MENU.fix, label: STOP_GH, hint: HINT_FIX }];
|
|
74
|
+
}
|
|
75
|
+
const STANDING = [
|
|
76
|
+
{ value: MENU.account, label: RECORD_ACCOUNT, hint: HINT_ACCOUNT },
|
|
77
|
+
{ value: MENU.doctor, label: CHECK_MACHINE, hint: HINT_DOCTOR },
|
|
78
|
+
{ value: MENU.help, label: SHOW_HELP },
|
|
79
|
+
{ value: MENU.quit, label: QUIT },
|
|
80
|
+
];
|
|
81
|
+
function orderClones(home) {
|
|
82
|
+
const rank = (clone) => (clone.setUp ? '1' : '0') + relative(home.cwd, clone.path);
|
|
83
|
+
return [...home.clones].sort((a, b) => rank(a).localeCompare(rank(b)));
|
|
84
|
+
}
|
|
85
|
+
function cloneChoice(cwd, clone) {
|
|
86
|
+
return { value: clone.path, label: printable(relative(cwd, clone.path)), hint: clone.setUp ? SET_UP : NOT_SET_UP };
|
|
87
|
+
}
|
|
88
|
+
function row(label, value) {
|
|
89
|
+
return label.padEnd(10) + ' ' + value;
|
|
90
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// The start screen: a summary of this folder, then one menu. Each action prints
|
|
2
|
+
// the command it stands for and runs that command's own run(). Loaded only
|
|
3
|
+
// through the runner start.ts returns, so a typed command never imports it.
|
|
4
|
+
//
|
|
5
|
+
// Quit exits 0. Esc and Ctrl-C exit 130, the same cancel setup uses. Neither writes.
|
|
6
|
+
import fixCommand from "../commands/fix.js";
|
|
7
|
+
import doctorCommand from "../commands/doctor.js";
|
|
8
|
+
import accountsGroup from "../commands/accounts.js";
|
|
9
|
+
import { COMMANDS } from "../program.js";
|
|
10
|
+
import { parseArgs } from "../ui/args.js";
|
|
11
|
+
import { specFor } from "../ui/command.js";
|
|
12
|
+
import { loadEntries } from "../ui/dispatch.js";
|
|
13
|
+
import * as out from "../ui/format.js";
|
|
14
|
+
import { renderTopHelp } from "../ui/help.js";
|
|
15
|
+
import { BACK, CANCEL } from "./engine.js";
|
|
16
|
+
import { readHome } from "./home-context.js";
|
|
17
|
+
import { BACK as HOME_BACK, MENU, commandFor, homeNote, listedClones, menuItems, summaryLines } from "./home-flow.js";
|
|
18
|
+
import { BACK_LABEL, BUSY, CANCELLED, LOGIN_HINT, LOGIN_MESSAGE, QUIT, WHAT_NEXT, WHICH_CLONE, homeTitle } from "./home-text.js";
|
|
19
|
+
import { formatCommand, loginProblem, printable } from "./setup-flow.js";
|
|
20
|
+
import { choosePrompter, runSetup } from "./setup-run.js";
|
|
21
|
+
const CANCELLED_CODE = 130;
|
|
22
|
+
const LOGIN_STEP = {
|
|
23
|
+
id: 'login', kind: 'text', message: LOGIN_MESSAGE, hint: LOGIN_HINT, flag: '<account>',
|
|
24
|
+
validate: (value) => loginProblem(String(value)),
|
|
25
|
+
};
|
|
26
|
+
const LOGIN_ASKED = { initial: undefined, choices: [], detail: undefined, canGoBack: true };
|
|
27
|
+
export async function runHome(deps = {}) {
|
|
28
|
+
const prompter = deps.prompter ?? await choosePrompter();
|
|
29
|
+
try {
|
|
30
|
+
return await openHome(prompter, deps);
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
prompter.close();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function openHome(prompter, deps) {
|
|
37
|
+
const cwd = deps.cwd ?? process.cwd();
|
|
38
|
+
prompter.intro?.(homeTitle(printable(cwd)));
|
|
39
|
+
prompter.busy?.(BUSY);
|
|
40
|
+
const home = await (deps.read ?? readHome)(cwd);
|
|
41
|
+
showLines(prompter, summaryLines(home));
|
|
42
|
+
const note = homeNote(home);
|
|
43
|
+
if (note)
|
|
44
|
+
prompter.note(note);
|
|
45
|
+
return chooseNext(prompter, home);
|
|
46
|
+
}
|
|
47
|
+
async function chooseNext(prompter, home) {
|
|
48
|
+
for (;;) {
|
|
49
|
+
const picked = await prompter.choose(WHAT_NEXT, menuItems(home));
|
|
50
|
+
if (picked === CANCEL)
|
|
51
|
+
return finish(prompter, CANCELLED, CANCELLED_CODE);
|
|
52
|
+
const code = await dispatchChoice(picked, prompter, home);
|
|
53
|
+
if (code !== null)
|
|
54
|
+
return code;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async function dispatchChoice(picked, prompter, home) {
|
|
58
|
+
if (picked === MENU.quit)
|
|
59
|
+
return finish(prompter, QUIT, 0);
|
|
60
|
+
if (picked === MENU.help)
|
|
61
|
+
return showTopHelp(prompter);
|
|
62
|
+
if (picked === MENU.setup)
|
|
63
|
+
return pickClone(prompter, home);
|
|
64
|
+
if (picked === MENU.fix)
|
|
65
|
+
return runOwn(prompter, fixCommand, [], commandFor({ kind: 'fix' }));
|
|
66
|
+
if (picked === MENU.doctor)
|
|
67
|
+
return runOwn(prompter, doctorCommand, [], commandFor({ kind: 'doctor' }));
|
|
68
|
+
if (picked === MENU.account)
|
|
69
|
+
return recordAccount(prompter);
|
|
70
|
+
return finish(prompter, QUIT, 0);
|
|
71
|
+
}
|
|
72
|
+
async function pickClone(prompter, home) {
|
|
73
|
+
const list = listedClones(home);
|
|
74
|
+
if (list.more)
|
|
75
|
+
showLines(prompter, [list.more]);
|
|
76
|
+
const options = [...list.clones, { value: HOME_BACK, label: BACK_LABEL }];
|
|
77
|
+
const picked = await prompter.choose(WHICH_CLONE, options);
|
|
78
|
+
if (picked === CANCEL)
|
|
79
|
+
return finish(prompter, CANCELLED, CANCELLED_CODE);
|
|
80
|
+
if (picked === HOME_BACK)
|
|
81
|
+
return null;
|
|
82
|
+
return runInClone(prompter, picked);
|
|
83
|
+
}
|
|
84
|
+
async function runInClone(prompter, cwd) {
|
|
85
|
+
const argv = commandFor({ kind: 'setup', path: cwd });
|
|
86
|
+
handOver(prompter, argv);
|
|
87
|
+
return runSetup(setupArgs(cwd), { interactive: true, prompter });
|
|
88
|
+
}
|
|
89
|
+
async function recordAccount(prompter) {
|
|
90
|
+
const login = await askLogin(prompter);
|
|
91
|
+
if (login === CANCEL)
|
|
92
|
+
return finish(prompter, CANCELLED, CANCELLED_CODE);
|
|
93
|
+
if (login === BACK)
|
|
94
|
+
return null;
|
|
95
|
+
const argv = commandFor({ kind: 'account', login });
|
|
96
|
+
return runOwn(prompter, accountsGroup.actions['add'], ['--', login], argv);
|
|
97
|
+
}
|
|
98
|
+
async function askLogin(prompter) {
|
|
99
|
+
for (;;) {
|
|
100
|
+
const reply = await prompter.ask(LOGIN_STEP, LOGIN_ASKED);
|
|
101
|
+
if (reply === CANCEL || reply === BACK)
|
|
102
|
+
return reply;
|
|
103
|
+
const problem = loginProblem(String(reply));
|
|
104
|
+
if (!problem)
|
|
105
|
+
return String(reply).trim();
|
|
106
|
+
prompter.note(problem);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async function runOwn(prompter, command, tokens, argv) {
|
|
110
|
+
handOver(prompter, argv);
|
|
111
|
+
prompter.suspend?.();
|
|
112
|
+
const parsed = parseArgs(tokens, specFor(command));
|
|
113
|
+
if (!parsed.ok) {
|
|
114
|
+
out.fail('repown', parsed.error);
|
|
115
|
+
return 2;
|
|
116
|
+
}
|
|
117
|
+
return command.run(parsed.value);
|
|
118
|
+
}
|
|
119
|
+
function setupArgs(cwd) {
|
|
120
|
+
return { positional: [], flags: new Map([['cwd', cwd]]) };
|
|
121
|
+
}
|
|
122
|
+
function finish(prompter, message, code) {
|
|
123
|
+
prompter.outro?.(message, code === CANCELLED_CODE);
|
|
124
|
+
return code;
|
|
125
|
+
}
|
|
126
|
+
function showLines(prompter, lines) {
|
|
127
|
+
if (prompter.show) {
|
|
128
|
+
prompter.show(lines);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
for (const line of lines)
|
|
132
|
+
out.line(' ' + line);
|
|
133
|
+
}
|
|
134
|
+
/** Closes the start screen's frame on the command it hands over to. */
|
|
135
|
+
function handOver(prompter, argv) {
|
|
136
|
+
const line = '> ' + formatCommand(argv);
|
|
137
|
+
if (prompter.outro)
|
|
138
|
+
prompter.outro(line);
|
|
139
|
+
else
|
|
140
|
+
out.detail(line);
|
|
141
|
+
}
|
|
142
|
+
async function showTopHelp(prompter) {
|
|
143
|
+
handOver(prompter, ['--help']);
|
|
144
|
+
const entries = await loadEntries(COMMANDS);
|
|
145
|
+
for (const line of renderTopHelp(entries))
|
|
146
|
+
out.line(line);
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// The words on the start screen, in one place, so the summary and both prompters
|
|
2
|
+
// say the same thing. What is shown when comes from home-flow.ts.
|
|
3
|
+
import { displayPath } from "../ui/format.js";
|
|
4
|
+
export const WHAT_NEXT = 'What next?';
|
|
5
|
+
export const WHICH_CLONE = 'Which clone?';
|
|
6
|
+
export const SETUP_CLONE = 'Set up a clone found here';
|
|
7
|
+
export const STOP_GH = 'Stop gh serving credentials';
|
|
8
|
+
export const RECORD_ACCOUNT = 'Record an account';
|
|
9
|
+
export const CHECK_MACHINE = 'Check this machine';
|
|
10
|
+
export const SHOW_HELP = 'Show help';
|
|
11
|
+
export const QUIT = 'Quit';
|
|
12
|
+
export const BACK_LABEL = '← Back';
|
|
13
|
+
export const BUSY = 'Reading this folder';
|
|
14
|
+
export const CD_NOTE = 'cd into a clone (or git clone one), then run repown';
|
|
15
|
+
export const NONE_RECORDED = 'none recorded';
|
|
16
|
+
export const HELPER_LINE = 'gh serves git\'s credentials: run repown fix';
|
|
17
|
+
export const HINT_FIX = 'repown fix';
|
|
18
|
+
export const HINT_ACCOUNT = 'repown accounts add';
|
|
19
|
+
export const HINT_DOCTOR = 'repown doctor';
|
|
20
|
+
export const NOT_SET_UP = 'not set up';
|
|
21
|
+
export const SET_UP = 'set up';
|
|
22
|
+
export const LOGIN_MESSAGE = 'The account\'s user name (login)';
|
|
23
|
+
export const LOGIN_HINT = 'the name you sign in with, e.g. octocat; not your email address';
|
|
24
|
+
export const CANCELLED = 'Cancelled: nothing was changed.';
|
|
25
|
+
export function homeTitle(cwd) {
|
|
26
|
+
return 'repown · not a clone: ' + displayPath(cwd);
|
|
27
|
+
}
|
|
28
|
+
export function recordedLine(names) {
|
|
29
|
+
return names.length + ' recorded: ' + names.join(', ');
|
|
30
|
+
}
|
|
31
|
+
/** Entries the registry file has, but that could not be read as accounts. */
|
|
32
|
+
export function unreadableLine(recorded, unreadable, path) {
|
|
33
|
+
const tail = unreadable + ' unreadable in ' + path + ': run repown accounts list';
|
|
34
|
+
return recorded === 0 ? tail : recorded + ' recorded, ' + tail;
|
|
35
|
+
}
|
|
36
|
+
export function couldNotRead(path) {
|
|
37
|
+
return 'could not read ' + path;
|
|
38
|
+
}
|
|
39
|
+
export function clonesNone(depth) {
|
|
40
|
+
return 'none below this folder (' + depth + ' levels)';
|
|
41
|
+
}
|
|
42
|
+
export function clonesSome(total, pending, ready) {
|
|
43
|
+
return total + ' below this folder: ' + pending + ' not set up, ' + ready + ' set up';
|
|
44
|
+
}
|
|
45
|
+
export function setupHint(pending, total) {
|
|
46
|
+
return pending > 0 ? pending + ' not set up' : total + ' set up';
|
|
47
|
+
}
|
|
48
|
+
export function moreClones(count) {
|
|
49
|
+
return 'and ' + count + ' more: repown scan';
|
|
50
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// The wizard drawn with plain lines: numbered choices, words instead of symbols, no
|
|
2
|
+
// colour needed. Used when the richer prompter can't be (NO_COLOR or FORCE_COLOR=0,
|
|
3
|
+
// always TERM=dumb, or @clack/prompts fails to load) and by the tests, which drive it
|
|
4
|
+
// through ordinary streams.
|
|
5
|
+
//
|
|
6
|
+
// Everything goes to the output stream it's given -- stderr in real use -- so stdout
|
|
7
|
+
// keeps carrying only what the commands themselves print. End of input or Ctrl-C at a
|
|
8
|
+
// question is a cancel, never an answer.
|
|
9
|
+
import { createInterface } from 'node:readline/promises';
|
|
10
|
+
import { BACK, CANCEL } from "./engine.js";
|
|
11
|
+
import { BACK_TO_REVIEW, PICK_QUESTION, RUN_THIS_STEP, reviewDefault, reviewLines, reviewOptions, reviewQuestion, stepConfirmLines, stepOptions, textWidth, wrap } from "./review-text.js";
|
|
12
|
+
/** Typed at a text question, it means "go back" (never used for a real answer here). */
|
|
13
|
+
export const BACK_WORD = '<';
|
|
14
|
+
export function plainPrompter(streams) {
|
|
15
|
+
const session = plainSession(streams);
|
|
16
|
+
// Text is indented two spaces, the review's four.
|
|
17
|
+
const width = textWidth(streams.output.columns, 4);
|
|
18
|
+
const wide = () => ({ ...session.io(), width });
|
|
19
|
+
return {
|
|
20
|
+
ask: (step, asked) => askStep(wide(), step, asked),
|
|
21
|
+
review: (review) => showReview(session.io(), review, width),
|
|
22
|
+
pickStep: (steps) => pickPlain(session.io(), steps),
|
|
23
|
+
choose: (message, options) => choosePlain(session.io(), message, options),
|
|
24
|
+
note: (message) => sayIndented(session.io(), wrap(message, width)),
|
|
25
|
+
show: (lines) => sayIndented(session.io(), lines.flatMap((text) => wrap(text, width))),
|
|
26
|
+
close: () => session.close(),
|
|
27
|
+
outro: (message) => session.io().say(message),
|
|
28
|
+
confirmStep: (confirm) => askStepConfirm(wide(), confirm),
|
|
29
|
+
suspend: () => session.suspend(),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function plainSession(streams) {
|
|
33
|
+
// On a real terminal, readline must own it: only then does Ctrl-C arrive as its
|
|
34
|
+
// SIGINT event (a cancel) instead of ending the process with a signal.
|
|
35
|
+
const terminal = streams.input.isTTY === true;
|
|
36
|
+
let rl = openReadline(streams, terminal);
|
|
37
|
+
let io = ioFor(rl, streams.output, terminal);
|
|
38
|
+
let held = true;
|
|
39
|
+
const reopen = () => {
|
|
40
|
+
if (held)
|
|
41
|
+
return io;
|
|
42
|
+
rl = openReadline(streams, terminal);
|
|
43
|
+
io = ioFor(rl, streams.output, terminal);
|
|
44
|
+
held = true;
|
|
45
|
+
return io;
|
|
46
|
+
};
|
|
47
|
+
const shut = () => { if (!held)
|
|
48
|
+
return; held = false; rl.close(); };
|
|
49
|
+
return { io: reopen, close: shut, suspend: shut };
|
|
50
|
+
}
|
|
51
|
+
function openReadline(streams, terminal) {
|
|
52
|
+
return createInterface({ input: streams.input, output: streams.output, terminal });
|
|
53
|
+
}
|
|
54
|
+
function sayIndented(io, lines) {
|
|
55
|
+
for (const line of lines)
|
|
56
|
+
io.say(' ' + line);
|
|
57
|
+
}
|
|
58
|
+
/** On a terminal, readline shows the prompt, so it can redraw it while the line is edited. */
|
|
59
|
+
function ioFor(rl, output, terminal) {
|
|
60
|
+
const lines = queue(rl);
|
|
61
|
+
let closed = false;
|
|
62
|
+
rl.on('close', () => { closed = true; });
|
|
63
|
+
const write = (text) => { output.write(text); };
|
|
64
|
+
const show = (text) => {
|
|
65
|
+
if (!terminal || closed) {
|
|
66
|
+
write(text);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
rl.setPrompt(text);
|
|
70
|
+
rl.prompt();
|
|
71
|
+
};
|
|
72
|
+
return { lines, write: show, say: (text) => write(text + '\n') };
|
|
73
|
+
}
|
|
74
|
+
/** The input as a queue of lines; null once it has ended or Ctrl-C was pressed. */
|
|
75
|
+
function queue(rl) {
|
|
76
|
+
const waiting = [];
|
|
77
|
+
const buffered = [];
|
|
78
|
+
const push = (line) => { const take = waiting.shift(); if (take)
|
|
79
|
+
take(line);
|
|
80
|
+
else
|
|
81
|
+
buffered.push(line); };
|
|
82
|
+
let ended = false;
|
|
83
|
+
rl.on('line', (line) => push(line));
|
|
84
|
+
rl.on('close', () => { ended = true; push(null); });
|
|
85
|
+
rl.on('SIGINT', () => { push(null); rl.close(); });
|
|
86
|
+
return () => {
|
|
87
|
+
if (buffered.length > 0)
|
|
88
|
+
return Promise.resolve(buffered[0] === null ? null : buffered.shift());
|
|
89
|
+
// Every read after the end is a cancel too, not a wait for a line that can't come.
|
|
90
|
+
if (ended)
|
|
91
|
+
return Promise.resolve(null);
|
|
92
|
+
return new Promise((resolve) => waiting.push(resolve));
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** The question, its hint and any detail under it, each wrapped and indented. */
|
|
96
|
+
async function askStep(io, step, asked) {
|
|
97
|
+
const { initial, detail } = asked;
|
|
98
|
+
io.say(step.message);
|
|
99
|
+
for (const line of [step.hint, detail].filter((text) => text).flatMap((text) => wrap(text, io.width)))
|
|
100
|
+
io.say(' ' + line);
|
|
101
|
+
if (step.kind === 'select')
|
|
102
|
+
return askSelect(io, asked);
|
|
103
|
+
if (step.kind === 'confirm')
|
|
104
|
+
return askConfirm(io, initial === true, asked.canGoBack);
|
|
105
|
+
const shown = typeof initial === 'string' && initial ? ' [' + initial + ']' : '';
|
|
106
|
+
if (asked.canGoBack)
|
|
107
|
+
io.say(' type ' + BACK_WORD + ' to go back');
|
|
108
|
+
const line = await prompt(io, ' >' + shown + ' ');
|
|
109
|
+
if (line === null)
|
|
110
|
+
return CANCEL;
|
|
111
|
+
if (line === BACK_WORD && asked.canGoBack)
|
|
112
|
+
return BACK;
|
|
113
|
+
return line || (typeof initial === 'string' ? initial : '');
|
|
114
|
+
}
|
|
115
|
+
async function choosePlain(io, message, options) {
|
|
116
|
+
io.say(message);
|
|
117
|
+
const labels = options.map((option) => option.label + (option.hint ? ' -- ' + option.hint : ''));
|
|
118
|
+
const index = await pickNumber(io, labels, 1);
|
|
119
|
+
return index === CANCEL ? CANCEL : options[index].value;
|
|
120
|
+
}
|
|
121
|
+
async function pickPlain(io, steps) {
|
|
122
|
+
io.say(PICK_QUESTION);
|
|
123
|
+
const index = await pickNumber(io, [...steps.map((step) => step.message), BACK_TO_REVIEW], 1);
|
|
124
|
+
if (index === CANCEL)
|
|
125
|
+
return CANCEL;
|
|
126
|
+
return index === steps.length ? BACK : steps[index].id;
|
|
127
|
+
}
|
|
128
|
+
async function askStepConfirm(io, confirm) {
|
|
129
|
+
for (const line of stepConfirmLines(confirm).flatMap((text) => wrap(text, io.width)))
|
|
130
|
+
io.say(' ' + line);
|
|
131
|
+
io.say(RUN_THIS_STEP);
|
|
132
|
+
const options = stepOptions();
|
|
133
|
+
const preset = options.findIndex((option) => option.value === confirm.initial) + 1;
|
|
134
|
+
const index = await pickNumber(io, options.map((option) => option.label), preset);
|
|
135
|
+
return index === CANCEL ? CANCEL : options[index].value;
|
|
136
|
+
}
|
|
137
|
+
async function askSelect(io, { choices, initial, canGoBack }) {
|
|
138
|
+
const labels = choices.map((choice) => choice.label + (choice.hint ? ' -- ' + choice.hint : ''));
|
|
139
|
+
const current = choices.findIndex((choice) => choice.value === initial);
|
|
140
|
+
const index = await pickNumber(io, canGoBack ? [...labels, 'Back'] : labels, current >= 0 ? current + 1 : 1);
|
|
141
|
+
if (index === CANCEL)
|
|
142
|
+
return CANCEL;
|
|
143
|
+
return index === choices.length ? BACK : choices[index].value;
|
|
144
|
+
}
|
|
145
|
+
async function askConfirm(io, yes, canGoBack) {
|
|
146
|
+
const index = await pickNumber(io, canGoBack ? ['Yes', 'No', 'Back'] : ['Yes', 'No'], yes ? 1 : 2);
|
|
147
|
+
if (index === CANCEL)
|
|
148
|
+
return CANCEL;
|
|
149
|
+
return index === 2 ? BACK : index === 0;
|
|
150
|
+
}
|
|
151
|
+
async function showReview(io, review, width) {
|
|
152
|
+
io.say('');
|
|
153
|
+
io.say(review.title);
|
|
154
|
+
for (const line of reviewLines(review, width))
|
|
155
|
+
io.say(line ? ' ' + line : '');
|
|
156
|
+
io.say('');
|
|
157
|
+
io.say(reviewQuestion(review));
|
|
158
|
+
const options = reviewOptions(review);
|
|
159
|
+
const preset = options.findIndex((option) => option.value === reviewDefault(review)) + 1;
|
|
160
|
+
const index = await pickNumber(io, options.map((option) => option.label + (option.hint ? ' -- ' + option.hint : '')), preset);
|
|
161
|
+
return index === CANCEL ? CANCEL : options[index].value;
|
|
162
|
+
}
|
|
163
|
+
/** Lists numbered options and reads a number until it's valid; Enter takes the default. */
|
|
164
|
+
async function pickNumber(io, labels, preset) {
|
|
165
|
+
labels.forEach((label, index) => io.say(' ' + (index + 1) + ') ' + label));
|
|
166
|
+
for (;;) {
|
|
167
|
+
const line = await prompt(io, ' choice [' + preset + ']: ');
|
|
168
|
+
if (line === null)
|
|
169
|
+
return CANCEL;
|
|
170
|
+
const picked = line === '' ? preset : Number(line);
|
|
171
|
+
if (Number.isInteger(picked) && picked >= 1 && picked <= labels.length)
|
|
172
|
+
return picked - 1;
|
|
173
|
+
io.say(' type a number from 1 to ' + labels.length);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** A line typed after `text`; null after Ctrl-C or the end of input, which leave the line unfinished. */
|
|
177
|
+
async function prompt(io, text) {
|
|
178
|
+
io.write(text);
|
|
179
|
+
const line = await io.lines();
|
|
180
|
+
if (line === null)
|
|
181
|
+
io.say('');
|
|
182
|
+
return line === null ? null : line.trim();
|
|
183
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// The words around a review, shared by both prompters so the plain and the drawn
|
|
2
|
+
// screens say the same thing: the numbered steps, the closing lines, the question, its
|
|
3
|
+
// choices and which one Enter takes, and "Change an answer". What goes IN the review
|
|
4
|
+
// comes from the flow (setup-flow.ts).
|
|
5
|
+
/** "Change an answer": the question, and the choice that returns to the review. */
|
|
6
|
+
export const PICK_QUESTION = 'Which answer do you want to change?';
|
|
7
|
+
export const BACK_TO_REVIEW = 'Back to the review';
|
|
8
|
+
export const RUN_THIS_STEP = 'Run this step?';
|
|
9
|
+
/** Yes, unless the command changes the whole machine: that needs a deliberate yes, as in the review. */
|
|
10
|
+
export function stepDefault(command) {
|
|
11
|
+
return changesMachine(command) ? 'skip' : 'yes';
|
|
12
|
+
}
|
|
13
|
+
export function stepOptions() {
|
|
14
|
+
return [
|
|
15
|
+
{ value: 'yes', label: 'Yes' },
|
|
16
|
+
{ value: 'skip', label: 'Skip' },
|
|
17
|
+
{ value: 'stop', label: 'Stop' },
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
/** What a step changes, why, and the command, in that order. */
|
|
21
|
+
export function stepConfirmLines(brief) {
|
|
22
|
+
return [...brief.changes, '', brief.why, '> ' + brief.command];
|
|
23
|
+
}
|
|
24
|
+
function changesMachine(command) {
|
|
25
|
+
return command.startsWith('repown fix');
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The body of the review, line by line, wrapped to `width`. `command` styles each
|
|
29
|
+
* command line (dimmed where the prompter can draw colour), so the plain words stay
|
|
30
|
+
* the eye's first stop.
|
|
31
|
+
*/
|
|
32
|
+
export function reviewLines(review, width = Infinity, command = (text) => text) {
|
|
33
|
+
const words = (text) => wrap(text, width);
|
|
34
|
+
// A command's continuation sits deeper, so it can't read as the next command.
|
|
35
|
+
const code = (text) => wrap(' ' + text, width - 2).map((line, index) => command(index ? ' ' + line : line));
|
|
36
|
+
if (review.settled)
|
|
37
|
+
return [...review.headline.flatMap(words), '', 'Nothing needs to change.', ...notesOf(review).flatMap(words)];
|
|
38
|
+
const steps = review.steps.flatMap((step, index) => [
|
|
39
|
+
...words((index + 1) + '. ' + step.what),
|
|
40
|
+
...code(step.command),
|
|
41
|
+
...step.detail.flatMap(code),
|
|
42
|
+
]);
|
|
43
|
+
return [...review.headline.flatMap(words), '', 'When you choose Run:', ...steps, ...notesOf(review).flatMap(words), '',
|
|
44
|
+
...words('These are ordinary commands: run them yourself, or in a script.'),
|
|
45
|
+
...words('This clone\'s settings go in its .git/config, which is never pushed.')];
|
|
46
|
+
}
|
|
47
|
+
/** The widest a line of text gets, however wide the window: longer lines are hard to read. */
|
|
48
|
+
export const MAX_WIDTH = 72;
|
|
49
|
+
/** The text width for a window `columns` wide, less `margin` for borders and gutters. */
|
|
50
|
+
export function textWidth(columns, margin) {
|
|
51
|
+
return Math.max(24, Math.min(MAX_WIDTH, (columns ?? 80) - margin));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* One line, wrapped at spaces to `width`. Continuation lines line up under the text:
|
|
55
|
+
* past any indent, a "1. " step number, or a "label " column.
|
|
56
|
+
*/
|
|
57
|
+
export function wrap(text, width) {
|
|
58
|
+
if (text.length <= width)
|
|
59
|
+
return [text];
|
|
60
|
+
const hang = ' '.repeat(/^\s*(?:\d+\. |\S+(?: \S+)? {2,})?/.exec(text)[0].length);
|
|
61
|
+
const lines = [];
|
|
62
|
+
let line = '';
|
|
63
|
+
for (const word of text.split(/(?<=\S) (?=\S)/)) {
|
|
64
|
+
if (line && line.length + 1 + word.length > width) {
|
|
65
|
+
lines.push(line);
|
|
66
|
+
line = hang + word.trimStart();
|
|
67
|
+
}
|
|
68
|
+
else
|
|
69
|
+
line = line ? line + ' ' + word : word;
|
|
70
|
+
}
|
|
71
|
+
return [...lines, line];
|
|
72
|
+
}
|
|
73
|
+
function notesOf(review) {
|
|
74
|
+
return review.notes.length > 0 ? ['', ...review.notes] : [];
|
|
75
|
+
}
|
|
76
|
+
export function reviewQuestion(review) {
|
|
77
|
+
if (review.settled)
|
|
78
|
+
return 'What now?';
|
|
79
|
+
return review.steps.length === 1 ? RUN_THIS_STEP : 'Run these ' + review.steps.length + ' steps?';
|
|
80
|
+
}
|
|
81
|
+
export function reviewOptions(review) {
|
|
82
|
+
if (review.settled)
|
|
83
|
+
return settledOptions(review);
|
|
84
|
+
return [
|
|
85
|
+
{ value: 'run', label: review.steps.length === 1 ? 'Run it' : 'Run them' },
|
|
86
|
+
{ value: 'back', label: 'Back', hint: 'to the last question' },
|
|
87
|
+
{ value: 'edit', label: 'Change an answer' },
|
|
88
|
+
{ value: 'decline', label: 'Decline', hint: 'change nothing and exit' },
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
/** Done, then a way to pick another account or sign in to gh. Change an answer stays once questions were asked. */
|
|
92
|
+
function settledOptions(review) {
|
|
93
|
+
const options = [
|
|
94
|
+
{ value: 'done', label: 'Done', hint: 'change nothing' },
|
|
95
|
+
{ value: 'account', label: 'Use another account', hint: 'choose a different account for this clone' },
|
|
96
|
+
];
|
|
97
|
+
if (review.ghSignIn)
|
|
98
|
+
options.push({ value: 'gh', label: review.ghSignIn });
|
|
99
|
+
if (review.edits !== false)
|
|
100
|
+
options.push({ value: 'edit', label: 'Change an answer' });
|
|
101
|
+
return options;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The choice Enter takes: the first, unless a step changes the whole machine
|
|
105
|
+
* (`fix`), which, like its own question, needs a deliberate yes.
|
|
106
|
+
*/
|
|
107
|
+
export function reviewDefault(review) {
|
|
108
|
+
const machineWide = review.steps.some((step) => changesMachine(step.command));
|
|
109
|
+
return !review.settled && machineWide ? 'decline' : reviewOptions(review)[0].value;
|
|
110
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// What one setup run changed. The diff is pure. The read is this clone's local
|
|
2
|
+
// config and the guard, through the Git wrapper. A credential helper is never
|
|
3
|
+
// run: it can print a live password, and this report shows config values only.
|
|
4
|
+
import { ACCOUNT_KEY, EMAIL_KEY, NAME_KEY, USE_CONFIG_ONLY_KEY } from "../core/identity.js";
|
|
5
|
+
import { guardState } from "../core/guard/hook.js";
|
|
6
|
+
const ALLOW_OWNER = 'repown.allowOwner';
|
|
7
|
+
const AUTO_UPSTREAM = 'push.autoSetupRemote';
|
|
8
|
+
const LEADING = [NAME_KEY, EMAIL_KEY, USE_CONFIG_ONLY_KEY, ACCOUNT_KEY];
|
|
9
|
+
export async function readCloneSnapshot(git, credentialKeys) {
|
|
10
|
+
const keys = [...LEADING, ...credentialKeys];
|
|
11
|
+
const found = await Promise.all(keys.map((key) => git.getConfig(key, 'local')));
|
|
12
|
+
const [allowOwner, autoUpstream, guard] = await Promise.all([
|
|
13
|
+
git.getAllConfig(ALLOW_OWNER, 'local'),
|
|
14
|
+
git.getConfig(AUTO_UPSTREAM, 'local'),
|
|
15
|
+
guardState(git),
|
|
16
|
+
]);
|
|
17
|
+
return {
|
|
18
|
+
values: keys.map((key, index) => [key, found[index] ?? null]),
|
|
19
|
+
allowOwner, autoUpstream, guard,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Lines for keys whose local value changed. Empty when the clone matches. */
|
|
23
|
+
export function cloneChangeLines(before, after) {
|
|
24
|
+
const next = new Map(after.values);
|
|
25
|
+
return [
|
|
26
|
+
...before.values.flatMap(([key, old]) => keep(valueChange(key, old, next.get(key) ?? null))),
|
|
27
|
+
...allowChange(before.allowOwner, after.allowOwner),
|
|
28
|
+
...keep(valueChange(AUTO_UPSTREAM, before.autoUpstream, after.autoUpstream)),
|
|
29
|
+
...keep(guardChange(before.guard, after.guard)),
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
/** Registry and gh are this machine, not this clone. Null means that one did not change. */
|
|
33
|
+
export function machineChangeLines(registryAdded, ghAccount) {
|
|
34
|
+
const registry = registryAdded === null ? null : " this machine's account registry: added " + registryAdded;
|
|
35
|
+
const gh = ghAccount === null ? null : " gh: " + ghAccount + " is now gh's active account (every terminal)";
|
|
36
|
+
return [...keep(registry), ...keep(gh)];
|
|
37
|
+
}
|
|
38
|
+
function keep(line) {
|
|
39
|
+
return line === null ? [] : [line];
|
|
40
|
+
}
|
|
41
|
+
function valueChange(key, before, after) {
|
|
42
|
+
if (before === after)
|
|
43
|
+
return null;
|
|
44
|
+
if (before === null)
|
|
45
|
+
return ' ' + key + ': (added) ' + after;
|
|
46
|
+
if (after === null)
|
|
47
|
+
return ' ' + key + ': ' + before + ' -> (removed)';
|
|
48
|
+
return ' ' + key + ': ' + before + ' -> ' + after;
|
|
49
|
+
}
|
|
50
|
+
function guardChange(before, after) {
|
|
51
|
+
if (before === after)
|
|
52
|
+
return null;
|
|
53
|
+
return ' push guard: ' + before + ' -> ' + after;
|
|
54
|
+
}
|
|
55
|
+
function allowChange(before, after) {
|
|
56
|
+
const added = missing(after, before);
|
|
57
|
+
const removed = missing(before, after);
|
|
58
|
+
return [
|
|
59
|
+
...keep(added.length === 0 ? null : ' ' + ALLOW_OWNER + ': (added) ' + added.join(', ')),
|
|
60
|
+
...keep(removed.length === 0 ? null : ' ' + ALLOW_OWNER + ': ' + removed.join(', ') + ' -> (removed)'),
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
/** Values in `source` that `other` does not already hold, extras included. */
|
|
64
|
+
function missing(source, other) {
|
|
65
|
+
const left = [...other];
|
|
66
|
+
return source.filter((value) => {
|
|
67
|
+
const at = left.indexOf(value);
|
|
68
|
+
if (at < 0)
|
|
69
|
+
return true;
|
|
70
|
+
left.splice(at, 1);
|
|
71
|
+
return false;
|
|
72
|
+
});
|
|
73
|
+
}
|