@venizia/vulcan 0.4.9-0 → 0.4.9-2

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 (141) hide show
  1. package/base/extensions/vulcan-guards.ts +48 -41
  2. package/base/hooks/antigravity-guards.ts +2 -2
  3. package/base/hooks/codex-guards.ts +15 -5
  4. package/base/hooks/guard-git.ts +8 -8
  5. package/base/hooks/guard-manifest.ts +4 -5
  6. package/base/hooks/guard-outside.ts +8 -8
  7. package/base/hooks/guard-package.ts +5 -6
  8. package/base/hooks/guard-remote-hosts.ts +6 -7
  9. package/base/hooks/guard-scripting.ts +5 -6
  10. package/base/hooks/guard-writes.ts +8 -9
  11. package/base/hooks/post-tool.ts +2 -2
  12. package/base/hooks/prompt-submit.ts +2 -2
  13. package/base/hooks/session-start.ts +3 -3
  14. package/base/hooks/stop.ts +2 -2
  15. package/base/hooks/subagent-start.ts +2 -2
  16. package/base/skills/docs-update/SKILL.md +4 -2
  17. package/base/skills/visualize-work/SKILL.md +9 -0
  18. package/package.json +3 -3
  19. package/src/agents/atlas-block.ts +43 -43
  20. package/src/agents/codex-hooks.ts +45 -43
  21. package/src/agents/detect.ts +19 -16
  22. package/src/agents/exclude.ts +96 -97
  23. package/src/agents/hooks-json.ts +38 -36
  24. package/src/agents/local.ts +46 -43
  25. package/src/agents/prompt.ts +45 -42
  26. package/src/agents/render.ts +201 -195
  27. package/src/agents/resolve.ts +133 -134
  28. package/src/agents/table.ts +12 -8
  29. package/src/commands/add.command.ts +2 -2
  30. package/src/commands/agents.command.ts +11 -17
  31. package/src/commands/clean.command.ts +6 -6
  32. package/src/commands/doctor.command.ts +4 -4
  33. package/src/commands/init.command.ts +12 -8
  34. package/src/commands/preset.command.ts +2 -2
  35. package/src/commands/profile.command.ts +42 -49
  36. package/src/commands/sync.command.ts +109 -111
  37. package/src/commands/upgrade.command.ts +80 -75
  38. package/src/commands/visualize.command.ts +7 -13
  39. package/src/commands/x.command.ts +55 -50
  40. package/src/common/format.ts +23 -21
  41. package/src/common/json.ts +8 -5
  42. package/src/common/layout-folder.ts +94 -91
  43. package/src/common/paths.ts +14 -12
  44. package/src/config/loader.ts +52 -49
  45. package/src/config/packages.ts +14 -11
  46. package/src/config/schema.ts +3 -3
  47. package/src/create/add-app.ts +519 -509
  48. package/src/create/add-lib.ts +237 -240
  49. package/src/create/bunfig.ts +26 -23
  50. package/src/create/checks.ts +36 -34
  51. package/src/create/diff.ts +21 -23
  52. package/src/create/dockerignore.ts +26 -24
  53. package/src/create/files.ts +7 -5
  54. package/src/create/init-monorepo.ts +75 -80
  55. package/src/create/lib-preset.ts +59 -63
  56. package/src/create/manifest-json.ts +10 -4
  57. package/src/create/names.ts +217 -210
  58. package/src/create/next.ts +26 -23
  59. package/src/create/plan.ts +15 -12
  60. package/src/create/preset-diff.ts +301 -303
  61. package/src/create/preset-menu.ts +38 -32
  62. package/src/create/preset.ts +202 -197
  63. package/src/create/process.ts +79 -72
  64. package/src/create/scratch.ts +48 -49
  65. package/src/create/workspace.ts +404 -396
  66. package/src/doctor/checks.ts +1027 -1014
  67. package/src/doctor/makefile.ts +17 -13
  68. package/src/doctor/runner.ts +88 -118
  69. package/src/doctor/skills.ts +97 -95
  70. package/src/doctor/tags.ts +42 -37
  71. package/src/hooks/codex-patch.ts +25 -22
  72. package/src/hooks/command-view.ts +44 -39
  73. package/src/hooks/decision.ts +37 -37
  74. package/src/hooks/guard-git.ts +51 -49
  75. package/src/hooks/guard-manifest.ts +58 -55
  76. package/src/hooks/guard-outside.ts +93 -91
  77. package/src/hooks/guard-package.ts +82 -79
  78. package/src/hooks/guard-scripting.ts +19 -14
  79. package/src/hooks/guard-writes.ts +154 -149
  80. package/src/hooks/owner-reply.ts +127 -127
  81. package/src/hooks/post-tool.ts +94 -93
  82. package/src/hooks/project-guards.ts +65 -62
  83. package/src/hooks/project-tmp.ts +27 -24
  84. package/src/hooks/remote-hosts.ts +153 -130
  85. package/src/hooks/session-digest.ts +98 -101
  86. package/src/hooks/shell.ts +509 -499
  87. package/src/hooks/subagent-card.ts +11 -7
  88. package/src/hooks/trusted-scripts.ts +106 -104
  89. package/src/init/scaffold.ts +135 -132
  90. package/src/local/clean.ts +38 -36
  91. package/src/local/format.ts +21 -17
  92. package/src/local/scan.ts +144 -138
  93. package/src/manifest/render.ts +110 -104
  94. package/src/owner/card.ts +51 -46
  95. package/src/owner/profile.ts +99 -93
  96. package/src/owner/reply-switch.ts +9 -6
  97. package/src/owner/shape.ts +71 -69
  98. package/src/owner/survey.ts +173 -171
  99. package/src/owner/view.ts +62 -59
  100. package/src/owner/violations.ts +119 -123
  101. package/src/rules/base.ts +15 -11
  102. package/src/rules/parser.ts +39 -32
  103. package/src/rules/slots.ts +8 -6
  104. package/src/signals/claims.ts +30 -28
  105. package/src/signals/classify.ts +38 -33
  106. package/src/signals/loop.ts +39 -37
  107. package/src/signals/state.ts +79 -81
  108. package/src/skills/frontmatter.ts +28 -28
  109. package/src/sync/agent-files.ts +217 -199
  110. package/src/sync/agent-removal.ts +56 -59
  111. package/src/sync/atlas-block.ts +45 -43
  112. package/src/sync/base-rules.ts +32 -30
  113. package/src/sync/claude-launch.ts +36 -34
  114. package/src/sync/exclude.ts +77 -69
  115. package/src/sync/local-choice.ts +50 -47
  116. package/src/sync/planner.ts +59 -61
  117. package/src/sync/prune.ts +52 -48
  118. package/src/sync/settings.ts +166 -167
  119. package/src/ui/capability.ts +12 -7
  120. package/src/ui/keyboard.ts +38 -38
  121. package/src/ui/prompt.ts +79 -77
  122. package/src/visualize/assets.ts +33 -24
  123. package/src/visualize/charts.ts +179 -176
  124. package/src/visualize/compare-css.ts +3 -2
  125. package/src/visualize/compare-render.ts +9 -7
  126. package/src/visualize/diagrams.ts +189 -174
  127. package/src/visualize/grid-span.ts +34 -0
  128. package/src/visualize/icons.ts +7 -5
  129. package/src/visualize/kinds-render.ts +50 -45
  130. package/src/visualize/kinds-validate.ts +12 -10
  131. package/src/visualize/labels.ts +21 -17
  132. package/src/visualize/layout.ts +112 -97
  133. package/src/visualize/page.ts +92 -104
  134. package/src/visualize/render.ts +143 -140
  135. package/src/visualize/sections.ts +395 -384
  136. package/src/visualize/sources.ts +2 -2
  137. package/src/visualize/theme.ts +12 -10
  138. package/src/visualize/validate.ts +13 -9
  139. package/src/wiki/anchors.ts +38 -36
  140. package/src/wiki/command.ts +4 -4
  141. package/src/wiki/source-links.ts +46 -44
