@coze-arch/cli 0.1.5 → 0.1.8-alpha.2ee669
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/docs/deploy.md +12 -1
- package/lib/__templates__/expo/.cozeproj/scripts/dev_build.sh +3 -3
- package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +11 -3
- package/lib/__templates__/expo/.cozeproj/scripts/prepare-node-modules.sh +380 -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__/native-static/.coze +2 -2
- package/lib/__templates__/nextjs/.coze +1 -1
- package/lib/__templates__/nextjs/README.md +18 -0
- 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 +1 -1
- package/lib/__templates__/nextjs/scripts/dev.ps1 +2 -2
- package/lib/__templates__/nextjs/scripts/dev.sh +70 -12
- package/lib/__templates__/nextjs/scripts/local-workspace.cjs +302 -0
- package/lib/__templates__/nextjs/scripts/prepare-node-modules.sh +380 -0
- package/lib/__templates__/nextjs/scripts/prepare.sh +9 -4
- package/lib/__templates__/nextjs/scripts/validate.sh +11 -1
- package/lib/__templates__/nextjs/src/app/layout.tsx +0 -4
- 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/dev.sh +21 -7
- package/lib/__templates__/nuxt-vue/scripts/local-workspace.cjs +302 -0
- package/lib/__templates__/nuxt-vue/scripts/prepare-node-modules.sh +380 -0
- package/lib/__templates__/nuxt-vue/scripts/prepare.sh +9 -4
- 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 +10 -1
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +4 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +12 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_run.ps1 +4 -2
- package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +84 -10
- package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +4 -0
- package/lib/__templates__/taro/.cozeproj/scripts/local-workspace.cjs +351 -0
- package/lib/__templates__/taro/.cozeproj/scripts/pack.ps1 +3 -1
- package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +24 -6
- package/lib/__templates__/taro/.cozeproj/scripts/prepare-node-modules.sh +380 -0
- package/lib/__templates__/taro/.cozeproj/scripts/taro-build.cjs +66 -0
- package/lib/__templates__/taro/.cozeproj/scripts/validate.ps1 +3 -1
- package/lib/__templates__/taro/.cozeproj/scripts/validate.sh +7 -0
- package/lib/__templates__/taro/_gitignore +1 -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/dev.ps1 +2 -2
- package/lib/__templates__/vite/scripts/dev.sh +21 -7
- package/lib/__templates__/vite/scripts/local-workspace.cjs +302 -0
- package/lib/__templates__/vite/scripts/prepare-node-modules.sh +380 -0
- package/lib/__templates__/vite/scripts/prepare.sh +9 -4
- package/lib/__templates__/vite/scripts/validate.sh +7 -1
- package/lib/__templates__/vite/template.config.js +20 -6
- package/lib/cli.js +511 -199
- package/package.json +3 -2
- package/lib/__templates__/nextjs/.babelrc +0 -15
|
@@ -0,0 +1,380 @@
|
|
|
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
|
+
INSTALL_STATE_FILE="$WORK_DIR/.install-state"
|
|
102
|
+
|
|
103
|
+
# Previous versions mirrored the whole project into WORK_DIR. That gives pnpm
|
|
104
|
+
# and framework dev servers two paths for the same source tree, which breaks
|
|
105
|
+
# file watching and on-demand compilation. Keep only managed local outputs.
|
|
106
|
+
cleanup_legacy_workspace() {
|
|
107
|
+
local workspace_dir="$1" entry name
|
|
108
|
+
if [ -L "$workspace_dir" ] || { [ -e "$workspace_dir" ] && [ ! -d "$workspace_dir" ]; }; then
|
|
109
|
+
rm -rf "$workspace_dir"
|
|
110
|
+
return
|
|
111
|
+
fi
|
|
112
|
+
if [ ! -d "$workspace_dir" ]; then
|
|
113
|
+
return 0
|
|
114
|
+
fi
|
|
115
|
+
|
|
116
|
+
while IFS= read -r -d '' entry; do
|
|
117
|
+
name="$(basename "$entry")"
|
|
118
|
+
[ "$name" = "node_modules" ] || rm -rf "$entry"
|
|
119
|
+
done < <(find "$workspace_dir" -mindepth 1 -maxdepth 1 -print0)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
# Preserve existing project caches during dependency preparation.
|
|
123
|
+
cleanup_legacy_shadow_project() {
|
|
124
|
+
local entry name
|
|
125
|
+
while IFS= read -r -d '' entry; do
|
|
126
|
+
name="$(basename "$entry")"
|
|
127
|
+
case "$name" in
|
|
128
|
+
node_modules|client|server|caches|.install-state) continue ;;
|
|
129
|
+
esac
|
|
130
|
+
rm -rf "$entry"
|
|
131
|
+
done < <(find "$WORK_DIR" -mindepth 1 -maxdepth 1 -print0)
|
|
132
|
+
|
|
133
|
+
for workspace in client server; do
|
|
134
|
+
cleanup_legacy_workspace "$WORK_DIR/$workspace"
|
|
135
|
+
done
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
link_install_entry() {
|
|
139
|
+
local source_path="$1" target_path="$2"
|
|
140
|
+
rm -rf "$target_path"
|
|
141
|
+
if [ -e "$source_path" ]; then
|
|
142
|
+
ln -s "$source_path" "$target_path"
|
|
143
|
+
fi
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
hash_install_input() {
|
|
147
|
+
if command -v sha256sum >/dev/null 2>&1; then
|
|
148
|
+
sha256sum | awk '{print $1}'
|
|
149
|
+
elif command -v shasum >/dev/null 2>&1; then
|
|
150
|
+
shasum -a 256 | awk '{print $1}'
|
|
151
|
+
else
|
|
152
|
+
cksum | awk '{print $1 "-" $2}'
|
|
153
|
+
fi
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
append_file_to_install_fingerprint() {
|
|
157
|
+
local relative_path="$1"
|
|
158
|
+
local source_path="$PROJECT_ROOT/$relative_path"
|
|
159
|
+
printf 'path=%s\n' "$relative_path"
|
|
160
|
+
if [ -f "$source_path" ]; then
|
|
161
|
+
cat "$source_path"
|
|
162
|
+
else
|
|
163
|
+
printf '<missing>\n'
|
|
164
|
+
fi
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
append_directory_to_install_fingerprint() {
|
|
168
|
+
local relative_path="$1"
|
|
169
|
+
local source_path="$PROJECT_ROOT/$relative_path"
|
|
170
|
+
local file
|
|
171
|
+
printf 'path=%s\n' "$relative_path"
|
|
172
|
+
if [ ! -d "$source_path" ]; then
|
|
173
|
+
printf '<missing>\n'
|
|
174
|
+
return
|
|
175
|
+
fi
|
|
176
|
+
|
|
177
|
+
while IFS= read -r file; do
|
|
178
|
+
printf 'file=%s\n' "${file#"${PROJECT_ROOT}/"}"
|
|
179
|
+
cat "$file"
|
|
180
|
+
done < <(find -P "$source_path" -type f -print | LC_ALL=C sort)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
# Everything that can change the resolved dependency tree. The leading version marker
|
|
184
|
+
# invalidates every stored state when this scheme itself changes.
|
|
185
|
+
calculate_install_fingerprint() {
|
|
186
|
+
local filename workspace argument
|
|
187
|
+
{
|
|
188
|
+
printf 'prepare-node-modules-install-state=1\n'
|
|
189
|
+
printf 'node-version=%s\n' "$(node --version)"
|
|
190
|
+
printf 'pnpm-version=%s\n' "$(pnpm --version)"
|
|
191
|
+
printf 'node-env=%s\n' "${NODE_ENV:-}"
|
|
192
|
+
printf 'npm-config-production=%s\n' "${npm_config_production:-}"
|
|
193
|
+
printf 'npm-config-production-upper=%s\n' "${NPM_CONFIG_PRODUCTION:-}"
|
|
194
|
+
printf 'pnpm-config-production=%s\n' "${PNPM_CONFIG_PRODUCTION:-}"
|
|
195
|
+
for argument in "$@"; do
|
|
196
|
+
printf 'install-argument=%s\n' "$argument"
|
|
197
|
+
done
|
|
198
|
+
for filename in package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc; do
|
|
199
|
+
append_file_to_install_fingerprint "$filename"
|
|
200
|
+
done
|
|
201
|
+
for workspace in client server; do
|
|
202
|
+
append_file_to_install_fingerprint "$workspace/package.json"
|
|
203
|
+
done
|
|
204
|
+
append_directory_to_install_fingerprint patches
|
|
205
|
+
} | hash_install_input
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
install_requires_refresh() {
|
|
209
|
+
local argument
|
|
210
|
+
for argument in "$@"; do
|
|
211
|
+
case "$argument" in
|
|
212
|
+
--force|-f|--lockfile-only|--fix-lockfile) return 0 ;;
|
|
213
|
+
esac
|
|
214
|
+
done
|
|
215
|
+
return 1
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
# Reuse skips pnpm install entirely, so it must not skip side effects that live outside
|
|
219
|
+
# node_modules. preinstall is exempt: it only guards which package manager may run, and
|
|
220
|
+
# there is no install to guard when the tree is already in place.
|
|
221
|
+
package_has_install_lifecycle() {
|
|
222
|
+
local package_path="$1"
|
|
223
|
+
node - "$package_path" <<'NODE'
|
|
224
|
+
const fs = require('node:fs');
|
|
225
|
+
const [packagePath] = process.argv.slice(2);
|
|
226
|
+
|
|
227
|
+
try {
|
|
228
|
+
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
|
|
229
|
+
const scripts = packageJson && typeof packageJson.scripts === 'object' && packageJson.scripts
|
|
230
|
+
? packageJson.scripts
|
|
231
|
+
: {};
|
|
232
|
+
const hasLifecycle = ['install', 'postinstall', 'prepare'].some(
|
|
233
|
+
name => typeof scripts[name] === 'string' && scripts[name].trim() !== '',
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
process.exit(hasLifecycle ? 0 : 1);
|
|
237
|
+
} catch {
|
|
238
|
+
process.exit(0);
|
|
239
|
+
}
|
|
240
|
+
NODE
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
install_reuse_is_safe() {
|
|
244
|
+
local workspace package_path
|
|
245
|
+
for package_path in "$PROJECT_ROOT/.pnpmfile.cjs" "$PROJECT_ROOT/.pnpmfile.js"; do
|
|
246
|
+
if [ -e "$package_path" ]; then
|
|
247
|
+
echo "[node_modules] Reinstalling because pnpm hooks are present: $package_path"
|
|
248
|
+
return 1
|
|
249
|
+
fi
|
|
250
|
+
done
|
|
251
|
+
|
|
252
|
+
if [ -f "$PROJECT_ROOT/.npmrc" ] && grep -Eq '^[[:space:]]*pnpmfile[[:space:]]*=' "$PROJECT_ROOT/.npmrc"; then
|
|
253
|
+
echo "[node_modules] Reinstalling because .npmrc configures a pnpm hook."
|
|
254
|
+
return 1
|
|
255
|
+
fi
|
|
256
|
+
|
|
257
|
+
for workspace in . client server; do
|
|
258
|
+
package_path="$PROJECT_ROOT/$workspace/package.json"
|
|
259
|
+
[ -f "$package_path" ] || continue
|
|
260
|
+
if package_has_install_lifecycle "$package_path"; then
|
|
261
|
+
echo "[node_modules] Reinstalling because $package_path has an install lifecycle script."
|
|
262
|
+
return 1
|
|
263
|
+
fi
|
|
264
|
+
done
|
|
265
|
+
return 0
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
dependencies_are_ready() {
|
|
269
|
+
local workspace
|
|
270
|
+
[ -f "$WORK_DIR/node_modules/.modules.yaml" ] || return 1
|
|
271
|
+
for workspace in client server; do
|
|
272
|
+
if [ -f "$PROJECT_ROOT/$workspace/package.json" ] &&
|
|
273
|
+
[ ! -f "$WORK_DIR/$workspace/node_modules/.modules.yaml" ]; then
|
|
274
|
+
return 1
|
|
275
|
+
fi
|
|
276
|
+
done
|
|
277
|
+
return 0
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
# 判断顺序按代价从小到大排。指纹要读云盘上的 lockfile 和整个 patches/,所以放最后:只有其余
|
|
281
|
+
# 条件都满足、确实可能复用时才付这份开销。build.sh 和 prepare.sh 不设开关,第一行就返回了。
|
|
282
|
+
install_can_be_reused() {
|
|
283
|
+
local installed_fingerprint=""
|
|
284
|
+
|
|
285
|
+
[ "${COZE_REUSE_NODE_MODULES:-}" = "1" ] || return 1
|
|
286
|
+
install_requires_refresh "$@" && return 1
|
|
287
|
+
[ -f "$INSTALL_STATE_FILE" ] || return 1
|
|
288
|
+
IFS= read -r installed_fingerprint < "$INSTALL_STATE_FILE" || return 1
|
|
289
|
+
dependencies_are_ready || return 1
|
|
290
|
+
install_reuse_is_safe || return 1
|
|
291
|
+
[ "$installed_fingerprint" = "$(calculate_install_fingerprint "$@")" ] || return 1
|
|
292
|
+
return 0
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
write_install_state() {
|
|
296
|
+
local fingerprint="$1" temporary_state
|
|
297
|
+
temporary_state="$(mktemp "$WORK_DIR/.install-state.XXXXXX")"
|
|
298
|
+
printf '%s\n' "$fingerprint" > "$temporary_state"
|
|
299
|
+
mv -f "$temporary_state" "$INSTALL_STATE_FILE"
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
copy_install_metadata() {
|
|
303
|
+
local filename source_file target_file workspace
|
|
304
|
+
for filename in pnpm-lock.yaml pnpm-workspace.yaml .npmrc; do
|
|
305
|
+
source_file="$PROJECT_ROOT/$filename"
|
|
306
|
+
target_file="$WORK_DIR/$filename"
|
|
307
|
+
rm -f "$target_file"
|
|
308
|
+
if [ -f "$source_file" ]; then
|
|
309
|
+
cp "$source_file" "$target_file"
|
|
310
|
+
fi
|
|
311
|
+
done
|
|
312
|
+
|
|
313
|
+
link_install_entry "$PROJECT_ROOT/package.json" "$WORK_DIR/package.json"
|
|
314
|
+
link_install_entry "$PROJECT_ROOT/patches" "$WORK_DIR/patches"
|
|
315
|
+
link_install_entry "$PROJECT_ROOT/scripts" "$WORK_DIR/scripts"
|
|
316
|
+
|
|
317
|
+
for workspace in client server; do
|
|
318
|
+
if [ -f "$PROJECT_ROOT/$workspace/package.json" ]; then
|
|
319
|
+
mkdir -p "$WORK_DIR/$workspace"
|
|
320
|
+
link_install_entry "$PROJECT_ROOT/$workspace/package.json" "$WORK_DIR/$workspace/package.json"
|
|
321
|
+
link_install_entry "$PROJECT_ROOT/$workspace/scripts" "$WORK_DIR/$workspace/scripts"
|
|
322
|
+
else
|
|
323
|
+
rm -rf "$WORK_DIR/$workspace"
|
|
324
|
+
fi
|
|
325
|
+
done
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
link_node_modules() {
|
|
329
|
+
local source_dir="$1" target_dir="$2"
|
|
330
|
+
local current=""
|
|
331
|
+
mkdir -p "$target_dir"
|
|
332
|
+
if [ -L "$source_dir/node_modules" ]; then
|
|
333
|
+
current="$(readlink "$source_dir/node_modules")"
|
|
334
|
+
case "$current" in
|
|
335
|
+
"$NM_ROOT"/*/node_modules|"$NM_ROOT"/*/*/node_modules) ;;
|
|
336
|
+
*)
|
|
337
|
+
echo "[node_modules] refusing to replace unmanaged symlink: $source_dir/node_modules" >&2
|
|
338
|
+
exit 1
|
|
339
|
+
;;
|
|
340
|
+
esac
|
|
341
|
+
rm "$source_dir/node_modules"
|
|
342
|
+
elif [ -e "$source_dir/node_modules" ]; then
|
|
343
|
+
rm -rf "$source_dir/node_modules"
|
|
344
|
+
fi
|
|
345
|
+
ln -s "$target_dir" "$source_dir/node_modules"
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
cleanup_legacy_shadow_project
|
|
349
|
+
copy_install_metadata
|
|
350
|
+
|
|
351
|
+
if install_can_be_reused "$@"; then
|
|
352
|
+
echo "[node_modules] Reusing unchanged dependencies in $WORK_DIR"
|
|
353
|
+
else
|
|
354
|
+
# A stale state file must not survive a failed install.
|
|
355
|
+
rm -f "$INSTALL_STATE_FILE"
|
|
356
|
+
echo "[node_modules] Installing dependencies in $WORK_DIR"
|
|
357
|
+
if ! (cd "$WORK_DIR" && pnpm install "$@"); then
|
|
358
|
+
exit 1
|
|
359
|
+
fi
|
|
360
|
+
|
|
361
|
+
if [ -f "$WORK_DIR/pnpm-lock.yaml" ]; then
|
|
362
|
+
temporary_lockfile="$(mktemp "$PROJECT_ROOT/.pnpm-lock.yaml.XXXXXX")"
|
|
363
|
+
cp "$WORK_DIR/pnpm-lock.yaml" "$temporary_lockfile"
|
|
364
|
+
chmod 0644 "$temporary_lockfile"
|
|
365
|
+
mv -f "$temporary_lockfile" "$PROJECT_ROOT/pnpm-lock.yaml"
|
|
366
|
+
fi
|
|
367
|
+
|
|
368
|
+
# pnpm may have rewritten the lockfile, so record the fingerprint of the tree as it
|
|
369
|
+
# now stands. Otherwise the next run would see a changed input and reinstall.
|
|
370
|
+
write_install_state "$(calculate_install_fingerprint "$@")"
|
|
371
|
+
fi
|
|
372
|
+
|
|
373
|
+
link_node_modules "$PROJECT_ROOT" "$WORK_DIR/node_modules"
|
|
374
|
+
for workspace in client server; do
|
|
375
|
+
if [ -f "$PROJECT_ROOT/$workspace/package.json" ]; then
|
|
376
|
+
link_node_modules "$PROJECT_ROOT/$workspace" "$WORK_DIR/$workspace/node_modules"
|
|
377
|
+
fi
|
|
378
|
+
done
|
|
379
|
+
|
|
380
|
+
echo "[node_modules] Dependencies ready"
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-require-imports -- Standalone CommonJS runtime shipped without CLI dependencies. */
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const { spawn } = require('node:child_process');
|
|
6
|
+
|
|
7
|
+
const scriptRoot = fs.realpathSync(path.resolve(__dirname, '..', '..'));
|
|
8
|
+
const [action, target, ...extraArgs] = process.argv.slice(2);
|
|
9
|
+
const normalizedTarget = target === 'h5' ? 'web' : target;
|
|
10
|
+
const taroTypeByTarget = {
|
|
11
|
+
web: 'h5',
|
|
12
|
+
weapp: 'weapp',
|
|
13
|
+
tt: 'tt',
|
|
14
|
+
};
|
|
15
|
+
const validTargets = new Set(['web', 'weapp', 'tt']);
|
|
16
|
+
const validActions = new Set(['build', 'preview']);
|
|
17
|
+
|
|
18
|
+
function run(command, args, options = {}) {
|
|
19
|
+
const child = spawn(command, args, {
|
|
20
|
+
cwd: options.cwd || process.cwd(),
|
|
21
|
+
env: options.env || process.env,
|
|
22
|
+
stdio: 'inherit',
|
|
23
|
+
});
|
|
24
|
+
child.once('exit', (code, signal) => {
|
|
25
|
+
if (signal) {
|
|
26
|
+
process.kill(process.pid, signal);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
process.exit(code || 0);
|
|
30
|
+
});
|
|
31
|
+
child.once('error', error => {
|
|
32
|
+
console.error(error.message);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!validActions.has(action) || !validTargets.has(normalizedTarget)) {
|
|
38
|
+
console.error('Usage: taro-build.cjs <build|preview> <web|weapp|tt|h5> [...args]');
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (action === 'preview' && normalizedTarget === 'web') {
|
|
43
|
+
console.error('Taro preview is only supported for weapp or tt.');
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (process.env.COZE_TARO_LOCAL_ACTIVE !== scriptRoot) {
|
|
48
|
+
run(process.execPath, [
|
|
49
|
+
path.join(scriptRoot, '.cozeproj', 'scripts', 'local-workspace.cjs'),
|
|
50
|
+
'taro-build',
|
|
51
|
+
action,
|
|
52
|
+
normalizedTarget,
|
|
53
|
+
...extraArgs,
|
|
54
|
+
], { cwd: scriptRoot });
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const taroArgs = ['exec', 'taro', 'build', '--type', taroTypeByTarget[normalizedTarget]];
|
|
59
|
+
if (action === 'preview') {
|
|
60
|
+
taroArgs.push('--preview');
|
|
61
|
+
}
|
|
62
|
+
taroArgs.push(...extraArgs);
|
|
63
|
+
|
|
64
|
+
run('pnpm', taroArgs, {
|
|
65
|
+
cwd: process.env.COZE_WORKSPACE_PATH || scriptRoot,
|
|
66
|
+
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
$ErrorActionPreference = "Stop"
|
|
2
2
|
|
|
3
|
-
$
|
|
3
|
+
$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot "../..")).Path
|
|
4
|
+
$workspace = if ($env:COZE_WORKSPACE_PATH) { $env:COZE_WORKSPACE_PATH } else { $projectRoot }
|
|
5
|
+
$env:COZE_WORKSPACE_PATH = $workspace
|
|
4
6
|
Set-Location $workspace
|
|
5
7
|
|
|
6
8
|
& pnpm validate
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
set -Eeuo pipefail
|
|
3
3
|
|
|
4
|
+
ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
|
|
5
|
+
if [[ "${COZE_TARO_LOCAL_ACTIVE:-}" != "${ROOT_DIR}" ]]; then
|
|
6
|
+
exec node "$ROOT_DIR/.cozeproj/scripts/local-workspace.cjs" validate "$@"
|
|
7
|
+
fi
|
|
8
|
+
COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-${ROOT_DIR}}"
|
|
9
|
+
export COZE_WORKSPACE_PATH
|
|
10
|
+
|
|
4
11
|
cd "${COZE_WORKSPACE_PATH}"
|
|
5
12
|
|
|
6
13
|
echo "🔍 Running validate..."
|
|
@@ -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 会跳过。
|
|
@@ -56,9 +56,9 @@ $startupTimeoutSeconds = 30
|
|
|
56
56
|
$startupDeadline = (Get-Date).AddSeconds($startupTimeoutSeconds)
|
|
57
57
|
while ((Get-Date) -lt $startupDeadline) {
|
|
58
58
|
if ($process.HasExited) {
|
|
59
|
-
Write-Error "Dev server exited before listening on port $port."
|
|
60
59
|
Get-Content $errorLogFile -Tail 20 -ErrorAction SilentlyContinue
|
|
61
60
|
Remove-Item $pidFile -Force -ErrorAction SilentlyContinue
|
|
61
|
+
Write-Error "Dev server exited before listening on port $port." -ErrorAction Continue
|
|
62
62
|
exit 1
|
|
63
63
|
}
|
|
64
64
|
|
|
@@ -71,9 +71,9 @@ while ((Get-Date) -lt $startupDeadline) {
|
|
|
71
71
|
Start-Sleep -Milliseconds 200
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
Write-Error "Dev server did not listen on port $port within $startupTimeoutSeconds seconds."
|
|
75
74
|
Stop-ProcessTree $process.Id
|
|
76
75
|
Get-Content $errorLogFile -Tail 20 -ErrorAction SilentlyContinue
|
|
77
76
|
Remove-Item $pidFile -Force -ErrorAction SilentlyContinue
|
|
77
|
+
Write-Error "Dev server did not listen on port $port within $startupTimeoutSeconds seconds." -ErrorAction Continue
|
|
78
78
|
exit 1
|
|
79
79
|
<% } %>
|
|
@@ -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,18 +44,22 @@ 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"
|
|
45
51
|
PID_FILE="${LOG_DIR}/server.pid"
|
|
46
52
|
|
|
47
53
|
# detached 出去的进程没人负责回收,超过这个时长就自己退出,避免端口与内存长期泄露。
|
|
48
|
-
MAX_RUNTIME_SECONDS=
|
|
54
|
+
MAX_RUNTIME_SECONDS=3600
|
|
55
|
+
|
|
56
|
+
timeout_watchdog_enabled() {
|
|
57
|
+
[[ -z "${COZE_EVAL:-}" && -z "${COZE_PROJECT_TYPE:-}" ]]
|
|
58
|
+
}
|
|
49
59
|
|
|
50
60
|
# 真正被 detach 的是这层 bash wrapper:它是进程组 leader,组内 watchdog 到点回收整组
|
|
51
61
|
# (wrapper -> pnpm -> tsx -> node);被包的进程自己先退出时也顺手清空进程组,不留残余。
|
|
52
|
-
RUN_WITH_TIMEOUT='
|
|
62
|
+
RUN_WITH_TIMEOUT="$(declare -f timeout_watchdog_enabled)"'
|
|
53
63
|
timeout_seconds=$1
|
|
54
64
|
shift
|
|
55
65
|
|
|
@@ -57,6 +67,7 @@ shift
|
|
|
57
67
|
child_pid=$!
|
|
58
68
|
|
|
59
69
|
# 先忽略 TERM,才能在向整组发 TERM(自己也在组里)之后存活下来补一发 KILL。
|
|
70
|
+
if timeout_watchdog_enabled; then
|
|
60
71
|
( trap "" TERM
|
|
61
72
|
sleep "${timeout_seconds}"
|
|
62
73
|
echo "[dev] 后台进程运行超过 ${timeout_seconds}s,回收进程组 $$。"
|
|
@@ -64,6 +75,7 @@ child_pid=$!
|
|
|
64
75
|
sleep 5
|
|
65
76
|
kill -KILL -- "-$$" 2>/dev/null || true
|
|
66
77
|
) &
|
|
78
|
+
fi
|
|
67
79
|
|
|
68
80
|
wait "${child_pid}"
|
|
69
81
|
kill -KILL -- "-$$" 2>/dev/null || true
|
|
@@ -130,7 +142,7 @@ mkdir -p "${LOG_DIR}"
|
|
|
130
142
|
|
|
131
143
|
export PORT="${DEPLOY_RUN_PORT}"
|
|
132
144
|
server_pid="$(spawn_detached "${COZE_WORKSPACE_PATH}" "${LOG_FILE}" \
|
|
133
|
-
"$(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)"
|
|
134
146
|
echo "${server_pid}" > "${PID_FILE}"
|
|
135
147
|
|
|
136
148
|
sleep 1
|
|
@@ -142,7 +154,9 @@ if [[ -z "${server_pid}" ]] || ! kill -0 "${server_pid}" 2>/dev/null; then
|
|
|
142
154
|
fi
|
|
143
155
|
|
|
144
156
|
echo "Dev server started (PID: ${server_pid})."
|
|
145
|
-
|
|
157
|
+
if timeout_watchdog_enabled; then
|
|
158
|
+
echo "Auto stop after ${MAX_RUNTIME_SECONDS}s."
|
|
159
|
+
fi
|
|
146
160
|
echo "Log file: ${LOG_FILE}"
|
|
147
161
|
echo "PID file: ${PID_FILE}"
|
|
148
162
|
<% } %>
|