deictic 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/API.md +26 -17
  2. package/README.md +6 -26
  3. package/dist/annotations/client.d.ts +1 -1
  4. package/dist/annotations/client.js +1 -1
  5. package/dist/annotations/index.d.ts +2 -101
  6. package/dist/annotations/index.js +1 -22
  7. package/dist/astro.d.ts +1 -1
  8. package/dist/astro.js +1 -1
  9. package/dist/cli.d.ts +1 -0
  10. package/dist/cli.js +37 -0
  11. package/dist/cli.js.map +1 -0
  12. package/dist/index.js +1 -10
  13. package/dist/logs/client.d.ts +1 -1
  14. package/dist/logs/client.js +1 -1
  15. package/dist/logs/index.d.ts +2 -18
  16. package/dist/logs/index.js +1 -21
  17. package/dist/network/client.d.ts +1 -1
  18. package/dist/network/client.js +1 -1
  19. package/dist/network/index.d.ts +1 -1
  20. package/dist/network/index.js +1 -37
  21. package/dist/recommended/index.d.ts +7 -7
  22. package/dist/recommended/index.js +1 -26
  23. package/dist/recordings/client.d.ts +1 -1
  24. package/dist/recordings/client.js +1 -1
  25. package/dist/recordings/index.d.ts +1 -1
  26. package/dist/screenshots/client.d.ts +1 -1
  27. package/dist/screenshots/client.js +1 -108
  28. package/dist/screenshots/dom.d.ts +1 -1
  29. package/dist/screenshots/dom.js +1 -1
  30. package/dist/screenshots/index.d.ts +1 -1
  31. package/dist/screenshots/index.js +1 -103
  32. package/dist/screenshots/rrweb.d.ts +1 -1
  33. package/dist/screenshots/rrweb.js +1 -1
  34. package/dist/shared/annotations.js +22 -0
  35. package/dist/shared/annotations.js.map +1 -0
  36. package/dist/shared/board.js +190 -0
  37. package/dist/shared/board.js.map +1 -0
  38. package/dist/shared/client.js +108 -0
  39. package/dist/shared/endpoints.js +4 -1
  40. package/dist/shared/endpoints.js.map +1 -1
  41. package/dist/shared/find.js +156 -0
  42. package/dist/shared/find.js.map +1 -0
  43. package/dist/shared/frame.js +75 -0
  44. package/dist/shared/frame.js.map +1 -0
  45. package/dist/shared/index.d.ts +5 -0
  46. package/dist/shared/index.d.ts.map +1 -1
  47. package/dist/shared/index2.d.ts +85 -88
  48. package/dist/shared/index2.d.ts.map +1 -1
  49. package/dist/shared/index3.d.ts +83 -89
  50. package/dist/shared/index3.d.ts.map +1 -1
  51. package/dist/shared/index4.d.ts +98 -50
  52. package/dist/shared/index4.d.ts.map +1 -1
  53. package/dist/shared/index5.d.ts +13 -219
  54. package/dist/shared/index5.d.ts.map +1 -1
  55. package/dist/shared/index6.d.ts +55 -61
  56. package/dist/shared/index6.d.ts.map +1 -1
  57. package/dist/shared/index7.d.ts +224 -0
  58. package/dist/shared/index7.d.ts.map +1 -0
  59. package/dist/shared/index8.d.ts +52 -0
  60. package/dist/shared/index8.d.ts.map +1 -0
  61. package/dist/shared/index9.d.ts +70 -0
  62. package/dist/shared/index9.d.ts.map +1 -0
  63. package/dist/shared/init.js +472 -0
  64. package/dist/shared/init.js.map +1 -0
  65. package/dist/shared/local.js +165 -0
  66. package/dist/shared/local.js.map +1 -0
  67. package/dist/shared/logs.js +22 -0
  68. package/dist/shared/logs.js.map +1 -0
  69. package/dist/shared/network.js +39 -0
  70. package/dist/shared/network.js.map +1 -0
  71. package/dist/shared/packages.js +125 -0
  72. package/dist/shared/packages.js.map +1 -0
  73. package/dist/shared/recommended.js +26 -0
  74. package/dist/shared/recommended.js.map +1 -0
  75. package/dist/shared/recording.js +316 -0
  76. package/dist/shared/recording.js.map +1 -0
  77. package/dist/shared/recordings.js +2 -1
  78. package/dist/shared/recordings.js.map +1 -1
  79. package/dist/shared/screenshots.js +108 -0
  80. package/dist/shared/screenshots.js.map +1 -0
  81. package/dist/shared/src.js +10 -0
  82. package/dist/shared/src.js.map +1 -0
  83. package/dist/shared/task.js +108 -0
  84. package/dist/shared/task.js.map +1 -0
  85. package/dist/shared/tasks.js +7 -180
  86. package/dist/shared/tasks.js.map +1 -1
  87. package/dist/shared/transcribe.js +128 -0
  88. package/dist/shared/transcribe.js.map +1 -0
  89. package/dist/shared/vb.js +13 -0
  90. package/dist/shared/vb.js.map +1 -0
  91. package/dist/shared/vite.d.ts +16 -0
  92. package/dist/shared/vite.d.ts.map +1 -0
  93. package/dist/shared/vite.js +61 -0
  94. package/dist/shared/vite.js.map +1 -0
  95. package/dist/shared/voice.js +84 -0
  96. package/dist/{voice/index.js.map → shared/voice.js.map} +1 -1
  97. package/dist/shared/voice2.js +90 -0
  98. package/dist/shared/voice2.js.map +1 -0
  99. package/dist/shared/watch.js +209 -0
  100. package/dist/shared/watch.js.map +1 -0
  101. package/dist/shared/watchers.js +62 -0
  102. package/dist/shared/watchers.js.map +1 -0
  103. package/dist/tailwind/client.d.ts +1 -1
  104. package/dist/tailwind/client.js +1 -1
  105. package/dist/tailwind/index.d.ts +1 -1
  106. package/dist/tailwind/index.js +1 -1
  107. package/dist/tasks/client.d.ts +1 -1
  108. package/dist/tasks/client.js +1 -1
  109. package/dist/tasks/index.d.ts +1 -1
  110. package/dist/tasks/index.js +3 -1
  111. package/dist/vite.d.ts +2 -15
  112. package/dist/vite.js +2 -60
  113. package/dist/voice/client.d.ts +1 -1
  114. package/dist/voice/client.js +1 -1
  115. package/dist/voice/index.d.ts +2 -52
  116. package/dist/voice/index.js +1 -84
  117. package/package.json +6 -9
  118. package/skills/deictic/SKILL.md +27 -42
  119. package/skills/deictic/references/recordings.md +9 -108
  120. package/skills/deictic/references/tasks.md +21 -112
  121. package/skills/deictic/references/watch.md +13 -68
  122. package/src/cli/board.ts +204 -0
  123. package/src/cli/bun.ts +133 -0
  124. package/src/cli/edit.ts +56 -0
  125. package/src/cli/find.ts +147 -0
  126. package/src/cli/frame.ts +90 -0
  127. package/src/cli/init.ts +388 -0
  128. package/src/cli/recording.ts +383 -0
  129. package/src/cli/task.ts +126 -0
  130. package/src/cli/transcribe.ts +129 -0
  131. package/src/cli/vb.ts +11 -0
  132. package/src/cli/voice.ts +59 -0
  133. package/src/cli/watch.ts +221 -0
  134. package/src/cli.ts +37 -0
  135. package/src/index.ts +5 -0
  136. package/src/plugins/logs/index.ts +1 -0
  137. package/src/plugins/network/index.ts +1 -0
  138. package/src/plugins/recordings/index.ts +1 -0
  139. package/src/plugins/screenshots/index.ts +5 -1
  140. package/src/plugins/tasks/index.ts +3 -2
  141. package/src/plugins/tasks/local.ts +19 -6
  142. package/src/plugins/tasks/watchers.ts +41 -6
  143. package/src/tools/endpoints.ts +3 -0
  144. package/src/tools/packages.ts +107 -0
  145. package/dist/annotations/index.d.ts.map +0 -1
  146. package/dist/annotations/index.js.map +0 -1
  147. package/dist/index.js.map +0 -1
  148. package/dist/logs/index.d.ts.map +0 -1
  149. package/dist/logs/index.js.map +0 -1
  150. package/dist/network/index.js.map +0 -1
  151. package/dist/recommended/index.js.map +0 -1
  152. package/dist/screenshots/index.js.map +0 -1
  153. package/dist/vite.d.ts.map +0 -1
  154. package/dist/vite.js.map +0 -1
  155. package/dist/voice/index.d.ts.map +0 -1
  156. package/skills/deictic/references/install.md +0 -103
  157. package/skills/deictic/scripts/frame.mjs +0 -62
  158. package/skills/deictic/scripts/lib.mjs +0 -283
  159. package/skills/deictic/scripts/recording.mjs +0 -266
  160. package/skills/deictic/scripts/task.mjs +0 -120
  161. package/skills/deictic/scripts/transcribe.mjs +0 -93
  162. package/skills/deictic/scripts/vb.mjs +0 -12
  163. package/skills/deictic/scripts/voice.mjs +0 -50
  164. package/skills/deictic/scripts/watch.mjs +0 -152
  165. /package/dist/{screenshots → shared}/client.js.map +0 -0
