@codeyam-editor/codeyam-editor 0.1.0-staging.46bd02f → 0.1.0-staging.6c5526a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/npm/container.js CHANGED
@@ -8,7 +8,7 @@ const path = require("path");
8
8
  * inline monitoring UI at http://localhost:14199.
9
9
  */
10
10
 
11
- const { ensureServer, rootDir } = require("./utils");
11
+ const { ensureServer, rootDir, killChildProcess } = require("./utils");
12
12
  const { open } = require("./open");
13
13
 
14
14
  const PORT = 14199;
@@ -23,9 +23,13 @@ async function main() {
23
23
 
24
24
  process.on("SIGINT", () => {
25
25
  clearInterval(keepAlive);
26
- if (serverChild) serverChild.kill("SIGINT");
26
+ killChildProcess(serverChild);
27
27
  process.exit(0);
28
28
  });
29
29
  }
30
30
 
31
- main();
31
+ module.exports = { main };
32
+
33
+ if (require.main === module) {
34
+ main();
35
+ }
package/npm/editor-dev.js CHANGED
@@ -19,7 +19,7 @@
19
19
  const { execSync, spawn } = require("child_process");
20
20
  const path = require("path");
21
21
  const fs = require("fs");
22
- const { ensureServer, restartServer, waitForPort, ensureBinary, uiDistDir, rootDir } = require("./utils");
22
+ const { ensureServer, restartServer, waitForPort, ensureBinary, uiDistDir, rootDir, killChildProcess } = require("./utils");
23
23
  const { open } = require("./open");
24
24
 
25
25
  // Tell the Rust binary to emit "codeyam-editor-dev" in all instructions,
@@ -29,9 +29,13 @@ process.env.CODEYAM_CLI = "codeyam-editor-dev";
29
29
  const args = process.argv.slice(2);
30
30
  const KNOWN_SUBCOMMANDS = ["start", "init", "editor"];
31
31
 
