@lark-apaas/fullstack-cli 1.1.61-alpha.20260818172555 → 1.1.61-alpha.20260819082510
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/dist/index.js +168 -3482
- package/package.json +1 -4
- package/templates/scripts/dev.js +79 -429
- package/templates/scripts/dev.sh +0 -2
- package/templates/tsconfig.dev.json +7 -0
- package/dist/client-dependency-graph.d.ts +0 -37
- package/dist/client-dependency-graph.js +0 -466
- package/templates/scripts/cache-generation-preflight.mjs +0 -1346
- package/templates/scripts/cache-generation-prune.mjs +0 -174
- package/templates/scripts/cache-runtime-coordinator.mjs +0 -487
- package/templates/scripts/patch-vite-dependency-graph-hash.mjs +0 -141
- package/templates/scripts/server-cache-module-resolver.cjs +0 -63
- package/templates/scripts/server-cache-runtime.mjs +0 -681
- package/templates/scripts/server-startup-runtime.mjs +0 -496
- package/templates/scripts/server-transition-runtime.mjs +0 -655
- package/templates/scripts/vite-cache-runtime.mjs +0 -3100
- package/templates/scripts/workspace-client-runtime.mjs +0 -176
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/fullstack-cli",
|
|
3
|
-
"version": "1.1.61-alpha.
|
|
3
|
+
"version": "1.1.61-alpha.20260819082510",
|
|
4
4
|
"description": "CLI tool for fullstack template management",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -34,15 +34,12 @@
|
|
|
34
34
|
"@lark-apaas/http-client": "^0.1.7",
|
|
35
35
|
"@lydell/node-pty": "1.1.0",
|
|
36
36
|
"@vercel/nft": "^0.30.3",
|
|
37
|
-
"acorn": "^8.15.0",
|
|
38
|
-
"acorn-walk": "^8.3.4",
|
|
39
37
|
"commander": "^13.0.0",
|
|
40
38
|
"debug": "^4.4.3",
|
|
41
39
|
"dotenv": "^16.0.0",
|
|
42
40
|
"drizzle-kit": "0.31.5",
|
|
43
41
|
"drizzle-orm": "0.44.6",
|
|
44
42
|
"es-toolkit": "^1.44.0",
|
|
45
|
-
"esbuild": "^0.25.0",
|
|
46
43
|
"inflection": "^3.0.2",
|
|
47
44
|
"pinyin-pro": "^3.27.0",
|
|
48
45
|
"postgres": "^3.4.3",
|
package/templates/scripts/dev.js
CHANGED
|
@@ -3,15 +3,12 @@
|
|
|
3
3
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
|
-
const { spawn, execSync
|
|
6
|
+
const { spawn, execSync } = require('child_process');
|
|
7
7
|
const readline = require('readline');
|
|
8
8
|
|
|
9
9
|
// ── Project root ──────────────────────────────────────────────────────────────
|
|
10
10
|
const PROJECT_ROOT = path.resolve(__dirname, '..');
|
|
11
11
|
process.chdir(PROJECT_ROOT);
|
|
12
|
-
// Runtime scripts may be image-owned and therefore live outside the workspace.
|
|
13
|
-
// Keep the project identity explicit for validation and all supervised children.
|
|
14
|
-
process.env.MIAODA_WORKSPACE_ROOT ||= PROJECT_ROOT;
|
|
15
12
|
|
|
16
13
|
// ── Load .env ─────────────────────────────────────────────────────────────────
|
|
17
14
|
function loadEnv() {
|
|
@@ -34,73 +31,13 @@ loadEnv();
|
|
|
34
31
|
|
|
35
32
|
// ── Configuration ─────────────────────────────────────────────────────────────
|
|
36
33
|
const LOG_DIR = process.env.LOG_DIR || 'logs';
|
|
37
|
-
const MAX_RESTART_COUNT =
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
: Infinity;
|
|
34
|
+
const MAX_RESTART_COUNT = process.env.MAX_RESTART_COUNT != null && process.env.MAX_RESTART_COUNT !== ''
|
|
35
|
+
? parseInt(process.env.MAX_RESTART_COUNT, 10)
|
|
36
|
+
: Infinity;
|
|
41
37
|
const RESTART_DELAY = parseInt(process.env.RESTART_DELAY, 10) || 2;
|
|
42
38
|
const MAX_DELAY = 8;
|
|
43
39
|
const SERVER_PORT = process.env.SERVER_PORT || '3000';
|
|
44
40
|
const CLIENT_DEV_PORT = process.env.CLIENT_DEV_PORT || '8080';
|
|
45
|
-
const CACHE_HANDOFF_EXIT_CODE = 75;
|
|
46
|
-
const NEST_ORCHESTRATOR_OWNER_FILE = path.resolve(
|
|
47
|
-
process.env.MIAODA_NEST_ORCHESTRATOR_OWNER_FILE ||
|
|
48
|
-
'/tmp/event/MIAODA_NEST_ORCHESTRATOR_OWNER.json'
|
|
49
|
-
);
|
|
50
|
-
let CACHE_BOOTSTRAP_ENABLED =
|
|
51
|
-
process.env.MIAODA_CACHE_BOOTSTRAP_ENABLED === 'true';
|
|
52
|
-
let STARTUP_EXPERIMENT_ENABLED =
|
|
53
|
-
process.env.MIAODA_STARTUP_EXPERIMENT_ENABLED === 'true';
|
|
54
|
-
const CACHE_RUNTIME_SCRIPTS_ROOT = path.resolve(
|
|
55
|
-
process.env.MIAODA_CACHE_RUNTIME_SCRIPTS_ROOT ||
|
|
56
|
-
path.join(PROJECT_ROOT, 'scripts')
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
function readExternalNestOrchestratorOwner() {
|
|
60
|
-
try {
|
|
61
|
-
const owner = JSON.parse(
|
|
62
|
-
fs.readFileSync(NEST_ORCHESTRATOR_OWNER_FILE, 'utf8')
|
|
63
|
-
);
|
|
64
|
-
const pid = Number(owner.pid);
|
|
65
|
-
if (
|
|
66
|
-
owner.schemaVersion !== 1 ||
|
|
67
|
-
owner.mode !== (process.env.MIAODA_NEST_START_MODE || 'workspace') ||
|
|
68
|
-
!Number.isSafeInteger(pid) ||
|
|
69
|
-
pid <= 1 ||
|
|
70
|
-
pid === process.pid
|
|
71
|
-
) {
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
process.kill(pid, 0);
|
|
75
|
-
return { pid, mode: owner.mode };
|
|
76
|
-
} catch {
|
|
77
|
-
return undefined;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function dependencyHandoffAlreadyCompleted() {
|
|
82
|
-
if (!fs.existsSync(path.join(PROJECT_ROOT, 'node_modules'))) return false;
|
|
83
|
-
try {
|
|
84
|
-
const cacheRoot = path.resolve(
|
|
85
|
-
process.env.MIAODA_CACHE_ROOT ||
|
|
86
|
-
path.join(PROJECT_ROOT, '.miaoda-cache', 'current')
|
|
87
|
-
);
|
|
88
|
-
const readyFile = path.resolve(
|
|
89
|
-
process.env.MIAODA_DEPENDENCY_READY_FILE ||
|
|
90
|
-
path.join(PROJECT_ROOT, '.miaoda-runtime', 'dependencies-ready.json')
|
|
91
|
-
);
|
|
92
|
-
const generation = JSON.parse(
|
|
93
|
-
fs.readFileSync(path.join(cacheRoot, 'generation.json'), 'utf8')
|
|
94
|
-
);
|
|
95
|
-
const ready = JSON.parse(fs.readFileSync(readyFile, 'utf8'));
|
|
96
|
-
return (
|
|
97
|
-
/^[a-f0-9]{64}$/.test(generation.generationId || '') &&
|
|
98
|
-
ready.generationId === generation.generationId
|
|
99
|
-
);
|
|
100
|
-
} catch {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
41
|
|
|
105
42
|
fs.mkdirSync(LOG_DIR, { recursive: true });
|
|
106
43
|
|
|
@@ -113,16 +50,11 @@ const devLogFd = fs.openSync(devLogPath, 'a');
|
|
|
113
50
|
function timestamp() {
|
|
114
51
|
const now = new Date();
|
|
115
52
|
return (
|
|
116
|
-
now.getFullYear() +
|
|
117
|
-
'-' +
|
|
118
|
-
String(now.
|
|
119
|
-
'
|
|
120
|
-
String(now.
|
|
121
|
-
' ' +
|
|
122
|
-
String(now.getHours()).padStart(2, '0') +
|
|
123
|
-
':' +
|
|
124
|
-
String(now.getMinutes()).padStart(2, '0') +
|
|
125
|
-
':' +
|
|
53
|
+
now.getFullYear() + '-' +
|
|
54
|
+
String(now.getMonth() + 1).padStart(2, '0') + '-' +
|
|
55
|
+
String(now.getDate()).padStart(2, '0') + ' ' +
|
|
56
|
+
String(now.getHours()).padStart(2, '0') + ':' +
|
|
57
|
+
String(now.getMinutes()).padStart(2, '0') + ':' +
|
|
126
58
|
String(now.getSeconds()).padStart(2, '0')
|
|
127
59
|
);
|
|
128
60
|
}
|
|
@@ -136,33 +68,21 @@ const STDOUT_MAX_INFLIGHT = 1000;
|
|
|
136
68
|
function writeOutput(msg) {
|
|
137
69
|
// File first and synchronously — read-logs reads this file; it must never be gated
|
|
138
70
|
// by the terminal consumer.
|
|
139
|
-
try {
|
|
140
|
-
fs.writeSync(devStdLogFd, msg);
|
|
141
|
-
} catch {}
|
|
71
|
+
try { fs.writeSync(devStdLogFd, msg); } catch {}
|
|
142
72
|
// stdout mirror via async fs.write: if process.stdout is a pty/pipe whose consumer
|
|
143
73
|
// stalls, the block happens on the libuv threadpool, NOT the event loop — so the
|
|
144
74
|
// synchronous log-FILE writes above (and subsequent readline callbacks) keep running.
|
|
145
75
|
// Drop overflow when too many writes are already pending (best-effort mirror).
|
|
146
|
-
if (_stdoutInFlight >= STDOUT_MAX_INFLIGHT) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
76
|
+
if (_stdoutInFlight >= STDOUT_MAX_INFLIGHT) { return; }
|
|
149
77
|
_stdoutInFlight++;
|
|
150
|
-
try {
|
|
151
|
-
fs.write(1, msg, () => {
|
|
152
|
-
_stdoutInFlight--;
|
|
153
|
-
});
|
|
154
|
-
} catch {
|
|
155
|
-
_stdoutInFlight--;
|
|
156
|
-
}
|
|
78
|
+
try { fs.write(1, msg, () => { _stdoutInFlight--; }); } catch { _stdoutInFlight--; }
|
|
157
79
|
}
|
|
158
80
|
|
|
159
81
|
/** Structured event log → terminal + dev.std.log + dev.log */
|
|
160
82
|
function logEvent(level, name, message) {
|
|
161
83
|
const msg = `[${timestamp()}] [${level}] [${name}] ${message}\n`;
|
|
162
84
|
writeOutput(msg);
|
|
163
|
-
try {
|
|
164
|
-
fs.writeSync(devLogFd, msg);
|
|
165
|
-
} catch {}
|
|
85
|
+
try { fs.writeSync(devLogFd, msg); } catch {}
|
|
166
86
|
}
|
|
167
87
|
|
|
168
88
|
// ── Process group management ──────────────────────────────────────────────────
|
|
@@ -174,16 +94,11 @@ function killProcessGroup(pid, signal) {
|
|
|
174
94
|
|
|
175
95
|
function killOrphansByPort(port) {
|
|
176
96
|
try {
|
|
177
|
-
const pids = execSync(`lsof -ti :${port}`, {
|
|
178
|
-
encoding: 'utf8',
|
|
179
|
-
timeout: 5000,
|
|
180
|
-
}).trim();
|
|
97
|
+
const pids = execSync(`lsof -ti :${port}`, { encoding: 'utf8', timeout: 5000 }).trim();
|
|
181
98
|
if (pids) {
|
|
182
99
|
const pidList = pids.split('\n').filter(Boolean);
|
|
183
100
|
for (const p of pidList) {
|
|
184
|
-
try {
|
|
185
|
-
process.kill(parseInt(p, 10), 'SIGKILL');
|
|
186
|
-
} catch {}
|
|
101
|
+
try { process.kill(parseInt(p, 10), 'SIGKILL'); } catch {}
|
|
187
102
|
}
|
|
188
103
|
return pidList;
|
|
189
104
|
}
|
|
@@ -196,23 +111,16 @@ let stopping = false;
|
|
|
196
111
|
const managedProcesses = []; // { name, pid, child }
|
|
197
112
|
|
|
198
113
|
function sleep(ms) {
|
|
199
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
114
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
200
115
|
}
|
|
201
116
|
|
|
202
117
|
/**
|
|
203
118
|
* Start and supervise a process with auto-restart and log piping.
|
|
204
119
|
* Returns a promise that resolves when the process loop ends.
|
|
205
120
|
*/
|
|
206
|
-
function startProcess({
|
|
207
|
-
name,
|
|
208
|
-
command,
|
|
209
|
-
args,
|
|
210
|
-
cleanupPort,
|
|
211
|
-
stopRestartOnExitCodes = [],
|
|
212
|
-
}) {
|
|
121
|
+
function startProcess({ name, command, args, cleanupPort }) {
|
|
213
122
|
const logFilePath = path.join(LOG_DIR, `${name}.std.log`);
|
|
214
123
|
const logFd = fs.openSync(logFilePath, 'a');
|
|
215
|
-
const logicalLogFds = new Map();
|
|
216
124
|
|
|
217
125
|
const entry = { name, pid: null, child: null };
|
|
218
126
|
managedProcesses.push(entry);
|
|
@@ -233,39 +141,14 @@ function startProcess({
|
|
|
233
141
|
entry.child = child;
|
|
234
142
|
|
|
235
143
|
const startTime = Date.now();
|
|
236
|
-
logEvent(
|
|
237
|
-
'INFO',
|
|
238
|
-
name,
|
|
239
|
-
`Process started (PGID: ${child.pid}): ${command} ${args.join(' ')}`
|
|
240
|
-
);
|
|
144
|
+
logEvent('INFO', name, `Process started (PGID: ${child.pid}): ${command} ${args.join(' ')}`);
|
|
241
145
|
|
|
242
146
|
// Pipe stdout and stderr through readline for timestamped logging
|
|
243
|
-
const pipeLines = stream => {
|
|
244
|
-
const rl = readline.createInterface({
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
rl.on('line', line => {
|
|
249
|
-
const tagged = /^MIAODA_LOG\t(client|server)\t(.*)$/.exec(line);
|
|
250
|
-
const logicalName = tagged?.[1] || name;
|
|
251
|
-
const logicalLine = tagged?.[2] ?? line;
|
|
252
|
-
const msg = `[${timestamp()}] [${logicalName}] ${logicalLine}\n`;
|
|
253
|
-
try {
|
|
254
|
-
fs.writeSync(logFd, msg);
|
|
255
|
-
} catch {}
|
|
256
|
-
if (tagged) {
|
|
257
|
-
try {
|
|
258
|
-
let logicalFd = logicalLogFds.get(logicalName);
|
|
259
|
-
if (logicalFd === undefined) {
|
|
260
|
-
logicalFd = fs.openSync(
|
|
261
|
-
path.join(LOG_DIR, `${logicalName}.std.log`),
|
|
262
|
-
'a'
|
|
263
|
-
);
|
|
264
|
-
logicalLogFds.set(logicalName, logicalFd);
|
|
265
|
-
}
|
|
266
|
-
fs.writeSync(logicalFd, msg);
|
|
267
|
-
} catch {}
|
|
268
|
-
}
|
|
147
|
+
const pipeLines = (stream) => {
|
|
148
|
+
const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
|
|
149
|
+
rl.on('line', (line) => {
|
|
150
|
+
const msg = `[${timestamp()}] [${name}] ${line}\n`;
|
|
151
|
+
try { fs.writeSync(logFd, msg); } catch {}
|
|
269
152
|
writeOutput(msg);
|
|
270
153
|
});
|
|
271
154
|
};
|
|
@@ -276,16 +159,15 @@ function startProcess({
|
|
|
276
159
|
// NOTE: must use 'exit', not 'close'. With shell:true, grandchild processes
|
|
277
160
|
// (e.g. nest's server) inherit stdout pipes. 'close' won't fire until ALL
|
|
278
161
|
// pipe holders exit, causing dev.js to hang when npm/nest dies but server survives.
|
|
279
|
-
const exitCode = await new Promise(resolve => {
|
|
280
|
-
child.on('exit', code => resolve(code ?? 1));
|
|
162
|
+
const exitCode = await new Promise((resolve) => {
|
|
163
|
+
child.on('exit', (code) => resolve(code ?? 1));
|
|
281
164
|
child.on('error', () => resolve(1));
|
|
282
165
|
});
|
|
283
|
-
const completedHandoff = stopRestartOnExitCodes.includes(exitCode);
|
|
284
166
|
|
|
285
167
|
// Kill the entire process group
|
|
286
168
|
if (entry.pid) {
|
|
287
169
|
killProcessGroup(entry.pid, 'SIGTERM');
|
|
288
|
-
await sleep(
|
|
170
|
+
await sleep(2000);
|
|
289
171
|
killProcessGroup(entry.pid, 'SIGKILL');
|
|
290
172
|
}
|
|
291
173
|
entry.pid = null;
|
|
@@ -295,74 +177,31 @@ function startProcess({
|
|
|
295
177
|
if (cleanupPort) {
|
|
296
178
|
const orphans = killOrphansByPort(cleanupPort);
|
|
297
179
|
if (orphans.length > 0) {
|
|
298
|
-
logEvent(
|
|
299
|
-
'WARN',
|
|
300
|
-
name,
|
|
301
|
-
`Killed orphan processes on port ${cleanupPort}: ${orphans.join(' ')}`
|
|
302
|
-
);
|
|
180
|
+
logEvent('WARN', name, `Killed orphan processes on port ${cleanupPort}: ${orphans.join(' ')}`);
|
|
303
181
|
await sleep(500);
|
|
304
182
|
}
|
|
305
183
|
}
|
|
306
184
|
|
|
307
185
|
if (stopping) break;
|
|
308
186
|
|
|
309
|
-
if (completedHandoff) {
|
|
310
|
-
logEvent(
|
|
311
|
-
'INFO',
|
|
312
|
-
name,
|
|
313
|
-
`Process completed one-way handoff with code ${exitCode}`
|
|
314
|
-
);
|
|
315
|
-
try {
|
|
316
|
-
fs.closeSync(logFd);
|
|
317
|
-
} catch {}
|
|
318
|
-
for (const logicalFd of logicalLogFds.values()) {
|
|
319
|
-
try {
|
|
320
|
-
fs.closeSync(logicalFd);
|
|
321
|
-
} catch {}
|
|
322
|
-
}
|
|
323
|
-
return exitCode;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
187
|
const runDuration = (Date.now() - startTime) / 1000;
|
|
327
188
|
if (runDuration >= 60) {
|
|
328
189
|
restartCount = 0;
|
|
329
|
-
logEvent(
|
|
330
|
-
'INFO',
|
|
331
|
-
name,
|
|
332
|
-
`Ran for ${Math.round(runDuration)}s, resetting restart counter`
|
|
333
|
-
);
|
|
190
|
+
logEvent('INFO', name, `Ran for ${Math.round(runDuration)}s, resetting restart counter`);
|
|
334
191
|
} else {
|
|
335
192
|
restartCount++;
|
|
336
193
|
}
|
|
337
194
|
if (restartCount >= MAX_RESTART_COUNT) {
|
|
338
|
-
logEvent(
|
|
339
|
-
'ERROR',
|
|
340
|
-
name,
|
|
341
|
-
`Max restart count (${MAX_RESTART_COUNT}) reached, giving up`
|
|
342
|
-
);
|
|
195
|
+
logEvent('ERROR', name, `Max restart count (${MAX_RESTART_COUNT}) reached, giving up`);
|
|
343
196
|
break;
|
|
344
197
|
}
|
|
345
198
|
|
|
346
|
-
const delay = Math.min(
|
|
347
|
-
|
|
348
|
-
MAX_DELAY
|
|
349
|
-
);
|
|
350
|
-
logEvent(
|
|
351
|
-
'WARN',
|
|
352
|
-
name,
|
|
353
|
-
`Process exited with code ${exitCode}, restarting (${restartCount}/${MAX_RESTART_COUNT}) in ${delay}s...`
|
|
354
|
-
);
|
|
199
|
+
const delay = Math.min(RESTART_DELAY * (1 << Math.max(0, restartCount - 1)), MAX_DELAY);
|
|
200
|
+
logEvent('WARN', name, `Process exited with code ${exitCode}, restarting (${restartCount}/${MAX_RESTART_COUNT}) in ${delay}s...`);
|
|
355
201
|
await sleep(delay * 1000);
|
|
356
202
|
}
|
|
357
203
|
|
|
358
|
-
try {
|
|
359
|
-
fs.closeSync(logFd);
|
|
360
|
-
} catch {}
|
|
361
|
-
for (const logicalFd of logicalLogFds.values()) {
|
|
362
|
-
try {
|
|
363
|
-
fs.closeSync(logicalFd);
|
|
364
|
-
} catch {}
|
|
365
|
-
}
|
|
204
|
+
try { fs.closeSync(logFd); } catch {}
|
|
366
205
|
};
|
|
367
206
|
|
|
368
207
|
return run();
|
|
@@ -392,11 +231,7 @@ async function cleanup() {
|
|
|
392
231
|
// Force kill any remaining
|
|
393
232
|
for (const entry of managedProcesses) {
|
|
394
233
|
if (entry.pid) {
|
|
395
|
-
logEvent(
|
|
396
|
-
'WARN',
|
|
397
|
-
'main',
|
|
398
|
-
`Force killing process group (PGID: ${entry.pid})`
|
|
399
|
-
);
|
|
234
|
+
logEvent('WARN', 'main', `Force killing process group (PGID: ${entry.pid})`);
|
|
400
235
|
killProcessGroup(entry.pid, 'SIGKILL');
|
|
401
236
|
}
|
|
402
237
|
}
|
|
@@ -407,12 +242,8 @@ async function cleanup() {
|
|
|
407
242
|
|
|
408
243
|
logEvent('INFO', 'main', 'All processes stopped');
|
|
409
244
|
|
|
410
|
-
try {
|
|
411
|
-
|
|
412
|
-
} catch {}
|
|
413
|
-
try {
|
|
414
|
-
fs.closeSync(devLogFd);
|
|
415
|
-
} catch {}
|
|
245
|
+
try { fs.closeSync(devStdLogFd); } catch {}
|
|
246
|
+
try { fs.closeSync(devLogFd); } catch {}
|
|
416
247
|
|
|
417
248
|
process.exit(0);
|
|
418
249
|
}
|
|
@@ -430,253 +261,72 @@ function cleanStaleDist() {
|
|
|
430
261
|
}
|
|
431
262
|
}
|
|
432
263
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
return startProcess({
|
|
439
|
-
name: 'server',
|
|
440
|
-
command: process.execPath,
|
|
441
|
-
args: [serverStartupRuntimeScript],
|
|
442
|
-
cleanupPort: SERVER_PORT,
|
|
443
|
-
});
|
|
444
|
-
}
|
|
264
|
+
// ── Dev session state (consumed by /dev/health) ───────────────────────────────
|
|
265
|
+
// 记录本次 dev 会话首次拉起 server 的时刻。/dev/health 只用它做「超时兜底」:
|
|
266
|
+
// 超过上限仍未探测到存活的 Nest 进程,才允许判定为终态失败。
|
|
267
|
+
// 写失败一律静默——health 是旁路能力,绝不能反向拖垮 dev 主流程。
|
|
268
|
+
const devStatusPath = path.join(LOG_DIR, 'dev-status.json');
|
|
445
269
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
270
|
+
function writeDevStatus() {
|
|
271
|
+
const tmp = `${devStatusPath}.${process.pid}.tmp`;
|
|
272
|
+
try {
|
|
273
|
+
fs.writeFileSync(
|
|
274
|
+
tmp,
|
|
275
|
+
JSON.stringify({
|
|
276
|
+
schema: 1,
|
|
277
|
+
firstStartedAt: Date.now(),
|
|
278
|
+
supervisorPid: process.pid,
|
|
279
|
+
})
|
|
280
|
+
);
|
|
281
|
+
fs.renameSync(tmp, devStatusPath); // 原子替换,避免读到半个文件
|
|
282
|
+
} catch {
|
|
283
|
+
try { fs.unlinkSync(tmp); } catch {}
|
|
458
284
|
}
|
|
459
|
-
if (stopping) return 0;
|
|
460
|
-
|
|
461
|
-
// If the image-owned orchestrator exits, resume the normal supervised
|
|
462
|
-
// workspace service. By this point node_modules is expected to be ready; if
|
|
463
|
-
// it is not, server-startup-runtime keeps the original bounded wait.
|
|
464
|
-
logEvent(
|
|
465
|
-
'WARN',
|
|
466
|
-
'server',
|
|
467
|
-
`Image-owned Nest orchestrator pid=${owner.pid} exited; starting local supervisor`
|
|
468
|
-
);
|
|
469
|
-
return startProcess({
|
|
470
|
-
name: 'server',
|
|
471
|
-
command: process.execPath,
|
|
472
|
-
args: [serverStartupRuntimeScript],
|
|
473
|
-
cleanupPort: SERVER_PORT,
|
|
474
|
-
});
|
|
475
285
|
}
|
|
476
286
|
|
|
477
287
|
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
478
288
|
async function main() {
|
|
479
289
|
logEvent('INFO', 'main', '========== Dev session started ==========');
|
|
480
290
|
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
logEvent(
|
|
484
|
-
'INFO',
|
|
485
|
-
'main',
|
|
486
|
-
'Using same-origin HMR routing; ignored the dedicated MIAODA_WS_HOST'
|
|
487
|
-
);
|
|
488
|
-
}
|
|
291
|
+
cleanStaleDist();
|
|
292
|
+
writeDevStatus();
|
|
489
293
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
'
|
|
497
|
-
);
|
|
498
|
-
const workspaceClientRuntimeScript = path.join(
|
|
499
|
-
CACHE_RUNTIME_SCRIPTS_ROOT,
|
|
500
|
-
'workspace-client-runtime.mjs'
|
|
501
|
-
);
|
|
502
|
-
if (
|
|
503
|
-
CACHE_BOOTSTRAP_ENABLED &&
|
|
504
|
-
!STARTUP_EXPERIMENT_ENABLED &&
|
|
505
|
-
dependencyHandoffAlreadyCompleted()
|
|
506
|
-
) {
|
|
507
|
-
CACHE_BOOTSTRAP_ENABLED = false;
|
|
508
|
-
process.env.MIAODA_CACHE_BOOTSTRAP_ENABLED = 'false';
|
|
509
|
-
logEvent(
|
|
510
|
-
'INFO',
|
|
511
|
-
'main',
|
|
512
|
-
'Dependency handoff already completed; starting original services directly'
|
|
513
|
-
);
|
|
514
|
-
}
|
|
515
|
-
if (STARTUP_EXPERIMENT_ENABLED) {
|
|
516
|
-
try {
|
|
517
|
-
for (const requiredPath of [
|
|
518
|
-
serverStartupRuntimeScript,
|
|
519
|
-
workspaceClientRuntimeScript,
|
|
520
|
-
]) {
|
|
521
|
-
if (!fs.existsSync(requiredPath)) {
|
|
522
|
-
throw new Error(
|
|
523
|
-
`MIAODA_STARTUP_EXPERIMENT_INPUT_MISSING: ${requiredPath}`
|
|
524
|
-
);
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
} catch (error) {
|
|
528
|
-
STARTUP_EXPERIMENT_ENABLED = false;
|
|
529
|
-
process.env.MIAODA_STARTUP_EXPERIMENT_ENABLED = 'false';
|
|
530
|
-
logEvent(
|
|
531
|
-
'WARN',
|
|
532
|
-
'main',
|
|
533
|
-
`Startup experiment unavailable; using original services: ${error.message}`
|
|
534
|
-
);
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
if (CACHE_BOOTSTRAP_ENABLED && !STARTUP_EXPERIMENT_ENABLED) {
|
|
538
|
-
process.env.MIAODA_CACHE_VALIDATION_RECEIPT_FILE ||= path.join(
|
|
539
|
-
PROJECT_ROOT,
|
|
540
|
-
'.miaoda-runtime',
|
|
541
|
-
'cache-validation-receipt.json'
|
|
542
|
-
);
|
|
543
|
-
try {
|
|
544
|
-
for (const requiredPath of [coordinatorRuntimeScript]) {
|
|
545
|
-
if (!fs.existsSync(requiredPath)) {
|
|
546
|
-
throw new Error(
|
|
547
|
-
`MIAODA_CACHE_BOOTSTRAP_INPUT_MISSING: ${requiredPath}`
|
|
548
|
-
);
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
execFileSync(
|
|
552
|
-
process.execPath,
|
|
553
|
-
[coordinatorRuntimeScript, '--validate-only'],
|
|
554
|
-
{
|
|
555
|
-
cwd: PROJECT_ROOT,
|
|
556
|
-
env: { ...process.env },
|
|
557
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
558
|
-
}
|
|
559
|
-
);
|
|
560
|
-
} catch (error) {
|
|
561
|
-
CACHE_BOOTSTRAP_ENABLED = false;
|
|
562
|
-
const reason = error?.stderr?.toString().trim() || error.message;
|
|
563
|
-
logEvent(
|
|
564
|
-
'WARN',
|
|
565
|
-
'main',
|
|
566
|
-
`Cache generation preflight rejected; using dependency-backed startup: ${reason}`
|
|
567
|
-
);
|
|
568
|
-
}
|
|
294
|
+
// Initialize action plugins
|
|
295
|
+
writeOutput('\n🔌 Initializing action plugins...\n');
|
|
296
|
+
try {
|
|
297
|
+
execSync('fullstack-cli action-plugin init', { cwd: PROJECT_ROOT, stdio: 'inherit' });
|
|
298
|
+
writeOutput('✅ Action plugins initialized\n\n');
|
|
299
|
+
} catch {
|
|
300
|
+
writeOutput('⚠️ Action plugin initialization failed, continuing anyway...\n\n');
|
|
569
301
|
}
|
|
570
302
|
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
stdio: 'inherit',
|
|
579
|
-
});
|
|
580
|
-
writeOutput('✅ Action plugins initialized\n\n');
|
|
581
|
-
} catch {
|
|
582
|
-
writeOutput(
|
|
583
|
-
'⚠️ Action plugin initialization failed, continuing anyway...\n\n'
|
|
584
|
-
);
|
|
585
|
-
}
|
|
586
|
-
};
|
|
303
|
+
// Start server and client
|
|
304
|
+
const serverPromise = startProcess({
|
|
305
|
+
name: 'server',
|
|
306
|
+
command: 'npm',
|
|
307
|
+
args: ['run', 'dev:server'],
|
|
308
|
+
cleanupPort: SERVER_PORT,
|
|
309
|
+
});
|
|
587
310
|
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
}
|
|
594
|
-
logEvent(
|
|
595
|
-
'INFO',
|
|
596
|
-
'main',
|
|
597
|
-
'Skipped dynamic Action Plugin initialization in cache-only mode'
|
|
598
|
-
);
|
|
599
|
-
}
|
|
311
|
+
const clientPromise = startProcess({
|
|
312
|
+
name: 'client',
|
|
313
|
+
command: 'npm',
|
|
314
|
+
args: ['run', 'dev:client'],
|
|
315
|
+
cleanupPort: CLIENT_DEV_PORT,
|
|
316
|
+
});
|
|
600
317
|
|
|
601
318
|
writeOutput(`📋 Dev processes running. Press Ctrl+C to stop.\n`);
|
|
602
319
|
writeOutput(`📄 Logs: ${devStdLogPath}\n\n`);
|
|
603
320
|
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
'INFO',
|
|
607
|
-
'main',
|
|
608
|
-
`Starting normal workspace Vite with Nest mode ${
|
|
609
|
-
process.env.MIAODA_NEST_START_MODE || 'workspace'
|
|
610
|
-
}`
|
|
611
|
-
);
|
|
612
|
-
const serverPromise =
|
|
613
|
-
(process.env.MIAODA_NEST_START_MODE || 'workspace') ===
|
|
614
|
-
'bundle-handoff'
|
|
615
|
-
? followExternalNestOrchestratorOrStartLocal(
|
|
616
|
-
serverStartupRuntimeScript
|
|
617
|
-
)
|
|
618
|
-
: startProcess({
|
|
619
|
-
name: 'server',
|
|
620
|
-
command: process.execPath,
|
|
621
|
-
args: [serverStartupRuntimeScript],
|
|
622
|
-
cleanupPort: SERVER_PORT,
|
|
623
|
-
});
|
|
624
|
-
const clientPromise = startProcess({
|
|
625
|
-
name: 'client',
|
|
626
|
-
command: process.execPath,
|
|
627
|
-
args: [workspaceClientRuntimeScript],
|
|
628
|
-
cleanupPort: CLIENT_DEV_PORT,
|
|
629
|
-
});
|
|
630
|
-
await Promise.all([serverPromise, clientPromise]);
|
|
631
|
-
} else if (CACHE_BOOTSTRAP_ENABLED) {
|
|
632
|
-
const exitCode = await startProcess({
|
|
633
|
-
name: 'coordinator',
|
|
634
|
-
command: process.execPath,
|
|
635
|
-
args: [coordinatorRuntimeScript],
|
|
636
|
-
stopRestartOnExitCodes: [CACHE_HANDOFF_EXIT_CODE],
|
|
637
|
-
});
|
|
638
|
-
if (exitCode === CACHE_HANDOFF_EXIT_CODE && !stopping) {
|
|
639
|
-
CACHE_BOOTSTRAP_ENABLED = false;
|
|
640
|
-
process.env.MIAODA_CACHE_BOOTSTRAP_ENABLED = 'false';
|
|
641
|
-
process.env.NODE_PATH = '';
|
|
642
|
-
logEvent(
|
|
643
|
-
'INFO',
|
|
644
|
-
'main',
|
|
645
|
-
'Dependencies restored; cache runtime retired before original services start'
|
|
646
|
-
);
|
|
647
|
-
initializeActionPlugins();
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
if (
|
|
652
|
-
!STARTUP_EXPERIMENT_ENABLED &&
|
|
653
|
-
!CACHE_BOOTSTRAP_ENABLED &&
|
|
654
|
-
!stopping
|
|
655
|
-
) {
|
|
656
|
-
// Feature-off and post-cache handoff keep the original process boundary
|
|
657
|
-
// and exact npm commands. Failures therefore surface through the original
|
|
658
|
-
// Preview starting/failed/retry UI instead of a cached-page fallback.
|
|
659
|
-
const serverPromise = startProcess({
|
|
660
|
-
name: 'server',
|
|
661
|
-
command: 'npm',
|
|
662
|
-
args: ['run', 'dev:server'],
|
|
663
|
-
cleanupPort: SERVER_PORT,
|
|
664
|
-
});
|
|
665
|
-
const clientPromise = startProcess({
|
|
666
|
-
name: 'client',
|
|
667
|
-
command: 'npm',
|
|
668
|
-
args: ['run', 'dev:client'],
|
|
669
|
-
cleanupPort: CLIENT_DEV_PORT,
|
|
670
|
-
});
|
|
671
|
-
await Promise.all([serverPromise, clientPromise]);
|
|
672
|
-
}
|
|
321
|
+
// Wait for both (they loop until stopping or max restarts)
|
|
322
|
+
await Promise.all([serverPromise, clientPromise]);
|
|
673
323
|
|
|
674
324
|
if (!cleanupDone) {
|
|
675
325
|
await cleanup();
|
|
676
326
|
}
|
|
677
327
|
}
|
|
678
328
|
|
|
679
|
-
main().catch(err => {
|
|
329
|
+
main().catch((err) => {
|
|
680
330
|
console.error('Fatal error:', err);
|
|
681
331
|
process.exit(1);
|
|
682
332
|
});
|