@coze-arch/cli 0.1.4 → 0.1.5-alpha.581217
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/docs/deploy.md +12 -1
- package/lib/__templates__/expo/.cozeproj/scripts/dev_build.ps1 +1 -1
- package/lib/__templates__/expo/.cozeproj/scripts/dev_build.sh +3 -3
- package/lib/__templates__/expo/.cozeproj/scripts/dev_run.ps1 +3 -3
- package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +11 -3
- package/lib/__templates__/expo/.cozeproj/scripts/prepare-node-modules.sh +212 -0
- package/lib/__templates__/expo/.cozeproj/scripts/prod_build.sh +3 -3
- package/lib/__templates__/expo/client/components/Screen.tsx +65 -25
- package/lib/__templates__/expo/client/heroui/components/toast/toast.tsx +6 -2
- package/lib/__templates__/expo/client/heroui/helpers/internal/hooks/use-controllable-state.ts +1 -1
- package/lib/__templates__/nextjs/eslint.config.mjs +11 -5
- package/lib/__templates__/nextjs/next.config.ts +10 -0
- package/lib/__templates__/nextjs/package.json +2 -5
- package/lib/__templates__/nextjs/pnpm-lock.yaml +14 -945
- 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 +86 -10
- package/lib/__templates__/nextjs/scripts/prepare-node-modules.sh +212 -0
- package/lib/__templates__/nextjs/scripts/prepare.sh +1 -4
- package/lib/__templates__/nextjs/src/app/layout.tsx +0 -4
- package/lib/__templates__/nextjs/src/server.ts +1 -1
- package/lib/__templates__/nuxt-vue/scripts/build.sh +1 -1
- package/lib/__templates__/nuxt-vue/scripts/dev.sh +11 -3
- package/lib/__templates__/nuxt-vue/scripts/prepare-node-modules.sh +212 -0
- package/lib/__templates__/nuxt-vue/scripts/prepare.sh +1 -4
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +5 -1
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +4 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +3 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_run.ps1 +4 -2
- package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +29 -7
- package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +4 -0
- package/lib/__templates__/taro/.cozeproj/scripts/pack.ps1 +3 -1
- package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +8 -0
- package/lib/__templates__/taro/.cozeproj/scripts/prepare-node-modules.sh +212 -0
- package/lib/__templates__/taro/.cozeproj/scripts/validate.ps1 +3 -1
- package/lib/__templates__/taro/.cozeproj/scripts/validate.sh +4 -0
- package/lib/__templates__/taro/_gitignore +1 -0
- package/lib/__templates__/vite/scripts/build.ps1 +1 -4
- package/lib/__templates__/vite/scripts/build.sh +1 -1
- package/lib/__templates__/vite/scripts/dev.ps1 +23 -7
- package/lib/__templates__/vite/scripts/dev.sh +11 -3
- package/lib/__templates__/vite/scripts/ensure-deps.ps1 +33 -0
- package/lib/__templates__/vite/scripts/prepare-node-modules.sh +212 -0
- package/lib/__templates__/vite/scripts/prepare.ps1 +1 -6
- package/lib/__templates__/vite/scripts/prepare.sh +1 -4
- package/lib/cli.js +154 -20
- package/package.json +1 -1
- package/lib/__templates__/nextjs/.babelrc +0 -15
|
@@ -8,7 +8,7 @@ Write-Host "Installing dependencies..."
|
|
|
8
8
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
9
9
|
|
|
10
10
|
Write-Host "Building the Next.js project..."
|
|
11
|
-
& pnpm next build
|
|
11
|
+
& pnpm next build --webpack
|
|
12
12
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
13
13
|
|
|
14
14
|
Write-Host "Bundling server with tsup..."
|
|
@@ -6,10 +6,10 @@ COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-$(pwd)}"
|
|
|
6
6
|
cd "${COZE_WORKSPACE_PATH}"
|
|
7
7
|
|
|
8
8
|
echo "Installing dependencies..."
|
|
9
|
-
|
|
9
|
+
bash "$COZE_WORKSPACE_PATH/scripts/prepare-node-modules.sh" --prefer-frozen-lockfile --prefer-offline --loglevel debug --reporter=append-only
|
|
10
10
|
|
|
11
11
|
echo "Building the Next.js project..."
|
|
12
|
-
pnpm next build
|
|
12
|
+
pnpm next build --webpack
|
|
13
13
|
|
|
14
14
|
echo "Bundling server with tsup..."
|
|
15
15
|
pnpm tsup src/server.ts --format cjs --platform node --target node20 --outDir dist --no-splitting --no-minify
|
|
@@ -28,7 +28,7 @@ Stop-ListeningProcess $port
|
|
|
28
28
|
|
|
29
29
|
<% if (process.env.NODE_ENV === 'test') { %>
|
|
30
30
|
$env:PORT = "$port"
|
|
31
|
-
& pnpm
|
|
31
|
+
& pnpm next dev --webpack --hostname 0.0.0.0 --port $port
|
|
32
32
|
exit $LASTEXITCODE
|
|
33
33
|
<% } else { %>
|
|
34
34
|
$logDirectory = if ($env:COZE_LOG_DIR) { $env:COZE_LOG_DIR } else { Join-Path $workspace "logs" }
|
|
@@ -45,7 +45,7 @@ if (Test-Path $pidFile) {
|
|
|
45
45
|
$previousPort = $env:PORT
|
|
46
46
|
$env:PORT = "$port"
|
|
47
47
|
try {
|
|
48
|
-
$process = Start-Process -FilePath $env:ComSpec -ArgumentList @("/d", "/s", "/c", "pnpm
|
|
48
|
+
$process = Start-Process -FilePath $env:ComSpec -ArgumentList @("/d", "/s", "/c", "pnpm next dev --webpack --hostname 0.0.0.0 --port $port") -WorkingDirectory $workspace -RedirectStandardOutput $logFile -RedirectStandardError $errorLogFile -PassThru
|
|
49
49
|
} finally {
|
|
50
50
|
if ($null -eq $previousPort) { Remove-Item Env:PORT -ErrorAction SilentlyContinue } else { $env:PORT = $previousPort }
|
|
51
51
|
}
|
|
@@ -52,24 +52,77 @@ kill_port_if_listening() {
|
|
|
52
52
|
echo "Port ${DEPLOY_RUN_PORT} cleared."
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
# Next 的路由清单由 watchpack 每次启动重新扫 src 下的 app/pages 目录现算,而 watchpack
|
|
56
|
+
# 不会进入软链目录,软链子树里的 page/route 会被静默跳过:既不注册路由,也不打任何日志,
|
|
57
|
+
# 表现为该路由一直 404 而 .next 里没有它的产物。这里只做提示,不改动源码。
|
|
58
|
+
warn_source_symlinks() {
|
|
59
|
+
local source_dir="${COZE_WORKSPACE_PATH}/src"
|
|
60
|
+
local links link
|
|
61
|
+
if [[ ! -e "${source_dir}" ]]; then
|
|
62
|
+
return
|
|
63
|
+
fi
|
|
64
|
+
links=$(find "${source_dir}" -type l 2>/dev/null || true)
|
|
65
|
+
if [[ -z "${links}" ]]; then
|
|
66
|
+
return
|
|
67
|
+
fi
|
|
68
|
+
echo "Warning: 以下路径是软链,Next 不会进入其中,里面的 page/route 不会被编译:" >&2
|
|
69
|
+
while IFS= read -r link; do
|
|
70
|
+
echo " ${link}" >&2
|
|
71
|
+
done <<< "${links}"
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
# 云盘挂载偶尔会把新建文件的 mtime 设为 Unix epoch。Watchpack 会因此把路由文件当作
|
|
75
|
+
# 启动前的旧状态,导致 Next 的首轮扫描漏掉 route/page。只在云盘项目启动前刷新 app/pages
|
|
76
|
+
# 的目录和普通文件时间戳;不跟随软链、不改文件内容。
|
|
77
|
+
refresh_drive_route_mtimes() {
|
|
78
|
+
local drive_root="${COZE_DRIVE_ROOT:-/Coze/Drive}"
|
|
79
|
+
local project_root route_dir
|
|
80
|
+
project_root="$(pwd -P)"
|
|
81
|
+
if [[ -d "${drive_root}" ]]; then
|
|
82
|
+
drive_root="$(cd "${drive_root}" && pwd -P)"
|
|
83
|
+
else
|
|
84
|
+
drive_root="${drive_root%/}"
|
|
85
|
+
fi
|
|
86
|
+
|
|
87
|
+
case "${project_root}" in
|
|
88
|
+
"${drive_root}"|"${drive_root}"/*) ;;
|
|
89
|
+
*) return ;;
|
|
90
|
+
esac
|
|
91
|
+
|
|
92
|
+
for route_dir in "${COZE_WORKSPACE_PATH}/src/app" "${COZE_WORKSPACE_PATH}/src/pages"; do
|
|
93
|
+
[[ -d "${route_dir}" ]] || continue
|
|
94
|
+
if ! find -P "${route_dir}" \( -type f -o -type d \) -exec touch {} + 2>/dev/null; then
|
|
95
|
+
echo "Warning: failed to refresh Next route timestamps in ${route_dir}." >&2
|
|
96
|
+
fi
|
|
97
|
+
done
|
|
98
|
+
}
|
|
99
|
+
|
|
55
100
|
<% if (process.env.NODE_ENV === 'test') { %>
|
|
56
101
|
echo "Clearing port ${DEPLOY_RUN_PORT} before start."
|
|
57
102
|
kill_port_if_listening
|
|
103
|
+
bash "${COZE_WORKSPACE_PATH}/scripts/prepare-node-modules.sh" --prefer-frozen-lockfile --prefer-offline --loglevel debug --reporter=append-only
|
|
104
|
+
warn_source_symlinks
|
|
105
|
+
refresh_drive_route_mtimes
|
|
58
106
|
echo "Starting HTTP service on port ${DEPLOY_RUN_PORT} for dev..."
|
|
59
107
|
|
|
60
108
|
# 测试环境保持服务为前台进程,便于 e2e 收集输出并停止进程。
|
|
61
|
-
|
|
109
|
+
# 开发环境直接使用 Next 的 dev CLI,避免云盘文件事件在 custom server 链路中丢失。
|
|
110
|
+
exec pnpm next dev --webpack --hostname 0.0.0.0 --port "${DEPLOY_RUN_PORT}"
|
|
62
111
|
<% } else { %>
|
|
63
112
|
LOG_DIR="${COZE_LOG_DIR:-${COZE_WORKSPACE_PATH}/logs}"
|
|
64
113
|
LOG_FILE="${LOG_DIR}/app.log"
|
|
65
114
|
PID_FILE="${LOG_DIR}/server.pid"
|
|
66
115
|
|
|
67
116
|
# detached 出去的进程没人负责回收,超过这个时长就自己退出,避免端口与内存长期泄露。
|
|
68
|
-
MAX_RUNTIME_SECONDS=
|
|
117
|
+
MAX_RUNTIME_SECONDS=3600
|
|
118
|
+
|
|
119
|
+
timeout_watchdog_enabled() {
|
|
120
|
+
[[ -z "${COZE_EVAL:-}" && -z "${COZE_PROJECT_TYPE:-}" ]]
|
|
121
|
+
}
|
|
69
122
|
|
|
70
123
|
# 真正被 detach 的是这层 bash wrapper:它是进程组 leader,组内 watchdog 到点回收整组
|
|
71
|
-
# (wrapper -> pnpm ->
|
|
72
|
-
RUN_WITH_TIMEOUT='
|
|
124
|
+
# (wrapper -> pnpm -> next -> node);被包的进程自己先退出时也顺手清空进程组,不留残余。
|
|
125
|
+
RUN_WITH_TIMEOUT="$(declare -f timeout_watchdog_enabled)"'
|
|
73
126
|
timeout_seconds=$1
|
|
74
127
|
shift
|
|
75
128
|
|
|
@@ -77,6 +130,7 @@ shift
|
|
|
77
130
|
child_pid=$!
|
|
78
131
|
|
|
79
132
|
# 先忽略 TERM,才能在向整组发 TERM(自己也在组里)之后存活下来补一发 KILL。
|
|
133
|
+
if timeout_watchdog_enabled; then
|
|
80
134
|
( trap "" TERM
|
|
81
135
|
sleep "${timeout_seconds}"
|
|
82
136
|
echo "[dev] 后台进程运行超过 ${timeout_seconds}s,回收进程组 $$。"
|
|
@@ -84,6 +138,7 @@ child_pid=$!
|
|
|
84
138
|
sleep 5
|
|
85
139
|
kill -KILL -- "-$$" 2>/dev/null || true
|
|
86
140
|
) &
|
|
141
|
+
fi
|
|
87
142
|
|
|
88
143
|
wait "${child_pid}"
|
|
89
144
|
kill -KILL -- "-$$" 2>/dev/null || true
|
|
@@ -132,7 +187,7 @@ stop_detached() {
|
|
|
132
187
|
kill -KILL -- "-${pid}" 2>/dev/null || true
|
|
133
188
|
}
|
|
134
189
|
|
|
135
|
-
READY_RETRIES=
|
|
190
|
+
READY_RETRIES=30
|
|
136
191
|
READY_PROBE_HOSTS=("127.0.0.1" "::1")
|
|
137
192
|
LOG_TAIL_LINES=40
|
|
138
193
|
|
|
@@ -142,6 +197,21 @@ dump_log() {
|
|
|
142
197
|
echo "---- end of ${LOG_FILE} ----" >&2
|
|
143
198
|
}
|
|
144
199
|
|
|
200
|
+
# 服务刚就绪时 app-paths-manifest.json 还是空的(dev 按需编译,没请求就没产物),
|
|
201
|
+
# 但 .next/dev/types/routes.d.ts 已经写出了本次启动发现到的全部路由。把它落进日志,
|
|
202
|
+
# 排查时才能区分“路由没被发现”和“路由发现了但还没编译”。
|
|
203
|
+
dump_discovered_routes() {
|
|
204
|
+
local route_types="${COZE_WORKSPACE_PATH}/.next/dev/types/routes.d.ts"
|
|
205
|
+
if [[ ! -f "${route_types}" ]]; then
|
|
206
|
+
return
|
|
207
|
+
fi
|
|
208
|
+
{
|
|
209
|
+
echo "---- discovered routes (${route_types}) ----"
|
|
210
|
+
grep -E '^type (AppRoutes|AppRouteHandlerRoutes|PageRoutes) ' "${route_types}" || true
|
|
211
|
+
echo "---- end of discovered routes ----"
|
|
212
|
+
} >> "${LOG_FILE}"
|
|
213
|
+
}
|
|
214
|
+
|
|
145
215
|
# 仅用于无 ss/lsof 时的兜底探测。服务可能只 bind IPv4 loopback、只 bind IPv6 loopback
|
|
146
216
|
# (server.listen(port, 'localhost') 会变成 ::1 独占),或 bind 双栈通配地址,
|
|
147
217
|
port_connectable() {
|
|
@@ -215,7 +285,7 @@ wait_for_ready() {
|
|
|
215
285
|
return 2
|
|
216
286
|
}
|
|
217
287
|
|
|
218
|
-
# 监听端口的是孙进程(pnpm ->
|
|
288
|
+
# 监听端口的是孙进程(pnpm -> next -> node),只清端口会漏掉上层 pnpm/next,
|
|
219
289
|
# 所以先按上次记录的 PID 把整个进程组回收掉。
|
|
220
290
|
if [[ -f "${PID_FILE}" ]]; then
|
|
221
291
|
stop_detached "$(cat "${PID_FILE}" 2>/dev/null || true)"
|
|
@@ -224,6 +294,9 @@ fi
|
|
|
224
294
|
|
|
225
295
|
echo "Clearing port ${DEPLOY_RUN_PORT} before start."
|
|
226
296
|
kill_port_if_listening
|
|
297
|
+
bash "${COZE_WORKSPACE_PATH}/scripts/prepare-node-modules.sh" --prefer-frozen-lockfile --prefer-offline --loglevel debug --reporter=append-only
|
|
298
|
+
warn_source_symlinks
|
|
299
|
+
refresh_drive_route_mtimes
|
|
227
300
|
echo "Starting HTTP service on port ${DEPLOY_RUN_PORT} for dev..."
|
|
228
301
|
|
|
229
302
|
mkdir -p "${LOG_DIR}"
|
|
@@ -231,7 +304,7 @@ mkdir -p "${LOG_DIR}"
|
|
|
231
304
|
|
|
232
305
|
export PORT="${DEPLOY_RUN_PORT}"
|
|
233
306
|
server_pid="$(spawn_detached "${COZE_WORKSPACE_PATH}" "${LOG_FILE}" \
|
|
234
|
-
"$(command -v pnpm)"
|
|
307
|
+
"$(command -v pnpm)" next dev --webpack --hostname 0.0.0.0 --port "${DEPLOY_RUN_PORT}")"
|
|
235
308
|
if [[ -z "${server_pid}" ]]; then
|
|
236
309
|
echo "Dev server failed to start: 未获取到后台进程 PID。" >&2
|
|
237
310
|
dump_log
|
|
@@ -247,8 +320,7 @@ fi
|
|
|
247
320
|
|
|
248
321
|
if [[ "${ready_status}" -ne 0 ]]; then
|
|
249
322
|
if [[ "${ready_status}" -eq 2 ]]; then
|
|
250
|
-
|
|
251
|
-
echo "Dev server 在 ${READY_RETRIES} 次探测(每次 1s)内未就绪于端口 ${DEPLOY_RUN_PORT}(进程 ${server_pid} 仍在运行,已回收)。" >&2
|
|
323
|
+
echo "Dev server did not listen on port ${DEPLOY_RUN_PORT} after ${READY_RETRIES} attempts; stopping PID ${server_pid}." >&2
|
|
252
324
|
else
|
|
253
325
|
echo "Dev server failed to start. See ${LOG_FILE}." >&2
|
|
254
326
|
fi
|
|
@@ -259,8 +331,12 @@ if [[ "${ready_status}" -ne 0 ]]; then
|
|
|
259
331
|
exit 1
|
|
260
332
|
fi
|
|
261
333
|
|
|
334
|
+
dump_discovered_routes
|
|
335
|
+
|
|
262
336
|
echo "Dev server started (PID: ${server_pid}), listening on port ${DEPLOY_RUN_PORT}."
|
|
263
|
-
|
|
337
|
+
if timeout_watchdog_enabled; then
|
|
338
|
+
echo "Auto stop after ${MAX_RUNTIME_SECONDS}s."
|
|
339
|
+
fi
|
|
264
340
|
echo "Log file: ${LOG_FILE}"
|
|
265
341
|
echo "PID file: ${PID_FILE}"
|
|
266
342
|
<% } %>
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Install dependencies on local disk and expose them to the source tree by symlink.
|
|
3
|
+
set -euo pipefail
|
|
4
|
+
|
|
5
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
6
|
+
if [ "$(basename "$(dirname "$SCRIPT_DIR")")" = ".cozeproj" ]; then
|
|
7
|
+
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd -P)"
|
|
8
|
+
else
|
|
9
|
+
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
|
|
10
|
+
fi
|
|
11
|
+
|
|
12
|
+
command -v pnpm >/dev/null 2>&1 || {
|
|
13
|
+
echo "[node_modules] pnpm is required" >&2
|
|
14
|
+
exit 1
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
DRIVE_ROOT="${COZE_DRIVE_ROOT:-/Coze/Drive}"
|
|
18
|
+
if [ -d "$DRIVE_ROOT" ]; then
|
|
19
|
+
DRIVE_ROOT="$(cd "$DRIVE_ROOT" && pwd -P)"
|
|
20
|
+
else
|
|
21
|
+
DRIVE_ROOT="${DRIVE_ROOT%/}"
|
|
22
|
+
fi
|
|
23
|
+
case "$PROJECT_ROOT" in
|
|
24
|
+
"$DRIVE_ROOT"|"$DRIVE_ROOT"/*) ;;
|
|
25
|
+
*)
|
|
26
|
+
(cd "$PROJECT_ROOT" && pnpm install "$@")
|
|
27
|
+
exit 0
|
|
28
|
+
;;
|
|
29
|
+
esac
|
|
30
|
+
|
|
31
|
+
NM_ROOT="/tmp/nm"
|
|
32
|
+
PROJECT_ID="$(basename "$PROJECT_ROOT")-$(printf '%s' "$PROJECT_ROOT" | cksum | awk '{print $1}')"
|
|
33
|
+
mkdir -p "$NM_ROOT"
|
|
34
|
+
WORK_DIR="$NM_ROOT/$PROJECT_ID"
|
|
35
|
+
case "$WORK_DIR" in
|
|
36
|
+
"$DRIVE_ROOT"|"$DRIVE_ROOT"/*)
|
|
37
|
+
echo "[node_modules] local dependency directory must not be on Coze Drive: $WORK_DIR" >&2
|
|
38
|
+
exit 1
|
|
39
|
+
;;
|
|
40
|
+
esac
|
|
41
|
+
|
|
42
|
+
LOCK_DIR="$NM_ROOT/$PROJECT_ID.lock"
|
|
43
|
+
LOCK_TIMEOUT_SECONDS="${COZE_NODE_MODULES_LOCK_TIMEOUT_SECONDS:-120}"
|
|
44
|
+
case "$LOCK_TIMEOUT_SECONDS" in
|
|
45
|
+
''|*[!0-9]*|0)
|
|
46
|
+
echo "[node_modules] COZE_NODE_MODULES_LOCK_TIMEOUT_SECONDS must be a positive integer" >&2
|
|
47
|
+
exit 1
|
|
48
|
+
;;
|
|
49
|
+
esac
|
|
50
|
+
|
|
51
|
+
lock_owner_is_alive() {
|
|
52
|
+
local owner_pid=""
|
|
53
|
+
[ -f "$LOCK_DIR/pid" ] || return 1
|
|
54
|
+
IFS= read -r owner_pid < "$LOCK_DIR/pid" || return 1
|
|
55
|
+
case "$owner_pid" in
|
|
56
|
+
''|*[!0-9]*) return 1 ;;
|
|
57
|
+
esac
|
|
58
|
+
kill -0 "$owner_pid" 2>/dev/null
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
release_lock() {
|
|
62
|
+
local owner_pid=""
|
|
63
|
+
[ -f "$LOCK_DIR/pid" ] || return 0
|
|
64
|
+
IFS= read -r owner_pid < "$LOCK_DIR/pid" || true
|
|
65
|
+
if [ "$owner_pid" = "$$" ]; then
|
|
66
|
+
rm -f "$LOCK_DIR/pid"
|
|
67
|
+
rmdir "$LOCK_DIR" 2>/dev/null || true
|
|
68
|
+
fi
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
lock_started_at="$(date +%s)"
|
|
72
|
+
missing_owner_checks=0
|
|
73
|
+
while ! mkdir "$LOCK_DIR" 2>/dev/null; do
|
|
74
|
+
if lock_owner_is_alive; then
|
|
75
|
+
missing_owner_checks=0
|
|
76
|
+
else
|
|
77
|
+
missing_owner_checks=$((missing_owner_checks + 1))
|
|
78
|
+
if [ "$missing_owner_checks" -ge 2 ]; then
|
|
79
|
+
stale_lock_dir="$LOCK_DIR.stale.$$"
|
|
80
|
+
if mv "$LOCK_DIR" "$stale_lock_dir" 2>/dev/null; then
|
|
81
|
+
echo "[node_modules] Recovering stale dependency preparation lock."
|
|
82
|
+
rm -rf "$stale_lock_dir"
|
|
83
|
+
fi
|
|
84
|
+
missing_owner_checks=0
|
|
85
|
+
continue
|
|
86
|
+
fi
|
|
87
|
+
fi
|
|
88
|
+
|
|
89
|
+
lock_now="$(date +%s)"
|
|
90
|
+
if [ $((lock_now - lock_started_at)) -ge "$LOCK_TIMEOUT_SECONDS" ]; then
|
|
91
|
+
echo "[node_modules] Timed out waiting ${LOCK_TIMEOUT_SECONDS}s for dependency preparation lock: $LOCK_DIR" >&2
|
|
92
|
+
exit 1
|
|
93
|
+
fi
|
|
94
|
+
echo "[node_modules] Waiting for another dependency preparation for this project..."
|
|
95
|
+
sleep 1
|
|
96
|
+
done
|
|
97
|
+
printf '%s\n' "$$" > "$LOCK_DIR/pid"
|
|
98
|
+
trap release_lock EXIT
|
|
99
|
+
|
|
100
|
+
mkdir -p "$WORK_DIR"
|
|
101
|
+
|
|
102
|
+
# Previous versions mirrored the whole project into WORK_DIR. That gives pnpm
|
|
103
|
+
# and framework dev servers two paths for the same source tree, which breaks
|
|
104
|
+
# file watching and on-demand compilation. Keep only managed local outputs.
|
|
105
|
+
cleanup_legacy_workspace() {
|
|
106
|
+
local workspace_dir="$1" entry name
|
|
107
|
+
if [ -L "$workspace_dir" ] || { [ -e "$workspace_dir" ] && [ ! -d "$workspace_dir" ]; }; then
|
|
108
|
+
rm -rf "$workspace_dir"
|
|
109
|
+
return
|
|
110
|
+
fi
|
|
111
|
+
if [ ! -d "$workspace_dir" ]; then
|
|
112
|
+
return 0
|
|
113
|
+
fi
|
|
114
|
+
|
|
115
|
+
while IFS= read -r -d '' entry; do
|
|
116
|
+
name="$(basename "$entry")"
|
|
117
|
+
[ "$name" = "node_modules" ] || rm -rf "$entry"
|
|
118
|
+
done < <(find "$workspace_dir" -mindepth 1 -maxdepth 1 -print0)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
cleanup_legacy_shadow_project() {
|
|
122
|
+
local entry name
|
|
123
|
+
while IFS= read -r -d '' entry; do
|
|
124
|
+
name="$(basename "$entry")"
|
|
125
|
+
case "$name" in
|
|
126
|
+
node_modules|client|server) continue ;;
|
|
127
|
+
esac
|
|
128
|
+
rm -rf "$entry"
|
|
129
|
+
done < <(find "$WORK_DIR" -mindepth 1 -maxdepth 1 -print0)
|
|
130
|
+
|
|
131
|
+
for workspace in client server; do
|
|
132
|
+
cleanup_legacy_workspace "$WORK_DIR/$workspace"
|
|
133
|
+
done
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
link_install_entry() {
|
|
137
|
+
local source_path="$1" target_path="$2"
|
|
138
|
+
rm -rf "$target_path"
|
|
139
|
+
if [ -e "$source_path" ]; then
|
|
140
|
+
ln -s "$source_path" "$target_path"
|
|
141
|
+
fi
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
copy_install_metadata() {
|
|
145
|
+
local filename source_file target_file workspace
|
|
146
|
+
for filename in pnpm-lock.yaml pnpm-workspace.yaml .npmrc; do
|
|
147
|
+
source_file="$PROJECT_ROOT/$filename"
|
|
148
|
+
target_file="$WORK_DIR/$filename"
|
|
149
|
+
rm -f "$target_file"
|
|
150
|
+
if [ -f "$source_file" ]; then
|
|
151
|
+
cp "$source_file" "$target_file"
|
|
152
|
+
fi
|
|
153
|
+
done
|
|
154
|
+
|
|
155
|
+
link_install_entry "$PROJECT_ROOT/package.json" "$WORK_DIR/package.json"
|
|
156
|
+
link_install_entry "$PROJECT_ROOT/patches" "$WORK_DIR/patches"
|
|
157
|
+
link_install_entry "$PROJECT_ROOT/scripts" "$WORK_DIR/scripts"
|
|
158
|
+
|
|
159
|
+
for workspace in client server; do
|
|
160
|
+
if [ -f "$PROJECT_ROOT/$workspace/package.json" ]; then
|
|
161
|
+
mkdir -p "$WORK_DIR/$workspace"
|
|
162
|
+
link_install_entry "$PROJECT_ROOT/$workspace/package.json" "$WORK_DIR/$workspace/package.json"
|
|
163
|
+
link_install_entry "$PROJECT_ROOT/$workspace/scripts" "$WORK_DIR/$workspace/scripts"
|
|
164
|
+
else
|
|
165
|
+
rm -rf "$WORK_DIR/$workspace"
|
|
166
|
+
fi
|
|
167
|
+
done
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
link_node_modules() {
|
|
171
|
+
local source_dir="$1" target_dir="$2"
|
|
172
|
+
local current=""
|
|
173
|
+
mkdir -p "$target_dir"
|
|
174
|
+
if [ -L "$source_dir/node_modules" ]; then
|
|
175
|
+
current="$(readlink "$source_dir/node_modules")"
|
|
176
|
+
case "$current" in
|
|
177
|
+
"$NM_ROOT"/*/node_modules|"$NM_ROOT"/*/*/node_modules) ;;
|
|
178
|
+
*)
|
|
179
|
+
echo "[node_modules] refusing to replace unmanaged symlink: $source_dir/node_modules" >&2
|
|
180
|
+
exit 1
|
|
181
|
+
;;
|
|
182
|
+
esac
|
|
183
|
+
rm "$source_dir/node_modules"
|
|
184
|
+
elif [ -e "$source_dir/node_modules" ]; then
|
|
185
|
+
rm -rf "$source_dir/node_modules"
|
|
186
|
+
fi
|
|
187
|
+
ln -s "$target_dir" "$source_dir/node_modules"
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
cleanup_legacy_shadow_project
|
|
191
|
+
copy_install_metadata
|
|
192
|
+
|
|
193
|
+
echo "[node_modules] Installing dependencies in $WORK_DIR"
|
|
194
|
+
if ! (cd "$WORK_DIR" && pnpm install "$@"); then
|
|
195
|
+
exit 1
|
|
196
|
+
fi
|
|
197
|
+
|
|
198
|
+
if [ -f "$WORK_DIR/pnpm-lock.yaml" ]; then
|
|
199
|
+
temporary_lockfile="$(mktemp "$PROJECT_ROOT/.pnpm-lock.yaml.XXXXXX")"
|
|
200
|
+
cp "$WORK_DIR/pnpm-lock.yaml" "$temporary_lockfile"
|
|
201
|
+
chmod 0644 "$temporary_lockfile"
|
|
202
|
+
mv -f "$temporary_lockfile" "$PROJECT_ROOT/pnpm-lock.yaml"
|
|
203
|
+
fi
|
|
204
|
+
|
|
205
|
+
link_node_modules "$PROJECT_ROOT" "$WORK_DIR/node_modules"
|
|
206
|
+
for workspace in client server; do
|
|
207
|
+
if [ -f "$PROJECT_ROOT/$workspace/package.json" ]; then
|
|
208
|
+
link_node_modules "$PROJECT_ROOT/$workspace" "$WORK_DIR/$workspace/node_modules"
|
|
209
|
+
fi
|
|
210
|
+
done
|
|
211
|
+
|
|
212
|
+
echo "[node_modules] Dependencies ready"
|
|
@@ -6,7 +6,4 @@ COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-$(pwd)}"
|
|
|
6
6
|
cd "${COZE_WORKSPACE_PATH}"
|
|
7
7
|
|
|
8
8
|
echo "Installing dependencies..."
|
|
9
|
-
|
|
10
|
-
if command -v coze-dev > /dev/null 2>&1 && coze-dev check-bins --help > /dev/null 2>&1; then
|
|
11
|
-
coze-dev check-bins --fix
|
|
12
|
-
fi
|
|
9
|
+
bash "$COZE_WORKSPACE_PATH/scripts/prepare-node-modules.sh" --prefer-frozen-lockfile --prefer-offline --loglevel debug --reporter=append-only
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { Metadata } from 'next';
|
|
2
|
-
import { Inspector } from 'react-dev-inspector';
|
|
3
2
|
import './globals.css';
|
|
4
3
|
|
|
5
4
|
export const metadata: Metadata = {
|
|
@@ -61,12 +60,9 @@ export default function RootLayout({
|
|
|
61
60
|
}: Readonly<{
|
|
62
61
|
children: React.ReactNode;
|
|
63
62
|
}>) {
|
|
64
|
-
const isDev = process.env.COZE_PROJECT_ENV === 'DEV';
|
|
65
|
-
|
|
66
63
|
return (
|
|
67
64
|
<html lang="en">
|
|
68
65
|
<body className={`antialiased`}>
|
|
69
|
-
{isDev && <Inspector />}
|
|
70
66
|
{children}
|
|
71
67
|
</body>
|
|
72
68
|
</html>
|
|
@@ -7,7 +7,7 @@ const hostname = process.env.HOSTNAME || 'localhost';
|
|
|
7
7
|
const port = parseInt(process.env.PORT || '<%= port %>', 10);
|
|
8
8
|
|
|
9
9
|
// Create Next.js app
|
|
10
|
-
const app = next({ dev, hostname, port });
|
|
10
|
+
const app = next({ dev, hostname, port, webpack: true });
|
|
11
11
|
const handle = app.getRequestHandler();
|
|
12
12
|
|
|
13
13
|
app.prepare().then(() => {
|
|
@@ -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
|
+
bash "$COZE_WORKSPACE_PATH/scripts/prepare-node-modules.sh" --prefer-frozen-lockfile --prefer-offline --loglevel debug --reporter=append-only
|
|
10
10
|
|
|
11
11
|
echo "Building the Nuxt.js project..."
|
|
12
12
|
pnpm nuxt build
|
|
@@ -65,11 +65,15 @@ LOG_FILE="${LOG_DIR}/server.log"
|
|
|
65
65
|
PID_FILE="${LOG_DIR}/server.pid"
|
|
66
66
|
|
|
67
67
|
# detached 出去的进程没人负责回收,超过这个时长就自己退出,避免端口与内存长期泄露。
|
|
68
|
-
MAX_RUNTIME_SECONDS=
|
|
68
|
+
MAX_RUNTIME_SECONDS=3600
|
|
69
|
+
|
|
70
|
+
timeout_watchdog_enabled() {
|
|
71
|
+
[[ -z "${COZE_EVAL:-}" && -z "${COZE_PROJECT_TYPE:-}" ]]
|
|
72
|
+
}
|
|
69
73
|
|
|
70
74
|
# 真正被 detach 的是这层 bash wrapper:它是进程组 leader,组内 watchdog 到点回收整组
|
|
71
75
|
# (wrapper -> pnpm -> nuxt -> node);被包的进程自己先退出时也顺手清空进程组,不留残余。
|
|
72
|
-
RUN_WITH_TIMEOUT='
|
|
76
|
+
RUN_WITH_TIMEOUT="$(declare -f timeout_watchdog_enabled)"'
|
|
73
77
|
timeout_seconds=$1
|
|
74
78
|
shift
|
|
75
79
|
|
|
@@ -77,6 +81,7 @@ shift
|
|
|
77
81
|
child_pid=$!
|
|
78
82
|
|
|
79
83
|
# 先忽略 TERM,才能在向整组发 TERM(自己也在组里)之后存活下来补一发 KILL。
|
|
84
|
+
if timeout_watchdog_enabled; then
|
|
80
85
|
( trap "" TERM
|
|
81
86
|
sleep "${timeout_seconds}"
|
|
82
87
|
echo "[dev] 后台进程运行超过 ${timeout_seconds}s,回收进程组 $$。"
|
|
@@ -84,6 +89,7 @@ child_pid=$!
|
|
|
84
89
|
sleep 5
|
|
85
90
|
kill -KILL -- "-$$" 2>/dev/null || true
|
|
86
91
|
) &
|
|
92
|
+
fi
|
|
87
93
|
|
|
88
94
|
wait "${child_pid}"
|
|
89
95
|
kill -KILL -- "-$$" 2>/dev/null || true
|
|
@@ -262,7 +268,9 @@ if [[ "${ready_status}" -ne 0 ]]; then
|
|
|
262
268
|
fi
|
|
263
269
|
|
|
264
270
|
echo "Dev server started (PID: ${server_pid}), listening on port ${DEPLOY_RUN_PORT}."
|
|
265
|
-
|
|
271
|
+
if timeout_watchdog_enabled; then
|
|
272
|
+
echo "Auto stop after ${MAX_RUNTIME_SECONDS}s."
|
|
273
|
+
fi
|
|
266
274
|
echo "Log file: ${LOG_FILE}"
|
|
267
275
|
echo "PID file: ${PID_FILE}"
|
|
268
276
|
<% } %>
|