32
- /** Check if the first non-flag argument is a known subcommand. */
33
- function hasSubcommand() {
34
- for (const arg of args) {
32
+ /**
33
+ * Check if the first non-flag argument is a known subcommand.
34
+ * Accepts an explicit argv array so the helper is testable without mutating
35
+ * process.argv (the module-level `args` const is the production default).
36
+ */
37
+ function hasSubcommand(argv = args) {
38
+ for (const arg of argv) {
35
39
  if (arg.startsWith("-")) continue;
36
40
  return KNOWN_SUBCOMMANDS.includes(arg);
37
41
  }
@@ -48,73 +52,77 @@ function buildBinary() {
48
52
  }
49
53
  }
50
54
 
51
- // --- Passthrough mode: build binary, then forward to it ---
52
- if (hasSubcommand()) {
53
- buildBinary();
54
- const binary = ensureBinary();
55
- const child = spawn(binary, args, {
56
- stdio: "inherit",
57
- cwd: process.cwd(),
58
- env: { ...process.env, CODEYAM_EDITOR_UI_DIR: uiDistDir() },
59
- });
60
- child.on("exit", (code) => {
61
- process.exit(code ?? 1);
62
- });
63
- } else {
64
- // --- Launcher mode: build everything, start server, open browser ---
65
- const SERVER_PORT = 14199;
66
- const shouldRestart = args.includes("--restart");
67
-
68
- function readProjectPort() {
69
- try {
70
- const configPath = path.join(process.cwd(), ".codeyam", "editor.json");
71
- const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
72
- return config.port || 3000;
73
- } catch {
74
- return 3000;
75
- }
76
- }
77
-
78
- async function main() {
79
- const projectDir = process.cwd();
80
- console.error(`Editor repo: ${rootDir}`);
81
- console.error(`Target project: ${projectDir}`);
55
+ module.exports = { hasSubcommand, buildBinary };
82
56
 
57
+ if (require.main === module) {
58
+ // --- Passthrough mode: build binary, then forward to it ---
59
+ if (hasSubcommand()) {
83
60
  buildBinary();
84
-
85
- // Build the UI from the editor repo
86
- console.error("Building UI...");
87
- try {
88
- execSync("npx vite build", { stdio: "inherit", cwd: path.join(rootDir, "ui") });
89
- } catch {
90
- console.error("Failed to build UI. Continuing with existing build (if any)...");
91
- }
92
-
93
- const serverChild = shouldRestart
94
- ? await restartServer(SERVER_PORT)
95
- : await ensureServer(SERVER_PORT);
96
-
97
- const appPort = readProjectPort();
98
- const viteInternalPort = appPort + 1;
99
- console.error(`Waiting for dev server on port ${viteInternalPort}...`);
100
- const viteReady = await waitForPort(viteInternalPort, 15000, 300);
101
- if (!viteReady) {
102
- console.error(`Warning: Dev server not ready on port ${viteInternalPort}. Live Preview may not work initially.`);
103
- }
104
-
105
- const url = `http://localhost:${SERVER_PORT}`;
106
- console.error(`Editor UI: ${url}`);
107
- console.error(`Live Preview: http://localhost:${appPort}`);
108
- console.error(`Project: ${projectDir}`);
109
- open(url);
110
- const keepAlive = setInterval(() => {}, 60_000);
111
-
112
- process.on("SIGINT", () => {
113
- clearInterval(keepAlive);
114
- if (serverChild) serverChild.kill("SIGINT");
115
- process.exit(0);
61
+ const binary = ensureBinary();
62
+ const child = spawn(binary, args, {
63
+ stdio: "inherit",
64
+ cwd: process.cwd(),
65
+ env: { ...process.env, CODEYAM_EDITOR_UI_DIR: uiDistDir() },
66
+ });
67
+ child.on("exit", (code) => {
68
+ process.exit(code ?? 1);
116
69
  });
70
+ } else {
71
+ // --- Launcher mode: build everything, start server, open browser ---
72
+ const SERVER_PORT = 14199;
73
+ const shouldRestart = args.includes("--restart");
74
+
75
+ const readProjectPort = () => {
76
+ try {
77
+ const configPath = path.join(process.cwd(), ".codeyam", "editor.json");
78
+ const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
79
+ return config.port || 3000;
80
+ } catch {
81
+ return 3000;
82
+ }
83
+ };
84
+
85
+ const launcherMain = async () => {
86
+ const projectDir = process.cwd();
87
+ console.error(`Editor repo: ${rootDir}`);
88
+ console.error(`Target project: ${projectDir}`);
89
+
90
+ buildBinary();
91
+
92
+ // Build the UI from the editor repo
93
+ console.error("Building UI...");
94
+ try {
95
+ execSync("npx vite build", { stdio: "inherit", cwd: path.join(rootDir, "ui") });
96
+ } catch {
97
+ console.error("Failed to build UI. Continuing with existing build (if any)...");
98
+ }
99
+
100
+ const serverChild = shouldRestart
101
+ ? await restartServer(SERVER_PORT)
102
+ : await ensureServer(SERVER_PORT);
103
+
104
+ const appPort = readProjectPort();
105
+ const viteInternalPort = appPort + 1;
106
+ console.error(`Waiting for dev server on port ${viteInternalPort}...`);
107
+ const viteReady = await waitForPort(viteInternalPort, 15000, 300);
108
+ if (!viteReady) {
109
+ console.error(`Warning: Dev server not ready on port ${viteInternalPort}. Live Preview may not work initially.`);
110
+ }
111
+
112
+ const url = `http://localhost:${SERVER_PORT}`;
113
+ console.error(`Editor UI: ${url}`);
114
+ console.error(`Live Preview: http://localhost:${appPort}`);
115
+ console.error(`Project: ${projectDir}`);
116
+ open(url);
117
+ const keepAlive = setInterval(() => {}, 60_000);
118
+
119
+ process.on("SIGINT", () => {
120
+ clearInterval(keepAlive);
121
+ killChildProcess(serverChild);
122
+ process.exit(0);
123
+ });
124
+ };
125
+
126
+ launcherMain();
117
127
  }
118
-
119
- main();
120
128
  }
package/npm/editor.js CHANGED
@@ -13,18 +13,125 @@
13
13
  */
14
14
 
15
15
  const { execSync } = require("child_process");
16
+ const fs = require("fs");
17
+ const os = require("os");
16
18
  const path = require("path");
17
- const { ensureServer, restartServer, waitForPort, rootDir } = require("./utils");
19
+ const { ensureServer, stopServer, isPortInUse, waitForPort, rootDir, killChildProcess } = require("./utils");
18
20
  const { open } = require("./open");
19
21
 
20
- const SERVER_PORT = 14199;
21
- const VITE_PORT = 5173;
22
+ const SERVER_PORT = parseInt(process.env.CODEYAM_EDITOR_PORT || "14199", 10);
23
+ const VITE_PORT = parseInt(process.env.PORT || "5173", 10);
22
24
  // The reverse proxy sits on VITE_PORT; the actual Vite dev server runs on
23
25
  // VITE_PORT + 1. We need to wait for the real Vite server, not the proxy.
24
26
  const VITE_INTERNAL_PORT = VITE_PORT + 1;
25
27
  const shouldRestart = process.argv.includes("--restart");
26
28
 
29
+ function buildBinary() {
30
+ console.error("Building Rust binary...");
31
+ try {
32
+ execSync("cargo build", { stdio: "inherit", cwd: rootDir });
33
+ return true;
34
+ } catch {
35
+ console.error("Failed to build Rust binary. Continuing with existing build (if any)...");
36
+ return false;
37
+ }
38
+ }
39
+
40
+ // Resolve <CARGO_HOME>/bin (falling back to $HOME/.cargo/bin). Mirrors
41
+ // `installed_binary_path()` in crates/control-api/src/pty_broker_startup.rs
42
+ // — the Rust broker auto-spawn execs the binary at this path, so we MUST
43
+ // keep it in sync with the freshly-built debug binary or `npm run editor`
44
+ // will start a server whose PTY-broker auto-spawn execs a stale install.
45
+ function cargoBinDir() {
46
+ if (process.env.CARGO_HOME) return path.join(process.env.CARGO_HOME, "bin");
47
+ return path.join(os.homedir(), ".cargo", "bin");
48
+ }
49
+
50
+ // Install target/debug/codeyam-editor to <cargo_bin>/codeyam-editor
51
+ // atomically (write to tmp in same dir, then rename), and refresh the
52
+ // codeyam-editor-pty-broker hardlink so it points at the new inode.
53
+ //
54
+ // The PTY broker auto-spawn (control-api/src/pty_broker_startup.rs) always
55
+ // resolves the *installed* path, NOT current_exe() — that was a deliberate
56
+ // fix to prevent a fork-bomb where test binaries spawned themselves. So a
57
+ // fresh `target/debug/codeyam-editor` is not enough; the install path must
58
+ // be refreshed too. Without this, build tabs hang on "Reconnecting..."
59
+ // because the broker daemon errors out (`unrecognized subcommand`) before
60
+ // the UDS socket opens.
61
+ function installBinary() {
62
+ const srcPath = path.join(rootDir, "target", "debug", "codeyam-editor");
63
+ if (!fs.existsSync(srcPath)) {
64
+ console.error(`Skipping install: ${srcPath} not found.`);
65
+ return;
66
+ }
67
+ const binDir = cargoBinDir();
68
+ const dstPath = path.join(binDir, "codeyam-editor");
69
+ const linkPath = path.join(binDir, "codeyam-editor-pty-broker");
70
+ const tmpPath = path.join(binDir, `.codeyam-editor.tmp.${process.pid}`);
71
+ console.error(`Installing fresh binary to ${dstPath}...`);
72
+ try {
73
+ fs.mkdirSync(binDir, { recursive: true });
74
+ fs.copyFileSync(srcPath, tmpPath);
75
+ fs.chmodSync(tmpPath, 0o755);
76
+ fs.renameSync(tmpPath, dstPath);
77
+ try {
78
+ fs.unlinkSync(linkPath);
79
+ } catch (e) {
80
+ if (e.code !== "ENOENT") throw e;
81
+ }
82
+ fs.linkSync(dstPath, linkPath);
83
+ } catch (err) {
84
+ console.error(`Failed to install binary: ${err.message}`);
85
+ try { fs.unlinkSync(tmpPath); } catch {}
86
+ }
87
+ }
88
+
89
+ // Evict Rust build artifacts not touched in the last day so target/ doesn't
90
+ // grow unboundedly. Must run after the old server is stopped (it holds files
91
+ // in target/debug/ open) and before the new server starts. Non-fatal.
92
+ function sweepStaleArtifacts() {
93
+ console.error("Sweeping stale Rust build artifacts (idle >1 day)...");
94
+ try {
95
+ execSync("cargo sweep --time 1", { stdio: "inherit", cwd: rootDir });
96
+ } catch {
97
+ console.error("cargo-sweep failed (install with `cargo install cargo-sweep`). Continuing.");
98
+ }
99
+ }
100
+
27
101
  async function main() {
102
+ // Sync ports to editor.json so the Rust backend uses them
103
+ const editorJsonPath = path.join(rootDir, ".codeyam", "editor.json");
104
+ if (fs.existsSync(editorJsonPath)) {
105
+ const editorJson = JSON.parse(fs.readFileSync(editorJsonPath, "utf8"));
106
+ editorJson.port = VITE_PORT;
107
+ editorJson.startCommand = `cd ui && npx vite --port ${VITE_PORT}`;
108
+
109
+ const uiApp = editorJson.apps?.find(a => a.dir === "ui");
110
+ if (uiApp) {
111
+ uiApp.port = VITE_PORT;
112
+ uiApp.startCommand = `npx vite --port ${VITE_PORT}`;
113
+ }
114
+
115
+ if (editorJson.proxy) {
116
+ editorJson.proxy.controlPort = SERVER_PORT;
117
+ editorJson.proxy.httpPort = SERVER_PORT - 99;
118
+ editorJson.proxy.dbPort = SERVER_PORT - 98;
119
+ }
120
+ fs.writeFileSync(editorJsonPath, JSON.stringify(editorJson, null, 2));
121
+ }
122
+
123
+ // Rebuild the Rust binary so code changes are picked up on restart.
124
+ // This script is only invoked from a source checkout (via `npm run editor`),
125
+ // so Cargo.toml and the crates tree are always present.
126
+ const buildOk = buildBinary();
127
+ // Refresh <cargo_bin>/codeyam-editor (and the pty-broker hardlink) so
128
+ // the broker auto-spawn execs a binary that knows the `pty-broker
129
+ // daemon` subcommand. Skip if the build failed — installing a stale
130
+ // debug binary over a working install is worse than leaving it.
131
+ if (buildOk) {
132
+ installBinary();
133
+ }
134
+
28
135
  // Build the UI so the backend can serve it as static files
29
136
  console.error("Building UI...");
30
137
  try {
@@ -37,10 +144,17 @@ async function main() {
37
144
  }
38
145
 
39
146
  // Start the Rust backend — it serves ui/dist/ and launches the Vite dev
40
- // server (configured via startCommand in editor.json) for Live Preview HMR
41
- const serverChild = shouldRestart
42
- ? await restartServer(SERVER_PORT)
43
- : await ensureServer(SERVER_PORT);
147
+ // server (configured via startCommand in editor.json) for Live Preview HMR.
148
+ // On --restart: stop the existing server first, sweep stale artifacts while
149
+ // target/ is idle (no process holding files open), then start fresh.
150
+ if (shouldRestart && (await isPortInUse(SERVER_PORT))) {
151
+ console.error(`Stopping existing server on port ${SERVER_PORT}...`);
152
+ await stopServer(SERVER_PORT);
153
+ }
154
+ if (shouldRestart) {
155
+ sweepStaleArtifacts();
156
+ }
157
+ const serverChild = await ensureServer(SERVER_PORT);
44
158
 
45
159
  // Wait for the actual Vite dev server on the internal port (not the reverse
46
160
  // proxy on VITE_PORT, which starts immediately with the Rust binary).
@@ -59,9 +173,19 @@ async function main() {
59
173
 
60
174
  process.on("SIGINT", () => {
61
175
  clearInterval(keepAlive);
62
- if (serverChild) serverChild.kill("SIGINT");
176
+ killChildProcess(serverChild);
63
177
  process.exit(0);
64
178
  });
65
179
  }
66
180
 
67
- main();
181
+ module.exports = {
182
+ buildBinary,
183
+ cargoBinDir,
184
+ installBinary,
185
+ main,
186
+ sweepStaleArtifacts,
187
+ };
188
+
189
+ if (require.main === module) {
190
+ main();
191
+ }