repown 0.4.0 → 0.5.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +72 -1
  2. package/README.md +4 -1
  3. package/dist/cli.js +4 -4
  4. package/dist/commands/accounts.js +19 -13
  5. package/dist/commands/doctor.js +7 -5
  6. package/dist/commands/fix.js +5 -5
  7. package/dist/commands/guard.js +7 -5
  8. package/dist/commands/off.js +4 -4
  9. package/dist/commands/reauthor.js +5 -5
  10. package/dist/commands/scan.js +7 -6
  11. package/dist/commands/setup.js +3 -3
  12. package/dist/commands/start.js +6 -6
  13. package/dist/commands/status.js +19 -15
  14. package/dist/commands/use.js +11 -11
  15. package/dist/core/blockers.js +8 -7
  16. package/dist/core/config-snapshot.js +97 -0
  17. package/dist/core/credential/gcm.js +2 -2
  18. package/dist/core/credential/gh.js +12 -5
  19. package/dist/core/credential/repair.js +1 -1
  20. package/dist/core/git.js +9 -4
  21. package/dist/core/guard/check.js +44 -8
  22. package/dist/core/guard/hook.js +1 -1
  23. package/dist/core/hosts/github.js +11 -5
  24. package/dist/core/hosts/index.js +21 -3
  25. package/dist/core/identity.js +1 -1
  26. package/dist/core/inspect.js +6 -6
  27. package/dist/core/push-destination.js +1 -1
  28. package/dist/core/push-state.js +5 -5
  29. package/dist/core/reauthor.js +3 -3
  30. package/dist/core/registry.js +1 -1
  31. package/dist/core/unpushed.js +10 -9
  32. package/dist/program.js +10 -10
  33. package/dist/ui/args.js +3 -3
  34. package/dist/ui/command.js +1 -1
  35. package/dist/ui/dispatch.js +6 -6
  36. package/dist/ui/format.js +31 -0
  37. package/dist/ui/help.js +2 -2
  38. package/dist/ui/prompt.js +1 -1
  39. package/dist/wizard/clack.js +56 -21
  40. package/dist/wizard/engine.js +1 -1
  41. package/dist/wizard/home-context.js +23 -10
  42. package/dist/wizard/home-flow.js +33 -11
  43. package/dist/wizard/home-run.js +85 -44
  44. package/dist/wizard/home-text.js +14 -3
  45. package/dist/wizard/plain.js +2 -2
  46. package/dist/wizard/review-text.js +19 -4
  47. package/dist/wizard/setup-changes.js +2 -2
  48. package/dist/wizard/setup-context.js +20 -16
  49. package/dist/wizard/setup-flow.js +112 -34
  50. package/dist/wizard/setup-run.js +31 -28
  51. package/npm-shrinkwrap.json +378 -17
  52. package/package.json +3 -3
@@ -8,15 +8,15 @@
8
8
  // text question, offered from the second question on. Esc and Ctrl-C cancel.
9
9
  import { styleText } from 'node:util';
10
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";
11
+ import { BACK, CANCEL } from './engine.js';
12
+ import { BACK_WORD } from './plain.js';
13
+ import { BACK_TO_REVIEW, COMMAND_MARK, PICK_QUESTION, RUN_THIS_STEP, labelOf, reviewDefault, reviewLines, reviewOptions, reviewQuestion, stepConfirmLines, stepOptions, textWidth, wrap } from './review-text.js';
14
14
  /** A value no real choice can have. */
15
15
  const GO_BACK = '\u0000back';
16
16
  export function clackPrompter(streams) {
17
17
  const io = { input: streams.input, output: streams.output };
18
18
  return {
19
- ask: (step, asked) => askDrawn(step, asked, io),
19
+ ask: (step, asked) => askStep(step, asked, io),
20
20
  review: (review) => showReview(review, io),
21
21
  pickStep: (steps) => pickClack(steps, io),
22
22
  choose: (message, options) => chooseClack(message, options, io),
@@ -24,18 +24,23 @@ export function clackPrompter(streams) {
24
24
  show: (lines) => logLines(io, lines),
25
25
  confirmStep: (confirm) => showStep(confirm, io),
26
26
  close: () => { },
27
- intro: (title) => p.intro(title, io),
27
+ intro: (title) => p.intro(badge(title), io),
28
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)),
29
+ outro: (message, cancelled) => (cancelled ? p.cancel(message, io) : p.outro(commandOr(message), io)),
30
30
  // A line, not clack's spinner: the spinner takes over Ctrl-C and exits 0, where a
31
31
  // cancel must exit 130.
32
32
  busy: (message) => p.log.step(message, io),
33
33
  };