@@ -12,49 +12,6 @@ const mergeArrays = (opts: { base: unknown[]; overlay: unknown[] }): unknown[] =
12
12
  return [...opts.base, ...opts.overlay.filter((entry) => !seen.has(JSON.stringify(entry)))];
13
13
  };
14
14
 
15
- /** `overlay` wins key by key; one level deep into the object-of-entries keys (SyncConstants.MERGE_ONE_LEVEL). */
16
- export const mergeSettings = (opts: { base: TSettings; overlay: TSettings }): TSettings => {
17
- const merged: TSettings = { ...opts.base };
18
-
19
- for (const [key, value] of Object.entries(opts.overlay)) {
20
- const current = { value: merged[key] };
21
- const incoming = { value };
22
-
23
- if (
24
- SyncConstants.UNION_ARRAYS.has(key) &&
25
- Array.isArray(current.value) &&
26
- Array.isArray(value)
27
- ) {
28
- merged[key] = mergeArrays({ base: current.value, overlay: value });
29
- continue;
30
- }
31
-
32
- if (
33
- !SyncConstants.MERGE_ONE_LEVEL.has(key) ||
34
- !isSettingsObject(current) ||
35
- !isSettingsObject(incoming)
36
- ) {
37
- merged[key] = value;
38
- continue;
39
- }
40
-
41
- const inner: TSettings = { ...current.value };
42
-
43
- for (const [innerKey, innerValue] of Object.entries(incoming.value)) {
44
- const existing = inner[innerKey];
45
-
46
- inner[innerKey] =
47
- Array.isArray(existing) && Array.isArray(innerValue)
48
- ? mergeArrays({ base: existing, overlay: innerValue })
49
- : innerValue;
50
- }
51
-
52
- merged[key] = inner;
53
- }
54
-
55
- return merged;
56
- };
57
-
58
15
  const toKey = (opts: { value: unknown }): string => JSON.stringify(opts.value);
