@lark-apaas/coding-template-nestjs-react-fullstack 0.1.36-alpha.20260904132704 → 0.1.36-alpha.20260904154046
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/package.json +1 -1
- package/template/scripts/dev.js +27 -87
package/package.json
CHANGED
package/template/scripts/dev.js
CHANGED
|
@@ -32,10 +32,9 @@ loadEnv();
|
|
|
32
32
|
|
|
33
33
|
// ── Configuration ─────────────────────────────────────────────────────────────
|
|
34
34
|
const LOG_DIR = process.env.LOG_DIR || 'logs';
|
|
35
|
-
const MAX_RESTART_COUNT =
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
: Infinity;
|
|
35
|
+
const MAX_RESTART_COUNT = process.env.MAX_RESTART_COUNT != null && process.env.MAX_RESTART_COUNT !== ''
|
|
36
|
+
? parseInt(process.env.MAX_RESTART_COUNT, 10)
|
|
37
|
+
: Infinity;
|
|
39
38
|
const RESTART_DELAY = parseInt(process.env.RESTART_DELAY, 10) || 2;
|
|
40
39
|
const MAX_DELAY = 8;
|
|
41
40
|
const SERVER_PORT = process.env.SERVER_PORT || '3000';
|
|
@@ -52,37 +51,26 @@ const devLogFd = fs.openSync(devLogPath, 'a');
|
|
|
52
51
|
function timestamp() {
|
|
53
52
|
const now = new Date();
|
|
54
53
|
return (
|
|
55
|
-
now.getFullYear() +
|
|
56
|
-
'-' +
|
|
57
|
-
String(now.
|
|
58
|
-
'
|
|
59
|
-
String(now.
|
|
60
|
-
' ' +
|
|
61
|
-
String(now.getHours()).padStart(2, '0') +
|
|
62
|
-
':' +
|
|
63
|
-
String(now.getMinutes()).padStart(2, '0') +
|
|
64
|
-
':' +
|
|
54
|
+
now.getFullYear() + '-' +
|
|
55
|
+
String(now.getMonth() + 1).padStart(2, '0') + '-' +
|
|
56
|
+
String(now.getDate()).padStart(2, '0') + ' ' +
|
|
57
|
+
String(now.getHours()).padStart(2, '0') + ':' +
|
|
58
|
+
String(now.getMinutes()).padStart(2, '0') + ':' +
|
|
65
59
|
String(now.getSeconds()).padStart(2, '0')
|
|
66
60
|
);
|
|
67
61
|
}
|
|
68
62
|
|
|
69
63
|
/** Write to terminal + dev.std.log */
|
|
70
64
|
function writeOutput(msg) {
|
|
71
|
-
try {
|
|
72
|
-
|
|
73
|
-
} catch {}
|
|
74
|
-
try {
|
|
75
|
-
fs.writeSync(devStdLogFd, msg);
|
|
76
|
-
} catch {}
|
|
65
|
+
try { process.stdout.write(msg); } catch {}
|
|
66
|
+
try { fs.writeSync(devStdLogFd, msg); } catch {}
|
|
77
67
|
}
|
|
78
68
|
|
|
79
69
|
/** Structured event log → terminal + dev.std.log + dev.log */
|
|
80
70
|
function logEvent(level, name, message) {
|
|
81
71
|
const msg = `[${timestamp()}] [${level}] [${name}] ${message}\n`;
|
|
82
72
|
writeOutput(msg);
|
|
83
|
-
try {
|
|
84
|
-
fs.writeSync(devLogFd, msg);
|
|
85
|
-
} catch {}
|
|
73
|
+
try { fs.writeSync(devLogFd, msg); } catch {}
|
|
86
74
|
}
|
|
87
75
|
|
|
88
76
|
// ── Process group management ──────────────────────────────────────────────────
|
|
@@ -94,16 +82,11 @@ function killProcessGroup(pid, signal) {
|
|
|
94
82
|
|
|
95
83
|
function killOrphansByPort(port) {
|
|
96
84
|
try {
|
|
97
|
-
const pids = execSync(`lsof -ti :${port}`, {
|
|
98
|
-
encoding: 'utf8',
|
|
99
|
-
timeout: 5000,
|
|
100
|
-
}).trim();
|
|
85
|
+
const pids = execSync(`lsof -ti :${port}`, { encoding: 'utf8', timeout: 5000 }).trim();
|
|
101
86
|
if (pids) {
|
|
102
87
|
const pidList = pids.split('\n').filter(Boolean);
|
|
103
88
|
for (const p of pidList) {
|
|
104
|
-
try {
|
|
105
|
-
process.kill(parseInt(p, 10), 'SIGKILL');
|
|
106
|
-
} catch {}
|
|
89
|
+
try { process.kill(parseInt(p, 10), 'SIGKILL'); } catch {}
|
|
107
90
|
}
|
|
108
91
|
return pidList;
|
|
109
92
|
}
|
|
@@ -146,23 +129,14 @@ function startProcess({ name, command, args, cleanupPort }) {
|
|
|
146
129
|
entry.child = child;
|
|
147
130
|
|
|
148
131
|
const startTime = Date.now();
|
|
149
|
-
logEvent(
|
|
150
|
-
'INFO',
|
|
151
|
-
name,
|
|
152
|
-
`Process started (PGID: ${child.pid}): ${command} ${args.join(' ')}`,
|
|
153
|
-
);
|
|
132
|
+
logEvent('INFO', name, `Process started (PGID: ${child.pid}): ${command} ${args.join(' ')}`);
|
|
154
133
|
|
|
155
134
|
// Pipe stdout and stderr through readline for timestamped logging
|
|
156
135
|
const pipeLines = (stream) => {
|
|
157
|
-
const rl = readline.createInterface({
|
|
158
|
-
input: stream,
|
|
159
|
-
crlfDelay: Infinity,
|
|
160
|
-
});
|
|
136
|
+
const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
|
|
161
137
|
rl.on('line', (line) => {
|
|
162
138
|
const msg = `[${timestamp()}] [${name}] ${line}\n`;
|
|
163
|
-
try {
|
|
164
|
-
fs.writeSync(logFd, msg);
|
|
165
|
-
} catch {}
|
|
139
|
+
try { fs.writeSync(logFd, msg); } catch {}
|
|
166
140
|
writeOutput(msg);
|
|
167
141
|
});
|
|
168
142
|
};
|
|
@@ -191,11 +165,7 @@ function startProcess({ name, command, args, cleanupPort }) {
|
|
|
191
165
|
if (cleanupPort) {
|
|
192
166
|
const orphans = killOrphansByPort(cleanupPort);
|
|
193
167
|
if (orphans.length > 0) {
|
|
194
|
-
logEvent(
|
|
195
|
-
'WARN',
|
|
196
|
-
name,
|
|
197
|
-
`Killed orphan processes on port ${cleanupPort}: ${orphans.join(' ')}`,
|
|
198
|
-
);
|
|
168
|
+
logEvent('WARN', name, `Killed orphan processes on port ${cleanupPort}: ${orphans.join(' ')}`);
|
|
199
169
|
await sleep(500);
|
|
200
170
|
}
|
|
201
171
|
}
|
|
@@ -205,38 +175,21 @@ function startProcess({ name, command, args, cleanupPort }) {
|
|
|
205
175
|
const runDuration = (Date.now() - startTime) / 1000;
|
|
206
176
|
if (runDuration >= 60) {
|
|
207
177
|
restartCount = 0;
|
|
208
|
-
logEvent(
|
|
209
|
-
'INFO',
|
|
210
|
-
name,
|
|
211
|
-
`Ran for ${Math.round(runDuration)}s, resetting restart counter`,
|
|
212
|
-
);
|
|
178
|
+
logEvent('INFO', name, `Ran for ${Math.round(runDuration)}s, resetting restart counter`);
|
|
213
179
|
} else {
|
|
214
180
|
restartCount++;
|
|
215
181
|
}
|
|
216
182
|
if (restartCount >= MAX_RESTART_COUNT) {
|
|
217
|
-
logEvent(
|
|
218
|
-
'ERROR',
|
|
219
|
-
name,
|
|
220
|
-
`Max restart count (${MAX_RESTART_COUNT}) reached, giving up`,
|
|
221
|
-
);
|
|
183
|
+
logEvent('ERROR', name, `Max restart count (${MAX_RESTART_COUNT}) reached, giving up`);
|
|
222
184
|
break;
|
|
223
185
|
}
|
|
224
186
|
|
|
225
|
-
const delay = Math.min(
|
|
226
|
-
|
|
227
|
-
MAX_DELAY,
|
|
228
|
-
);
|
|
229
|
-
logEvent(
|
|
230
|
-
'WARN',
|
|
231
|
-
name,
|
|
232
|
-
`Process exited with code ${exitCode}, restarting (${restartCount}/${MAX_RESTART_COUNT}) in ${delay}s...`,
|
|
233
|
-
);
|
|
187
|
+
const delay = Math.min(RESTART_DELAY * (1 << Math.max(0, restartCount - 1)), MAX_DELAY);
|
|
188
|
+
logEvent('WARN', name, `Process exited with code ${exitCode}, restarting (${restartCount}/${MAX_RESTART_COUNT}) in ${delay}s...`);
|
|
234
189
|
await sleep(delay * 1000);
|
|
235
190
|
}
|
|
236
191
|
|
|
237
|
-
try {
|
|
238
|
-
fs.closeSync(logFd);
|
|
239
|
-
} catch {}
|
|
192
|
+
try { fs.closeSync(logFd); } catch {}
|
|
240
193
|
};
|
|
241
194
|
|
|
242
195
|
return run();
|
|
@@ -266,11 +219,7 @@ async function cleanup() {
|
|
|
266
219
|
// Force kill any remaining
|
|
267
220
|
for (const entry of managedProcesses) {
|
|
268
221
|
if (entry.pid) {
|
|
269
|
-
logEvent(
|
|
270
|
-
'WARN',
|
|
271
|
-
'main',
|
|
272
|
-
`Force killing process group (PGID: ${entry.pid})`,
|
|
273
|
-
);
|
|
222
|
+
logEvent('WARN', 'main', `Force killing process group (PGID: ${entry.pid})`);
|
|
274
223
|
killProcessGroup(entry.pid, 'SIGKILL');
|
|
275
224
|
}
|
|
276
225
|
}
|
|
@@ -281,12 +230,8 @@ async function cleanup() {
|
|
|
281
230
|
|
|
282
231
|
logEvent('INFO', 'main', 'All processes stopped');
|
|
283
232
|
|
|
284
|
-
try {
|
|
285
|
-
|
|
286
|
-
} catch {}
|
|
287
|
-
try {
|
|
288
|
-
fs.closeSync(devLogFd);
|
|
289
|
-
} catch {}
|
|
233
|
+
try { fs.closeSync(devStdLogFd); } catch {}
|
|
234
|
+
try { fs.closeSync(devLogFd); } catch {}
|
|
290
235
|
|
|
291
236
|
process.exit(0);
|
|
292
237
|
}
|
|
@@ -313,15 +258,10 @@ async function main() {
|
|
|
313
258
|
// Initialize action plugins
|
|
314
259
|
writeOutput('\n🔌 Initializing action plugins...\n');
|
|
315
260
|
try {
|
|
316
|
-
execSync('fullstack-cli action-plugin init', {
|
|
317
|
-
cwd: PROJECT_ROOT,
|
|
318
|
-
stdio: 'inherit',
|
|
319
|
-
});
|
|
261
|
+
execSync('fullstack-cli action-plugin init', { cwd: PROJECT_ROOT, stdio: 'inherit' });
|
|
320
262
|
writeOutput('✅ Action plugins initialized\n\n');
|
|
321
263
|
} catch {
|
|
322
|
-
writeOutput(
|
|
323
|
-
'⚠️ Action plugin initialization failed, continuing anyway...\n\n',
|
|
324
|
-
);
|
|
264
|
+
writeOutput('⚠️ Action plugin initialization failed, continuing anyway...\n\n');
|
|
325
265
|
}
|
|
326
266
|
|
|
327
267
|
// Start server and client
|