34
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);
35
+ /** The title black on cyan. Its padding only comes with the colour, so a plain title is unchanged. */
36
+ function badge(title) {
37
+ const padded = ' ' + title + ' ';
38
+ const styled = styleText(['bgCyan', 'black'], padded);
39
+ return styled === padded ? title : styled;
40
+ }
41
+ /** A `$ command` hand-over line cyan; any other closing line as it is. */
42
+ function commandOr(message) {
43
+ return message.startsWith(COMMAND_MARK) ? styleText('cyan', message) : message;
39
44
  }
40
45
  async function pickClack(steps, io) {
41
46
  const id = await p.select({ ...io, message: PICK_QUESTION, options: pickOptions(steps) });
@@ -77,13 +82,13 @@ function backOption(asked) {
77
82
  async function askSelect(step, asked, io) {
78
83
  const options = [...asked.choices.map((choice) => ({ ...choice })), ...backOption(asked)];
79
84
  const preset = typeof asked.initial === 'string' ? { initialValue: asked.initial } : {};
80
- const value = await p.select({ ...io, ...preset, message: messageOf(step, io), options });
85
+ const value = await p.select({ ...io, ...preset, message: messageOf(step, io, asked.detail), options });
81
86
  if (p.isCancel(value))
82
87
  return CANCEL;
83
88
  return value === GO_BACK ? BACK : value;
84
89
  }
85
90
  async function askConfirm(step, asked, io) {
86
- const value = await p.select({ ...io, message: messageOf(step, io), initialValue: asked.initial === true ? 'yes' : 'no',
91
+ const value = await p.select({ ...io, message: messageOf(step, io, asked.detail), initialValue: asked.initial === true ? 'yes' : 'no',
87
92
  options: [{ value: 'yes', label: 'Yes' }, { value: 'no', label: 'No' }, ...backOption(asked)] });
88
93
  if (p.isCancel(value))
89
94
  return CANCEL;
@@ -92,7 +97,7 @@ async function askConfirm(step, asked, io) {
92
97
  async function askText(step, asked, io) {
93
98
  const preset = typeof asked.initial === 'string' ? { initialValue: asked.initial } : {};
94
99
  const extra = asked.canGoBack ? 'type ' + BACK_WORD + ' to go back' : undefined;
95
- const value = await p.text({ ...io, ...preset, message: messageOf(step, io, extra),
100
+ const value = await p.text({ ...io, ...preset, message: messageOf(step, io, asked.detail, extra),
96
101
  validate: (typed) => (isBack(typed, asked) ? undefined : step.validate?.((typed ?? '').trim()) ?? undefined) });
97
102
  if (p.isCancel(value))
98
103
  return CANCEL;
@@ -104,28 +109,58 @@ function isBack(typed, asked) {
104
109
  }
105
110
  /**
106
111
  * 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.
112
+ * so a text question draws it itself, with clack's own bar (`|` where Unicode isn't drawn).
113
+ */
114
+ const TEXT_GUTTER = styleText('gray', p.S_BAR) + ' ';
115
+ /**
116
+ * The question, and under it the hint and the step's detail, wrapped in the gutter --
117
+ * visible whatever the answer shows. The detail sits in the question's own block, as the
118
+ * plain prompter draws it, never as a line above that reads as the previous answer's.
108
119
  */
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(' · ');
120
+ function messageOf(step, io, detail, extra) {
113
121
  const gutter = step.kind === 'text' ? TEXT_GUTTER : '';
114
- return [step.message, ...(hint ? wrap(hint, widthOf(io, GUTTER)) : [])].join('\n' + gutter);
122
+ const width = widthOf(io, GUTTER);
123
+ const notes = [step.hint, detail].flatMap((text) => (text ? wrap(text, width) : []));
124
+ return [step.message, ...hintRows(notes, extra, width)].join('\n' + gutter);
125
+ }
126
+ /** The hint rows, then the extra ("type < to go back") on the last row if it fits whole, else on its own. */
127
+ function hintRows(rows, extra, width) {
128
+ if (!extra)
129
+ return [...rows];
130
+ const joined = rows.length > 0 ? rows[rows.length - 1] + ' · ' + extra : extra;
131
+ if (joined.length <= width)
132
+ return [...rows.slice(0, -1), joined];
133
+ return [...rows, ...wrap(extra, width)];
115
134
  }
116
135
  async function showStep(confirm, io) {
117
136
  logLines(io, stepConfirmLines(confirm));
118
137
  return chosen(confirm, io);
119
138
  }
120
139
  function logLines(io, lines) {
121
- p.log.message(lines.flatMap((text) => wrap(text, widthOf(io, GUTTER))).join('\n'), io);
140
+ p.log.message(lines.flatMap((text) => accented(text, widthOf(io, GUTTER))).join('\n'), io);
141
+ }
142
+ /**
143
+ * One shown line, wrapped, then styled: a label's value bold cyan, a `$ command` cyan.
144
+ * Styling after wrapping keeps every line as wide as it is without colour (ADR-027).
145
+ */
146
+ function accented(text, width) {
147
+ const rows = wrap(text, width);
148
+ if (text.startsWith(COMMAND_MARK))
149
+ return rows.map((row) => styleText('cyan', row));
150
+ const label = labelOf(text);
151
+ return rows.map((row, index) => accentLine(row, index === 0 ? label : ' '.repeat(label.length)));
152
+ }
153
+ function accentLine(line, label) {
154
+ if (!label || !line.startsWith(label))
155
+ return line;
156
+ return label + styleText(['bold', 'cyan'], line.slice(label.length));
122
157
  }
123
158
  async function chosen(confirm, io) {
124
159
  const choice = await p.select({ ...io, message: RUN_THIS_STEP, options: stepOptions(), initialValue: confirm.initial });
125
160
  return p.isCancel(choice) ? CANCEL : choice;
126
161
  }
127
162
  async function showReview(review, io) {
128
- p.note(reviewLines(review, widthOf(io, BOX), (text) => styleText('dim', text)).join('\n'), review.title, io);
163
+ p.note(reviewLines(review, widthOf(io, BOX), (text) => styleText('cyan', text)).join('\n'), review.title, io);
129
164
  const choice = await p.select({ ...io, message: reviewQuestion(review), options: reviewOptions(review),
130
165
  initialValue: reviewDefault(review) });
131
166
  return p.isCancel(choice) ? CANCEL : choice;
@@ -73,7 +73,7 @@ async function ask(step, at, context, prompter) {
73
73
  const { answers, canGoBack } = at;
74
74
  const initial = answers[step.id] ?? await step.initial?.(answers, context);
75
75
  const choices = step.choices?.(answers, context) ?? [];
76
- const detail = step.detail?.(answers, context);
76
+ const detail = await step.detail?.(answers, context);
77
77
  return prompter.ask(drawn(step, answers, context), { initial, choices, detail, canGoBack });
78
78
  }
79
79
  function drawn(step, answers, context) {
@@ -3,19 +3,27 @@
3
3
  // it cannot list is skipped; `repown scan` is the command that reports those.
4
4
  import { readdir } from 'node:fs/promises';
5
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";
6
+ import { Git } from '../core/git.js';
7
+ import { snapshotOf } from '../core/config-snapshot.js';
8
+ import { inspectAuth, inspectRepo } from '../core/inspect.js';
9
+ import { planRepair } from '../core/credential/repair.js';
10
+ import { loadRegistry } from '../core/registry.js';
11
+ import { identityProblems } from '../commands/status.js';
12
+ import { CONCURRENCY, discover, mapLimited } from '../commands/scan.js';
13
+ import { signedInLogins } from './setup-flow.js';
12
14
  /** How many levels below this folder the start screen looks. `repown scan` looks further. */
13
15
  export const HOME_DEPTH = 2;
14
16
  export async function readHome(cwd, deps = {}) {
15
- const [registry, ghIsHelper, clones] = await Promise.all([
16
- registryOf(deps), helperOf(cwd, deps), clonesOf(cwd, deps),
17
+ const [registry, ghIsHelper, clones, identity] = await Promise.all([
18
+ registryOf(deps), helperOf(cwd, deps), clonesOf(cwd, deps), (deps.identity ?? identityOf)(cwd),
17
19
  ]);
18
- return { cwd: resolve(cwd), registry, ghIsHelper, clones };
20
+ return { cwd: resolve(cwd), registry, ghIsHelper, clones, identity };
21
+ }
22
+ /** Outside a clone, git's own lookup is the global (or system) value. */
23
+ async function identityOf(cwd) {
24
+ const git = new Git(cwd);
25
+ const [name, email] = await Promise.all([git.getConfig('user.name'), git.getConfig('user.email')]);
26
+ return { name, email };
19
27
  }
20
28
  function registryOf(deps) {
21
29
  return deps.registry ? deps.registry() : loadRegistry();
@@ -85,7 +93,7 @@ async function childNames(cwd) {
85
93
  }
86
94
  async function describeClone(dir, deps) {
87
95
  const inspect = deps.inspect ?? inspectRepo;
88
- const repo = await inspect(new Git(dir));
96
+ const repo = await inspect(snapshotOf(new Git(dir)));
89
97
  if (!repo.isRepo)
90
98
  return null;
91
99
  return { path: resolve(dir), setUp: identityProblems(repo).length === 0 };
@@ -99,3 +107,8 @@ function skipHomeEntry(name) {
99
107
  return true;
100
108
  return process.platform === 'win32' && name.toLowerCase() === 'appdata';
101
109
  }
110
+ /** The github.com logins gh and Git Credential Manager are signed in as; read only when Record an account asks. */
111
+ export async function readSignedIn(cwd) {
112
+ const auth = await inspectAuth(new Git(cwd));
113
+ return signedInLogins(auth.ghPresent ? auth.gh : null, auth.stored);
114
+ }
@@ -1,13 +1,13 @@
1
1
  // Pure: the summary, the menu and the clone list, and the command an action
2
2
  // stands for. Drawing and running live in home-run.ts.
3
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";
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, HINT_REMOVE, NONE_RECORDED, NOT_SET_UP, QUIT, RECORD_ACCOUNT, REMOVE_ACCOUNT, SET_UP, SETUP_CLONE, SHOW_HELP, STOP_GH, clonesNone, clonesSome, couldNotRead, moreClones, recordedLine, setupHint, unreadableLine, } from './home-text.js';
7
+ import { addAccountArgv, printable } from './setup-flow.js';
8
8
  /** Shown choices. `back` returns to the menu; the others run a command or leave. */
9
9
  export const MENU = {
10
- setup: 'setup', fix: 'fix', account: 'account', doctor: 'doctor', help: 'help', quit: 'quit',
10
+ setup: 'setup', fix: 'fix', account: 'account', remove: 'remove', doctor: 'doctor', help: 'help', quit: 'quit',
11
11
  };
12
12
  export const BACK = 'back';
13
13
  const LIST_CAP = 20;
@@ -21,7 +21,21 @@ export function homeNote(home) {
21
21
  return home.clones.length === 0 ? CD_NOTE : null;
22
22
  }
23
23
  export function menuItems(home) {
24
- return [...setupItem(home), ...fixItem(home), ...STANDING];
24
+ return [...setupItem(home), ...fixItem(home), RECORD, ...removeItem(home), ...AFTER_REMOVE];
25
+ }
26
+ /** A recorded account's choice value; prefixed so no login (`back`, say) is ever another choice. */
27
+ const ACCOUNT_VALUE = 'account:';
28
+ export function accountOfChoice(value) {
29
+ return value.startsWith(ACCOUNT_VALUE) ? value.slice(ACCOUNT_VALUE.length) : null;
30
+ }
31
+ /** The recorded accounts, each with the name and email it commits as. */
32
+ export function accountChoices(home) {
33
+ if (!home.registry.ok)
34
+ return [];
35
+ const accounts = home.registry.value.accounts;
36
+ return Object.keys(accounts).sort().map((login) => ({
37
+ value: ACCOUNT_VALUE + login, label: printable(login), hint: printable(accounts[login].name + ' <' + accounts[login].email + '>'),
38
+ }));
25
39
  }
26
40
  export function listedClones(home) {
27
41
  const ordered = orderClones(home);
@@ -31,8 +45,8 @@ export function listedClones(home) {
31
45
  }
32
46
  /**
33
47
  * 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.
48
+ * it. `accounts add` is the very command setup runs for a new account: every answer
49
+ * as a flag, and `--` before the login, so a login that starts with a dash stays the account.
36
50
  */
37
51
  export function commandFor(action) {
38
52
  if (action.kind === 'setup')
@@ -40,7 +54,9 @@ export function commandFor(action) {
40
54
  if (action.kind === 'fix')
41
55
  return ['fix'];
42
56
  if (action.kind === 'account')
43
- return ['accounts', 'add', '--', action.login];
57
+ return addAccountArgv(action);
58
+ if (action.kind === 'remove')
59
+ return ['accounts', 'remove', '--', action.login];
44
60
  return ['doctor'];
45
61
  }
46
62
  function accountsText(registry) {
@@ -67,13 +83,19 @@ function setupItem(home) {
67
83
  const pending = home.clones.filter((clone) => !clone.setUp).length;
68
84
  return [{ value: MENU.setup, label: SETUP_CLONE, hint: setupHint(pending, home.clones.length) }];
69
85
  }
86
+ /** Only when every entry reads: accounts remove refuses to rewrite a file it can't fully read. */
87
+ function removeItem(home) {
88
+ if (!home.registry.ok || home.registry.value.unreadable.length > 0)
89
+ return [];
90
+ return accountChoices(home).length > 0 ? [{ value: MENU.remove, label: REMOVE_ACCOUNT, hint: HINT_REMOVE }] : [];
91
+ }
70
92
  function fixItem(home) {
71
93
  if (!home.ghIsHelper)
72
94
  return [];
73
95
  return [{ value: MENU.fix, label: STOP_GH, hint: HINT_FIX }];
74
96
  }
75
- const STANDING = [
76
- { value: MENU.account, label: RECORD_ACCOUNT, hint: HINT_ACCOUNT },
97
+ const RECORD = { value: MENU.account, label: RECORD_ACCOUNT, hint: HINT_ACCOUNT };
98
+ const AFTER_REMOVE = [
77
99
  { value: MENU.doctor, label: CHECK_MACHINE, hint: HINT_DOCTOR },
78
100
  { value: MENU.help, label: SHOW_HELP },
79
101
  { value: MENU.quit, label: QUIT },
@@ -1,29 +1,29 @@
1
1
  // The start screen: a summary of this folder, then one menu. Each action prints
2
2
  // the command it stands for and runs that command's own run(). Loaded only
3
3
  // through the runner start.ts returns, so a typed command never imports it.
4
+ // Once fix, doctor, accounts add or accounts remove has run, failed or not, the screen opens again (ADR-028).
4
5
  //
5
6
  // 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";
7
+ import fixCommand from '../commands/fix.js';
8
+ import doctorCommand from '../commands/doctor.js';
9
+ import accountsGroup from '../commands/accounts.js';
10
+ import { COMMANDS } from '../program.js';
11
+ import { parseArgs } from '../ui/args.js';
12
+ import { specFor } from '../ui/command.js';
13
+ import { loadEntries } from '../ui/dispatch.js';
14
+ import * as out from '../ui/format.js';
15
+ import { renderTopHelp } from '../ui/help.js';
16
+ import { CANCEL, runFlow } from './engine.js';
17
+ import { readHome, readSignedIn } from './home-context.js';
18
+ import { BACK as HOME_BACK, MENU, accountChoices, accountOfChoice, commandFor, homeNote, listedClones, menuItems, summaryLines } from './home-flow.js';
19
+ import { BACK_LABEL, BUSY, CANCELLED, QUIT, WHAT_NEXT, WHICH_ACCOUNT, WHICH_CLONE, homeTitle, removeLabel, removeNote, removeQuestion } from './home-text.js';
20
+ import { formatCommand, loginStep, newAccountOf, printable, profileSteps } from './setup-flow.js';
21
+ import { COMMAND_MARK } from './review-text.js';
22
+ import { suggester } from './setup-context.js';
23
+ import { choosePrompter, runSetup } from './setup-run.js';
21
24
  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 };
25
+ /** The account questions from the top. Back from the login returns to the menu, as `opening`. */
26
+ const FIRST_PASS = { answers: {}, start: 0, given: new Set(), history: [], toOpening: true, showOpening: false };
27
27
  export async function runHome(deps = {}) {
28
28
  const prompter = deps.prompter ?? await choosePrompter();
29
29
  try {
@@ -33,7 +33,16 @@ export async function runHome(deps = {}) {
33
33
  prompter.close();
34
34
  }
35
35
  }
36
+ /** A machine-level action ran: open the start screen again, read afresh (ADR-028). */
37
+ const AGAIN = Symbol('again');
36
38
  async function openHome(prompter, deps) {
39
+ for (;;) {
40
+ const code = await homeRound(prompter, deps);
41
+ if (code !== AGAIN)
42
+ return code;
43
+ }
44
+ }
45
+ async function homeRound(prompter, deps) {
37
46
  const cwd = deps.cwd ?? process.cwd();
38
47
  prompter.intro?.(homeTitle(printable(cwd)));
39
48
  prompter.busy?.(BUSY);
@@ -42,19 +51,20 @@ async function openHome(prompter, deps) {
42
51
  const note = homeNote(home);
43
52
  if (note)
44
53
  prompter.note(note);
45
- return chooseNext(prompter, home);
54
+ return chooseNext(prompter, home, deps);
46
55
  }
47
- async function chooseNext(prompter, home) {
56
+ async function chooseNext(prompter, home, deps) {
48
57
  for (;;) {
49
58
  const picked = await prompter.choose(WHAT_NEXT, menuItems(home));
50
59
  if (picked === CANCEL)
51
60
  return finish(prompter, CANCELLED, CANCELLED_CODE);
52
- const code = await dispatchChoice(picked, prompter, home);
61
+ const code = await dispatchChoice(picked, prompter, home, deps);
53
62
  if (code !== null)
54
63
  return code;
55
64
  }
56
65
  }
57
- async function dispatchChoice(picked, prompter, home) {
66
+ /** Setup, help and Quit end the screen; once fix, doctor, accounts add or remove has run, it comes back. */
67
+ async function dispatchChoice(picked, prompter, home, deps) {
58
68
  if (picked === MENU.quit)
59
69
  return finish(prompter, QUIT, 0);
60
70
  if (picked === MENU.help)
@@ -62,11 +72,13 @@ async function dispatchChoice(picked, prompter, home) {
62
72
  if (picked === MENU.setup)
63
73
  return pickClone(prompter, home);
64
74
  if (picked === MENU.fix)
65
- return runOwn(prompter, fixCommand, [], commandFor({ kind: 'fix' }));
75
+ return again(runOwn(prompter, fixCommand, [], commandFor({ kind: 'fix' })));
66
76
  if (picked === MENU.doctor)
67
- return runOwn(prompter, doctorCommand, [], commandFor({ kind: 'doctor' }));
77
+ return again(runOwn(prompter, doctorCommand, [], commandFor({ kind: 'doctor' })));
68
78
  if (picked === MENU.account)
69
- return recordAccount(prompter);
79
+ return recordAccount(prompter, home, deps);
80
+ if (picked === MENU.remove)
81
+ return removeAccount(prompter, home);
70
82
  return finish(prompter, QUIT, 0);
71
83
  }
72
84
  async function pickClone(prompter, home) {
@@ -86,25 +98,54 @@ async function runInClone(prompter, cwd) {
86
98
  handOver(prompter, argv);
87
99
  return runSetup(setupArgs(cwd), { interactive: true, prompter });
88
100
  }
89
- async function recordAccount(prompter) {
90
- const login = await askLogin(prompter);
91
- if (login === CANCEL)
101
+ /**
102
+ * Setup's own questions for a new account (login, host, name, email) in this frame,
103
+ * then `accounts add` with every answer as a flag, so it has nothing left to ask.
104
+ */
105
+ async function recordAccount(prompter, home, deps) {
106
+ const ctx = accountContext(home, deps);
107
+ const walk = await runFlow({ steps: [loginStep(ctx), ...profileSteps(ctx)] }, ctx, FIRST_PASS, prompter);
108
+ if (walk.status === 'cancelled')
92
109
  return finish(prompter, CANCELLED, CANCELLED_CODE);
93
- if (login === BACK)
110
+ if (walk.status === 'opening')
94
111
  return null;
95
- const argv = commandFor({ kind: 'account', login });
96
- return runOwn(prompter, accountsGroup.actions['add'], ['--', login], argv);
112
+ const argv = commandFor({ kind: 'account', ...newAccountOf(walk.answers, ctx) });
113
+ return again(runOwn(prompter, accountsGroup.actions['add'], argv.slice(2), argv));
97
114
  }
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
- }
115
+ /** Which account, then a confirm that says what stays; Back from either returns to the menu. */
116
+ async function removeAccount(prompter, home) {
117
+ const picked = await prompter.choose(WHICH_ACCOUNT, [...accountChoices(home), { value: HOME_BACK, label: BACK_LABEL }]);
118
+ if (picked === CANCEL)
119
+ return finish(prompter, CANCELLED, CANCELLED_CODE);
120
+ const login = accountOfChoice(picked);
121
+ if (login === null)
122
+ return null;
123
+ const shown = printable(login);
124
+ showLines(prompter, [removeNote(shown)]);
125
+ // Back first: Enter twice in a row must not remove anything.
126
+ const sure = await prompter.choose(removeQuestion(shown), [{ value: HOME_BACK, label: BACK_LABEL }, { value: MENU.remove, label: removeLabel(shown) }]);
127
+ if (sure === CANCEL)
128
+ return finish(prompter, CANCELLED, CANCELLED_CODE);
129
+ if (sure === HOME_BACK)
130
+ return null;
131
+ const argv = commandFor({ kind: 'remove', login });
132
+ return again(runOwn(prompter, accountsGroup.actions['remove'], argv.slice(2), argv));
133
+ }
134
+ /** The command has said how it went, in its own output; the start screen opens again. */
135
+ async function again(ran) {
136
+ await ran;
137
+ return AGAIN;
138
+ }
139
+ function accountContext(home, deps) {
140
+ const recorded = home.registry.ok ? home.registry.value.accounts : {};
141
+ return {
142
+ recorded, host: 'github', machineIdentity: home.identity,
143
+ suggest: deps.suggest ?? suggester(), signedIn: deps.signedIn ?? once(() => readSignedIn(home.cwd)),
144
+ };
145
+ }
146
+ function once(read) {
147
+ let kept;
148
+ return () => (kept ??= read());
108
149
  }
109
150
  async function runOwn(prompter, command, tokens, argv) {
110
151
  handOver(prompter, argv);
@@ -133,7 +174,7 @@ function showLines(prompter, lines) {
133
174
  }
134
175
  /** Closes the start screen's frame on the command it hands over to. */
135
176
  function handOver(prompter, argv) {
136
- const line = '> ' + formatCommand(argv);
177
+ const line = COMMAND_MARK + formatCommand(argv);
137
178
  if (prompter.outro)
138
179
  prompter.outro(line);
139
180
  else
@@ -1,11 +1,13 @@
1
1
  // The words on the start screen, in one place, so the summary and both prompters
2
2
  // say the same thing. What is shown when comes from home-flow.ts.
3
- import { displayPath } from "../ui/format.js";
3
+ import { displayPath } from '../ui/format.js';
4
4
  export const WHAT_NEXT = 'What next?';
5
5
  export const WHICH_CLONE = 'Which clone?';
6
6
  export const SETUP_CLONE = 'Set up a clone found here';
7
7
  export const STOP_GH = 'Stop gh serving credentials';
8
8
  export const RECORD_ACCOUNT = 'Record an account';
9
+ export const REMOVE_ACCOUNT = 'Remove an account';
10
+ export const WHICH_ACCOUNT = 'Which account?';
9
11
  export const CHECK_MACHINE = 'Check this machine';
10
12
  export const SHOW_HELP = 'Show help';
11
13
  export const QUIT = 'Quit';
@@ -16,15 +18,24 @@ export const NONE_RECORDED = 'none recorded';
16
18
  export const HELPER_LINE = 'gh serves git\'s credentials: run repown fix';
17
19
  export const HINT_FIX = 'repown fix';
18
20
  export const HINT_ACCOUNT = 'repown accounts add';
21
+ export const HINT_REMOVE = 'repown accounts remove';
19
22
  export const HINT_DOCTOR = 'repown doctor';
20
23
  export const NOT_SET_UP = 'not set up';
21
24
  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
25
  export const CANCELLED = 'Cancelled: nothing was changed.';
25
26
  export function homeTitle(cwd) {
26
27
  return 'repown · not a clone: ' + displayPath(cwd);
27
28
  }
29
+ /** What removing leaves as it was: said before the confirm, so nobody expects their clones to change. */
30
+ export function removeNote(login) {
31
+ return 'clones pinned to ' + login + ' keep their settings and the push guard; repown use ' + login + ' needs it recorded again';
32
+ }
33
+ export function removeQuestion(login) {
34
+ return 'Remove ' + login + ' from this machine?';
35
+ }
36
+ export function removeLabel(login) {
37
+ return 'Remove ' + login;
38
+ }
28
39
  export function recordedLine(names) {
29
40
  return names.length + ' recorded: ' + names.join(', ');
30
41
  }
@@ -7,8 +7,8 @@
7
7
  // keeps carrying only what the commands themselves print. End of input or Ctrl-C at a
8
8
  // question is a cancel, never an answer.
9
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";
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
12
  /** Typed at a text question, it means "go back" (never used for a real answer here). */
13
13
  export const BACK_WORD = '<';
14
14
  export function plainPrompter(streams) {
@@ -17,9 +17,14 @@ export function stepOptions() {
17
17
  { value: 'stop', label: 'Stop' },
18
18
  ];
19
19
  }
20
+ /**
21
+ * What starts a command line drawn to copy. Not `>`: pasted whole, that is a redirect in
22
+ * every shell and leaves a file named `repown`; `$` there is only a harmless error.
23
+ */
24
+ export const COMMAND_MARK = '$ ';
20
25
  /** What a step changes, why, and the command, in that order. */
21
26
  export function stepConfirmLines(brief) {
22
- return [...brief.changes, '', brief.why, '> ' + brief.command];
27
+ return [...brief.changes, '', brief.why, COMMAND_MARK + brief.command];
23
28
  }
24
29
  /** `fix` changes the whole machine; `reauthor` rewrites commits. */
25
30
  function needsDeliberateYes(command) {
@@ -31,8 +36,8 @@ function settledVerdict(review) {
31
36
  }
32
37
  /**
33
38
  * The body of the review, line by line, wrapped to `width`. `command` styles each
34
- * command line (dimmed where the prompter can draw colour), so the plain words stay
35
- * the eye's first stop.
39
+ * command line (cyan where the prompter can draw colour, ADR-027), so a command never
40
+ * reads as one of the plain words around it.
36
41
  */
37
42
  export function reviewLines(review, width = Infinity, command = (text) => text) {
38
43
  const words = (text) => wrap(text, width);
@@ -55,6 +60,16 @@ export const MAX_WIDTH = 72;
55
60
  export function textWidth(columns, margin) {
56
61
  return Math.max(24, Math.min(MAX_WIDTH, (columns ?? 80) - margin));
57
62
  }
63
+ /**
64
+ * What a wrapped line's continuation hangs under: any indent, then a "1. " step number or
65
+ * a "label " column (one or two words, then two spaces or more). Group 1 is the label.
66
+ */
67
+ const HANG = /^\s*(?:\d+\. |(\S+(?: \S+)? {2,}(?=\S)))?/;
68
+ /** The label column of a `label value` line, padding included; '' for any other line. */
69
+ export function labelOf(text) {
70
+ const found = HANG.exec(text);
71
+ return found[1] !== undefined && found[0] === found[1] ? found[1] : '';
72
+ }
58
73
  /**
59
74
  * One line, wrapped at spaces to `width`. Continuation lines line up under the text:
60
75
  * past any indent, a "1. " step number, or a "label " column.
@@ -62,7 +77,7 @@ export function textWidth(columns, margin) {
62
77
  export function wrap(text, width) {
63
78
  if (text.length <= width)
64
79
  return [text];
65
- const hang = ' '.repeat(/^\s*(?:\d+\. |\S+(?: \S+)? {2,})?/.exec(text)[0].length);
80
+ const hang = ' '.repeat(HANG.exec(text)[0].length);
66
81
  const lines = [];
67
82
  let line = '';
68
83
  for (const word of text.split(/(?<=\S) (?=\S)/)) {
@@ -1,8 +1,8 @@
1
1
  // What one setup run changed. The diff is pure. The read is this clone's local
2
2
  // config and the guard, through the Git wrapper. A credential helper is never
3
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";
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
6
  const ALLOW_OWNER = 'repown.allowOwner';
7
7
  const AUTO_UPSTREAM = 'push.autoSetupRemote';
8
8
  const LEADING = [NAME_KEY, EMAIL_KEY, USE_CONFIG_ONLY_KEY, ACCOUNT_KEY];