@coze-arch/cli 0.1.8 → 0.1.9-alpha.01bd9e
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/README.md +18 -0
- package/lib/__templates__/expo/.cozeproj/scripts/dev_run.ps1 +2 -2
- package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +1 -1
- package/lib/__templates__/expo/.cozeproj/scripts/prepare-node-modules.sh +178 -10
- package/lib/__templates__/expo/client/package.json +1 -1
- package/lib/__templates__/nextjs/.coze +1 -1
- package/lib/__templates__/nextjs/README.md +18 -0
- package/lib/__templates__/nextjs/eslint.config.mjs +1 -1
- package/lib/__templates__/nextjs/next.config.ts +0 -9
- package/lib/__templates__/nextjs/scripts/build.ps1 +1 -1
- package/lib/__templates__/nextjs/scripts/build.sh +2 -2
- package/lib/__templates__/nextjs/scripts/dev.ps1 +2 -2
- package/lib/__templates__/nextjs/scripts/dev.sh +16 -55
- package/lib/__templates__/nextjs/scripts/local-workspace.cjs +302 -0
- package/lib/__templates__/nextjs/scripts/prepare.sh +10 -2
- package/lib/__templates__/nextjs/scripts/validate.sh +11 -1
- package/lib/__templates__/nextjs/src/server.ts +1 -1
- package/lib/__templates__/nextjs/template.config.js +20 -6
- package/lib/__templates__/nextjs/tsconfig.json +0 -1
- package/lib/__templates__/nuxt-vue/.coze +1 -1
- package/lib/__templates__/nuxt-vue/README.md +20 -2
- package/lib/__templates__/nuxt-vue/scripts/build.sh +1 -1
- package/lib/__templates__/nuxt-vue/scripts/dev.sh +10 -4
- package/lib/__templates__/nuxt-vue/scripts/local-workspace.cjs +302 -0
- package/lib/__templates__/nuxt-vue/scripts/prepare.sh +10 -2
- package/lib/__templates__/nuxt-vue/scripts/validate.sh +11 -1
- package/lib/__templates__/taro/.coze +2 -2
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +6 -1
- package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +10 -1
- package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +56 -4
- package/lib/__templates__/taro/.cozeproj/scripts/local-workspace.cjs +351 -0
- package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +16 -6
- package/lib/__templates__/taro/.cozeproj/scripts/prepare-node-modules.sh +178 -10
- package/lib/__templates__/taro/.cozeproj/scripts/taro-build.cjs +66 -0
- package/lib/__templates__/taro/.cozeproj/scripts/validate.sh +3 -0
- package/lib/__templates__/taro/package.json +5 -5
- package/lib/__templates__/vite/.coze +1 -1
- package/lib/__templates__/vite/README.md +20 -0
- package/lib/__templates__/vite/scripts/build.sh +1 -1
- package/lib/__templates__/vite/scripts/dev.sh +10 -4
- package/lib/__templates__/vite/scripts/local-workspace.cjs +302 -0
- package/lib/__templates__/vite/scripts/prepare.sh +10 -2
- package/lib/__templates__/vite/scripts/validate.sh +7 -1
- package/lib/__templates__/vite/template.config.js +20 -6
- package/lib/cli.js +377 -184
- package/package.json +2 -1
- package/lib/__templates__/nextjs/scripts/prepare-node-modules.sh +0 -212
- package/lib/__templates__/nuxt-vue/scripts/prepare-node-modules.sh +0 -212
- package/lib/__templates__/vite/scripts/prepare-node-modules.sh +0 -212
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
"build": "pnpm exec concurrently --kill-others-on-fail --kill-signal SIGKILL -n lint,tsc,web,weapp,tt,server -c red,blue,green,yellow,cyan,magenta \"pnpm lint:build\" \"pnpm tsc\" \"pnpm build:web\" \"pnpm build:weapp\" \"pnpm build:tt\" \"pnpm build:server\"",
|
|
8
8
|
"build:pack": "pnpm exec concurrently --kill-others-on-fail --kill-signal SIGKILL -n weapp,tt -c yellow,cyan \"pnpm build:weapp\" \"pnpm build:tt\"",
|
|
9
9
|
"build:server": "pnpm --filter server build",
|
|
10
|
-
"build:tt": "taro build
|
|
11
|
-
"build:weapp": "taro build
|
|
12
|
-
"build:web": "taro build
|
|
10
|
+
"build:tt": "node .cozeproj/scripts/taro-build.cjs build tt",
|
|
11
|
+
"build:weapp": "node .cozeproj/scripts/taro-build.cjs build weapp",
|
|
12
|
+
"build:web": "node .cozeproj/scripts/taro-build.cjs build web",
|
|
13
13
|
"dev": "pnpm exec concurrently --kill-others --kill-signal SIGKILL -n web,server -c blue,green \"pnpm dev:web\" \"pnpm dev:server\"",
|
|
14
14
|
"dev:server": "pnpm --filter server dev",
|
|
15
15
|
"dev:tt": "taro build --type tt --watch",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"lint:build": "eslint \"src/**/*.{js,jsx,ts,tsx,css}\" --max-warnings=0 --quiet",
|
|
23
23
|
"lint:fix": "eslint \"src/**/*.{js,jsx,ts,tsx,css}\" --fix",
|
|
24
24
|
"new": "taro new",
|
|
25
|
-
"preview:tt": "taro
|
|
26
|
-
"preview:weapp": "taro
|
|
25
|
+
"preview:tt": "node .cozeproj/scripts/taro-build.cjs preview tt",
|
|
26
|
+
"preview:weapp": "node .cozeproj/scripts/taro-build.cjs preview weapp",
|
|
27
27
|
"tsc": "npx tsc --noEmit --skipLibCheck",
|
|
28
28
|
"validate": "pnpm exec concurrently --kill-others-on-fail --kill-signal SIGKILL -n lint,tsc -c red,blue \"pnpm lint:build\" \"pnpm tsc\""
|
|
29
29
|
},
|
|
@@ -9,7 +9,7 @@ run = ["bash", "./scripts/dev.sh"]
|
|
|
9
9
|
run_win = ["powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "./scripts/dev.ps1"]
|
|
10
10
|
validate = ["bash", "./scripts/validate.sh"]
|
|
11
11
|
validate_win = ["powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "./scripts/validate.ps1"]
|
|
12
|
-
deps = ["git"] # -> apt install git
|
|
12
|
+
deps = ["git", "rsync"] # -> apt install git rsync
|
|
13
13
|
|
|
14
14
|
[deploy]
|
|
15
15
|
build = ["bash","./scripts/build.sh"]
|
|
@@ -228,6 +228,8 @@ app.innerHTML = `
|
|
|
228
228
|
|
|
229
229
|
**必须使用 pnpm 管理依赖**
|
|
230
230
|
|
|
231
|
+
云盘开发先修改源码 `package.json`,再执行 `bash ./scripts/prepare.sh`;以下安装命令仅适用于普通本地项目。
|
|
232
|
+
|
|
231
233
|
```bash
|
|
232
234
|
# ✅ 安装依赖
|
|
233
235
|
pnpm install
|
|
@@ -416,3 +418,21 @@ const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
|
416
418
|
2. 将整个项目上传到服务器
|
|
417
419
|
3. 运行 `pnpm install --prod`
|
|
418
420
|
4. 运行 `coze-dev start` 启动服务
|
|
421
|
+
|
|
422
|
+
## 云盘源码与本地开发
|
|
423
|
+
|
|
424
|
+
Vite、Next.js、Nuxt 的 Unix 开发脚本在 `COZE_DRIVE_ROOT`(默认 `/Coze/Drive`)内通过
|
|
425
|
+
`scripts/local-workspace.cjs` 将源码用 `rsync` 同步到本机临时目录。系统需提供 Node.js、pnpm、bash 和 rsync。
|
|
426
|
+
所有开发与修复仍在云盘源码目录完成,从该目录执行 `.coze` 的 `[dev]` 命令。
|
|
427
|
+
|
|
428
|
+
- `prepare.sh` 同步并在本地安装依赖;安装成功且云盘依赖输入未变化时,仅将更新的锁文件回写云盘。
|
|
429
|
+
- `dev.sh` 在本地镜像运行开发服务,每轮 rsync 结束后等待一秒再同步,框架监听本地文件进行热更新。
|
|
430
|
+
同步失败或依赖输入变化时停止服务;修复后重新运行 `prepare.sh` / `dev.sh`。
|
|
431
|
+
- `validate.sh` 使用独立的本地 `check` 目录,避免类型生成影响正在运行的 `dev` 服务。
|
|
432
|
+
- 同步排除 `.git`、`node_modules`、日志和框架输出;不要在镜像修复源码或将依赖、编译产物回传云盘。
|
|
433
|
+
日志和 PID 默认保留在源码目录的 `logs/`,实际镜像路径见脚本输出。
|
|
434
|
+
|
|
435
|
+
本地镜像仅用于 `[dev].build/run/validate`。部署配置、`build.sh`、`start.sh` 保持原流程;
|
|
436
|
+
普通本地目录和 Windows PowerShell 入口也保持原流程。
|
|
437
|
+
存量项目通过新版 CLI 的 `coze-dev patch --dry-run` / `coze-dev patch` 升级,沿用 patch 总开关。
|
|
438
|
+
仅升级完整匹配旧模板的三个 Unix 开发脚本并保留端口;自定义开发脚本、命令或同名 helper 会跳过。
|
|
@@ -6,7 +6,7 @@ COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-$(pwd)}"
|
|
|
6
6
|
cd "${COZE_WORKSPACE_PATH}"
|
|
7
7
|
|
|
8
8
|
echo "Installing dependencies..."
|
|
9
|
-
|
|
9
|
+
pnpm install --prefer-frozen-lockfile --prefer-offline --loglevel debug --reporter=append-only
|
|
10
10
|
|
|
11
11
|
echo "Building frontend with Vite..."
|
|
12
12
|
pnpm vite build
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
set -Eeuo pipefail
|
|
3
3
|
|
|
4
|
+
# Resolve the source project from this script, then let the Web runner choose its local mirror.
|
|
5
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
6
|
+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd -P)"
|
|
7
|
+
if [[ "${COZE_WEB_LOCAL_ACTIVE:-}" != "${PROJECT_ROOT}" ]]; then
|
|
8
|
+
exec node "${SCRIPT_DIR}/local-workspace.cjs" dev "$@"
|
|
9
|
+
fi
|
|
10
|
+
COZE_WORKSPACE_PATH="${PROJECT_ROOT}"
|
|
11
|
+
|
|
4
12
|
<% if (process.env.NODE_ENV === 'test') { %>
|
|
5
13
|
# 测试环境:支持环境变量覆盖端口
|
|
6
14
|
PORT="${PORT:-<%= port %>}"
|
|
7
|
-
COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-$(pwd)}"
|
|
8
15
|
DEPLOY_RUN_PORT="${DEPLOY_RUN_PORT:-${PORT}}"
|
|
9
16
|
<% } else { %>
|
|
10
17
|
PORT=<%= port %>
|
|
11
|
-
COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-$(pwd)}"
|
|
12
18
|
DEPLOY_RUN_PORT="${DEPLOY_RUN_PORT:-${PORT}}"
|
|
13
19
|
<% } %>
|
|
14
20
|
|
|
@@ -38,7 +44,7 @@ kill_port_if_listening
|
|
|
38
44
|
echo "Starting express + Vite dev server on port ${DEPLOY_RUN_PORT}..."
|
|
39
45
|
|
|
40
46
|
# 测试环境保持服务为前台进程,便于 e2e 收集输出并停止进程。
|
|
41
|
-
exec env PORT="${DEPLOY_RUN_PORT}" pnpm tsx watch server/server.ts
|
|
47
|
+
exec env PORT="${DEPLOY_RUN_PORT}" node "${SCRIPT_DIR}/local-workspace.cjs" watch pnpm tsx watch server/server.ts
|
|
42
48
|
<% } else { %>
|
|
43
49
|
LOG_DIR="${COZE_LOG_DIR:-${COZE_WORKSPACE_PATH}/logs}"
|
|
44
50
|
LOG_FILE="${LOG_DIR}/server.log"
|
|
@@ -136,7 +142,7 @@ mkdir -p "${LOG_DIR}"
|
|
|
136
142
|
|
|
137
143
|
export PORT="${DEPLOY_RUN_PORT}"
|
|
138
144
|
server_pid="$(spawn_detached "${COZE_WORKSPACE_PATH}" "${LOG_FILE}" \
|
|
139
|
-
"$(command -v pnpm)" tsx watch server/server.ts)"
|
|
145
|
+
"$(command -v node)" "${SCRIPT_DIR}/local-workspace.cjs" watch "$(command -v pnpm)" tsx watch server/server.ts)"
|
|
140
146
|
echo "${server_pid}" > "${PID_FILE}"
|
|
141
147
|
|
|
142
148
|
sleep 1
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Web source is owned by Drive. Only these wrappers may write to the local mirror.
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-require-imports -- Standalone CommonJS runtime shipped without CLI dependencies. */
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const os = require('node:os');
|
|
7
|
+
const { createHash } = require('node:crypto');
|
|
8
|
+
const { spawn, execFileSync } = require('node:child_process');
|
|
9
|
+
const { setTimeout: delay } = require('node:timers/promises');
|
|
10
|
+
|
|
11
|
+
const scriptRoot = fs.realpathSync(path.resolve(__dirname, '..'));
|
|
12
|
+
const source = fs.realpathSync(
|
|
13
|
+
process.env.COZE_WEB_LOCAL_ACTIVE === scriptRoot && process.env.COZE_WEB_SOURCE_PATH
|
|
14
|
+
? process.env.COZE_WEB_SOURCE_PATH : scriptRoot,
|
|
15
|
+
);
|
|
16
|
+
const drivePath = path.resolve(process.env.COZE_DRIVE_ROOT || '/Coze/Drive');
|
|
17
|
+
const drive = fs.existsSync(drivePath) ? fs.realpathSync(drivePath) : drivePath;
|
|
18
|
+
const inside = (root, candidate) => candidate === root || candidate.startsWith(`${root}${path.sep}`);
|
|
19
|
+
const onDrive = inside(drive, source);
|
|
20
|
+
const digest = value => createHash('sha256').update(value).digest('hex');
|
|
21
|
+
// Separate from /tmp/nm: older dependency helpers clean unknown entries there.
|
|
22
|
+
const cacheRoot = path.join(os.tmpdir(), `coze-web-${process.getuid()}`);
|
|
23
|
+
const projectCache = path.join(cacheRoot, digest(source));
|
|
24
|
+
const exclusions = [
|
|
25
|
+
'node_modules', '.git', '/logs', '.pnpm-store', '.next', '.nuxt', '.output',
|
|
26
|
+
'/dist', '/dist-server', '.cache', '.turbo', '*.tsbuildinfo',
|
|
27
|
+
'.eslintcache', '.stylelintcache', '/next-env.d.ts', '/tsconfig.json',
|
|
28
|
+
];
|
|
29
|
+
const installArgs = ['install', '--prefer-frozen-lockfile', '--prefer-offline'];
|
|
30
|
+
let interrupted = false;
|
|
31
|
+
const children = new Set();
|
|
32
|
+
|
|
33
|
+
function stopTree(child) {
|
|
34
|
+
if (!child.pid || child.exitCode !== null) return;
|
|
35
|
+
// Keep descendants in the detached launcher's group so readiness ownership works.
|
|
36
|
+
// Also stop grandchildren when the test/foreground path is signalled directly.
|
|
37
|
+
let rows = [];
|
|
38
|
+
try {
|
|
39
|
+
rows = execFileSync('ps', ['-axo', 'pid=,ppid='], { encoding: 'utf8' })
|
|
40
|
+
.trim().split('\n').map(line => line.trim().split(/\s+/).map(Number));
|
|
41
|
+
} catch { /* The launcher still owns and reaps its process group. */ }
|
|
42
|
+
const pids = [child.pid];
|
|
43
|
+
for (let i = 0; i < pids.length; i++) {
|
|
44
|
+
for (const [pid, parent] of rows) if (parent === pids[i]) pids.push(pid);
|
|
45
|
+
}
|
|
46
|
+
for (const pid of pids.reverse()) {
|
|
47
|
+
try { process.kill(pid, 'SIGTERM'); } catch { /* Already exited. */ }
|
|
48
|
+
}
|
|
49
|
+
const timer = setTimeout(() => {
|
|
50
|
+
for (const pid of pids) {
|
|
51
|
+
try { process.kill(pid, 'SIGKILL'); } catch { /* Already exited. */ }
|
|
52
|
+
}
|
|
53
|
+
}, 2000);
|
|
54
|
+
timer.unref();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const signal of ['SIGINT', 'SIGTERM']) {
|
|
58
|
+
process.on(signal, () => {
|
|
59
|
+
interrupted = true;
|
|
60
|
+
process.exitCode = signal === 'SIGINT' ? 130 : 143;
|
|
61
|
+
for (const child of children) stopTree(child);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function run(command, args, cwd, env = process.env) {
|
|
66
|
+
if (interrupted) return Promise.reject(new Error('Interrupted'));
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
const child = spawn(command, args, { cwd, env, stdio: 'inherit' });
|
|
69
|
+
children.add(child);
|
|
70
|
+
child.once('error', error => { children.delete(child); reject(error); });
|
|
71
|
+
child.once('exit', (code, signal) => {
|
|
72
|
+
children.delete(child);
|
|
73
|
+
if (code === 0) resolve();
|
|
74
|
+
else reject(new Error(`${command} exited with ${signal || code}`));
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function ensureCache() {
|
|
80
|
+
for (const dir of [cacheRoot, projectCache]) {
|
|
81
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
82
|
+
if (fs.lstatSync(dir).isSymbolicLink() || fs.statSync(dir).uid !== process.getuid()) {
|
|
83
|
+
throw new Error(`Refusing unmanaged workspace directory: ${dir}`);
|
|
84
|
+
}
|
|
85
|
+
if (inside(drive, fs.realpathSync(dir))) {
|
|
86
|
+
throw new Error(`Local workspace must be outside Coze Drive: ${dir}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function locked(action, name = 'sync') {
|
|
92
|
+
const lock = path.join(projectCache, `${name}.lock`);
|
|
93
|
+
const started = Date.now();
|
|
94
|
+
let missingOwner = 0;
|
|
95
|
+
while (true) {
|
|
96
|
+
if (interrupted) throw new Error('Interrupted');
|
|
97
|
+
try { fs.mkdirSync(lock); break; } catch (error) {
|
|
98
|
+
if (error.code !== 'EEXIST') throw error;
|
|
99
|
+
}
|
|
100
|
+
let alive = false;
|
|
101
|
+
try {
|
|
102
|
+
const pid = Number(fs.readFileSync(path.join(lock, 'pid'), 'utf8'));
|
|
103
|
+
if (Number.isInteger(pid) && pid > 0) { process.kill(pid, 0); alive = true; }
|
|
104
|
+
} catch (error) { if (error.code === 'EPERM') alive = true; }
|
|
105
|
+
missingOwner = alive ? 0 : missingOwner + 1;
|
|
106
|
+
if (missingOwner >= 2) {
|
|
107
|
+
const stale = `${lock}.stale-${process.pid}`;
|
|
108
|
+
try {
|
|
109
|
+
fs.renameSync(lock, stale);
|
|
110
|
+
fs.rmSync(stale, { recursive: true });
|
|
111
|
+
} catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
112
|
+
missingOwner = 0;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (Date.now() - started > 120000) throw new Error(`Timed out waiting for ${lock}`);
|
|
116
|
+
await delay(500);
|
|
117
|
+
}
|
|
118
|
+
fs.writeFileSync(path.join(lock, 'pid'), String(process.pid));
|
|
119
|
+
try { return await action(); }
|
|
120
|
+
finally { fs.rmSync(lock, { recursive: true, force: true }); }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function sync(local) {
|
|
124
|
+
if (!fs.existsSync(path.join(source, 'package.json'))) {
|
|
125
|
+
throw new Error(`Source project is unavailable: ${source}`);
|
|
126
|
+
}
|
|
127
|
+
fs.mkdirSync(local, { recursive: true });
|
|
128
|
+
if (fs.realpathSync(local) !== local) throw new Error(`Refusing workspace symlink: ${local}`);
|
|
129
|
+
// Checksums catch same-size edits with unchanged/epoch Drive mtimes. Do not use -t:
|
|
130
|
+
// changed local files get fresh mtimes and unchanged files retain watcher snapshots.
|
|
131
|
+
// Materialize source links so the framework never follows them back onto Drive.
|
|
132
|
+
await run('rsync', [
|
|
133
|
+
'-rLp', '--checksum', '--delete', '--delay-updates',
|
|
134
|
+
...exclusions.map(entry => `--exclude=${entry}`), `${source}/`, `${local}/`,
|
|
135
|
+
], source);
|
|
136
|
+
// Next rewrites these inputs (dev/types vs types and tsconfig defaults). Preserve
|
|
137
|
+
// generated local changes until their canonical source content actually changes.
|
|
138
|
+
for (const file of ['next-env.d.ts', 'tsconfig.json']) {
|
|
139
|
+
const input = path.join(source, file);
|
|
140
|
+
const output = path.join(local, file);
|
|
141
|
+
const state = path.join(projectCache, `${path.basename(local)}.${file}.source`);
|
|
142
|
+
const fingerprint = fs.existsSync(input) ? digest(fs.readFileSync(input)) : 'missing';
|
|
143
|
+
if (readOptional(state).toString() !== fingerprint || !fs.existsSync(output)) {
|
|
144
|
+
if (fingerprint !== 'missing') fs.copyFileSync(input, output);
|
|
145
|
+
else fs.rmSync(output, { force: true });
|
|
146
|
+
fs.writeFileSync(state, fingerprint);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function readOptional(file) {
|
|
152
|
+
try { return fs.readFileSync(file); }
|
|
153
|
+
catch (error) { if (error.code === 'ENOENT') return Buffer.alloc(0); throw error; }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function installInput(root) {
|
|
157
|
+
const hash = createHash('sha256');
|
|
158
|
+
const visit = (dir, all = false) => {
|
|
159
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
160
|
+
if (['node_modules', '.git', 'logs', '.pnpm-store', '.next', '.nuxt', '.output', 'dist', 'dist-server', '.cache', '.turbo'].includes(entry.name)) continue;
|
|
161
|
+
const file = path.join(dir, entry.name);
|
|
162
|
+
// Follow source links just as rsync -L does, while detecting loops explicitly.
|
|
163
|
+
const real = fs.realpathSync(file);
|
|
164
|
+
if (entry.isDirectory() || (entry.isSymbolicLink() && fs.statSync(file).isDirectory())) {
|
|
165
|
+
if (ancestors.has(real)) throw new Error(`Source symlink cycle: ${file}`);
|
|
166
|
+
ancestors.add(real);
|
|
167
|
+
visit(file, all || entry.name === 'patches');
|
|
168
|
+
ancestors.delete(real);
|
|
169
|
+
}
|
|
170
|
+
else if (all || ['package.json', 'pnpm-lock.yaml', 'pnpm-workspace.yaml', '.npmrc', '.pnpmfile.cjs', '.pnpmfile.js'].includes(entry.name)) {
|
|
171
|
+
hash.update(path.relative(root, file)).update('\0').update(readOptional(file)).update('\0');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const ancestors = new Set([fs.realpathSync(root)]);
|
|
176
|
+
visit(root);
|
|
177
|
+
return hash.digest('hex');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function localEnv(local) {
|
|
181
|
+
return {
|
|
182
|
+
...process.env,
|
|
183
|
+
COZE_WEB_LOCAL_ACTIVE: local,
|
|
184
|
+
COZE_WEB_SOURCE_PATH: source,
|
|
185
|
+
COZE_WEB_LOCAL_MIRROR: onDrive ? '1' : '',
|
|
186
|
+
COZE_WORKSPACE_PATH: local,
|
|
187
|
+
// Keep the existing discoverable log/PID location in the source project.
|
|
188
|
+
COZE_LOG_DIR: process.env.COZE_LOG_DIR || path.join(source, 'logs'),
|
|
189
|
+
PWD: local,
|
|
190
|
+
INIT_CWD: local,
|
|
191
|
+
...(onDrive ? { npm_config_store_dir: path.join(cacheRoot, 'store') } : {}),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function install(local, mode) {
|
|
196
|
+
const state = path.join(projectCache, `${mode}.install`);
|
|
197
|
+
const version = execFileSync('pnpm', ['--version'], { encoding: 'utf8' }).trim();
|
|
198
|
+
const fingerprint = () => digest([
|
|
199
|
+
installInput(local), version, process.version, process.env.NODE_ENV,
|
|
200
|
+
process.env.npm_config_production, process.env.NPM_CONFIG_PRODUCTION,
|
|
201
|
+
process.env.PNPM_CONFIG_PRODUCTION,
|
|
202
|
+
].join('\n'));
|
|
203
|
+
// Lifecycle scripts may generate files from arbitrary source, so do not reuse
|
|
204
|
+
// their install based on dependency metadata alone. Read manifests as text here
|
|
205
|
+
// (no executable hooks or private CLI dependencies in the standalone helper).
|
|
206
|
+
const manifest = readOptional(path.join(local, 'package.json')).toString();
|
|
207
|
+
const hasLifecycle = /"(?:install|postinstall|prepare)"\s*:/.test(manifest);
|
|
208
|
+
// Workspace and local file dependencies can run lifecycles outside the root.
|
|
209
|
+
// Let pnpm validate those installs instead of assuming the root marker is enough.
|
|
210
|
+
const hasLocalDependencies = fs.existsSync(path.join(local, 'pnpm-workspace.yaml')) ||
|
|
211
|
+
/"(?:workspace|file|link):/.test(manifest);
|
|
212
|
+
const hasHooks = ['.pnpmfile.cjs', '.pnpmfile.js'].some(file => fs.existsSync(path.join(local, file))) ||
|
|
213
|
+
/^\s*pnpmfile\s*=/m.test(readOptional(path.join(local, '.npmrc')).toString());
|
|
214
|
+
if (!hasLifecycle && !hasHooks && !hasLocalDependencies && readOptional(state).toString() === fingerprint() &&
|
|
215
|
+
fs.existsSync(path.join(local, 'node_modules', '.modules.yaml'))) return;
|
|
216
|
+
fs.rmSync(state, { force: true });
|
|
217
|
+
const before = installInput(source);
|
|
218
|
+
if (before !== installInput(local)) {
|
|
219
|
+
throw new Error('Dependency inputs changed during sync; rerun prepare.sh.');
|
|
220
|
+
}
|
|
221
|
+
await run('pnpm', [...installArgs, '--store-dir', path.join(cacheRoot, 'store')], local, localEnv(local));
|
|
222
|
+
if (before !== installInput(source)) {
|
|
223
|
+
throw new Error('Dependency inputs changed on Drive during installation; rerun prepare.sh. Lockfile was not overwritten.');
|
|
224
|
+
}
|
|
225
|
+
const lockfile = path.join(local, 'pnpm-lock.yaml');
|
|
226
|
+
if (fs.existsSync(lockfile) && !readOptional(lockfile).equals(readOptional(path.join(source, 'pnpm-lock.yaml')))) {
|
|
227
|
+
const temporary = path.join(source, `.pnpm-lock.yaml.${process.pid}.tmp`);
|
|
228
|
+
try {
|
|
229
|
+
fs.copyFileSync(lockfile, temporary);
|
|
230
|
+
fs.renameSync(temporary, path.join(source, 'pnpm-lock.yaml'));
|
|
231
|
+
} finally { fs.rmSync(temporary, { force: true }); }
|
|
232
|
+
}
|
|
233
|
+
fs.writeFileSync(state, fingerprint());
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function watch(args) {
|
|
237
|
+
const local = fs.realpathSync(process.env.COZE_WEB_LOCAL_ACTIVE || path.resolve(__dirname, '..'));
|
|
238
|
+
if (!onDrive) return run(args[0], args.slice(1), local);
|
|
239
|
+
ensureCache();
|
|
240
|
+
if (local !== path.join(fs.realpathSync(projectCache), 'dev')) {
|
|
241
|
+
throw new Error(`Refusing to watch an unmanaged workspace: ${local}`);
|
|
242
|
+
}
|
|
243
|
+
const preparedInput = installInput(local);
|
|
244
|
+
let running = true;
|
|
245
|
+
let syncError;
|
|
246
|
+
const command = run(args[0], args.slice(1), local).finally(() => { running = false; });
|
|
247
|
+
const poll = (async () => {
|
|
248
|
+
while (running && !interrupted) {
|
|
249
|
+
await delay(1000);
|
|
250
|
+
if (!running || interrupted) break;
|
|
251
|
+
try {
|
|
252
|
+
await locked(async () => {
|
|
253
|
+
await sync(local);
|
|
254
|
+
if (installInput(local) !== preparedInput) {
|
|
255
|
+
throw new Error('Dependency inputs changed; rerun prepare.sh and dev.sh before previewing.');
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
syncError = error;
|
|
261
|
+
for (const child of children) stopTree(child);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
})();
|
|
266
|
+
const results = await Promise.allSettled([command, poll]);
|
|
267
|
+
if (syncError) throw syncError;
|
|
268
|
+
if (results[0].status === 'rejected') throw results[0].reason;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async function main() {
|
|
272
|
+
const [action, ...args] = process.argv.slice(2);
|
|
273
|
+
if (action === 'watch') return watch(args);
|
|
274
|
+
if (!['prepare', 'dev', 'validate'].includes(action)) throw new Error(`Unknown Web action: ${action}`);
|
|
275
|
+
if (!onDrive) {
|
|
276
|
+
return run('bash', [path.join(source, 'scripts', `${action}.sh`), ...args], source, localEnv(source));
|
|
277
|
+
}
|
|
278
|
+
ensureCache();
|
|
279
|
+
const mode = action === 'validate' ? 'check' : 'dev';
|
|
280
|
+
const local = path.join(fs.realpathSync(projectCache), mode);
|
|
281
|
+
console.log(`[web] Source: ${source}\n[web] Local ${mode} workspace: ${local}`);
|
|
282
|
+
if (action === 'validate') {
|
|
283
|
+
return locked(async () => {
|
|
284
|
+
await locked(async () => {
|
|
285
|
+
await sync(local);
|
|
286
|
+
await install(local, mode);
|
|
287
|
+
});
|
|
288
|
+
await run('bash', [path.join(local, 'scripts', 'validate.sh'), ...args], local, localEnv(local));
|
|
289
|
+
}, 'check');
|
|
290
|
+
} else {
|
|
291
|
+
await locked(async () => {
|
|
292
|
+
await sync(local);
|
|
293
|
+
await install(local, mode);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
await run('bash', [path.join(local, 'scripts', `${action}.sh`), ...args], local, localEnv(local));
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
main().catch(error => {
|
|
300
|
+
console.error(`[web] ${error.message}`);
|
|
301
|
+
process.exitCode = process.exitCode || 1;
|
|
302
|
+
});
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
set -Eeuo pipefail
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
# Resolve the source project from this script, then let the Web runner choose its local mirror.
|
|
5
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
6
|
+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd -P)"
|
|
7
|
+
if [[ "${COZE_WEB_LOCAL_ACTIVE:-}" != "${PROJECT_ROOT}" ]]; then
|
|
8
|
+
exec node "${SCRIPT_DIR}/local-workspace.cjs" prepare "$@"
|
|
9
|
+
fi
|
|
10
|
+
COZE_WORKSPACE_PATH="${PROJECT_ROOT}"
|
|
5
11
|
|
|
6
12
|
cd "${COZE_WORKSPACE_PATH}"
|
|
7
13
|
|
|
8
14
|
echo "Installing dependencies..."
|
|
9
|
-
|
|
15
|
+
if [[ "${COZE_WEB_LOCAL_MIRROR:-}" != "1" ]]; then
|
|
16
|
+
pnpm install --prefer-frozen-lockfile --prefer-offline --loglevel debug --reporter=append-only
|
|
17
|
+
fi
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
set -Eeuo pipefail
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
# Resolve the source project from this script, then let the Web runner choose its local mirror.
|
|
5
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
6
|
+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd -P)"
|
|
7
|
+
if [[ "${COZE_WEB_LOCAL_ACTIVE:-}" != "${PROJECT_ROOT}" ]]; then
|
|
8
|
+
exec node "${SCRIPT_DIR}/local-workspace.cjs" validate "$@"
|
|
9
|
+
fi
|
|
10
|
+
COZE_WORKSPACE_PATH="${PROJECT_ROOT}"
|
|
5
11
|
|
|
6
12
|
cd "${COZE_WORKSPACE_PATH}"
|
|
7
13
|
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
|
|
2
2
|
import { spawn } from 'child_process';
|
|
3
|
-
import { resolve, join, basename } from 'path';
|
|
4
|
-
import {
|
|
3
|
+
import { resolve, join, basename, sep } from 'path';
|
|
4
|
+
import {
|
|
5
|
+
appendFileSync,
|
|
6
|
+
openSync,
|
|
7
|
+
closeSync,
|
|
8
|
+
mkdirSync,
|
|
9
|
+
existsSync,
|
|
10
|
+
realpathSync,
|
|
11
|
+
} from 'fs';
|
|
5
12
|
import { homedir } from 'os';
|
|
6
13
|
|
|
7
14
|
|
|
@@ -74,12 +81,19 @@ const config = {
|
|
|
74
81
|
|
|
75
82
|
const cmd = 'pnpm';
|
|
76
83
|
const args = ['update', 'coze-coding-dev-sdk@^0.7.28'];
|
|
77
|
-
console.log(
|
|
78
|
-
`\nTriggering: ${cmd} ${args.join(' ')} (running in background)`,
|
|
79
|
-
);
|
|
80
84
|
|
|
81
85
|
try {
|
|
82
|
-
const projectRoot =
|
|
86
|
+
const projectRoot = realpathSync(outputPath);
|
|
87
|
+
const drivePath = resolve(process.env.COZE_DRIVE_ROOT || '/Coze/Drive');
|
|
88
|
+
const driveRoot = existsSync(drivePath) ? realpathSync(drivePath) : drivePath;
|
|
89
|
+
if (projectRoot === driveRoot || projectRoot.startsWith(`${driveRoot}${sep}`)) {
|
|
90
|
+
// Cloud projects install through prepare.sh in the local development mirror.
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
console.log(
|
|
95
|
+
`\nTriggering: ${cmd} ${args.join(' ')} (running in background)`,
|
|
96
|
+
);
|
|
83
97
|
|
|
84
98
|
// Determine log directory
|
|
85
99
|
const cozeHome = process.env.COZE_HOME || join(homedir(), '.coze');
|