@coze-arch/cli 0.0.37 → 0.0.39-alpha.bdff32

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 (168) hide show
  1. package/README.md +69 -104
  2. package/docs/deploy.md +181 -0
  3. package/lib/__templates__/expo/.coze +5 -0
  4. package/lib/__templates__/expo/.cozeproj/scripts/dev_build.ps1 +10 -0
  5. package/lib/__templates__/expo/.cozeproj/scripts/dev_build.sh +4 -0
  6. package/lib/__templates__/expo/.cozeproj/scripts/dev_run.ps1 +74 -0
  7. package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +108 -19
  8. package/lib/__templates__/expo/.cozeproj/scripts/prod_build.ps1 +15 -0
  9. package/lib/__templates__/expo/.cozeproj/scripts/prod_run.ps1 +10 -0
  10. package/lib/__templates__/expo/.cozeproj/scripts/prod_run.sh +1 -1
  11. package/lib/__templates__/expo/.cozeproj/scripts/server_dev_run.sh +1 -1
  12. package/lib/__templates__/expo/.cozeproj/scripts/validate.ps1 +7 -0
  13. package/lib/__templates__/expo/client/metro.config.js +3 -1
  14. package/lib/__templates__/expo/pnpm-lock.yaml +5 -5
  15. package/lib/__templates__/expo/server/package.json +2 -2
  16. package/lib/__templates__/expo/server/src/index.ts +1 -1
  17. package/lib/__templates__/expo/template.config.js +11 -2
  18. package/lib/__templates__/native-static/.coze +4 -2
  19. package/lib/__templates__/native-static/scripts/serve.ps1 +7 -0
  20. package/lib/__templates__/nextjs/.coze +5 -0
  21. package/lib/__templates__/nextjs/package.json +7 -2
  22. package/lib/__templates__/nextjs/pnpm-lock.yaml +59 -334
  23. package/lib/__templates__/nextjs/scripts/build.ps1 +16 -0
  24. package/lib/__templates__/nextjs/scripts/dev.ps1 +63 -0
  25. package/lib/__templates__/nextjs/scripts/dev.sh +234 -7
  26. package/lib/__templates__/nextjs/scripts/prepare.ps1 +18 -0
  27. package/lib/__templates__/nextjs/scripts/start.ps1 +11 -0
  28. package/lib/__templates__/nextjs/scripts/validate.ps1 +8 -0
  29. package/lib/__templates__/nextjs/template.config.js +4 -2
  30. package/lib/__templates__/nuxt-vue/.coze +5 -0
  31. package/lib/__templates__/nuxt-vue/package.json +1 -1
  32. package/lib/__templates__/nuxt-vue/pnpm-lock.yaml +5 -5
  33. package/lib/__templates__/nuxt-vue/scripts/build.ps1 +12 -0
  34. package/lib/__templates__/nuxt-vue/scripts/dev.ps1 +63 -0
  35. package/lib/__templates__/nuxt-vue/scripts/dev.sh +232 -3
  36. package/lib/__templates__/nuxt-vue/scripts/prepare.ps1 +12 -0
  37. package/lib/__templates__/nuxt-vue/scripts/start.ps1 +11 -0
  38. package/lib/__templates__/nuxt-vue/scripts/validate.ps1 +8 -0
  39. package/lib/__templates__/phaser/.coze +18 -0
  40. package/lib/__templates__/phaser/README.md +45 -0
  41. package/lib/__templates__/phaser/_gitignore +23 -0
  42. package/lib/__templates__/phaser/_npmrc +14 -0
  43. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0001.png +0 -0
  44. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0002.png +0 -0
  45. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0003.png +0 -0
  46. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0004.png +0 -0
  47. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0005.png +0 -0
  48. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0006.png +0 -0
  49. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0007.png +0 -0
  50. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0008.png +0 -0
  51. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0009.png +0 -0
  52. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0010.png +0 -0
  53. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0011.png +0 -0
  54. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0012.png +0 -0
  55. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0013.png +0 -0
  56. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0014.png +0 -0
  57. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0015.png +0 -0
  58. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0016.png +0 -0
  59. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0017.png +0 -0
  60. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0018.png +0 -0
  61. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0019.png +0 -0
  62. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0020.png +0 -0
  63. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0021.png +0 -0
  64. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0022.png +0 -0
  65. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0023.png +0 -0
  66. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/frame_0024.png +0 -0
  67. package/lib/__templates__/phaser/assets/image_sequence/coze-game-loading/manifest.json +11 -0
  68. package/lib/__templates__/phaser/debug-runtime/flow-graph.json +20 -0
  69. package/lib/__templates__/phaser/index.html +12 -0
  70. package/lib/__templates__/phaser/package.json +31 -0
  71. package/lib/__templates__/phaser/pnpm-lock.yaml +661 -0
  72. package/lib/__templates__/phaser/scripts/build.ps1 +12 -0
  73. package/lib/__templates__/phaser/scripts/build.sh +8 -0
  74. package/lib/__templates__/phaser/scripts/dev.ps1 +49 -0
  75. package/lib/__templates__/phaser/scripts/dev.sh +82 -0
  76. package/lib/__templates__/phaser/scripts/prepare.ps1 +8 -0
  77. package/lib/__templates__/phaser/scripts/prepare.sh +7 -0
  78. package/lib/__templates__/phaser/scripts/spawn-detached.cjs +29 -0
  79. package/lib/__templates__/phaser/scripts/start.ps1 +49 -0
  80. package/lib/__templates__/phaser/scripts/start.sh +82 -0
  81. package/lib/__templates__/phaser/scripts/validate.ps1 +7 -0
  82. package/lib/__templates__/phaser/scripts/validate.sh +7 -0
  83. package/lib/__templates__/phaser/src/assets.ts +15 -0
  84. package/lib/__templates__/phaser/src/global.d.ts +39 -0
  85. package/lib/__templates__/phaser/src/main.ts +28 -0
  86. package/lib/__templates__/phaser/src/scene/BootScene.ts +213 -0
  87. package/lib/__templates__/phaser/src/scene/DevLoadingScene.ts +69 -0
  88. package/lib/__templates__/phaser/src/style.css +28 -0
  89. package/lib/__templates__/phaser/src/types.ts +32 -0
  90. package/lib/__templates__/phaser/src/utils/asset-loader.ts +319 -0
  91. package/lib/__templates__/phaser/src/utils/index.ts +2 -0
  92. package/lib/__templates__/phaser/src/utils/mark-editable.ts +13 -0
  93. package/lib/__templates__/phaser/template.config.js +43 -0
  94. package/lib/__templates__/phaser/tsconfig.json +23 -0
  95. package/lib/__templates__/phaser/vite.config.ts +96 -0
  96. package/lib/__templates__/phaser-static/.coze +15 -0
  97. package/lib/__templates__/phaser-static/README.md +40 -0
  98. package/lib/__templates__/phaser-static/_gitignore +6 -0
  99. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0001.png +0 -0
  100. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0002.png +0 -0
  101. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0003.png +0 -0
  102. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0004.png +0 -0
  103. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0005.png +0 -0
  104. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0006.png +0 -0
  105. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0007.png +0 -0
  106. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0008.png +0 -0
  107. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0009.png +0 -0
  108. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0010.png +0 -0
  109. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0011.png +0 -0
  110. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0012.png +0 -0
  111. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0013.png +0 -0
  112. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0014.png +0 -0
  113. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0015.png +0 -0
  114. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0016.png +0 -0
  115. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0017.png +0 -0
  116. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0018.png +0 -0
  117. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0019.png +0 -0
  118. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0020.png +0 -0
  119. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0021.png +0 -0
  120. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0022.png +0 -0
  121. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0023.png +0 -0
  122. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/frame_0024.png +0 -0
  123. package/lib/__templates__/phaser-static/assets/image_sequence/coze-game-loading/manifest.json +11 -0
  124. package/lib/__templates__/phaser-static/debug-runtime/flow-graph.json +20 -0
  125. package/lib/__templates__/phaser-static/index.html +17 -0
  126. package/lib/__templates__/phaser-static/scripts/serve.ps1 +7 -0
  127. package/lib/__templates__/phaser-static/src/assets.js +15 -0
  128. package/lib/__templates__/phaser-static/src/main.js +26 -0
  129. package/lib/__templates__/phaser-static/src/phaser.js +7 -0
  130. package/lib/__templates__/phaser-static/src/scene/BootScene.js +205 -0
  131. package/lib/__templates__/phaser-static/src/scene/DevLoadingScene.js +67 -0
  132. package/lib/__templates__/phaser-static/src/style.css +28 -0
  133. package/lib/__templates__/phaser-static/src/utils/asset-loader.js +267 -0
  134. package/lib/__templates__/phaser-static/src/utils/index.js +2 -0
  135. package/lib/__templates__/phaser-static/src/utils/mark-editable.js +3 -0
  136. package/lib/__templates__/phaser-static/template.config.js +35 -0
  137. package/lib/__templates__/pi-agent/package.json +1 -1
  138. package/lib/__templates__/pi-agent/pnpm-lock.yaml +5 -5
  139. package/lib/__templates__/taro/.coze +6 -0
  140. package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.ps1 +10 -0
  141. package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.ps1 +8 -0
  142. package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +1 -1
  143. package/lib/__templates__/taro/.cozeproj/scripts/dev_build.ps1 +3 -0
  144. package/lib/__templates__/taro/.cozeproj/scripts/dev_run.ps1 +62 -0
  145. package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +178 -68
  146. package/lib/__templates__/taro/.cozeproj/scripts/pack.ps1 +7 -0
  147. package/lib/__templates__/taro/.cozeproj/scripts/validate.ps1 +7 -0
  148. package/lib/__templates__/taro/AGENTS.md +11 -7
  149. package/lib/__templates__/taro/README.md +4 -4
  150. package/lib/__templates__/taro/config/index.ts +10 -2
  151. package/lib/__templates__/taro/pnpm-lock.yaml +5 -5
  152. package/lib/__templates__/taro/server/package.json +1 -1
  153. package/lib/__templates__/taro/server/src/main.ts +8 -2
  154. package/lib/__templates__/templates.json +51 -1
  155. package/lib/__templates__/vite/.coze +5 -0
  156. package/lib/__templates__/vite/package.json +1 -1
  157. package/lib/__templates__/vite/pnpm-lock.yaml +5 -5
  158. package/lib/__templates__/vite/scripts/build.ps1 +16 -0
  159. package/lib/__templates__/vite/scripts/dev.ps1 +63 -0
  160. package/lib/__templates__/vite/scripts/dev.sh +110 -1
  161. package/lib/__templates__/vite/scripts/prepare.ps1 +18 -0
  162. package/lib/__templates__/vite/scripts/start.ps1 +11 -0
  163. package/lib/__templates__/vite/scripts/validate.ps1 +8 -0
  164. package/lib/__templates__/vite/template.config.js +4 -2
  165. package/lib/cli.js +3719 -211
  166. package/lib/deploy/package-project.js +244 -0
  167. package/lib/deploy/package-project.py +183 -0
  168. package/package.json +5 -2
