@coze-arch/cli 0.1.7 → 0.1.8-alpha.382639

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.
Files changed (37) hide show
  1. package/README.md +18 -0
  2. package/lib/__templates__/expo/.cozeproj/scripts/prepare-node-modules.sh +178 -10
  3. package/lib/__templates__/native-static/.coze +2 -2
  4. package/lib/__templates__/nextjs/.coze +1 -1
  5. package/lib/__templates__/nextjs/README.md +18 -0
  6. package/lib/__templates__/nextjs/next.config.ts +0 -9
  7. package/lib/__templates__/nextjs/scripts/build.ps1 +1 -1
  8. package/lib/__templates__/nextjs/scripts/build.sh +2 -2
  9. package/lib/__templates__/nextjs/scripts/dev.ps1 +2 -2
  10. package/lib/__templates__/nextjs/scripts/dev.sh +16 -55
  11. package/lib/__templates__/nextjs/scripts/local-workspace.cjs +302 -0
  12. package/lib/__templates__/nextjs/scripts/prepare.sh +10 -2
  13. package/lib/__templates__/nextjs/scripts/validate.sh +11 -1
  14. package/lib/__templates__/nextjs/src/server.ts +1 -1
  15. package/lib/__templates__/nextjs/template.config.js +20 -6
  16. package/lib/__templates__/nextjs/tsconfig.json +0 -1
  17. package/lib/__templates__/nuxt-vue/.coze +1 -1
  18. package/lib/__templates__/nuxt-vue/README.md +20 -2
  19. package/lib/__templates__/nuxt-vue/scripts/build.sh +1 -1
  20. package/lib/__templates__/nuxt-vue/scripts/dev.sh +10 -4
  21. package/lib/__templates__/nuxt-vue/scripts/local-workspace.cjs +302 -0
  22. package/lib/__templates__/nuxt-vue/scripts/prepare.sh +10 -2
  23. package/lib/__templates__/nuxt-vue/scripts/validate.sh +11 -1
  24. package/lib/__templates__/taro/.cozeproj/scripts/prepare-node-modules.sh +178 -10
  25. package/lib/__templates__/vite/.coze +1 -1
  26. package/lib/__templates__/vite/README.md +20 -0
  27. package/lib/__templates__/vite/scripts/build.sh +1 -1
  28. package/lib/__templates__/vite/scripts/dev.sh +10 -4
  29. package/lib/__templates__/vite/scripts/local-workspace.cjs +302 -0
  30. package/lib/__templates__/vite/scripts/prepare.sh +10 -2
  31. package/lib/__templates__/vite/scripts/validate.sh +7 -1
  32. package/lib/__templates__/vite/template.config.js +20 -6
  33. package/lib/cli.js +367 -184
  34. package/package.json +2 -1
  35. package/lib/__templates__/nextjs/scripts/prepare-node-modules.sh +0 -212
  36. package/lib/__templates__/nuxt-vue/scripts/prepare-node-modules.sh +0 -212
  37. package/lib/__templates__/vite/scripts/prepare-node-modules.sh +0 -212
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coze-arch/cli",
3
- "version": "0.1.7",
3
+ "version": "0.1.8-alpha.382639",
4
4
  "private": false,
5
5
  "description": "coze coding devtools cli",
6
6
  "license": "MIT",
@@ -34,6 +34,7 @@
34
34
  "test:all": "bash scripts/test-coverage.sh",
35
35
  "test:cov": "vitest --run --passWithNoTests --coverage",
36
36
  "test:e2e": "NODE_ENV=test bash scripts/e2e.sh",
37
+ "test:integration:web": "node --test __tests__/integration/local-workspace.test.cjs",
37
38
  "test:perf": "vitest bench --run --config vitest.perf.config.ts",
38
39
  "test:perf:compare": "bash scripts/compare-perf.sh",
39
40
  "test:perf:save": "bash scripts/run-perf-with-output.sh"
@@ -1,212 +0,0 @@
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"
@@ -1,212 +0,0 @@
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"
@@ -1,212 +0,0 @@
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"