@dexhq/canvas 0.14.0 → 0.15.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 (229) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +5 -3
  3. package/app/canvas/activity.tsx +8 -15
  4. package/app/canvas/anchoring.ts +3 -5
  5. package/app/canvas/canvas-input.ts +151 -14
  6. package/app/canvas/canvas-keys.ts +9 -0
  7. package/app/canvas/canvas-model.ts +5 -42
  8. package/app/canvas/canvas-panels.ts +1 -17
  9. package/app/canvas/canvas-scene.ts +7 -3
  10. package/app/canvas/canvas-ui.ts +2 -1
  11. package/app/canvas/canvas.css +61 -26
  12. package/app/canvas/change-notice.tsx +10 -1
  13. package/app/canvas/comment-layer.tsx +2 -1
  14. package/app/canvas/comments.tsx +204 -82
  15. package/app/canvas/element-picker.ts +200 -27
  16. package/app/canvas/engine/canvas-gestures.ts +72 -17
  17. package/app/canvas/engine/canvas-store.ts +17 -1
  18. package/app/canvas/engine/engine.css +6 -3
  19. package/app/canvas/find-bar.tsx +817 -0
  20. package/app/canvas/find-model.ts +135 -0
  21. package/app/canvas/flow-canvas.tsx +18 -2
  22. package/app/canvas/follow-pin.ts +26 -6
  23. package/app/canvas/frame-node.tsx +9 -9
  24. package/app/canvas/imported-reviews.tsx +12 -22
  25. package/app/canvas/layout.ts +27 -0
  26. package/app/canvas/live-page.ts +142 -19
  27. package/app/canvas/page-find.ts +220 -0
  28. package/app/canvas/page-style.ts +23 -0
  29. package/app/canvas/section-node.tsx +2 -0
  30. package/app/canvas/spec-threads.ts +24 -23
  31. package/app/canvas/thread-focus.tsx +14 -12
  32. package/app/canvas/use-activity.ts +0 -8
  33. package/app/canvas/use-agent-changes.tsx +34 -40
  34. package/app/canvas/use-canvas-clipboard.tsx +11 -14
  35. package/app/canvas/xray.ts +3 -12
  36. package/app/frame.tsx +5 -0
  37. package/app/i18n/locales/en/messages.po +88 -8
  38. package/app/i18n/locales/en/messages.ts +1 -1
  39. package/app/i18n/locales/pseudo/messages.po +88 -8
  40. package/app/i18n/locales/pseudo/messages.ts +1 -1
  41. package/app/icons/glyphs.tsx +4 -0
  42. package/app/main.tsx +3 -0
  43. package/app/pages/canvas.tsx +27 -8
  44. package/app/pages/commands-settings.tsx +16 -2
  45. package/app/shell/feedback-form.tsx +10 -21
  46. package/app/shell/feedback-link.tsx +2 -1
  47. package/app/shell/scaled-frame.tsx +21 -9
  48. package/app/types.ts +21 -205
  49. package/app/ui/file-drop.ts +128 -0
  50. package/app/ui/shadcn/input-group.tsx +2 -2
  51. package/dist/cli/banner.d.mts +4 -2
  52. package/dist/cli/banner.mjs +34 -11
  53. package/dist/cli/commands/app.mjs +19 -11
  54. package/dist/cli/commands/hook.mjs +2 -1
  55. package/dist/config/project-files.d.mts +6 -0
  56. package/dist/config/project-files.mjs +14 -2
  57. package/dist/config/renamed.d.mts +0 -2
  58. package/dist/config/renamed.mjs +1 -3
  59. package/dist/content/capture.d.mts +2 -2
  60. package/dist/content/capture.mjs +2 -2
  61. package/dist/content/content.d.mts +3 -141
  62. package/dist/content/content.mjs +7 -62
  63. package/dist/content/flow-specs.mjs +1 -1
  64. package/dist/content/prototype-summary.d.mts +3 -39
  65. package/dist/content/prototype-summary.mjs +2 -3
  66. package/dist/content/prototypes.mjs +1 -1
  67. package/dist/content/text-replace.d.mts +30 -0
  68. package/dist/content/text-replace.mjs +309 -0
  69. package/dist/content/thread-module.mjs +1 -1
  70. package/dist/content/threads.mjs +1 -1
  71. package/dist/paste/paste.mjs +1 -1
  72. package/dist/paths.d.mts +0 -2
  73. package/dist/paths.mjs +1 -3
  74. package/dist/server/agent-env.d.mts +5 -1
  75. package/dist/server/agent-env.mjs +7 -2
  76. package/dist/server/agent-session.d.mts +14 -1
  77. package/dist/server/agent-session.mjs +84 -19
  78. package/dist/server/build-pictures.mjs +1 -1
  79. package/dist/server/channel.d.mts +13 -24
  80. package/dist/server/channel.mjs +55 -37
  81. package/dist/server/claimed-presence.d.mts +3 -2
  82. package/dist/server/claimed-presence.mjs +15 -7
  83. package/dist/server/close-check.mjs +2 -2
  84. package/dist/server/comments/actions.d.mts +153 -0
  85. package/dist/server/comments/actions.mjs +154 -0
  86. package/dist/server/comments/routes.d.mts +25 -0
  87. package/dist/server/comments/routes.mjs +88 -0
  88. package/dist/server/comments/tools.d.mts +6 -0
  89. package/dist/server/comments/tools.mjs +154 -0
  90. package/dist/server/dev-server.mjs +27 -13
  91. package/dist/server/file-lock.d.mts +2 -2
  92. package/dist/server/file-lock.mjs +130 -36
  93. package/dist/server/mcp.d.mts +0 -2
  94. package/dist/server/mcp.mjs +7 -8
  95. package/dist/server/me.d.mts +1 -1
  96. package/dist/server/open-page.d.mts +22 -0
  97. package/dist/server/open-page.mjs +20 -2
  98. package/dist/server/outcome.d.mts +22 -0
  99. package/dist/server/outcome.mjs +8 -0
  100. package/dist/server/own-copies.mjs +2 -2
  101. package/dist/server/people.d.mts +1 -1
  102. package/dist/server/port.d.mts +12 -0
  103. package/dist/server/port.mjs +66 -14
  104. package/dist/server/presence.d.mts +4 -1
  105. package/dist/server/presence.mjs +2 -2
  106. package/dist/server/requests.d.mts +5 -1
  107. package/dist/server/requests.mjs +33 -48
  108. package/dist/server/studio-for.d.mts +5 -0
  109. package/dist/server/studio-for.mjs +17 -3
  110. package/dist/server/sync-install.d.mts +8 -12
  111. package/dist/server/sync-install.mjs +12 -30
  112. package/dist/server/thread-source.mjs +2 -2
  113. package/dist/server/tools/describe.d.mts +1 -1
  114. package/dist/server/tools/describe.mjs +1 -1
  115. package/dist/server/tools/feedback.d.mts +1 -1
  116. package/dist/server/tools/reading.mjs +13 -4
  117. package/dist/server/tools/show.d.mts +1 -1
  118. package/dist/server/tools/snapshot.d.mts +1 -1
  119. package/dist/server/tools/thread-list.mjs +1 -1
  120. package/dist/server/tools/watch.mjs +5 -3
  121. package/dist/server/vite.mjs +2 -2
  122. package/dist/server/web-app.mjs +4 -7
  123. package/dist/server/work-groups.mjs +1 -1
  124. package/dist/setup/create.d.mts +4 -2
  125. package/dist/setup/create.mjs +67 -17
  126. package/dist/setup/design-system-checks.mjs +3 -12
  127. package/dist/setup/desktop-app-script.mjs +4 -3
  128. package/dist/setup/desktop-app-start.d.mts +8 -2
  129. package/dist/setup/desktop-app-start.mjs +64 -29
  130. package/dist/setup/desktop-app.d.mts +8 -0
  131. package/dist/setup/desktop-app.mjs +19 -3
  132. package/dist/setup/detect.d.mts +0 -2
  133. package/dist/setup/detect.mjs +1 -3
  134. package/dist/setup/doctor.mjs +1 -1
  135. package/dist/setup/migrate/row-titles.mjs +1 -1
  136. package/dist/setup/project.d.mts +6 -1
  137. package/dist/setup/project.mjs +9 -9
  138. package/dist/setup/starters.d.mts +2 -0
  139. package/dist/setup/starters.mjs +7 -1
  140. package/dist/shared/annotations-core.mjs +1 -1
  141. package/dist/shared/canvas-core.mjs +1 -1
  142. package/dist/shared/canvas-file.d.mts +201 -0
  143. package/dist/shared/canvas-file.mjs +1 -0
  144. package/dist/shared/comments/routes.d.mts +53 -0
  145. package/dist/shared/comments/routes.mjs +37 -0
  146. package/dist/shared/feedback.d.mts +8 -0
  147. package/dist/shared/feedback.mjs +10 -2
  148. package/dist/shared/page-review.d.mts +2 -11
  149. package/dist/shared/page-review.mjs +4 -13
  150. package/dist/shared/presence-core.d.mts +11 -0
  151. package/dist/shared/presence-core.mjs +11 -2
  152. package/dist/shared/review-file.d.mts +5 -0
  153. package/dist/shared/review-file.mjs +23 -13
  154. package/dist/shared/server-page.mjs +1 -1
  155. package/dist/shared/signup.d.mts +2 -5
  156. package/dist/shared/signup.mjs +2 -35
  157. package/dist/shared/studio-events.d.mts +0 -6
  158. package/dist/shared/studio-events.mjs +1 -5
  159. package/dist/shared/studio-files.d.mts +12 -0
  160. package/dist/shared/studio-files.mjs +6 -1
  161. package/dist/shared/studio-routes.d.mts +16 -62
  162. package/dist/shared/studio-routes.mjs +1 -69
  163. package/dist/shared/threads-core.d.mts +2 -2
  164. package/dist/shared/threads-core.mjs +1 -1
  165. package/extension/dist/feedback.js +148 -138
  166. package/extension/dist/manifest.json +1 -1
  167. package/extension/dist/picker.js +146 -136
  168. package/package.json +2 -2
  169. package/src/cli/banner.ts +45 -11
  170. package/src/cli/commands/app.ts +23 -8
  171. package/src/cli/commands/hook.ts +4 -1
  172. package/src/config/project-files.ts +21 -6
  173. package/src/config/renamed.ts +0 -3
  174. package/src/content/capture.ts +6 -12
  175. package/src/content/content.ts +7 -241
  176. package/src/content/flow-specs.ts +2 -1
  177. package/src/content/prototype-summary.ts +4 -44
  178. package/src/content/review-pages.ts +2 -1
  179. package/src/content/text-replace.ts +449 -0
  180. package/src/paths.ts +0 -8
  181. package/src/server/agent-env.ts +12 -2
  182. package/src/server/agent-session.ts +124 -34
  183. package/src/server/channel.ts +94 -52
  184. package/src/server/claimed-presence.ts +26 -10
  185. package/src/server/comments/actions.ts +352 -0
  186. package/src/server/comments/routes.ts +176 -0
  187. package/src/server/comments/tools.ts +317 -0
  188. package/src/server/dev-server.ts +36 -15
  189. package/src/server/file-lock.ts +205 -54
  190. package/src/server/mcp.ts +13 -14
  191. package/src/server/me.ts +2 -5
  192. package/src/server/open-page.ts +54 -4
  193. package/src/server/outcome.ts +26 -0
  194. package/src/server/people.ts +2 -1
  195. package/src/server/port.ts +94 -20
  196. package/src/server/presence.ts +11 -3
  197. package/src/server/requests.ts +60 -70
  198. package/src/server/studio-for.ts +26 -2
  199. package/src/server/sync-install.ts +35 -26
  200. package/src/server/thread-place.ts +2 -4
  201. package/src/server/tools/reading.ts +16 -3
  202. package/src/server/tools/watch.ts +7 -7
  203. package/src/server/web-app.ts +11 -9
  204. package/src/setup/create.ts +91 -17
  205. package/src/setup/design-system-checks.ts +7 -15
  206. package/src/setup/desktop-app-script.ts +4 -3
  207. package/src/setup/desktop-app-start.ts +94 -26
  208. package/src/setup/desktop-app.ts +23 -2
  209. package/src/setup/detect.ts +0 -4
  210. package/src/setup/project.ts +9 -8
  211. package/src/setup/starters.ts +8 -1
  212. package/src/shared/canvas-file.ts +205 -0
  213. package/src/shared/comments/routes.ts +79 -0
  214. package/src/shared/feedback.ts +14 -4
  215. package/src/shared/page-review.ts +11 -16
  216. package/src/shared/presence-core.ts +16 -2
  217. package/src/shared/review-file.ts +28 -12
  218. package/src/shared/server-page.ts +2 -1
  219. package/src/shared/signup.ts +4 -28
  220. package/src/shared/studio-events.ts +0 -13
  221. package/src/shared/studio-files.ts +14 -0
  222. package/src/shared/studio-routes.ts +21 -64
  223. package/src/shared/threads-core.ts +2 -2
  224. package/dist/server/tools/close-reply.d.mts +0 -6
  225. package/dist/server/tools/close-reply.mjs +0 -138
  226. package/dist/server/tools/comment.d.mts +0 -6
  227. package/dist/server/tools/comment.mjs +0 -49
  228. package/src/server/tools/close-reply.ts +0 -271
  229. package/src/server/tools/comment.ts +0 -96