@@ -0,0 +1,49 @@
1
+ $ErrorActionPreference = "Stop"
2
+
3
+ function Stop-ProcessTree([int] $processId) {
4
+ if ($processId -gt 0 -and (Get-Process -Id $processId -ErrorAction SilentlyContinue)) {
5
+ & taskkill.exe /PID $processId /T /F *> $null
6
+ }
7
+ }
8
+
9
+ function Stop-ListeningProcess([int] $port) {
10
+ $connections = Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue
11
+ $connections | Select-Object -ExpandProperty OwningProcess -Unique | ForEach-Object {
12
+ Stop-ProcessTree $_
13
+ }
14
+ }
15
+
16
+ $workspace = if ($env:COZE_WORKSPACE_PATH) { $env:COZE_WORKSPACE_PATH } else { (Get-Location).Path }
17
+ $defaultPort = <%= port %>
18
+ $port = if ($env:DEPLOY_RUN_PORT) { [int] $env:DEPLOY_RUN_PORT } elseif ($env:PORT) { [int] $env:PORT } else { $defaultPort }
19
+ Set-Location $workspace
20
+ Stop-ListeningProcess $port
21
+ $env:COZE_PHASER_GAME_ENV = "DEV"
22
+
23
+ <% if (process.env.NODE_ENV === 'test') { %>
24
+ & pnpm vite --port $port --host 127.0.0.1 --strictPort
25
+ exit $LASTEXITCODE
26
+ <% } else { %>
27
+ $logDirectory = if ($env:COZE_LOG_DIR) { $env:COZE_LOG_DIR } else { Join-Path $workspace "logs" }
28
+ $logFile = Join-Path $logDirectory "phaser-dev.log"
29
+ $errorLogFile = Join-Path $logDirectory "phaser-dev-error.log"
30
+ $pidFile = Join-Path $logDirectory "phaser-dev.pid"
31
+ New-Item -ItemType Directory -Force -Path $logDirectory | Out-Null
32
+
33
+ if (Test-Path $pidFile) {
34
+ Stop-ProcessTree ([int] (Get-Content $pidFile -Raw))
35
+ Remove-Item $pidFile -Force
36
+ }
37
+
38
+ $process = Start-Process -FilePath $env:ComSpec -ArgumentList @("/d", "/s", "/c", "pnpm vite --port $port --host 127.0.0.1 --strictPort") -WorkingDirectory $workspace -RedirectStandardOutput $logFile -RedirectStandardError $errorLogFile -PassThru
39
+ $process.Id | Set-Content $pidFile
40
+ Start-Sleep -Seconds 1
41
+ if ($process.HasExited) {
42
+ Get-Content $errorLogFile -Tail 20 -ErrorAction SilentlyContinue
43
+ Remove-Item $pidFile -Force -ErrorAction SilentlyContinue
44
+ exit 1
45
+ }
46
+
47
+ Write-Host "Phaser dev server started (PID: $($process.Id))."
48
+ Write-Host "Log file: $logFile"
49
+ <% } %>
@@ -0,0 +1,82 @@
1
+ #!/bin/bash
2
+ set -Eeuo pipefail
3
+
4
+ COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-$(pwd)}"
5
+ PORT="${PORT:-<%= port %>}"
6
+ DEPLOY_RUN_PORT="${DEPLOY_RUN_PORT:-${PORT}}"
7
+ cd "${COZE_WORKSPACE_PATH}"
8
+
9
+ get_listening_pids() {
10
+ local port="$1"
11
+
12
+ if command -v lsof >/dev/null 2>&1; then
13
+ lsof -tiTCP:"${port}" -sTCP:LISTEN 2>/dev/null || true
14
+ elif command -v ss >/dev/null 2>&1; then
15
+ ss -H -lntp 2>/dev/null \
16
+ | awk -v port="${port}" '$4 ~ ":"port"$"' \
17
+ | grep -o 'pid=[0-9]*' \
18
+ | cut -d= -f2 \
19
+ | sort -u || true
20
+ else
21
+ echo "Warning: neither lsof nor ss found, cannot check port ${port}." >&2
22
+ fi
23
+ }
24
+
25
+ kill_port_if_listening() {
26
+ local port="$1"
27
+ local pids
28
+
29
+ pids="$(get_listening_pids "${port}")"
30
+ if [[ -z "${pids}" ]]; then
31
+ echo "Port ${port} is free."
32
+ return
33
+ fi
34
+
35
+ echo "Port ${port} is in use by PIDs: ${pids//$'\n'/ } (SIGKILL)"
36
+ echo "${pids}" | xargs kill -9 2>/dev/null || true
37
+ sleep 1
38
+
39
+ pids="$(get_listening_pids "${port}")"
40
+ if [[ -n "${pids}" ]]; then
41
+ echo "Failed to clear port ${port}; remaining PIDs: ${pids//$'\n'/ }." >&2
42
+ return 1
43
+ fi
44
+
45
+ echo "Port ${port} cleared."
46
+ }
47
+
48
+ echo "Clearing port ${DEPLOY_RUN_PORT} before start."
49
+ kill_port_if_listening "${DEPLOY_RUN_PORT}"
50
+
51
+ <% if (process.env.NODE_ENV === 'test') { %>
52
+ # Keep the process attached in tests so the test runner can collect logs and stop it.
53
+ exec env COZE_PHASER_GAME_ENV=DEV pnpm vite \
54
+ --port "${DEPLOY_RUN_PORT}" \
55
+ --host 127.0.0.1 \
56
+ --strictPort
57
+ <% } else { %>
58
+ LOG_DIR="${COZE_WORKSPACE_PATH}/logs"
59
+ LOG_FILE="${LOG_DIR}/phaser-dev.log"
60
+ PID_FILE="${LOG_DIR}/phaser-dev.pid"
61
+ mkdir -p "${LOG_DIR}"
62
+
63
+ echo "Starting Phaser dev server on port ${DEPLOY_RUN_PORT}..."
64
+ server_pid="$(COZE_PHASER_GAME_ENV=DEV node scripts/spawn-detached.cjs \
65
+ "${LOG_FILE}" pnpm vite \
66
+ --port "${DEPLOY_RUN_PORT}" \
67
+ --host 127.0.0.1 \
68
+ --strictPort)"
69
+ echo "${server_pid}" > "${PID_FILE}"
70
+
71
+ sleep 1
72
+ if ! kill -0 "${server_pid}" 2>/dev/null; then
73
+ echo "Phaser dev server failed to start. See ${LOG_FILE}." >&2
74
+ tail -n 20 "${LOG_FILE}" >&2 || true
75
+ rm -f "${PID_FILE}"
76
+ exit 1
77
+ fi
78
+
79
+ echo "Phaser dev server started (PID: ${server_pid})."
80
+ echo "Log file: ${LOG_FILE}"
81
+ echo "PID file: ${PID_FILE}"
82
+ <% } %>
@@ -0,0 +1,8 @@
1
+ $ErrorActionPreference = "Stop"
2
+
3
+ $workspace = if ($env:COZE_WORKSPACE_PATH) { $env:COZE_WORKSPACE_PATH } else { (Get-Location).Path }
4
+ Set-Location $workspace
5
+
6
+ Write-Host "Installing dependencies..."
7
+ & pnpm install --prefer-frozen-lockfile --prefer-offline --reporter=append-only
8
+ exit $LASTEXITCODE
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -Eeuo pipefail
3
+
4
+ COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-$(pwd)}"
5
+ cd "${COZE_WORKSPACE_PATH}"
6
+
7
+ pnpm install --prefer-frozen-lockfile --prefer-offline --reporter=append-only
@@ -0,0 +1,29 @@
1
+ const fs = require('node:fs');
2
+ const { spawn } = require('node:child_process');
3
+
4
+ const [logFile, command, ...args] = process.argv.slice(2);
5
+
6
+ if (!logFile || !command) {
7
+ process.stderr.write(
8
+ 'Usage: node scripts/spawn-detached.cjs <log-file> <command> [...args]\n',
9
+ );
10
+ process.exit(1);
11
+ }
12
+
13
+ const logFd = fs.openSync(logFile, 'a');
14
+ const child = spawn(command, args, {
15
+ cwd: process.cwd(),
16
+ detached: process.platform !== 'win32',
17
+ env: process.env,
18
+ stdio: ['ignore', logFd, logFd],
19
+ });
20
+
21
+ fs.closeSync(logFd);
22
+ child.unref();
23
+
24
+ if (!child.pid) {
25
+ process.stderr.write('Failed to start detached process.\n');
26
+ process.exit(1);
27
+ }
28
+
29
+ process.stdout.write(String(child.pid));
@@ -0,0 +1,49 @@
1
+ $ErrorActionPreference = "Stop"
2
+
3
+ function Stop-ProcessTree([int] $processId) {
4
+ if ($processId -gt 0 -and (Get-Process -Id $processId -ErrorAction SilentlyContinue)) {
5
+ & taskkill.exe /PID $processId /T /F *> $null
6
+ }
7
+ }
8
+
9
+ function Stop-ListeningProcess([int] $port) {
10
+ $connections = Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue
11
+ $connections | Select-Object -ExpandProperty OwningProcess -Unique | ForEach-Object {
12
+ Stop-ProcessTree $_
13
+ }
14
+ }
15
+
16
+ $workspace = if ($env:COZE_WORKSPACE_PATH) { $env:COZE_WORKSPACE_PATH } else { (Get-Location).Path }
17
+ $defaultPort = <%= port %>
18
+ $port = if ($env:DEPLOY_RUN_PORT) { [int] $env:DEPLOY_RUN_PORT } elseif ($env:PORT) { [int] $env:PORT } else { $defaultPort }
19
+ Set-Location $workspace
20
+ Stop-ListeningProcess $port
21
+ $env:COZE_PHASER_GAME_ENV = "PROD"
22
+
23
+ <% if (process.env.NODE_ENV === 'test') { %>
24
+ & pnpm vite preview --port $port --host 127.0.0.1 --strictPort
25
+ exit $LASTEXITCODE
26
+ <% } else { %>
27
+ $logDirectory = if ($env:COZE_LOG_DIR) { $env:COZE_LOG_DIR } else { Join-Path $workspace "logs" }
28
+ $logFile = Join-Path $logDirectory "phaser-start.log"
29
+ $errorLogFile = Join-Path $logDirectory "phaser-start-error.log"
30
+ $pidFile = Join-Path $logDirectory "phaser-start.pid"
31
+ New-Item -ItemType Directory -Force -Path $logDirectory | Out-Null
32
+
33
+ if (Test-Path $pidFile) {
34
+ Stop-ProcessTree ([int] (Get-Content $pidFile -Raw))
35
+ Remove-Item $pidFile -Force
36
+ }
37
+
38
+ $process = Start-Process -FilePath $env:ComSpec -ArgumentList @("/d", "/s", "/c", "pnpm vite preview --port $port --host 127.0.0.1 --strictPort") -WorkingDirectory $workspace -RedirectStandardOutput $logFile -RedirectStandardError $errorLogFile -PassThru
39
+ $process.Id | Set-Content $pidFile
40
+ Start-Sleep -Seconds 1
41
+ if ($process.HasExited) {
42
+ Get-Content $errorLogFile -Tail 20 -ErrorAction SilentlyContinue
43
+ Remove-Item $pidFile -Force -ErrorAction SilentlyContinue
44
+ exit 1
45
+ }
46
+
47
+ Write-Host "Phaser production server started (PID: $($process.Id))."
48
+ Write-Host "Log file: $logFile"
49
+ <% } %>
@@ -0,0 +1,82 @@
1
+ #!/bin/bash
2
+ set -Eeuo pipefail
3
+
4
+ COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-$(pwd)}"
5
+ PORT="${PORT:-<%= port %>}"
6
+ DEPLOY_RUN_PORT="${DEPLOY_RUN_PORT:-${PORT}}"
7
+ cd "${COZE_WORKSPACE_PATH}"
8
+
9
+ get_listening_pids() {
10
+ local port="$1"
11
+
12
+ if command -v lsof >/dev/null 2>&1; then
13
+ lsof -tiTCP:"${port}" -sTCP:LISTEN 2>/dev/null || true
14
+ elif command -v ss >/dev/null 2>&1; then
15
+ ss -H -lntp 2>/dev/null \
16
+ | awk -v port="${port}" '$4 ~ ":"port"$"' \
17
+ | grep -o 'pid=[0-9]*' \
18
+ | cut -d= -f2 \
19
+ | sort -u || true
20
+ else
21
+ echo "Warning: neither lsof nor ss found, cannot check port ${port}." >&2
22
+ fi
23
+ }
24
+
25
+ kill_port_if_listening() {
26
+ local port="$1"
27
+ local pids
28
+
29
+ pids="$(get_listening_pids "${port}")"
30
+ if [[ -z "${pids}" ]]; then
31
+ echo "Port ${port} is free."
32
+ return
33
+ fi
34
+
35
+ echo "Port ${port} is in use by PIDs: ${pids//$'\n'/ } (SIGKILL)"
36
+ echo "${pids}" | xargs kill -9 2>/dev/null || true
37
+ sleep 1
38
+
39
+ pids="$(get_listening_pids "${port}")"
40
+ if [[ -n "${pids}" ]]; then
41
+ echo "Failed to clear port ${port}; remaining PIDs: ${pids//$'\n'/ }." >&2
42
+ return 1
43
+ fi
44
+
45
+ echo "Port ${port} cleared."
46
+ }
47
+
48
+ echo "Clearing port ${DEPLOY_RUN_PORT} before start."
49
+ kill_port_if_listening "${DEPLOY_RUN_PORT}"
50
+
51
+ <% if (process.env.NODE_ENV === 'test') { %>
52
+ # Keep the process attached in tests so the test runner can collect logs and stop it.
53
+ exec env COZE_PHASER_GAME_ENV=PROD pnpm vite preview \
54
+ --port "${DEPLOY_RUN_PORT}" \
55
+ --host 127.0.0.1 \
56
+ --strictPort
57
+ <% } else { %>
58
+ LOG_DIR="${COZE_WORKSPACE_PATH}/logs"
59
+ LOG_FILE="${LOG_DIR}/phaser-start.log"
60
+ PID_FILE="${LOG_DIR}/phaser-start.pid"
61
+ mkdir -p "${LOG_DIR}"
62
+
63
+ echo "Starting Phaser production server on port ${DEPLOY_RUN_PORT}..."
64
+ server_pid="$(COZE_PHASER_GAME_ENV=PROD node scripts/spawn-detached.cjs \
65
+ "${LOG_FILE}" pnpm vite preview \
66
+ --port "${DEPLOY_RUN_PORT}" \
67
+ --host 127.0.0.1 \
68
+ --strictPort)"
69
+ echo "${server_pid}" > "${PID_FILE}"
70
+
71
+ sleep 1
72
+ if ! kill -0 "${server_pid}" 2>/dev/null; then
73
+ echo "Phaser production server failed to start. See ${LOG_FILE}." >&2
74
+ tail -n 20 "${LOG_FILE}" >&2 || true
75
+ rm -f "${PID_FILE}"
76
+ exit 1
77
+ fi
78
+
79
+ echo "Phaser production server started (PID: ${server_pid})."
80
+ echo "Log file: ${LOG_FILE}"
81
+ echo "PID file: ${PID_FILE}"
82
+ <% } %>
@@ -0,0 +1,7 @@
1
+ $ErrorActionPreference = "Stop"
2
+
3
+ $workspace = if ($env:COZE_WORKSPACE_PATH) { $env:COZE_WORKSPACE_PATH } else { (Get-Location).Path }
4
+ Set-Location $workspace
5
+
6
+ & pnpm validate
7
+ exit $LASTEXITCODE
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -Eeuo pipefail
3
+
4
+ COZE_WORKSPACE_PATH="${COZE_WORKSPACE_PATH:-$(pwd)}"
5
+ cd "${COZE_WORKSPACE_PATH}"
6
+
7
+ pnpm validate
@@ -0,0 +1,15 @@
1
+ export const IMAGES: Record<string, string> = {};
2
+
3
+ export const IMAGE_SEQUENCES: Record<string, string> = {
4
+ 'coze-game-loading': 'assets/image_sequence/coze-game-loading',
5
+ };
6
+
7
+ export const VIDEOS: Record<string, string> = {};
8
+
9
+ export const AUDIOS: Record<string, string> = {};
10
+
11
+ // 注册素材到 PhaserBridge,可以在扣子网页游戏开发中定位
12
+ window.PhaserBridge?.markImageAssetMap?.(IMAGES);
13
+ window.PhaserBridge?.markImageSequenceAssetMap?.(IMAGE_SEQUENCES);
14
+ window.PhaserBridge?.markVideoAssetMap?.(VIDEOS);
15
+ window.PhaserBridge?.markAudioAssetMap?.(AUDIOS);
@@ -0,0 +1,39 @@
1
+ import type Phaser from 'phaser';
2
+
3
+ declare global {
4
+ interface LaunchSpec {
5
+ protocol_version: 1;
6
+ target: {
7
+ graph_id: string;
8
+ node_id: string;
9
+ };
10
+ input: Record<string, any>;
11
+ }
12
+
13
+ interface PhaserBridgeBinding {
14
+ launchGame: (launchSpec: LaunchSpec | undefined) => Phaser.Game;
15
+ }
16
+
17
+ interface PhaserBridgeApi {
18
+ bind: (binding: PhaserBridgeBinding) => Phaser.Game;
19
+ markEditable?: <T extends Phaser.GameObjects.GameObject>(
20
+ stableId: string,
21
+ gameObject: T,
22
+ options?: {
23
+ label?: string;
24
+ },
25
+ ) => T;
26
+ markImageAssetMap?: (images: Record<string, string>) => void;
27
+ markImageSequenceAssetMap?: (
28
+ imageSequences: Record<string, string>,
29
+ ) => void;
30
+ markVideoAssetMap?: (videos: Record<string, string>) => void;
31
+ markAudioAssetMap?: (audios: Record<string, string>) => void;
32
+ }
33
+
34
+ interface Window {
35
+ PhaserBridge?: PhaserBridgeApi;
36
+ }
37
+ }
38
+
39
+ export {};
@@ -0,0 +1,28 @@
1
+ import Phaser from 'phaser';
2
+
3
+ import { BootScene } from './scene/BootScene';
4
+ import { DevLoadingScene } from './scene/DevLoadingScene';
5
+ import './style.css';
6
+
7
+ const createGame = (_launchSpec?: LaunchSpec): Phaser.Game =>
8
+ new Phaser.Game({
9
+ type: Phaser.AUTO,
10
+ pixelArt: true,
11
+ parent: 'game',
12
+ // TODO 根据游戏的要求进行尺寸
13
+ width: 540,
14
+ height: 960,
15
+ backgroundColor: '#ffffff',
16
+ scale: {
17
+ mode: Phaser.Scale.FIT,
18
+ autoCenter: Phaser.Scale.CENTER_BOTH,
19
+ },
20
+ scene: [BootScene, DevLoadingScene],
21
+ });
22
+
23
+ // 注册游戏到 PhaserBridge,可以在扣子网页游戏开发中编辑评论
24
+ if (window.PhaserBridge) {
25
+ window.PhaserBridge.bind({ launchGame: createGame });
26
+ } else {
27
+ createGame();
28
+ }
@@ -0,0 +1,213 @@
1
+ import Phaser from 'phaser';
2
+
3
+ import {
4
+ getAssetLabelFromFileKey,
5
+ getImageSequenceRuntimeKeys,
6
+ loadAssets,
7
+ markEditable,
8
+ registerImageSequenceAnimations,
9
+ } from '@/utils';
10
+
11
+ import { AUDIOS, IMAGES, IMAGE_SEQUENCES, VIDEOS } from '../assets';
12
+
13
+ interface LoaderFileLike {
14
+ key: string;
15
+ }
16
+
17
+ const LOADING_TEXT_STYLE: Phaser.Types.GameObjects.Text.TextStyle = {
18
+ color: '#71717a',
19
+ fontFamily: '"PingFang SC", "Microsoft YaHei", sans-serif',
20
+ fontSize: '18px',
21
+ };
22
+
23
+ const DEV_LOADING_ASSET_KEY = 'coze-game-loading';
24
+ const devLoadingSource = IMAGE_SEQUENCES[DEV_LOADING_ASSET_KEY];
25
+
26
+ if (!devLoadingSource) {
27
+ throw new Error(`Image sequence asset not found: ${DEV_LOADING_ASSET_KEY}`);
28
+ }
29
+
30
+ const BOOT_IMAGE_SEQUENCES = {
31
+ [DEV_LOADING_ASSET_KEY]: devLoadingSource,
32
+ };
33
+
34
+ const BOOT_ASSETS = {
35
+ imageSequences: BOOT_IMAGE_SEQUENCES,
36
+ };
37
+
38
+ const GAME_IMAGE_SEQUENCES = Object.fromEntries(
39
+ Object.entries(IMAGE_SEQUENCES).filter(
40
+ ([assetKey]) => assetKey !== DEV_LOADING_ASSET_KEY,
41
+ ),
42
+ );
43
+
44
+ const GAME_ASSETS = {
45
+ images: IMAGES,
46
+ imageSequences: GAME_IMAGE_SEQUENCES,
47
+ videos: VIDEOS,
48
+ audios: AUDIOS,
49
+ };
50
+
51
+ export const DEV_LOADING_RUNTIME_KEYS =
52
+ getImageSequenceRuntimeKeys(DEV_LOADING_ASSET_KEY);
53
+
54
+ export class BootScene extends Phaser.Scene {
55
+ private loadingText?: Phaser.GameObjects.Text;
56
+ private progressBar?: Phaser.GameObjects.Graphics;
57
+ private progressText?: Phaser.GameObjects.Text;
58
+ private loadFailed = false;
59
+
60
+ constructor() {
61
+ super('boot');
62
+ }
63
+
64
+ preload(): void {
65
+ loadAssets(this, BOOT_ASSETS);
66
+ }
67
+
68
+ create(): void {
69
+ registerImageSequenceAnimations(this, BOOT_IMAGE_SEQUENCES);
70
+ this.createLoadingView();
71
+
72
+ const queuedFileCount = loadAssets(this, GAME_ASSETS);
73
+ if (queuedFileCount === 0) {
74
+ this.finishLoading();
75
+ return;
76
+ }
77
+
78
+ this.load.on(Phaser.Loader.Events.FILE_PROGRESS, this.onGameFile, this);
79
+ this.load.on(Phaser.Loader.Events.PROGRESS, this.onProgress, this);
80
+ this.load.once(
81
+ Phaser.Loader.Events.FILE_LOAD_ERROR,
82
+ this.onLoadError,
83
+ this,
84
+ );
85
+ this.load.once(Phaser.Loader.Events.COMPLETE, this.finishLoading, this);
86
+ this.load.start();
87
+ }
88
+
89
+ private createLoadingView(): void {
90
+ const { centerX, centerY } = this.cameras.main;
91
+
92
+ markEditable(
93
+ 'boot.loading-mascot',
94
+ this.add
95
+ .sprite(centerX, centerY - 58, DEV_LOADING_RUNTIME_KEYS.texture)
96
+ .setScale(0.56)
97
+ .play(DEV_LOADING_RUNTIME_KEYS.animation),
98
+ { label: '加载吉祥物' },
99
+ );
100
+
101
+ this.loadingText = markEditable(
102
+ 'boot.loading-text',
103
+ this.add
104
+ .text(
105
+ centerX,
106
+ centerY + 120,
107
+ '正在加载游戏资源…',
108
+ LOADING_TEXT_STYLE,
109
+ )
110
+ .setOrigin(0.5),
111
+ { label: '加载状态文案' },
112
+ );
113
+
114
+ this.progressBar = markEditable(
115
+ 'boot.loading-progress-bar',
116
+ this.add.graphics(),
117
+ { label: '加载进度条' },
118
+ );
119
+ this.renderProgressBar(0);
120
+
121
+ this.progressText = markEditable(
122
+ 'boot.loading-progress',
123
+ this.add
124
+ .text(centerX, centerY + 190, '0%', LOADING_TEXT_STYLE)
125
+ .setOrigin(0.5),
126
+ { label: '加载进度文本' },
127
+ );
128
+ }
129
+
130
+ private readonly onGameFile = (file: LoaderFileLike): void => {
131
+ const assetLabel = getAssetLabelFromFileKey(file.key, GAME_ASSETS);
132
+ this.loadingText?.setText(`正在加载${assetLabel}`);
133
+ };
134
+
135
+ private readonly onProgress = (progress: number): void => {
136
+ this.renderProgressBar(progress);
137
+ this.progressText?.setText(`${Math.round(progress * 100)}%`);
138
+ };
139
+
140
+ private readonly onLoadError = (file: LoaderFileLike): void => {
141
+ this.loadFailed = true;
142
+ const assetLabel = getAssetLabelFromFileKey(file.key, GAME_ASSETS);
143
+ this.loadingText?.setText(`${assetLabel}加载失败`);
144
+ };
145
+
146
+ private renderProgressBar(progress: number): void {
147
+ if (!this.progressBar) {
148
+ return;
149
+ }
150
+
151
+ const { centerX, centerY } = this.cameras.main;
152
+ const width = 300;
153
+ const height = 14;
154
+ const x = centerX - width / 2;
155
+ const y = centerY + 154;
156
+ const padding = 3;
157
+ const innerWidth = width - padding * 2;
158
+ const filledWidth = Phaser.Math.Clamp(progress, 0, 1) * innerWidth;
159
+
160
+ this.progressBar.clear();
161
+ this.progressBar.fillStyle(0x102536, 0.12);
162
+ this.progressBar.fillRoundedRect(x, y, width, height, height / 2);
163
+ this.progressBar.lineStyle(2, 0x102536, 0.28);
164
+ this.progressBar.strokeRoundedRect(x, y, width, height, height / 2);
165
+
166
+ if (filledWidth <= 0) {
167
+ return;
168
+ }
169
+
170
+ this.progressBar.fillStyle(0xef648d, 1);
171
+ this.progressBar.fillRoundedRect(
172
+ x + padding,
173
+ y + padding,
174
+ filledWidth,
175
+ height - padding * 2,
176
+ (height - padding * 2) / 2,
177
+ );
178
+
179
+ this.progressBar.fillStyle(0x165ca8, 1);
180
+ this.progressBar.fillCircle(
181
+ Phaser.Math.Clamp(
182
+ x + padding + filledWidth,
183
+ x + padding + 5,
184
+ x + width - padding - 5,
185
+ ),
186
+ y + height / 2,
187
+ 5,
188
+ );
189
+ }
190
+
191
+ private readonly finishLoading = (): void => {
192
+ this.removeLoaderListeners();
193
+
194
+ if (this.loadFailed) {
195
+ this.progressText?.setText('请刷新页面重试');
196
+ return;
197
+ }
198
+
199
+ registerImageSequenceAnimations(this, GAME_IMAGE_SEQUENCES);
200
+ this.scene.start('dev-loading');
201
+ };
202
+
203
+ private removeLoaderListeners(): void {
204
+ this.load.off(Phaser.Loader.Events.FILE_PROGRESS, this.onGameFile, this);
205
+ this.load.off(Phaser.Loader.Events.PROGRESS, this.onProgress, this);
206
+ this.load.off(
207
+ Phaser.Loader.Events.FILE_LOAD_ERROR,
208
+ this.onLoadError,
209
+ this,
210
+ );
211
+ this.load.off(Phaser.Loader.Events.COMPLETE, this.finishLoading, this);
212
+ }
213
+ }