@@ -0,0 +1,388 @@
1
+ /**
2
+ * `deictic init`: the recommended setup in a project, in one step. It installs deictic and what the recommended
3
+ * plugins import, with the project's package manager and as the project's own dev dependencies (deictic, the
4
+ * page's bundle and the skill all find them from the project, so they can't come in through another package), adds
5
+ * deictic to the bundler's config where it can find the place, and offers the agent skill.
6
+ */
7
+ import { spawnSync } from 'node:child_process';
8
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
9
+ import { dirname, join, relative } from 'node:path';
10
+ import { createInterface } from 'node:readline/promises';
11
+
12
+ import MagicString from 'magic-string';
13
+ import { type Expression, type ObjectExpression, parseSync, type Program } from 'oxc-parser';
14
+
15
+ import { recommended } from '#src/plugins/recommended/index.ts';
16
+
17
+ import { installCommand, packageManager, readJson, runnerOf } from '#src/tools/packages.ts';
18
+
19
+ import { fail, parseArgs } from './board.ts';
20
+ import { addToBunfig, addToServer, BY_HAND, MODULE, PLUGIN, serverOf } from './bun.ts';
21
+ import { addFirst, addImports, eolOf, indentAt } from './edit.ts';
22
+
23
+ export type Bundler = 'next' | 'astro' | 'vite' | 'rspack' | 'webpack' | 'bun';
24
+
25
+ interface Setup {
26
+ /** The config files it looks for, in order. */
27
+ files: string[];
28
+ /** The module deictic's integration comes from, and its name there. */
29
+ from: string;
30
+ name: string;
31
+ /** What goes in the config: in its `list`, or wrapped around the whole of it with `wrap`. */
32
+ list?: string;
33
+ value: string;
34
+ wrap?: boolean;
35
+ /** The snippet for a config it can't edit. */
36
+ snippet: string;
37
+ }
38
+
39
+ const extensions = (base: string) => ['ts', 'mts', 'js', 'mjs', 'cts', 'cjs'].map((e) => `${base}.${e}`);
40
+ const PLUGINS = '{ plugins: recommended() }';
41
+
42
+ const SETUPS: Record<Exclude<Bundler, 'bun'>, Setup> = {
43
+ next: {
44
+ files: extensions('next.config'),
45
+ from: 'deictic/next',
46
+ name: 'withDeictic',
47
+ value: PLUGINS,
48
+ wrap: true,
49
+ snippet: `export default withDeictic(nextConfig, ${PLUGINS});`,
50
+ },
51
+ astro: {
52
+ files: extensions('astro.config'),
53
+ from: 'deictic/astro',
54
+ name: 'deictic',
55
+ list: 'integrations',
56
+ value: `deictic(${PLUGINS})`,
57
+ snippet: `export default defineConfig({ integrations: [deictic(${PLUGINS})] });`,
58
+ },
59
+ vite: {
60
+ files: extensions('vite.config'),
61
+ from: 'deictic/vite',
62
+ name: 'deictic',
63
+ list: 'plugins',
64
+ value: `deictic(${PLUGINS})`,
65
+ snippet: `export default defineConfig({ plugins: [react(), deictic(${PLUGINS})] });`,
66
+ },
67
+ rspack: {
68
+ files: extensions('rspack.config'),
69
+ from: 'deictic/webpack',
70
+ name: 'DeicticPlugin',
71
+ list: 'plugins',
72
+ value: `new DeicticPlugin(${PLUGINS})`,
73
+ snippet: `export default { plugins: [new DeicticPlugin(${PLUGINS})] };`,
74
+ },
75
+ webpack: {
76
+ files: extensions('webpack.config'),
77
+ from: 'deictic/webpack',
78
+ name: 'DeicticPlugin',
79
+ list: 'plugins',
80
+ value: `new DeicticPlugin(${PLUGINS})`,
81
+ snippet: `export default { plugins: [new DeicticPlugin(${PLUGINS})] };`,
82
+ },
83
+ };
84
+
85
+ /** The project's root: the nearest folder with a package.json, from `dir` up. */
86
+ export function rootOf(dir: string): string | undefined {
87
+ for (let at = dir; ; at = dirname(at)) {
88
+ if (existsSync(join(at, 'package.json'))) return at;
89
+ if (dirname(at) === at) return undefined;
90
+ }
91
+ }
92
+
93
+ /** The project's dependencies and dev dependencies, by name. */
94
+ function dependenciesOf(root: string): Set<string> {
95
+ const manifest = readJson(join(root, 'package.json')) ?? {};
96
+ return new Set([manifest.dependencies, manifest.devDependencies].flatMap((d) => Object.keys((d as object | undefined) ?? {})));
97
+ }
98
+
99
+ /** The bundler the project runs its dev server with, from its dependencies, or none deictic knows. */
100
+ export function bundlerOf(root: string): Bundler | undefined {
101
+ const has = dependenciesOf(root);
102
+ if (has.has('next')) return 'next';
103
+ if (has.has('astro')) return 'astro';
104
+ if (has.has('vite') || SETUPS.vite.files.some((f) => existsSync(join(root, f)))) return 'vite';
105
+ if (has.has('@rspack/core') || has.has('@rspack/cli')) return 'rspack';
106
+ if (has.has('webpack')) return 'webpack';
107
+ // Bun's own bundler only where a server of the project's has it bundle a page: not every project Bun runs.
108
+ if (serverOf(root) !== undefined) return 'bun';
109
+ return undefined;
110
+ }
111
+
112
+ /** What the recommended setup imports, for the bundler: launch-editor opens files under webpack, which has no launcher. */
113
+ export function packagesFor(bundler: Bundler | undefined): string[] {
114
+ const plugins = recommended().flatMap((p) => p.packages ?? []);
115
+ return ['deictic', ...plugins, ...(bundler === 'webpack' || bundler === 'rspack' ? ['launch-editor'] : [])];
116
+ }
117
+
118
+ type Node = Expression | ObjectExpression;
119
+
120
+ /**
121
+ * The object an expression comes to: through `defineConfig(…)`, `(…)`, `satisfies`, a function's return and a top-level
122
+ * `const`. Not through another call: in `mergeConfig(base, {…})` or webpack-merge's `merge(common, {…})` the first
123
+ * argument is a base the project may build with too, and the plugin isn't the base's.
124
+ */
125
+ function objectOf(node: Node | null | undefined, program: Program, depth = 0): ObjectExpression | undefined {
126
+ if (node == null || depth > 8) return undefined;
127
+ const next = (n: Node | null | undefined) => objectOf(n, program, depth + 1);
128
+ if (node.type === 'ObjectExpression') return node;
129
+ if (node.type === 'ParenthesizedExpression' || node.type === 'TSAsExpression' || node.type === 'TSSatisfiesExpression')
130
+ return next(node.expression);
131
+ if (node.type === 'CallExpression') {
132
+ const { callee } = node;
133
+ const named = (callee.type === 'Identifier' ? callee : callee.type === 'MemberExpression' ? callee.property : undefined) as
134
+ { type: string; name?: string } | undefined;
135
+ const [first] = node.arguments;
136
+ if (named?.name !== 'defineConfig' || first === undefined || first.type === 'SpreadElement') return undefined;
137
+ return next(first);
138
+ }
139
+ if (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression') {
140
+ const body = node.body;
141
+ if (body === null) return undefined;
142
+ if (body.type !== 'BlockStatement') return next(body);
143
+ const returned = body.body.findLast((s) => s.type === 'ReturnStatement');
144
+ return returned?.type === 'ReturnStatement' ? next(returned.argument) : undefined;
145
+ }
146
+ if (node.type !== 'Identifier') return undefined;
147
+ for (const statement of program.body) {
148
+ const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;
149
+ if (declaration?.type !== 'VariableDeclaration') continue;
150
+ const found = declaration.declarations.find((d) => d.id.type === 'Identifier' && d.id.name === node.name);
151
+ if (found !== undefined) return next(found.init);
152
+ }
153
+ return undefined;
154
+ }
155
+
156
+ /** The config's exported value: `export default …` or `module.exports = …`, and whether it's CommonJS. */
157
+ function exportedOf(program: Program): { value: Expression; commonjs: boolean } | undefined {
158
+ for (const statement of program.body) {
159
+ if (statement.type === 'ExportDefaultDeclaration') {
160
+ const value = statement.declaration;
161
+ return value.type.endsWith('Declaration') && value.type !== 'FunctionDeclaration'
162
+ ? undefined
163
+ : { value: value as Expression, commonjs: false };
164
+ }
165
+ if (statement.type !== 'ExpressionStatement' || statement.expression.type !== 'AssignmentExpression') continue;
166
+ const { left, right } = statement.expression;
167
+ const exports =
168
+ left.type === 'MemberExpression' &&
169
+ left.object.type === 'Identifier' &&
170
+ left.object.name === 'module' &&
171
+ left.property.type === 'Identifier' &&
172
+ left.property.name === 'exports';
173
+ if (exports) return { value: right, commonjs: true };
174
+ }
175
+ return undefined;
176
+ }
177
+
178
+ /**
179
+ * The config with deictic added, or nothing where it can't tell where: deictic's import beside the config's, and its
180
+ * plugin last in the list (`plugins`, `integrations`), or the config wrapped (Next's). Written in the config's quotes,
181
+ * semicolons and line endings.
182
+ */
183
+ export function addToConfig(bundler: Exclude<Bundler, 'bun'>, file: string, code: string): string | undefined {
184
+ const setup = SETUPS[bundler];
185
+ const { program, errors } = parseSync(file, code, { preserveParens: false });
186
+ if (errors.length > 0) return undefined;
187
+ const exported = exportedOf(program);
188
+ if (exported === undefined) return undefined;
189
+ const eol = eolOf(code);
190
+ const out = new MagicString(code);
191
+
192
+ if (setup.wrap) {
193
+ out.appendLeft(exported.value.start, `${setup.name}(`);
194
+ out.appendRight(exported.value.end, `, ${setup.value})`);
195
+ } else {
196
+ const object = objectOf(exported.value, program);
197
+ if (object === undefined || !addTo(out, code, object, setup.list!, setup.value, eol)) return undefined;
198
+ }
199
+
200
+ const lines: [string, string][] = [
201
+ ['{ recommended }', 'deictic/recommended'],
202
+ [`{ ${setup.name} }`, setup.from],
203
+ ];
204
+ addImports(out, program, code, lines, exported.commonjs);
205
+ return out.toString();
206
+ }
207
+
208
+ /** Adds `value` last in the object's array `list`, or the list with it, as the object is laid out. False where `list` isn't an array. */
209
+ function addTo(out: MagicString, code: string, object: ObjectExpression, list: string, value: string, eol: string): boolean {
210
+ const property = object.properties.find(
211
+ (p) =>
212
+ p.type === 'Property' &&
213
+ !p.computed &&
214
+ ((p.key.type === 'Identifier' && p.key.name === list) || (p.key.type === 'Literal' && p.key.value === list)),
215
+ );
216
+ if (property !== undefined) {
217
+ if (property.type !== 'Property') return false;
218
+ let array = property.value;
219
+ while (array.type === 'TSAsExpression' || array.type === 'TSSatisfiesExpression') array = array.expression;
220
+ if (array.type !== 'ArrayExpression') return false;
221
+ const last = array.elements.findLast((e) => e !== null);
222
+ if (last == null) {
223
+ out.appendLeft(array.start + 1, value);
224
+ return true;
225
+ }
226
+ const multiline = code.slice(array.start, array.end).includes('\n');
227
+ out.appendLeft(last.end, multiline ? `,${eol}${indentAt(code, last.start)}${value}` : `, ${value}`);
228
+ return true;
229
+ }
230
+ addFirst(out, code, object.start, object.properties[0], `${list}: [${value}]`);
231
+ return true;
232
+ }
233
+
234
+ export interface InitOptions {
235
+ /** Where it runs: the project is the nearest folder with a package.json. */
236
+ cwd: string;
237
+ /** Installs the packages; false only says the command. */
238
+ install?: boolean;
239
+ /** Adds the skill (true), says how (false), or asks (undefined, on a terminal; says how elsewhere). */
240
+ skill?: boolean;
241
+ /** Says what it would do, and does none of it. */
242
+ dryRun?: boolean;
243
+ log?: (line: string) => void;
244
+ /** Runs a command in the project, and answers whether it succeeded. */
245
+ run?: (command: string, cwd: string) => boolean;
246
+ ask?: (question: string) => Promise<boolean>;
247
+ }
248
+
249
+ const runIn = (command: string, cwd: string) => spawnSync(command, { cwd, stdio: 'inherit', shell: true }).status === 0;
250
+
251
+ /** Sets the project up; throws what to do where it can't. */
252
+ export async function init(options: InitOptions): Promise<void> {
253
+ const { install = true, dryRun = false, log = console.log, run = runIn } = options;
254
+ const root = rootOf(options.cwd);
255
+ if (root === undefined) throw new Error('deictic: no package.json here or above: run it in your project');
256
+ const manager = packageManager(root);
257
+ const bundler = bundlerOf(root);
258
+ const has = dependenciesOf(root);
259
+
260
+ const wanted = packagesFor(bundler).filter((p) => !has.has(p));
261
+ const command = installCommand(manager, wanted, root);
262
+ if (wanted.length === 0) {
263
+ log('deictic: its packages are installed');
264
+ } else if (!install || dryRun) {
265
+ log(`deictic: install them with ${command}`);
266
+ } else {
267
+ log(`deictic: ${command}`);
268
+ if (!run(command, root)) throw new Error(`deictic: the install failed: run ${command} yourself, then this again`);
269
+ }
270
+
271
+ configure(root, bundler, dryRun, log);
272
+
273
+ const skill = `${runnerOf(manager)} skills add ${existsSync(join(root, 'node_modules', 'deictic')) ? './node_modules/deictic' : 'gkurt/deictic'}`;
274
+ const add =
275
+ options.skill ?? (options.ask === undefined ? false : await options.ask('deictic: add the skill, so your agent works the tasks?'));
276
+ if (!add || dryRun) {
277
+ log(`deictic: to have your agent work the tasks, add the skill: ${skill}`);
278
+ } else {
279
+ log(`deictic: ${skill}`);
280
+ if (!run(skill, root)) log(`deictic: adding the skill failed: run ${skill} yourself`);
281
+ }
282
+
283
+ log(`deictic: screenshots render in Playwright's Chromium (${runnerOf(manager)} playwright install chromium), else Chrome or Edge`);
284
+ log('deictic: start your dev server and Alt-click an element');
285
+ }
286
+
287
+ /** Adds deictic to the bundler's config, or says what to add where it can't. */
288
+ function configure(root: string, bundler: Bundler | undefined, dryRun: boolean, log: (line: string) => void): void {
289
+ if (bundler === undefined) {
290
+ log('deictic: no bundler it knows in package.json (Vite, Next.js, Astro, webpack, Rspack, Bun): see API.md#bundlers to add it');
291
+ return;
292
+ }
293
+ if (bundler === 'bun') {
294
+ configureBun(root, dryRun, log);
295
+ return;
296
+ }
297
+ const setup = SETUPS[bundler];
298
+ const file = setup.files.map((f) => join(root, f)).find((f) => existsSync(f));
299
+ const imports = `import { recommended } from 'deictic/recommended';\nimport { ${setup.name} } from '${setup.from}';`;
300
+ if (file === undefined) {
301
+ log(`deictic: no ${setup.files[0]} here: add deictic to your ${bundler} config:\n\n${imports}\n${setup.snippet}\n`);
302
+ return;
303
+ }
304
+ const name = relative(root, file).replaceAll('\\', '/');
305
+ const code = readFileSync(file, 'utf8');
306
+ if (/['"]deictic\//.test(code)) {
307
+ log(`deictic: ${name} has it already`);
308
+ return;
309
+ }
310
+ const added = addToConfig(bundler, file, code);
311
+ if (added === undefined) {
312
+ log(`deictic: couldn't tell where it goes in ${name}: add it yourself:\n\n${imports}\n${setup.snippet}\n`);
313
+ return;
314
+ }
315
+ if (!dryRun) writeFileSync(file, added);
316
+ log(`deictic: ${dryRun ? 'would add' : 'added'} it to ${name}`);
317
+ }
318
+
319
+ /** Bun's three steps: the plugin's module, bunfig.toml, and its routes in the server. Each says what to add where it can't. */
320
+ function configureBun(root: string, dryRun: boolean, log: (line: string) => void): void {
321
+ const write = (file: string, text: string) => !dryRun && writeFileSync(file, text);
322
+ const did = dryRun ? 'would add' : 'added';
323
+ const plugin = join(root, MODULE);
324
+ if (!existsSync(plugin)) {
325
+ write(plugin, PLUGIN);
326
+ log(`deictic: ${dryRun ? 'would write' : 'wrote'} ${MODULE}`);
327
+ } else if (!readFileSync(plugin, 'utf8').includes('deictic/bun')) {
328
+ log(`deictic: ${MODULE} is a module of yours: add deictic by hand, as API.md#bundlers says for Bun`);
329
+ return;
330
+ }
331
+
332
+ const bunfig = join(root, 'bunfig.toml');
333
+ const before = existsSync(bunfig) ? readFileSync(bunfig, 'utf8') : undefined;
334
+ const after = addToBunfig(before);
335
+ if (after === undefined) {
336
+ log(`deictic: couldn't tell where it goes in bunfig.toml: add it yourself:\n\n${BY_HAND.bunfig}\n`);
337
+ } else if (after === before) {
338
+ log('deictic: bunfig.toml has it already');
339
+ } else {
340
+ write(bunfig, after);
341
+ log(`deictic: ${did} it to bunfig.toml`);
342
+ }
343
+
344
+ const server = serverOf(root)!;
345
+ const name = relative(root, server).replaceAll('\\', '/');
346
+ const code = readFileSync(server, 'utf8');
347
+ if (code.includes(MODULE.replace(/\.ts$/, ''))) {
348
+ log(`deictic: ${name} has its routes already`);
349
+ return;
350
+ }
351
+ const added = addToServer(server, code, root);
352
+ if (added === undefined) {
353
+ log(`deictic: couldn't tell where its routes go in ${name}: add them yourself:\n\n${BY_HAND.server}\n`);
354
+ return;
355
+ }
356
+ write(server, added);
357
+ log(`deictic: ${did} its routes to ${name}`);
358
+ }
359
+
360
+ export const usage = `init [--dry-run] [--no-install] [--skill | --no-skill] sets deictic up in this project
361
+ --dry-run says what it would do, and does none of it
362
+ --no-install doesn't install: says the command instead
363
+ --skill adds the agent skill without asking; --no-skill says the command instead`;
364
+
365
+ /** Asks a yes-or-no question on the terminal, yes by default. */
366
+ async function ask(question: string): Promise<boolean> {
367
+ const lines = createInterface({ input: process.stdin, output: process.stdout });
368
+ try {
369
+ return !/^n/i.test((await lines.question(`${question} (Y/n) `)).trim());
370
+ } finally {
371
+ lines.close();
372
+ }
373
+ }
374
+
375
+ export async function run(args: string[]): Promise<void> {
376
+ const { values } = parseArgs(args, { 'dry-run': { type: 'boolean' }, install: { type: 'boolean' }, skill: { type: 'boolean' } });
377
+ try {
378
+ await init({
379
+ cwd: process.cwd(),
380
+ install: values.install,
381
+ skill: values.skill,
382
+ dryRun: values['dry-run'],
383
+ ask: process.stdin.isTTY && process.stdout.isTTY ? ask : undefined,
384
+ });
385
+ } catch (e) {
386
+ fail(e instanceof Error ? e.message : String(e));
387
+ }
388
+ }