@@ -6,15 +6,14 @@ import { join } from "node:path";
6
6
  import { shownFrom } from "../paths.ts";
7
7
  import { placePicks } from "../shared/annotations-core.ts";
8
8
  import {
9
- type CanvasShape,
10
9
  canvasFrames,
11
10
  type FrameKind,
12
11
  frameTitle,
13
12
  kinded,
14
- type Viewport,
15
13
  } from "../shared/canvas-core.ts";
14
+ import type { Frame } from "../shared/canvas-file.ts";
16
15
  import { rowOwner } from "../shared/people-core.ts";
17
- import { outlineSketch, type Sketch } from "../shared/sketch-core.ts";
16
+ import { outlineSketch } from "../shared/sketch-core.ts";
18
17
  import { knownFor, readCanvas, readPrototypeAnnotations } from "./content.ts";
19
18
  import { fileSpecTexts } from "./flow-specs.ts";
20
19
  import { prototypePaths, prototypeTitle, viewFiles } from "./prototypes.ts";
@@ -22,49 +21,10 @@ import { readText } from "./read-text.ts";
22
21
 
23
22
  const HEADING_RE = /^#\s+(.+)$/m;
24
23
 
25
- export type CanvasFrame = (
26
- | ViewFrame
27
- | { doc: string; id: string; sketch?: undefined; view?: undefined }
28
- | {
29
- doc?: undefined;
30
- id: string;
31
- sketch?: undefined;
32
- spec: string;
33
- view?: undefined;
34
- }
35
- | {
36
- doc?: undefined;
37
- id: string;
38
- sketch: Sketch;
39
- title: string;
40
- view?: undefined;
41
- }
42
- | UnknownFrame
43
- ) & { uid?: string };
44
-
45
- interface UnknownFrame {
46
- doc?: undefined;
47
- id: string;
48
- sketch?: undefined;
49
- spec?: undefined;
50
- view?: undefined;
51
- }
52
-
53
- /** A frame showing a view, in its viewport and state. */
54
- export interface ViewFrame {
55
- doc?: undefined;
56
- id: string;
57
- params?: Record<string, string>;
58
- sketch?: undefined;
59
- view: string;
60
- viewport?: Viewport;
61
- }
62
-
63
24
  /** Every frame on a prototype's canvas, with its key, as the studio shows it. */