59
16
 
60
17
  /** The command of a hook; `undefined` when `hook` is not an object with a command string. */
@@ -85,43 +42,6 @@ const extractEntryCommands = (opts: { entries: unknown }): string[] =>
85
42
  )
86
43
  : [];
87
44
 
88
- /** Every hook command of a settings file, across its events. */
89
- export const extractHookCommands = (opts: { settings: TSettings | undefined }): string[] => {
90
- const hooks = { value: opts.settings?.[SyncConstants.HOOKS_KEY] };
91
-
92
- return isSettingsObject(hooks)
93
- ? Object.values(hooks.value).flatMap((entries) => extractEntryCommands({ entries }))
94
- : [];
95
- };
96
-
97
- /**
98
- * `entries` without the hooks whose command is in `commands`: an entry's other hooks and keys stay,
99
- * an entry left with no hooks goes, and anything that is not an entry is kept as it is.
100
- */
101
- export const removeHookCommands = (opts: {
102
- entries: unknown[];
103
- commands: Set<string>;
104
- }): unknown[] =>
105
- opts.entries.flatMap((entry) => {
106
- const hookEntry = toHookEntry({ entry });
107
-
108
- if (hookEntry === undefined) {
109
- return [entry];
110
- }
111
-
112
- const kept = hookEntry.hooks.filter((hook) => {
113
- const command = toHookCommand({ hook });
114
-
115
- return command === undefined || !opts.commands.has(command);
116
- });
117
-
118
- if (kept.length === hookEntry.hooks.length) {
119
- return [entry];
120
- }
121
-
122
- return kept.length > 0 ? [{ ...hookEntry.entry, hooks: kept }] : [];
123
- });
124
-
125
45
  /** `settings` with its keys in `reference`'s order: keys `reference` has keep their place, new keys follow. */
