@lark-apaas/fullstack-cli 1.1.62-alpha.20260821103740 → 1.1.62-alpha.20260824081932
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 +1 -1
- package/package.json +1 -1
- package/templates/.gitignore.append +0 -3
- package/templates/scripts/dev.js +2 -1
- package/templates/scripts/dev.sh +9 -5
- package/templates/scripts/lib/__test__/preserve-dev-cache.test.ts +169 -25
- package/templates/scripts/lib/preserve-dev-cache.cjs +61 -5
- package/templates/scripts/lint.js +40 -2
package/dist/index.js
CHANGED
|
@@ -7698,7 +7698,7 @@ function sanitizeStructuredLog(value) {
|
|
|
7698
7698
|
delete sanitized.pid;
|
|
7699
7699
|
return sanitized;
|
|
7700
7700
|
}
|
|
7701
|
-
var TRANSIENT_CONNECTION_ERROR_PATTERN = /ECONNREFUSED|ECONNRESET|ETIMEDOUT|ENETUNREACH|proxy error|\[Proxy\] (?:Error:\s*$|Error during|Connection error|Non-connection error|Headers already sent|Service (?:recovered|did not recover))/i;
|
|
7701
|
+
var TRANSIENT_CONNECTION_ERROR_PATTERN = /ECONNREFUSED|ECONNRESET|ETIMEDOUT|ENETUNREACH|socket hang up|proxy error|\[Proxy\] (?:Error:\s*$|Error during|Connection error|Non-connection error|Headers already sent|Service (?:recovered|did not recover))/i;
|
|
7702
7702
|
function hasErrorInStdLines(lines) {
|
|
7703
7703
|
const filtered = lines.filter((line) => !TRANSIENT_CONNECTION_ERROR_PATTERN.test(line));
|
|
7704
7704
|
const combined = filtered.join("\n");
|
package/package.json
CHANGED
package/templates/scripts/dev.js
CHANGED
|
@@ -257,7 +257,8 @@ process.on('SIGHUP', cleanup);
|
|
|
257
257
|
// 后续启动 (agent 主动触发 npm run dev) → 全清 dist/, 让模型面对新鲜产物避免 stale
|
|
258
258
|
// 依赖升级 (lockfile hash 变) → 同后续启动 (清 dist), log 里点出 "deps changed"
|
|
259
259
|
// FORCE_CLEAN_DIST=true → escape hatch, 强清
|
|
260
|
-
//
|
|
260
|
+
// marker 放 os.tmpdir()/.miaoda-dev-boot (跟沙箱容器 lifecycle 同, 沙箱重启会清空,
|
|
261
|
+
// 语义对齐"沙箱首次启动"); 具体逻辑抽到 lib/preserve-dev-cache.cjs 便于单测。
|
|
261
262
|
const { preserveDevCache } = require('./lib/preserve-dev-cache.cjs');
|
|
262
263
|
|
|
263
264
|
// ── Main ──────────────────────────────────────────────────────────────────────
|
package/templates/scripts/dev.sh
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
# `npm run dev` 入口;按
|
|
3
|
-
#
|
|
2
|
+
# `npm run dev` 入口;按 MIAODA_DEP_CACHE_DIR 是否非空判断运行环境(与 miaoda-cli
|
|
3
|
+
# isSandboxEnv() 同口径):
|
|
4
|
+
# - 非空(沙箱平台注入的依赖缓存目录)→ 直接跑 dev.js
|
|
4
5
|
# (保活 / restart loop / 文件日志 —— 沙箱生产形态)。脚本同步由平台 pod 启动阶段做过,
|
|
5
6
|
# dev 入口不再额外 `npm run upgrade`。
|
|
6
7
|
# - 否则(本地)→ 走 miaoda app sync 兜底 + 跑 dev-local.js:纯 stdout、崩了就崩、Agent 友好。
|
|
7
|
-
# 显式想跑本地路径可用 `npm run dev:local
|
|
8
|
+
# 显式想跑本地路径可用 `npm run dev:local`(绕过本判断)。
|
|
9
|
+
#
|
|
10
|
+
# 不用 SANDBOX_ID:它由 sandbox_console 在绑定沙箱时才写入 .force/environment/env,预热池实例
|
|
11
|
+
# 在绑定前拿不到,会被误判成本地。保留为附加条件只为兼容,不影响判定结果。
|
|
8
12
|
set -euo pipefail
|
|
9
13
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
10
14
|
|
|
11
|
-
if [ -n "${SANDBOX_ID:-}" ]; then
|
|
15
|
+
if [ -n "${MIAODA_DEP_CACHE_DIR:-}" ] || [ -n "${SANDBOX_ID:-}" ]; then
|
|
12
16
|
exec node "$SCRIPT_DIR/dev.js" "$@"
|
|
13
17
|
fi
|
|
14
18
|
|
|
@@ -18,7 +22,7 @@ if [ ! -f "$SCRIPT_DIR/dev-local.js" ]; then
|
|
|
18
22
|
fi
|
|
19
23
|
|
|
20
24
|
# 本地启动前先跑一次 miaoda app sync:同步 platform-controlled 内容 + 升 @lark-apaas/* 到
|
|
21
|
-
# latest + 迁移老 npm scripts
|
|
25
|
+
# latest + 迁移老 npm scripts。沙箱不走这里(上面的沙箱分支已经 exec return)。
|
|
22
26
|
npx -y @lark-apaas/miaoda-cli@latest app sync || echo "[dev] miaoda app sync 失败,按现状继续" >&2
|
|
23
27
|
|
|
24
28
|
exec node "$SCRIPT_DIR/dev-local.js" "$@"
|
|
@@ -17,13 +17,13 @@ function makeLogger(entries: LogEntry[]) {
|
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const MARKER_FILE = '.miaoda-dev-boot';
|
|
21
|
-
|
|
22
20
|
describe('preserveDevCache', () => {
|
|
23
21
|
let tmpDir: string;
|
|
22
|
+
let markerPath: string;
|
|
24
23
|
|
|
25
24
|
beforeEach(() => {
|
|
26
25
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'preserve-dev-cache-'));
|
|
26
|
+
markerPath = path.join(tmpDir, '.miaoda-dev-boot-marker');
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
afterEach(() => {
|
|
@@ -43,11 +43,12 @@ describe('preserveDevCache', () => {
|
|
|
43
43
|
const r = preserveDevCache(tmpDir, {
|
|
44
44
|
log: makeLogger(logs),
|
|
45
45
|
forceClean: true,
|
|
46
|
+
markerPath,
|
|
46
47
|
});
|
|
47
48
|
|
|
48
49
|
expect(r.action).toBe('force_cleaned');
|
|
49
50
|
expect(fs.existsSync(path.join(tmpDir, 'dist'))).toBe(false);
|
|
50
|
-
expect(fs.existsSync(
|
|
51
|
+
expect(fs.existsSync(markerPath)).toBe(true);
|
|
51
52
|
expect(logs[0].msg).toContain('FORCE_CLEAN_DIST');
|
|
52
53
|
});
|
|
53
54
|
});
|
|
@@ -61,30 +62,42 @@ describe('preserveDevCache', () => {
|
|
|
61
62
|
);
|
|
62
63
|
const logs: LogEntry[] = [];
|
|
63
64
|
|
|
64
|
-
const r = preserveDevCache(tmpDir, {
|
|
65
|
+
const r = preserveDevCache(tmpDir, {
|
|
66
|
+
log: makeLogger(logs),
|
|
67
|
+
forceClean: false,
|
|
68
|
+
markerPath,
|
|
69
|
+
});
|
|
65
70
|
|
|
66
71
|
expect(r.action).toBe('first_boot_preserved');
|
|
67
72
|
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server', 'main.js'))).toBe(true);
|
|
68
|
-
expect(fs.existsSync(
|
|
73
|
+
expect(fs.existsSync(markerPath)).toBe(true);
|
|
69
74
|
expect(logs[0].msg).toContain('first boot');
|
|
70
75
|
});
|
|
71
76
|
|
|
72
77
|
it('handles no dist as first_boot_no_dist', () => {
|
|
73
78
|
const logs: LogEntry[] = [];
|
|
74
79
|
|
|
75
|
-
const r = preserveDevCache(tmpDir, {
|
|
80
|
+
const r = preserveDevCache(tmpDir, {
|
|
81
|
+
log: makeLogger(logs),
|
|
82
|
+
forceClean: false,
|
|
83
|
+
markerPath,
|
|
84
|
+
});
|
|
76
85
|
|
|
77
86
|
expect(r.action).toBe('first_boot_no_dist');
|
|
78
|
-
expect(fs.existsSync(
|
|
87
|
+
expect(fs.existsSync(markerPath)).toBe(true);
|
|
79
88
|
expect(logs[0].msg).toContain('no dist/');
|
|
80
89
|
});
|
|
81
90
|
|
|
82
91
|
it('marker stores current lockfile hash', () => {
|
|
83
92
|
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'initial-lock');
|
|
84
93
|
|
|
85
|
-
preserveDevCache(tmpDir, {
|
|
94
|
+
preserveDevCache(tmpDir, {
|
|
95
|
+
log: makeLogger([]),
|
|
96
|
+
forceClean: false,
|
|
97
|
+
markerPath,
|
|
98
|
+
});
|
|
86
99
|
|
|
87
|
-
const raw = fs.readFileSync(
|
|
100
|
+
const raw = fs.readFileSync(markerPath, 'utf-8');
|
|
88
101
|
const data = JSON.parse(raw);
|
|
89
102
|
expect(typeof data.lockfileHash).toBe('string');
|
|
90
103
|
expect(data.lockfileHash.length).toBeGreaterThan(0);
|
|
@@ -94,31 +107,45 @@ describe('preserveDevCache', () => {
|
|
|
94
107
|
describe('subsequent boot (marker exists)', () => {
|
|
95
108
|
it('cleans dist and rewrites marker when lockfile unchanged', () => {
|
|
96
109
|
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-content');
|
|
97
|
-
|
|
98
|
-
|
|
110
|
+
preserveDevCache(tmpDir, {
|
|
111
|
+
log: makeLogger([]),
|
|
112
|
+
forceClean: false,
|
|
113
|
+
markerPath,
|
|
114
|
+
});
|
|
99
115
|
|
|
100
116
|
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
101
117
|
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'stale.js'), 'stale');
|
|
102
118
|
|
|
103
119
|
const logs: LogEntry[] = [];
|
|
104
|
-
const r = preserveDevCache(tmpDir, {
|
|
120
|
+
const r = preserveDevCache(tmpDir, {
|
|
121
|
+
log: makeLogger(logs),
|
|
122
|
+
forceClean: false,
|
|
123
|
+
markerPath,
|
|
124
|
+
});
|
|
105
125
|
|
|
106
126
|
expect(r.action).toBe('subsequent_boot_cleaned');
|
|
107
127
|
expect(fs.existsSync(path.join(tmpDir, 'dist'))).toBe(false);
|
|
108
|
-
expect(fs.existsSync(
|
|
128
|
+
expect(fs.existsSync(markerPath)).toBe(true);
|
|
109
129
|
expect(logs[0].msg).toContain('subsequent boot');
|
|
110
130
|
});
|
|
111
131
|
|
|
112
132
|
it('reports deps_changed when lockfile hash mismatches marker', () => {
|
|
113
133
|
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v1');
|
|
114
|
-
preserveDevCache(tmpDir, {
|
|
134
|
+
preserveDevCache(tmpDir, {
|
|
135
|
+
log: makeLogger([]),
|
|
136
|
+
forceClean: false,
|
|
137
|
+
markerPath,
|
|
138
|
+
});
|
|
115
139
|
|
|
116
|
-
// 依赖升级: lockfile 变了
|
|
117
140
|
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v2');
|
|
118
141
|
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
119
142
|
|
|
120
143
|
const logs: LogEntry[] = [];
|
|
121
|
-
const r = preserveDevCache(tmpDir, {
|
|
144
|
+
const r = preserveDevCache(tmpDir, {
|
|
145
|
+
log: makeLogger(logs),
|
|
146
|
+
forceClean: false,
|
|
147
|
+
markerPath,
|
|
148
|
+
});
|
|
122
149
|
|
|
123
150
|
expect(r.action).toBe('deps_changed_cleaned');
|
|
124
151
|
expect(fs.existsSync(path.join(tmpDir, 'dist'))).toBe(false);
|
|
@@ -126,13 +153,21 @@ describe('preserveDevCache', () => {
|
|
|
126
153
|
});
|
|
127
154
|
|
|
128
155
|
it('reports subsequent_boot_no_dist when marker exists but no dist', () => {
|
|
129
|
-
preserveDevCache(tmpDir, {
|
|
156
|
+
preserveDevCache(tmpDir, {
|
|
157
|
+
log: makeLogger([]),
|
|
158
|
+
forceClean: false,
|
|
159
|
+
markerPath,
|
|
160
|
+
});
|
|
130
161
|
|
|
131
162
|
const logs: LogEntry[] = [];
|
|
132
|
-
const r = preserveDevCache(tmpDir, {
|
|
163
|
+
const r = preserveDevCache(tmpDir, {
|
|
164
|
+
log: makeLogger(logs),
|
|
165
|
+
forceClean: false,
|
|
166
|
+
markerPath,
|
|
167
|
+
});
|
|
133
168
|
|
|
134
169
|
expect(r.action).toBe('subsequent_boot_no_dist');
|
|
135
|
-
expect(fs.existsSync(
|
|
170
|
+
expect(fs.existsSync(markerPath)).toBe(true);
|
|
136
171
|
expect(logs[0].msg).toContain('no dist/');
|
|
137
172
|
});
|
|
138
173
|
});
|
|
@@ -140,25 +175,134 @@ describe('preserveDevCache', () => {
|
|
|
140
175
|
describe('lockfile detection', () => {
|
|
141
176
|
it('supports package-lock.json', () => {
|
|
142
177
|
fs.writeFileSync(path.join(tmpDir, 'package-lock.json'), '{}');
|
|
143
|
-
preserveDevCache(tmpDir, {
|
|
178
|
+
preserveDevCache(tmpDir, {
|
|
179
|
+
log: makeLogger([]),
|
|
180
|
+
forceClean: false,
|
|
181
|
+
markerPath,
|
|
182
|
+
});
|
|
144
183
|
|
|
145
|
-
const raw = fs.readFileSync(
|
|
184
|
+
const raw = fs.readFileSync(markerPath, 'utf-8');
|
|
146
185
|
expect(JSON.parse(raw).lockfileHash).not.toBeNull();
|
|
147
186
|
});
|
|
148
187
|
|
|
149
188
|
it('supports pnpm-lock.yaml', () => {
|
|
150
189
|
fs.writeFileSync(path.join(tmpDir, 'pnpm-lock.yaml'), 'lockfileVersion: 9');
|
|
151
|
-
preserveDevCache(tmpDir, {
|
|
190
|
+
preserveDevCache(tmpDir, {
|
|
191
|
+
log: makeLogger([]),
|
|
192
|
+
forceClean: false,
|
|
193
|
+
markerPath,
|
|
194
|
+
});
|
|
152
195
|
|
|
153
|
-
const raw = fs.readFileSync(
|
|
196
|
+
const raw = fs.readFileSync(markerPath, 'utf-8');
|
|
154
197
|
expect(JSON.parse(raw).lockfileHash).not.toBeNull();
|
|
155
198
|
});
|
|
156
199
|
|
|
157
200
|
it('stores null lockfileHash when no lockfile present', () => {
|
|
158
|
-
preserveDevCache(tmpDir, {
|
|
201
|
+
preserveDevCache(tmpDir, {
|
|
202
|
+
log: makeLogger([]),
|
|
203
|
+
forceClean: false,
|
|
204
|
+
markerPath,
|
|
205
|
+
});
|
|
159
206
|
|
|
160
|
-
const raw = fs.readFileSync(
|
|
207
|
+
const raw = fs.readFileSync(markerPath, 'utf-8');
|
|
161
208
|
expect(JSON.parse(raw).lockfileHash).toBeNull();
|
|
162
209
|
});
|
|
163
210
|
});
|
|
211
|
+
|
|
212
|
+
describe('first boot + deps changed (lockfile newer than buildinfo)', () => {
|
|
213
|
+
// 制造"lockfile mtime > buildinfo mtime": 先建 dist + buildinfo,sleep 20ms,再 touch lockfile
|
|
214
|
+
// 20ms 是保守值,避免 macOS/APFS mtime 精度问题让两个时间戳相等
|
|
215
|
+
function makeStaleDist() {
|
|
216
|
+
fs.mkdirSync(path.join(tmpDir, 'dist'), { recursive: true });
|
|
217
|
+
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
218
|
+
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'main.js'), 'compiled');
|
|
219
|
+
fs.writeFileSync(path.join(tmpDir, 'dist', 'tsconfig.node.tsbuildinfo'), '{}');
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
it('cleans dist when lockfile mtime > buildinfo mtime', async () => {
|
|
223
|
+
makeStaleDist();
|
|
224
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
225
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'newer-lock');
|
|
226
|
+
|
|
227
|
+
const logs: LogEntry[] = [];
|
|
228
|
+
const r = preserveDevCache(tmpDir, {
|
|
229
|
+
log: makeLogger(logs),
|
|
230
|
+
forceClean: false,
|
|
231
|
+
markerPath,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
expect(r.action).toBe('first_boot_deps_changed_cleaned');
|
|
235
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist'))).toBe(false);
|
|
236
|
+
expect(logs[0].msg).toContain('deps changed');
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('preserves dist when lockfile mtime <= buildinfo mtime', async () => {
|
|
240
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'older-lock');
|
|
241
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
242
|
+
makeStaleDist();
|
|
243
|
+
|
|
244
|
+
const logs: LogEntry[] = [];
|
|
245
|
+
const r = preserveDevCache(tmpDir, {
|
|
246
|
+
log: makeLogger(logs),
|
|
247
|
+
forceClean: false,
|
|
248
|
+
markerPath,
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
expect(r.action).toBe('first_boot_preserved');
|
|
252
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server', 'main.js'))).toBe(true);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('cleans dist when buildinfo is missing (dist exists but never compiled)', () => {
|
|
256
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock');
|
|
257
|
+
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
258
|
+
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'main.js'), 'compiled');
|
|
259
|
+
// 故意不建 tsconfig.node.tsbuildinfo
|
|
260
|
+
|
|
261
|
+
const r = preserveDevCache(tmpDir, {
|
|
262
|
+
log: makeLogger([]),
|
|
263
|
+
forceClean: false,
|
|
264
|
+
markerPath,
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
expect(r.action).toBe('first_boot_deps_changed_cleaned');
|
|
268
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist'))).toBe(false);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('preserves dist when no lockfile present (cannot judge staleness)', () => {
|
|
272
|
+
// dist 有 buildinfo 但没 lockfile
|
|
273
|
+
fs.mkdirSync(path.join(tmpDir, 'dist'), { recursive: true });
|
|
274
|
+
fs.writeFileSync(path.join(tmpDir, 'dist', 'tsconfig.node.tsbuildinfo'), '{}');
|
|
275
|
+
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
276
|
+
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'main.js'), 'compiled');
|
|
277
|
+
|
|
278
|
+
const r = preserveDevCache(tmpDir, {
|
|
279
|
+
log: makeLogger([]),
|
|
280
|
+
forceClean: false,
|
|
281
|
+
markerPath,
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
expect(r.action).toBe('first_boot_preserved');
|
|
285
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server', 'main.js'))).toBe(true);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
describe('marker path (sandbox lifecycle alignment)', () => {
|
|
290
|
+
it('defaults markerPath to os.tmpdir()/.miaoda-dev-boot when not passed', () => {
|
|
291
|
+
// 不传 markerPath, 走默认路径
|
|
292
|
+
const defaultMarker = path.join(os.tmpdir(), '.miaoda-dev-boot');
|
|
293
|
+
// 清理默认位置的残留 marker (可能来自其他测试),避免干扰本用例
|
|
294
|
+
try { fs.unlinkSync(defaultMarker); } catch { /* noop */ }
|
|
295
|
+
|
|
296
|
+
const r = preserveDevCache(tmpDir, {
|
|
297
|
+
log: makeLogger([]),
|
|
298
|
+
forceClean: false,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
expect(r.action).toBe('first_boot_no_dist');
|
|
302
|
+
expect(fs.existsSync(defaultMarker)).toBe(true);
|
|
303
|
+
|
|
304
|
+
// cleanup
|
|
305
|
+
try { fs.unlinkSync(defaultMarker); } catch { /* noop */ }
|
|
306
|
+
});
|
|
307
|
+
});
|
|
164
308
|
});
|
|
@@ -1,23 +1,33 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// dev.js 启动时的 dist 缓存策略:
|
|
4
|
-
// - 沙箱首次启动 (marker 不存在)
|
|
4
|
+
// - 沙箱首次启动 (marker 不存在) + dist 未过期: 保留 dist/, 让 nest --watch 复用上次编译产物
|
|
5
|
+
// - 沙箱首次启动 + dist 过期 (lockfile mtime > buildinfo mtime): 清 dist/, 避免 stale binding
|
|
5
6
|
// - 后续启动 (agent 主动触发 npm run dev): 全清 dist/, 让模型面对新鲜产物避免 stale
|
|
6
|
-
// - 依赖升级 (lockfile hash 变): 后续启动分支自然覆盖 (清 dist), log 里点出原因
|
|
7
|
+
// - 依赖升级 (marker 里 lockfile hash 变): 后续启动分支自然覆盖 (清 dist), log 里点出原因
|
|
7
8
|
// - FORCE_CLEAN_DIST=true: escape hatch, 强清
|
|
9
|
+
//
|
|
10
|
+
// marker 位置放 os.tmpdir()(通常是 /tmp), 跟沙箱容器 lifecycle 同 —— 沙箱重启时
|
|
11
|
+
// /tmp 会清空, marker 消失, 下次进 dev 视为"首次启动"复用 dist; 应用工作目录是
|
|
12
|
+
// persistent volume 会跨沙箱重启保留, 放这里的 marker 无法反映"沙箱首次启动"语义。
|
|
13
|
+
//
|
|
14
|
+
// dist 过期判定 (仅首次启动分支需要): lockfile mtime > dist/tsconfig.node.tsbuildinfo mtime
|
|
15
|
+
// 意味着依赖变动之后 dist 未重新编译, 保留会 stale binding。后续启动分支反正全清不需要判。
|
|
8
16
|
|
|
9
17
|
const fs = require('fs');
|
|
18
|
+
const os = require('os');
|
|
10
19
|
const path = require('path');
|
|
11
20
|
const crypto = require('crypto');
|
|
12
21
|
|
|
13
|
-
const
|
|
22
|
+
const MARKER_FILENAME = '.miaoda-dev-boot';
|
|
14
23
|
|
|
15
24
|
/**
|
|
16
25
|
* @param {string} projectRoot 项目根目录
|
|
17
26
|
* @param {object} [options]
|
|
18
27
|
* @param {(level: string, msg: string) => void} [options.log]
|
|
19
28
|
* @param {boolean} [options.forceClean]
|
|
20
|
-
* @
|
|
29
|
+
* @param {string} [options.markerPath] 覆盖 marker 文件路径 (单测用; 生产默认 /tmp/.miaoda-dev-boot)
|
|
30
|
+
* @returns {{ action: 'force_cleaned' | 'first_boot_preserved' | 'first_boot_deps_changed_cleaned' | 'first_boot_no_dist' | 'subsequent_boot_cleaned' | 'deps_changed_cleaned' | 'subsequent_boot_no_dist' }}
|
|
21
31
|
*/
|
|
22
32
|
function preserveDevCache(projectRoot, options) {
|
|
23
33
|
const log = (options && options.log) || defaultLogger;
|
|
@@ -25,9 +35,11 @@ function preserveDevCache(projectRoot, options) {
|
|
|
25
35
|
options && typeof options.forceClean === 'boolean'
|
|
26
36
|
? options.forceClean
|
|
27
37
|
: process.env.FORCE_CLEAN_DIST === 'true';
|
|
38
|
+
const markerPath =
|
|
39
|
+
(options && options.markerPath) ||
|
|
40
|
+
path.join(os.tmpdir(), MARKER_FILENAME);
|
|
28
41
|
|
|
29
42
|
const distPath = path.join(projectRoot, 'dist');
|
|
30
|
-
const markerPath = path.join(projectRoot, MARKER_FILE);
|
|
31
43
|
const currentLockfileHash = computeLockfileHash(projectRoot);
|
|
32
44
|
|
|
33
45
|
if (forceClean) {
|
|
@@ -45,6 +57,14 @@ function preserveDevCache(projectRoot, options) {
|
|
|
45
57
|
if (!markerExists) {
|
|
46
58
|
writeMarker(markerPath, currentLockfileHash);
|
|
47
59
|
if (distExists) {
|
|
60
|
+
if (distIsStaleVsLockfile(projectRoot)) {
|
|
61
|
+
fs.rmSync(distPath, { recursive: true, force: true });
|
|
62
|
+
log(
|
|
63
|
+
'INFO',
|
|
64
|
+
'preserveDevCache: first boot but deps changed (lockfile newer than buildinfo), cleaned dist/'
|
|
65
|
+
);
|
|
66
|
+
return { action: 'first_boot_deps_changed_cleaned' };
|
|
67
|
+
}
|
|
48
68
|
log('INFO', 'preserveDevCache: first boot, dist/ preserved');
|
|
49
69
|
return { action: 'first_boot_preserved' };
|
|
50
70
|
}
|
|
@@ -76,6 +96,42 @@ function preserveDevCache(projectRoot, options) {
|
|
|
76
96
|
return { action: 'subsequent_boot_no_dist' };
|
|
77
97
|
}
|
|
78
98
|
|
|
99
|
+
/**
|
|
100
|
+
* 判定当前 dist 是否 stale (相对当前 lockfile)。
|
|
101
|
+
*
|
|
102
|
+
* 依据: 依赖变动会更新 lockfile mtime; tsc --watch 编译完更新 buildinfo mtime。
|
|
103
|
+
* lockfile mtime > buildinfo mtime → dist 是"依赖变动前"编译的 → stale。
|
|
104
|
+
*
|
|
105
|
+
* 边界:
|
|
106
|
+
* - lockfile 不存在 → 返回 false (无法判断, 保守认为不 stale, 保留 dist)
|
|
107
|
+
* - buildinfo 不存在 → 返回 true (dist 里没编译过 → 保守清)
|
|
108
|
+
* - `git checkout` 之类 touch lockfile 但内容没改 → 误判 stale 多清一次 (可接受, 安全方向)
|
|
109
|
+
*/
|
|
110
|
+
function distIsStaleVsLockfile(projectRoot) {
|
|
111
|
+
const lockfiles = ['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml'];
|
|
112
|
+
let lockMtime = 0;
|
|
113
|
+
for (const name of lockfiles) {
|
|
114
|
+
try {
|
|
115
|
+
lockMtime = Math.max(
|
|
116
|
+
lockMtime,
|
|
117
|
+
fs.statSync(path.join(projectRoot, name)).mtimeMs
|
|
118
|
+
);
|
|
119
|
+
} catch {
|
|
120
|
+
// lockfile 不存在,跳过
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (lockMtime === 0) return false;
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
const buildinfoMtime = fs.statSync(
|
|
127
|
+
path.join(projectRoot, 'dist', 'tsconfig.node.tsbuildinfo')
|
|
128
|
+
).mtimeMs;
|
|
129
|
+
return lockMtime > buildinfoMtime;
|
|
130
|
+
} catch {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
79
135
|
function computeLockfileHash(projectRoot) {
|
|
80
136
|
const candidates = ['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml'];
|
|
81
137
|
for (const name of candidates) {
|
|
@@ -76,13 +76,49 @@ function isStylelintTarget(filePath) {
|
|
|
76
76
|
return filePath.endsWith('.css');
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
const STYLELINT_CONFIG_FILES = [
|
|
80
|
+
'.stylelintrc',
|
|
81
|
+
'.stylelintrc.js',
|
|
82
|
+
'.stylelintrc.cjs',
|
|
83
|
+
'.stylelintrc.json',
|
|
84
|
+
'stylelint.config.js',
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 老模板(fullstack-nestjs-template 1.x 时代)的应用跑不了 stylelint,跑了就挂、把 pre-commit
|
|
89
|
+
* 卡死。两种形态都实测过:
|
|
90
|
+
*
|
|
91
|
+
* - 缺 scripts.stylelint → `npm error Missing script: "stylelint"`
|
|
92
|
+
* - 缺 .stylelintrc.* → `ConfigurationError: No configuration provided`
|
|
93
|
+
*
|
|
94
|
+
* 注意光判断 stylelint 装没装拦不住:@lark-apaas/fullstack-presets 的 dependencies 里有
|
|
95
|
+
* stylelint,只要 dep 了 presets 二进制就在。缺任一件就跳过这一步。
|
|
96
|
+
*/
|
|
97
|
+
function canRunStylelint() {
|
|
98
|
+
let pkg;
|
|
99
|
+
try {
|
|
100
|
+
pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));
|
|
101
|
+
} catch {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!pkg.scripts || !pkg.scripts.stylelint) return false;
|
|
106
|
+
|
|
107
|
+
return STYLELINT_CONFIG_FILES.some(file => fs.existsSync(path.join(cwd, file)));
|
|
108
|
+
}
|
|
109
|
+
|
|
79
110
|
async function runDefaultLint() {
|
|
80
111
|
const taskSpecs = [
|
|
81
112
|
[getBinName('npm'), ['run', 'eslint']],
|
|
82
113
|
[getBinName('npm'), ['run', 'type:check']],
|
|
83
|
-
[getBinName('npm'), ['run', 'stylelint']],
|
|
84
114
|
];
|
|
85
115
|
|
|
116
|
+
if (canRunStylelint()) {
|
|
117
|
+
taskSpecs.push([getBinName('npm'), ['run', 'stylelint']]);
|
|
118
|
+
} else {
|
|
119
|
+
console.warn('[lint] Skip stylelint: missing scripts.stylelint or stylelint config');
|
|
120
|
+
}
|
|
121
|
+
|
|
86
122
|
process.exit(await runTasksSerially(taskSpecs));
|
|
87
123
|
}
|
|
88
124
|
|
|
@@ -120,7 +156,9 @@ async function runSelectiveLint(inputFiles) {
|
|
|
120
156
|
taskSpecs.push([getBinName('npx'), ['eslint', '--quiet', ...eslintFiles]]);
|
|
121
157
|
}
|
|
122
158
|
|
|
123
|
-
if (stylelintFiles.length > 0) {
|
|
159
|
+
if (stylelintFiles.length > 0 && !canRunStylelint()) {
|
|
160
|
+
console.warn('[lint] Skip stylelint: missing scripts.stylelint or stylelint config');
|
|
161
|
+
} else if (stylelintFiles.length > 0) {
|
|
124
162
|
taskSpecs.push([getBinName('npx'), ['stylelint', '--quiet', ...stylelintFiles]]);
|
|
125
163
|
}
|
|
126
164
|
|