64
25
  export async function prototypeFrames(root: string, slug: string) {
65
26
  try {
66
- const canvas: CanvasShape<CanvasFrame> = await readCanvas(slug, root);
67
- return canvasFrames(canvas);
27
+ return canvasFrames(await readCanvas(slug, root));
68
28
  } catch {
69
29
  return [];
70
30
  }
@@ -86,7 +46,7 @@ interface SummaryFrame {
86
46
  }
87
47
 
88
48
  const shownBy = (
89
- frame: CanvasFrame
49
+ frame: Frame
90
50
  ): Pick<
91
51
  SummaryFrame,
92
52
  "doc" | "kind" | "sketch" | "spec" | "title" | "view"
@@ -16,6 +16,7 @@ import {
16
16
  idFor,
17
17
  placeFrameInCell,
18
18
  } from "../shared/canvas-core.ts";
19
+ import type { Canvas } from "../shared/canvas-file.ts";
19
20
  import type {
20
21
  PageComment,
21
22
  PageReviewFile,
@@ -30,7 +31,7 @@ import {
30
31
  type Snapshot,
31
32
  saveSnapshots,
32
33
  } from "./capture.ts";
33
- import { type Canvas, readCanvas } from "./content.ts";
34
+ import { readCanvas } from "./content.ts";
34
35
 
35
36
  /** The row for pages commented on outside every task. */
36
37
  const ELSEWHERE = { id: "elsewhere", title: "Outside the tasks" };
@@ -0,0 +1,449 @@
1
+ // Find and replace on the canvas: every place in a person's own code that
2
+ // writes text the frames show, changed at once, and put back at once. Only
3
+ // text is changed: what is between tags, and strings and template text that
4
+ // are not code (an import, a class name, a key, a type). And only text the
5
+ // frames were seen showing: a string is changed only when the canvas saw it
6
+ // on a page, so a class name or an icon's name that happens to hold the
7
+ // words is left alone.
8
+
9
+ import { randomUUID } from "node:crypto";
10
+ import { readdir, readFile, writeFile } from "node:fs/promises";
11
+ import { join, relative } from "node:path";
12
+ import { parseJsx } from "./jsx-source.ts";
13
+
14
+ const SOURCE_RE = /\.(?:tsx|ts|jsx|js)$/;
15
+ const SKIPPED = new Set(["comments", "docs", "node_modules"]);
16
+ const JSX_UNSAFE_RE = /[{}<>]/;
17
+ const SPACES_RE = /\s+/g;
18
+ const REGEX_CHARS_RE = /[.*+?^${}()|[\]\\]/g;
19
+ const MAX_LENGTH = 2000;
20
+ /** How many replacements can still be put back, newest kept. */
21
+ const UNDO_KEPT = 20;
22
+
23
+ /** Calls whose strings are class names, or a module's path. */
24
+ const CODE_CALLS = new Set([
25
+ "classNames",
26
+ "clsx",
27
+ "cn",
28
+ "cva",
29
+ "cx",
30
+ "require",
31
+ "tv",
32
+ "twMerge",
33
+ ]);
34
+
35
+ /** Props whose strings say how something looks or works, not what it says. */
36
+ const CODE_PROPS = new Set([
37
+ "as",
38
+ "className",
39
+ "class",
40
+ "href",
41
+ "htmlFor",
42
+ "icon",
43
+ "id",
44
+ "key",
45
+ "name",
46
+ "rel",
47
+ "role",
48
+ "size",
49
+ "src",
50
+ "style",
51
+ "target",
52
+ "type",
53
+ "variant",
54
+ ]);
55
+
56
+ const COMPARISONS = new Set(["==", "===", "!=", "!=="]);
57
+
58
+ /** One file as it was before a replacement and after it. */
59
+ interface Changed {
60
+ after: string;
61
+ before: string;
62
+ path: string;
63
+ /** How many places it changes. */
64
+ replaced?: number;
65
+ }
66
+
67
+ /** What a replacement did, and how to put it back. */
68
+ export interface Replaced {
69
+ /** The files changed, relative to the folder. */
70
+ files: string[];
71
+ /** Files left as they were: changed since they were read, or unwritable. */
72
+ left?: number;
73
+ /** How many places were changed. */
74
+ replaced: number;
75
+ /** Text found but not changed: the new words cannot be written there. */
76
+ skipped: number;
77
+ /** Puts it all back (undoReplace), while nothing has changed since. */
78
+ undo?: string;
79
+ }
80
+
81
+ const undos = new Map<string, Changed[]>();
82
+
83
+ async function sourceFiles(dir: string): Promise<string[]> {
84
+ let entries: import("node:fs").Dirent[];
85
+ try {
86
+ entries = await readdir(dir, { withFileTypes: true });
87
+ } catch {
88
+ return [];
89
+ }
90
+ const files: string[] = [];
91
+ for (const entry of entries) {
92
+ // A link could lead out of the person's folder: only their own files.
93
+ if (SKIPPED.has(entry.name) || entry.isSymbolicLink()) {
94
+ continue;
95
+ }
96
+ const path = join(dir, entry.name);
97
+ if (entry.isDirectory()) {
98
+ files.push(...(await sourceFiles(path)));
99
+ } else if (SOURCE_RE.test(entry.name)) {
100
+ files.push(path);
101
+ }
102
+ }
103
+ return files;
104
+ }
105
+
106
+ const words = (text: string) => text.replace(SPACES_RE, " ").trim();
107
+
108
+ /** A node of Babel's tree, as far as the walk reads it. */
109
+ interface Node {
110
+ end?: number | null;
111
+ start?: number | null;
112
+ type: string;
113
+ [key: string]: unknown;
114
+ }
115
+
116
+ const isNode = (value: unknown): value is Node =>
117
+ typeof value === "object" &&
118
+ value !== null &&
119
+ typeof (value as Node).type === "string";
120
+
121
+ const calleeName = (node: Node): string | undefined => {
122
+ const callee = node.callee as Node | undefined;
123
+ if (callee?.type === "Identifier") {
124
+ return callee.name as string;
125
+ }
126
+ return;
127
+ };
128
+
129
+ /** An object property's name, when it is written as a name or a string. */
130
+ const keyName = (node: Node): string | undefined => {
131
+ const named = node.key as Node | undefined;
132
+ if (named?.type === "Identifier") {
133
+ return named.name as string;
134
+ }
135
+ return named?.type === "StringLiteral" ? (named.value as string) : undefined;
136
+ };
137
+
138
+ const propName = (node: Node): string | undefined => {
139
+ const name = node.name as Node | undefined;
140
+ return name?.type === "JSXIdentifier" ? (name.name as string) : undefined;
141
+ };
142
+
143
+ const CALLS = new Set(["CallExpression", "NewExpression"]);
144
+
145
+ const FUNCTIONS = new Set([
146
+ "ArrowFunctionExpression",
147
+ "FunctionDeclaration",
148
+ "FunctionExpression",
149
+ ]);
150
+
151
+ /**
152
+ * What a call is given is the call's to read, not the page's words:
153
+ * useState("Save"), getItem(draft ? "Save" : "Old"), import("./save").
154
+ * A callback it is given is words again: what it renders shows.
155
+ */
156
+ const givenToCall = (parent: Node, key: string) =>
157
+ CALLS.has(parent.type) && key === "arguments";
158
+
159
+ /** Whether a string at this place in the tree is code, not words. */
160
+ const isCode = (parent: Node, key: string): boolean => {
161
+ // A type is code; what `as const` or `satisfies` wraps is not.
162
+ if (parent.type.startsWith("TS")) {
163
+ return key !== "expression";
164
+ }
165
+ switch (parent.type) {
166
+ case "ImportDeclaration":
167
+ case "ImportExpression":
168
+ case "SwitchCase":
169
+ return true;
170
+ case "ExportAllDeclaration":
171
+ case "ExportNamedDeclaration":
172
+ return key === "source";
173
+ case "ObjectProperty":
174
+ // A key, or the value of one that names code: { id: "address" }.
175
+ return key === "key" || CODE_PROPS.has(keyName(parent) ?? "");
176
+ case "ObjectMethod":
177
+ case "ClassProperty":
178
+ case "MemberExpression":
179
+ case "OptionalMemberExpression":
180
+ return key === "key" || key === "property";
181
+ case "BinaryExpression":
182
+ return COMPARISONS.has(parent.operator as string);
183
+ case "CallExpression":
184
+ return CODE_CALLS.has(calleeName(parent) ?? "");
185
+ case "JSXAttribute": {
186
+ const name = propName(parent) ?? "";
187
+ return (
188
+ CODE_PROPS.has(name) ||
189
+ name.startsWith("data-") ||
190
+ name.startsWith("aria-")
191
+ );
192
+ }
193
+ default:
194
+ return false;
195
+ }
196
+ };
197
+
198
+ /** A run of text in the code: its place, and how it is written there. */
199
+ interface Run {
200
+ /** A prop's value, written in JSX, where a backslash escapes nothing. */
201
+ attribute?: boolean;
202
+ end: number;
203
+ /** "jsx" between tags; else the quote it is in. */
204
+ kind: "jsx" | '"' | "'" | "`";
205
+ /**
206
+ * Part of a text a page shows (template text, around what it holds), not
207
+ * all of one: a string or the words between tags show whole.
208
+ */
209
+ part: boolean;
210
+ start: number;
211
+ /** What it says, as a page shows it. */
212
+ value: string;
213
+ }
214
+
215
+ /** Every run of words in a module that is not code. */
216
+ function textRuns(code: string, filename: string): Run[] {
217
+ const program = parseJsx(code, filename);
218
+ if (!program) {
219
+ return [];
220
+ }
221
+ const runs: Run[] = [];
222
+ /** "given": in what a call is given, code until a callback starts. */
223
+ type Context = boolean | "given";
224
+ const walk = (node: Node, inCode: Context) => {
225
+ for (const [key, value] of Object.entries(node)) {
226
+ if (key === "loc" || key === "extra" || key.endsWith("Comments")) {
227
+ continue;
228
+ }
229
+ for (const child of Array.isArray(value) ? value : [value]) {
230
+ if (isNode(child)) {
231
+ visit(child, node, key, inCode);
232
+ }
233
+ }
234
+ }
235
+ };
236
+ const visit = (node: Node, parent: Node, key: string, inCode: Context) => {
237
+ let asCode: Context = inCode || isCode(parent, key);
238
+ if (!asCode && givenToCall(parent, key)) {
239
+ asCode = "given";
240
+ }
241
+ if (asCode === "given" && FUNCTIONS.has(node.type)) {
242
+ asCode = false;
243
+ }
244
+ const { start, end } = node;
245
+ if (typeof start !== "number" || typeof end !== "number") {
246
+ walk(node, asCode);
247
+ return;
248
+ }
249
+ if (node.type === "JSXText") {
250
+ runs.push({
251
+ kind: "jsx",
252
+ part: false,
253
+ start,
254
+ end,
255
+ value: node.value as string,
256
+ });
257
+ return;
258
+ }
259
+ if (asCode) {
260
+ walk(node, asCode);
261
+ return;
262
+ }
263
+ if (node.type === "StringLiteral") {
264
+ const quote = code[start] as Run["kind"];
265
+ runs.push({
266
+ attribute: parent.type === "JSXAttribute",
267
+ kind: quote,
268
+ part: parent.type === "BinaryExpression",
269
+ start: start + 1,
270
+ end: end - 1,
271
+ value: node.value as string,
272
+ });
273
+ return;
274
+ }
275
+ if (node.type === "TemplateElement") {
276
+ const cooked = (node.value as { cooked?: string } | undefined)?.cooked;
277
+ runs.push({ kind: "`", part: true, start, end, value: cooked ?? "" });
278
+ return;
279
+ }
280
+ walk(node, asCode);
281
+ };
282
+ walk(program as unknown as Node, false);
283
+ return runs;
284
+ }
285
+
286
+ /** The new words as they must be written in a run, or undefined if they cannot. */
287
+ const written = (
288
+ { attribute, kind }: Run,
289
+ value: string
290
+ ): string | undefined => {
291
+ if (kind === "jsx") {
292
+ return JSX_UNSAFE_RE.test(value) ? undefined : value;
293
+ }
294
+ if (attribute) {
295
+ return value.includes(kind) ? undefined : value;
296
+ }
297
+ const escaped = value.replaceAll("\\", "\\\\").replaceAll(kind, `\\${kind}`);
298
+ return kind === "`" ? escaped.replaceAll("${", "\\${") : escaped;
299
+ };
300
+
301
+ const finder = (find: string) =>
302
+ new RegExp(find.replace(REGEX_CHARS_RE, "\\$&"), "gi");
303
+
304
+ /** One module with `find` changed to `to` in its runs the pages showed. */
305
+ function replaceIn(
306
+ code: string,
307
+ filename: string,
308
+ find: string,
309
+ to: string,
310
+ seen: readonly string[]
311
+ ) {
312
+ const matches = finder(find);
313
+ let replaced = 0;
314
+ let skipped = 0;
315
+ let next = code;
316
+ // From the end, so each run's place is still where the tree said.
317
+ const runs = textRuns(code, filename)
318
+ .filter(({ part, value }) => {
319
+ // As the page has it, case and all, and whole: "address", an id, is
320
+ // not the "address" in a sentence the page shows.
321
+ const said = words(value);
322
+ return (
323
+ said.search(matches) !== -1 &&
324
+ (part ? seen.some((text) => text.includes(said)) : seen.includes(said))
325
+ );
326
+ })
327
+ .sort((a, b) => b.start - a.start);
328
+ for (const run of runs) {
329
+ const raw = code.slice(run.start, run.end);
330
+ const value = written(run, to);
331
+ const count = raw.match(finder(find))?.length ?? 0;
332
+ if (value === undefined) {
333
+ skipped += count;
334
+ continue;
335
+ }
336
+ if (count === 0) {
337
+ // Written with escapes the page does not show: not certain enough.
338
+ skipped += 1;
339
+ continue;
340
+ }
341
+ next =
342
+ next.slice(0, run.start) +
343
+ raw.replace(finder(find), () => value) +
344
+ next.slice(run.end);
345
+ replaced += count;
346
+ }
347
+ // Never a change that would not run: the module as it was, then.
348
+ if (next !== code && !parseJsx(next, filename)) {
349
+ return { next: code, replaced: 0, skipped: skipped + replaced };
350
+ }
351
+ return { next, replaced, skipped };
352
+ }
353
+
354
+ /**
355
+ * Change `find` to `to`, ignoring case, wherever the modules in `folder`
356
+ * write it as text that one of the `seen` texts (what the frames showed)
357
+ * holds. Each changed file can be put back with the `undo` it answers with.
358
+ */
359
+ export async function replaceText(
360
+ folder: string,
361
+ find: string,
362
+ to: string,
363
+ seen: readonly string[]
364
+ ): Promise<Replaced> {
365
+ const nothing: Replaced = { files: [], replaced: 0, skipped: 0 };
366
+ if (!find.trim() || find.length > MAX_LENGTH || to.length > MAX_LENGTH) {
367
+ return nothing;
368
+ }
369
+ const shown = seen.map(words);
370
+ const changed: Changed[] = [];
371
+ let skipped = 0;
372
+ for (const path of await sourceFiles(folder)) {
373
+ const before = await readFile(path, "utf8");
374
+ // Most modules do not hold the words at all: no need to parse them.
375
+ if (before.search(finder(find)) === -1) {
376
+ continue;
377
+ }
378
+ const result = replaceIn(before, path, find, to, shown);
379
+ skipped += result.skipped;
380
+ if (result.next !== before) {
381
+ changed.push({
382
+ path,
383
+ before,
384
+ after: result.next,
385
+ replaced: result.replaced,
386
+ });
387
+ }
388
+ }
389
+ // A file changed since it was read keeps that change: it is left, as is
390
+ // one that cannot be written. Only what was written counts.
391
+ const saved: Changed[] = [];
392
+ let replaced = 0;
393
+ for (const change of changed) {
394
+ try {
395
+ if ((await readFile(change.path, "utf8")) === change.before) {
396
+ await writeFile(change.path, change.after);
397
+ saved.push(change);
398
+ replaced += change.replaced ?? 0;
399
+ }
400
+ } catch {
401
+ // Gone, or not writable: left as it is.
402
+ }
403
+ }
404
+ const left = changed.length - saved.length;
405
+ if (saved.length === 0) {
406
+ return { ...nothing, skipped, ...(left > 0 ? { left } : {}) };
407
+ }
408
+ const undo = randomUUID();
409
+ undos.set(undo, saved);
410
+ for (const old of undos.keys()) {
411
+ if (undos.size <= UNDO_KEPT) {
412
+ break;
413
+ }
414
+ undos.delete(old);
415
+ }
416
+ return {
417
+ files: saved.map(({ path }) => relative(folder, path)),
418
+ ...(left > 0 ? { left } : {}),
419
+ replaced,
420
+ skipped,
421
+ undo,
422
+ };
423
+ }
424
+
425
+ /**
426
+ * Put back a replacement: each file it changed, unless it has changed again
427
+ * since, which keeps the newer change. Answers with the files put back and
428
+ * the ones left, relative to `folder`.
429
+ */
430
+ export async function undoReplace(
431
+ folder: string,
432
+ undo: string
433
+ ): Promise<{ kept: string[]; restored: string[] }> {
434
+ const changed = undos.get(undo) ?? [];
435
+ undos.delete(undo);
436
+ const restored: string[] = [];
437
+ const kept: string[] = [];
438
+ for (const { path, before, after } of changed) {
439
+ const now = await readFile(path, "utf8").catch(() => undefined);
440
+ const name = relative(folder, path);
441
+ if (now === after) {
442
+ await writeFile(path, before);
443
+ restored.push(name);
444
+ } else {
445
+ kept.push(name);
446
+ }
447
+ }
448
+ return { kept, restored };
449
+ }
package/src/paths.ts CHANGED
@@ -63,14 +63,6 @@ export const STUDIO_REACT: { range: string; major: number } = (() => {
63
63
  return { range, major: Number(range.match(MAJOR_RE)?.[0]) };
64
64
  })();
65
65
 
66
- /** The range the studio asks of a peer dependency: "^2.1.10". */
67
- export const studioPeer = (name: string): string | undefined =>
68
- (
69
- JSON.parse(readFileSync(join(PACKAGE_ROOT, "package.json"), "utf8")) as {
70
- peerDependencies?: Record<string, string>;
71
- }
72
- ).peerDependencies?.[name];
73
-
74
66
  /**
75
67
  * The highest major a version or range allows ("^18 || ^19" → 19), or
76
68
  * undefined when it names none ("catalog:", "latest").
@@ -1,3 +1,5 @@
1
+ import { dirname } from "node:path";
2
+
1
3
  // What says a process belongs to a Claude Code session. Dex is often
2
4
  // started by an agent (`dex dev` from Claude Code), so its environment
3
5
  // carries these; a Claude Code started from Dex would inherit them and
@@ -52,9 +54,17 @@ const quoted = (value: string): string => `'${value.replaceAll("'", "'\\''")}'`;
52
54
  * profile sets the rest, as in any new window. However the terminal app was
53
55
  * started, even by an agent's session, nothing of that session reaches what
54
56
  * runs. A shell of the same kind stays open once `command` ends.
57
+ *
58
+ * `runtime`, the Node running Dex, goes first on that shell's PATH: a
59
+ * profile may not put Node there at all (nvm loaded only in some shells),
60
+ * and `dex`, its hooks and its MCP server all run on it.
55
61
  */
56
- export const inFreshShell = (command: string): string => {
62
+ export const inFreshShell = (
63
+ command: string,
64
+ runtime: string = process.execPath
65
+ ): string => {
57
66
  const kept = KEPT_FOR_LOGIN.map((name) => `${name}="$${name}"`).join(" ");
58
- const then = `${command}; exec "$SHELL" -l`;
67
+ const path = `export PATH=${quoted(dirname(runtime))}:"$PATH"`;
68
+ const then = `${path}; ${command}; exec "$SHELL" -l`;
59
69
  return `exec /usr/bin/env -i ${kept} "$SHELL" -lic ${quoted(then)}`;
60
70
  };