126
46
  const orderKeysLike = (opts: { settings: TSettings; reference: TSettings }): TSettings => {
127
47
  const keys = [
@@ -132,120 +52,199 @@ const orderKeysLike = (opts: { settings: TSettings; reference: TSettings }): TSe
132
52
  return Object.fromEntries(keys.map((key) => [key, opts.settings[key]]));
133
53
  };
134
54
 
135
- /**
136
- * `settings` in the key order the user's file already has, here and one level down: a sync that only
137
- * reorders keys rewrites the file, and `doctor` then calls a fresh sync stale.
138
- */
139
- export const keepKeyOrder = (opts: { settings: TSettings; reference: TSettings }): TSettings => {
140
- const ordered = orderKeysLike(opts);
141
-
142
- for (const key of Object.keys(ordered).filter((entry) =>
143
- SyncConstants.MERGE_ONE_LEVEL.has(entry),
144
- )) {
145
- const value = { value: ordered[key] };
146
- const previous = { value: opts.reference[key] };
147
-
148
- if (isSettingsObject(value) && isSettingsObject(previous)) {
149
- ordered[key] = orderKeysLike({ settings: value.value, reference: previous.value });
55
+ export class ProjectSettings {
56
+ /** `overlay` wins key by key; one level deep into the object-of-entries keys (SyncConstants.MERGE_ONE_LEVEL). */
57
+ static merge(opts: { base: TSettings; overlay: TSettings }): TSettings {
58
+ const merged: TSettings = { ...opts.base };
59
+
60
+ for (const [key, value] of Object.entries(opts.overlay)) {
61
+ const current = { value: merged[key] };
62
+ const incoming = { value };
63
+
64
+ if (
65
+ SyncConstants.UNION_ARRAYS.has(key) &&
66
+ Array.isArray(current.value) &&
67
+ Array.isArray(value)
68
+ ) {
69
+ merged[key] = mergeArrays({ base: current.value, overlay: value });
70
+ continue;
71
+ }
72
+
73
+ if (
74
+ !SyncConstants.MERGE_ONE_LEVEL.has(key) ||
75
+ !isSettingsObject(current) ||
76
+ !isSettingsObject(incoming)
77
+ ) {
78
+ merged[key] = value;
79
+ continue;
80
+ }
81
+
82
+ const inner: TSettings = { ...current.value };
83
+
84
+ for (const [innerKey, innerValue] of Object.entries(incoming.value)) {
85
+ const existing = inner[innerKey];
86
+
87
+ inner[innerKey] =
88
+ Array.isArray(existing) && Array.isArray(innerValue)
89
+ ? mergeArrays({ base: existing, overlay: innerValue })
90
+ : innerValue;
91
+ }
92
+
93
+ merged[key] = inner;
150
94
  }
95
+
96
+ return merged;
151
97
  }
152
98
 
153
- return ordered;
154
- };
99
+ /** Every hook command of a settings file, across its events. */
100
+ static extractHookCommands(opts: { settings: TSettings | undefined }): string[] {
101
+ const hooks = { value: opts.settings?.[SyncConstants.HOOKS_KEY] };
155
102
 
156
- /** `settings` without what `managed` (the overlay of a previous sync) put there; anything else stays. */
157
- export const removeManagedSettings = (opts: {
158
- settings: TSettings;
159
- managed: TSettings;
160
- }): TSettings => {
161
- const remaining: TSettings = { ...opts.settings };
162
-
163
- for (const [key, managedValue] of Object.entries(opts.managed)) {
164
- const current = { value: remaining[key] };
165
- const managed = { value: managedValue };
166
-
167
- if (
168
- SyncConstants.UNION_ARRAYS.has(key) &&
169
- Array.isArray(current.value) &&
170
- Array.isArray(managedValue)
171
- ) {
172
- const managedEntries = new Set(managedValue.map((entry) => toKey({ value: entry })));
173
- const kept = current.value.filter((entry) => !managedEntries.has(toKey({ value: entry })));
174
-
175
- if (kept.length > 0) {
176
- remaining[key] = kept;
177
- } else {
178
- delete remaining[key];
103
+ return isSettingsObject(hooks)
104
+ ? Object.values(hooks.value).flatMap((entries) => extractEntryCommands({ entries }))
105
+ : [];
106
+ }
107
+
108
+ /**
109
+ * `entries` without the hooks whose command is in `commands`: an entry's other hooks and keys stay,
110
+ * an entry left with no hooks goes, and anything that is not an entry is kept as it is.
111
+ */
112
+ static removeHookCommands(opts: { entries: unknown[]; commands: Set<string> }): unknown[] {
113
+ return opts.entries.flatMap((entry) => {
114
+ const hookEntry = toHookEntry({ entry });
115
+
116
+ if (hookEntry === undefined) {
117
+ return [entry];
179
118
  }
180
119
 
181
- continue;
182
- }
120
+ const kept = hookEntry.hooks.filter((hook) => {
121
+ const command = toHookCommand({ hook });
183
122
 
184
- if (
185
- !SyncConstants.MERGE_ONE_LEVEL.has(key) ||
186
- !isSettingsObject(current) ||
187
- !isSettingsObject(managed)
188
- ) {
189
- if (toKey({ value: remaining[key] }) === toKey({ value: managedValue })) {
190
- delete remaining[key];
123
+ return command === undefined || !opts.commands.has(command);
124
+ });
125
+
126
+ if (kept.length === hookEntry.hooks.length) {
127
+ return [entry];
191
128
  }
192
129
 
193
- continue;
130
+ return kept.length > 0 ? [{ ...hookEntry.entry, hooks: kept }] : [];
131
+ });
132
+ }
133
+
134
+ /**
135
+ * `settings` in the key order the user's file already has, here and one level down: a sync that only
136
+ * reorders keys rewrites the file, and `doctor` then calls a fresh sync stale.
137
+ */
138
+ static keepKeyOrder(opts: { settings: TSettings; reference: TSettings }): TSettings {
139
+ const ordered = orderKeysLike(opts);
140
+
141
+ for (const key of Object.keys(ordered).filter((entry) =>
142
+ SyncConstants.MERGE_ONE_LEVEL.has(entry),
143
+ )) {
144
+ const value = { value: ordered[key] };
145
+ const previous = { value: opts.reference[key] };
146
+
147
+ if (isSettingsObject(value) && isSettingsObject(previous)) {
148
+ ordered[key] = orderKeysLike({ settings: value.value, reference: previous.value });
149
+ }
194
150
  }
195
151
 
196
- const inner: TSettings = { ...current.value };
152
+ return ordered;
153
+ }
154
+
155
+ /** `settings` without what `managed` (the overlay of a previous sync) put there; anything else stays. */
156
+ static removeManaged(opts: { settings: TSettings; managed: TSettings }): TSettings {
157
+ const remaining: TSettings = { ...opts.settings };
197
158
 
198
- for (const [innerKey, innerManaged] of Object.entries(managed.value)) {
199
- const innerCurrent = inner[innerKey];
159
+ for (const [key, managedValue] of Object.entries(opts.managed)) {
160
+ const current = { value: remaining[key] };
161
+ const managed = { value: managedValue };
200
162
 
201
- if (Array.isArray(innerCurrent) && Array.isArray(innerManaged)) {
202
- const managedKeys = new Set(innerManaged.map((entry) => toKey({ value: entry })));
203
- const unmatched = innerCurrent.filter((entry) => !managedKeys.has(toKey({ value: entry })));
204
- // A hand-edited entry no longer matches whole: its managed hooks still go, by command.
205
- const kept =
206
- key === SyncConstants.HOOKS_KEY
207
- ? removeHookCommands({
208
- entries: unmatched,
209
- commands: new Set(extractEntryCommands({ entries: innerManaged })),
210
- })
211
- : unmatched;
163
+ if (
164
+ SyncConstants.UNION_ARRAYS.has(key) &&
165
+ Array.isArray(current.value) &&
166
+ Array.isArray(managedValue)
167
+ ) {
168
+ const managedEntries = new Set(managedValue.map((entry) => toKey({ value: entry })));
169
+ const kept = current.value.filter((entry) => !managedEntries.has(toKey({ value: entry })));
212
170
 
213
171
  if (kept.length > 0) {
214
- inner[innerKey] = kept;
172
+ remaining[key] = kept;
215
173
  } else {
216
- delete inner[innerKey];
174
+ delete remaining[key];
175
+ }
176
+
177
+ continue;
178
+ }
179
+
180
+ if (
181
+ !SyncConstants.MERGE_ONE_LEVEL.has(key) ||
182
+ !isSettingsObject(current) ||
183
+ !isSettingsObject(managed)
184
+ ) {
185
+ if (toKey({ value: remaining[key] }) === toKey({ value: managedValue })) {
186
+ delete remaining[key];
217
187
  }
218
188
 
219
189
  continue;
220
190
  }
221
191
 
222
- if (toKey({ value: innerCurrent }) === toKey({ value: innerManaged })) {
223
- delete inner[innerKey];
192
+ const inner: TSettings = { ...current.value };
193
+
194
+ for (const [innerKey, innerManaged] of Object.entries(managed.value)) {
195
+ const innerCurrent = inner[innerKey];
196
+
197
+ if (Array.isArray(innerCurrent) && Array.isArray(innerManaged)) {
198
+ const managedKeys = new Set(innerManaged.map((entry) => toKey({ value: entry })));
199
+ const unmatched = innerCurrent.filter(
200
+ (entry) => !managedKeys.has(toKey({ value: entry })),
201
+ );
202
+ // A hand-edited entry no longer matches whole: its managed hooks still go, by command.
203
+ const kept =
204
+ key === SyncConstants.HOOKS_KEY
205
+ ? ProjectSettings.removeHookCommands({
206
+ entries: unmatched,
207
+ commands: new Set(extractEntryCommands({ entries: innerManaged })),
208
+ })
209
+ : unmatched;
210
+
211
+ if (kept.length > 0) {
212
+ inner[innerKey] = kept;
213
+ } else {
214
+ delete inner[innerKey];
215
+ }
216
+
217
+ continue;
218
+ }
219
+
220
+ if (toKey({ value: innerCurrent }) === toKey({ value: innerManaged })) {
221
+ delete inner[innerKey];
222
+ }
224
223
  }
225
- }
226
224
 
227
- if (Object.keys(inner).length > 0) {
228
- remaining[key] = inner;
229
- } else {
230
- delete remaining[key];
225
+ if (Object.keys(inner).length > 0) {
226
+ remaining[key] = inner;
227
+ } else {
228
+ delete remaining[key];
229
+ }
231
230
  }
231
+
232
+ return remaining;
232
233
  }
233
234
 
234
- return remaining;
235
- };
235
+ /** The project's own hooks from config, in Claude Code's settings shape. */
236
+ static buildHook(opts: { hooks: TResolvedConfig['hooks'] }): TSettings {
237
+ const byEvent: Record<string, unknown[]> = {};
236
238
 
237
- /** The project's own hooks from config, in Claude Code's settings shape. */
238
- export const buildProjectHookSettings = (opts: { hooks: TResolvedConfig['hooks'] }): TSettings => {
239
- const byEvent: Record<string, unknown[]> = {};
239
+ for (const hook of opts.hooks) {
240
+ const entry = {
241
+ ...(hook.matcher === undefined ? {} : { matcher: hook.matcher }),
242
+ hooks: [{ type: 'command', command: hook.command }],
243
+ };
240
244
 
241
- for (const hook of opts.hooks) {
242
- const entry = {
243
- ...(hook.matcher === undefined ? {} : { matcher: hook.matcher }),
244
- hooks: [{ type: 'command', command: hook.command }],
245
- };
245
+ byEvent[hook.event] = [...(byEvent[hook.event] ?? []), entry];
246
+ }
246
247
 
247
- byEvent[hook.event] = [...(byEvent[hook.event] ?? []), entry];
248
+ return { hooks: byEvent };
248
249
  }
249
-
250
- return { hooks: byEvent };
251
- };
250
+ }
@@ -1,7 +1,12 @@
1
- /**
2
- * Whether the menus can draw: both ends are a terminal and it understands cursor movement. Anything
3
- * else (a pipe, a redirect, `TERM=dumb`) keeps the plain questions, so what scripts and logs see does
4
- * not change.
5
- */
6
- export const menusAvailable = (): boolean =>
7
- process.stdin.isTTY === true && process.stdout.isTTY === true && process.env.TERM !== 'dumb';
1
+ export class TerminalCapability {
2
+ /**
3
+ * Whether the menus can draw: both ends are a terminal and it understands cursor movement. Anything
4
+ * else (a pipe, a redirect, `TERM=dumb`) keeps the plain questions, so what scripts and logs see does
5
+ * not change.
6
+ */
7
+ static menusAvailable(): boolean {
8
+ return (
9
+ process.stdin.isTTY === true && process.stdout.isTTY === true && process.env.TERM !== 'dumb'
10
+ );
11
+ }
12
+ }
@@ -8,43 +8,43 @@ const REFUSAL = [
8
8
  /** The command was given piped lines where a menu needs key presses; the way out is already printed. */
9
9
  export class PipedInputRefusal extends Error {}
10
10
 
11
- /**
12
- * A keyboard in raw mode sends CR for Enter, never LF. LF means the answers were piped in (a script
13
- * feeding a pty), and a menu would wait for a key that never comes. So while a menu is open, the first
14
- * LF restores the terminal, prints the way out and ends the menu with a `PipedInputRefusal`: a
15
- * refusal, not a hang. It is thrown, not `process.exit`, so the `finally` blocks of the flow run (the
16
- * scratch folder of a preset is removed) and the router turns it into exit code 1.
17
- */
18
- export const guardAgainstPipedLines = async <TResult>(
19
- run: () => Promise<TResult>,
20
- ): Promise<TResult> => {
21
- let refuseMenu: (error: PipedInputRefusal) => void = () => {};
22
- const refused = new Promise<never>((_resolve, reject) => {
23
- refuseMenu = reject;
24
- });
25
- const refuse = (chunk: Buffer | string): void => {
26
- const bytes = typeof chunk === 'string' ? Buffer.from(chunk) : chunk;
27
-
28
- if (!bytes.includes(LINE_FEED)) {
29
- return;
11
+ export class MenuInput {
12
+ /**
13
+ * A keyboard in raw mode sends CR for Enter, never LF. LF means the answers were piped in (a script
14
+ * feeding a pty), and a menu would wait for a key that never comes. So while a menu is open, the first
15
+ * LF restores the terminal, prints the way out and ends the menu with a `PipedInputRefusal`: a
16
+ * refusal, not a hang. It is thrown, not `process.exit`, so the `finally` blocks of the flow run (the
17
+ * scratch folder of a preset is removed) and the router turns it into exit code 1.
18
+ */
19
+ static async guardAgainstPipedLines<TResult>(run: () => Promise<TResult>): Promise<TResult> {
20
+ let refuseMenu: (error: PipedInputRefusal) => void = () => {};
21
+ const refused = new Promise<never>((_resolve, reject) => {
22
+ refuseMenu = reject;
23
+ });
24
+ const refuse = (chunk: Buffer | string): void => {
25
+ const bytes = typeof chunk === 'string' ? Buffer.from(chunk) : chunk;
26
+
27
+ if (!bytes.includes(LINE_FEED)) {
28
+ return;
29
+ }
30
+
31
+ if (process.stdin.isTTY === true) {
32
+ process.stdin.setRawMode(false);
33
+ }
34
+
35
+ process.stdout.write(SHOW_CURSOR);
36
+ process.stderr.write(`\n${REFUSAL}\n`);
37
+ refuseMenu(new PipedInputRefusal(REFUSAL));
38
+ };
39
+
40
+ process.stdin.on('data', refuse);
41
+
42
+ try {
43
+ const outcome = await Promise.race([run(), refused]);
44
+
45
+ return outcome;
46
+ } finally {
47
+ process.stdin.off('data', refuse);
30
48
  }
31
-
32
- if (process.stdin.isTTY === true) {
33
- process.stdin.setRawMode(false);
34
- }
35
-
36
- process.stdout.write(SHOW_CURSOR);
37
- process.stderr.write(`\n${REFUSAL}\n`);
38
- refuseMenu(new PipedInputRefusal(REFUSAL));
39
- };
40
-
41
- process.stdin.on('data', refuse);
42
-
43
- try {
44
- const outcome = await Promise.race([run(), refused]);
45
-
46
- return outcome;
47
- } finally {
48
- process.stdin.off('data', refuse);
49
49
  }
50
- };
50
+ }
package/src/ui/prompt.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { confirm, isCancel, multiselect, note, select, text } from '@clack/prompts';
2
- import { guardAgainstPipedLines } from './keyboard.ts';
2
+ import { MenuInput } from './keyboard.ts';
3
3
 
4
4
  /** One line of a menu: what it is called, and a short note beside it. */
5
5
  interface IChoice {
@@ -8,89 +8,91 @@ interface IChoice {
8
8
  hint?: string;
9
9
  }
10
10
 
11
- /**
12
- * Free text, only where there is nothing to pick from (a new name). A default, when there is one, is
13
- * shown and taken on Enter; `validate` returns the message to show, or nothing when the text is fine.
14
- * Every prompt answers `null` when the user cancelled (Esc, Ctrl-C): the command stops, never picks
15
- * for them.
16
- */
17
- export const promptText = async (opts: {
18
- message: string;
19
- defaultValue?: string;
20
- validate?: (value: string) => string | undefined;
21
- }): Promise<string | null> => {
22
- const value = await guardAgainstPipedLines(() =>
23
- text({
24
- message: opts.message,
25
- ...(opts.defaultValue === undefined
26
- ? {}
27
- : { placeholder: opts.defaultValue, defaultValue: opts.defaultValue }),
28
- validate: (input) => opts.validate?.(input ?? ''),
29
- }),
30
- );
11
+ export class TerminalPrompt {
12
+ /**
13
+ * Free text, only where there is nothing to pick from (a new name). A default, when there is one, is
14
+ * shown and taken on Enter; `validate` returns the message to show, or nothing when the text is fine.
15
+ * Every prompt answers `null` when the user cancelled (Esc, Ctrl-C): the command stops, never picks
16
+ * for them.
17
+ */
18
+ static async promptText(opts: {
19
+ message: string;
20
+ defaultValue?: string;
21
+ validate?: (value: string) => string | undefined;
22
+ }): Promise<string | null> {
23
+ const value = await MenuInput.guardAgainstPipedLines(() =>
24
+ text({
25
+ message: opts.message,
26
+ ...(opts.defaultValue === undefined
27
+ ? {}
28
+ : { placeholder: opts.defaultValue, defaultValue: opts.defaultValue }),
29
+ validate: (input) => opts.validate?.(input ?? ''),
30
+ }),
31
+ );
31
32
 
32
- return isCancel(value) ? null : value;
33
- };
33
+ return isCancel(value) ? null : value;
34
+ }
34
35
 
35
- /** Pick one: arrows to move, Enter to choose. */
36
- export const promptSelect = async (opts: {
37
- message: string;
38
- choices: IChoice[];
39
- initial?: string;
40
- }): Promise<string | null> => {
41
- const value = await guardAgainstPipedLines(() =>
42
- select({
43
- message: opts.message,
44
- options: opts.choices.map((choice) => ({
45
- value: choice.value,
46
- label: choice.label,
47
- ...(choice.hint === undefined ? {} : { hint: choice.hint }),
48
- })),
49
- initialValue: opts.initial,
50
- }),
51
- );
36
+ /** Pick one: arrows to move, Enter to choose. */
37
+ static async promptSelect(opts: {
38
+ message: string;
39
+ choices: IChoice[];
40
+ initial?: string;
41
+ }): Promise<string | null> {
42
+ const value = await MenuInput.guardAgainstPipedLines(() =>
43
+ select({
44
+ message: opts.message,
45
+ options: opts.choices.map((choice) => ({
46
+ value: choice.value,
47
+ label: choice.label,
48
+ ...(choice.hint === undefined ? {} : { hint: choice.hint }),
49
+ })),
50
+ initialValue: opts.initial,
51
+ }),
52
+ );
52
53
 
53
- return isCancel(value) ? null : value;
54
- };
54
+ return isCancel(value) ? null : value;
55
+ }
55
56
 
56
- /** Tick any number: arrows to move, space to toggle, `a` for all, Enter to confirm; what is ticked at the start is `initial`. */
57
- export const promptMultiselect = async (opts: {
58
- message: string;
59
- choices: IChoice[];
60
- initial: string[];
61
- }): Promise<string[] | null> => {
62
- const value = await guardAgainstPipedLines(() =>
63
- multiselect({
64
- message: opts.message,
65
- options: opts.choices.map((choice) => ({
66
- value: choice.value,
67
- label: choice.label,
68
- ...(choice.hint === undefined ? {} : { hint: choice.hint }),
69
- })),
70
- initialValues: opts.initial,
71
- required: false,
72
- }),
73
- );
57
+ /** Tick any number: arrows to move, space to toggle, `a` for all, Enter to confirm; what is ticked at the start is `initial`. */
58
+ static async promptMultiselect(opts: {
59
+ message: string;
60
+ choices: IChoice[];
61
+ initial: string[];
62
+ }): Promise<string[] | null> {
63
+ const value = await MenuInput.guardAgainstPipedLines(() =>
64
+ multiselect({
65
+ message: opts.message,
66
+ options: opts.choices.map((choice) => ({
67
+ value: choice.value,
68
+ label: choice.label,
69
+ ...(choice.hint === undefined ? {} : { hint: choice.hint }),
70
+ })),
71
+ initialValues: opts.initial,
72
+ required: false,
73
+ }),
74
+ );
74
75
 
75
- return isCancel(value) ? null : value;
76
- };
76
+ return isCancel(value) ? null : value;
77
+ }
77
78
 
78
- export const promptConfirm = async (opts: {
79
- message: string;
80
- initial?: boolean;
81
- }): Promise<boolean | null> => {
82
- const value = await guardAgainstPipedLines(() =>
83
- confirm({ message: opts.message, initialValue: opts.initial ?? true }),
84
- );
79
+ static async promptConfirm(opts: {
80
+ message: string;
81
+ initial?: boolean;
82
+ }): Promise<boolean | null> {
83
+ const value = await MenuInput.guardAgainstPipedLines(() =>
84
+ confirm({ message: opts.message, initialValue: opts.initial ?? true }),
85
+ );
85
86
 
86
- return isCancel(value) ? null : value;
87
- };
87
+ return isCancel(value) ? null : value;
88
+ }
88
89
 
89
- /** The summary of what is about to be written, then the confirm; true only when the user said yes. */
90
- export const promptPlan = async (opts: { title: string; lines: string[] }): Promise<boolean> => {
91
- note(opts.lines.join('\n'), opts.title);
90
+ /** The summary of what is about to be written, then the confirm; true only when the user said yes. */
91
+ static async promptPlan(opts: { title: string; lines: string[] }): Promise<boolean> {
92
+ note(opts.lines.join('\n'), opts.title);
92
93
 
93
- const confirmed = await promptConfirm({ message: 'Write these files?' });
94
+ const confirmed = await TerminalPrompt.promptConfirm({ message: 'Write these files?' });
94
95
 
95
- return confirmed === true;
96
- };
96
+ return confirmed === true;
97
+ }
98
+ }