@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,244 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Create the source tar.gz consumed by CreateDeployHistoryV2.
5
+ *
6
+ * Keep this implementation behaviorally aligned with package-project.py, which
7
+ * remains in this directory as the Python reference/backup implementation.
8
+ */
9
+
10
+ 'use strict';
11
+
12
+ const {
13
+ existsSync,
14
+ mkdirSync,
15
+ readFileSync,
16
+ readdirSync,
17
+ statSync,
18
+ } = require('node:fs');
19
+ const path = require('node:path');
20
+ const { spawnSync } = require('node:child_process');
21
+
22
+ const TOML = require('@iarna/toml');
23
+ const tar = require('tar');
24
+
25
+ const EXCLUDED_PARTS = new Set([
26
+ '.codegraph',
27
+ '.next',
28
+ '.venv',
29
+ '__pycache__',
30
+ 'node_modules',
31
+ 'site-packages',
32
+ ]);
33
+ const LOCAL_METADATA_FILES = new Set([
34
+ '.coze-deploy-upload.json',
35
+ '.host.json',
36
+ ]);
37
+ const UPLOAD_SESSION_TEMP_PREFIX = '.coze-deploy-upload-';
38
+ const REPRODUCIBLE_ARCHIVE_MTIME = new Date(0);
39
+ const PROJECT_METADATA_FILE_MODE = 0o644;
40
+
41
+ const parseArguments = argv => {
42
+ const positional = [];
43
+ let pages = false;
44
+ for (const argument of argv) {
45
+ if (argument === '--pages') {
46
+ pages = true;
47
+ } else if (argument.startsWith('-')) {
48
+ throw new Error(`Unknown argument: ${argument}`);
49
+ } else {
50
+ positional.push(argument);
51
+ }
52
+ }
53
+ if (positional.length !== 2) {
54
+ throw new Error(
55
+ 'Usage: package-project.js <source_dir> <output_path> [--pages]',
56
+ );
57
+ }
58
+ return {
59
+ sourceDir: path.resolve(positional[0]),
60
+ outputPath: path.resolve(positional[1]),
61
+ pages,
62
+ };
63
+ };
64
+
65
+ const buildCommandEnv = () => {
66
+ const env = { ...process.env };
67
+ const cliBinDir = path.resolve(__dirname, '..', '..', 'node_modules', '.bin');
68
+ env.PATH = env.PATH
69
+ ? `${cliBinDir}${path.delimiter}${env.PATH}`
70
+ : cliBinDir;
71
+ return env;
72
+ };
73
+
74
+ const readCozeConfig = sourceDir => {
75
+ const cozePath = path.join(sourceDir, '.coze');
76
+ if (!existsSync(cozePath)) {
77
+ return {};
78
+ }
79
+ return TOML.parse(readFileSync(cozePath, 'utf8'));
80
+ };
81
+
82
+ const readCommand = (config, section, key) => {
83
+ const command = config[section]?.[key];
84
+ if (command === undefined) {
85
+ return undefined;
86
+ }
87
+ if (
88
+ !Array.isArray(command) ||
89
+ !command.every(part => typeof part === 'string' && part.length > 0)
90
+ ) {
91
+ throw new Error(`.coze ${section}.${key} must be a string array`);
92
+ }
93
+ return command;
94
+ };
95
+
96
+ const readPlatformCommand = (config, section, key) => {
97
+ const defaultCommand = readCommand(config, section, key);
98
+ const windowsCommand = process.platform === 'win32'
99
+ ? readCommand(config, section, `${key}_win`)
100
+ : undefined;
101
+
102
+ if (windowsCommand?.length) {
103
+ return { command: windowsCommand, key: `${key}_win` };
104
+ }
105
+ return defaultCommand?.length ? { command: defaultCommand, key } : undefined;
106
+ };
107
+
108
+ const runCommand = (command, sourceDir, env) => {
109
+ const result = spawnSync(command[0], command.slice(1), {
110
+ cwd: sourceDir,
111
+ env,
112
+ stdio: 'inherit',
113
+ });
114
+ if (result.error) {
115
+ throw result.error;
116
+ }
117
+ if (result.status !== 0) {
118
+ throw new Error(
119
+ `Command ${command.join(' ')} exited with code ${String(result.status)}`,
120
+ );
121
+ }
122
+ };
123
+
124
+ const runPackCommand = sourceDir => {
125
+ const configuredCommand = readPlatformCommand(
126
+ readCozeConfig(sourceDir),
127
+ 'dev',
128
+ 'pack',
129
+ );
130
+ if (configuredCommand) {
131
+ process.stderr.write(
132
+ `[package] Running dev.${configuredCommand.key}: ${configuredCommand.command.join(' ')}\n`,
133
+ );
134
+ runCommand(configuredCommand.command, sourceDir, buildCommandEnv());
135
+ }
136
+ };
137
+
138
+ const hasIndexHtml = directory =>
139
+ existsSync(path.join(directory, 'index.html')) &&
140
+ statSync(path.join(directory, 'index.html')).isFile();
141
+
142
+ const preparePagesOutput = sourceDir => {
143
+ if (hasIndexHtml(sourceDir)) {
144
+ process.stderr.write(`[package] Pages static source: ${sourceDir}\n`);
145
+ return sourceDir;
146
+ }
147
+
148
+ const cozePath = path.join(sourceDir, '.coze');
149
+ if (!existsSync(cozePath)) {
150
+ throw new Error(
151
+ 'Pages source contains neither index.html nor a .coze file. ' +
152
+ 'Add index.html or configure a Next.js build in .coze.',
153
+ );
154
+ }
155
+
156
+ const config = readCozeConfig(sourceDir);
157
+ const template = config.project?.template;
158
+ if (typeof template !== 'string' || template.trim().toLowerCase() !== 'nextjs') {
159
+ throw new Error(
160
+ 'Pages source contains no index.html; .coze project.template must be "nextjs"',
161
+ );
162
+ }
163
+ if (!readCommand(config, 'deploy', 'build')) {
164
+ throw new Error(
165
+ 'Pages source contains no index.html; .coze deploy.build is required',
166
+ );
167
+ }
168
+ process.stderr.write(`[package] Pages Next.js source: ${sourceDir}\n`);
169
+ return sourceDir;
170
+ };
171
+
172
+ const shouldInclude = (archivePath, excludeCozeFile = false) => {
173
+ const parts = archivePath.split(/[\\/]/u);
174
+ if (
175
+ parts.some(
176
+ part =>
177
+ LOCAL_METADATA_FILES.has(part) ||
178
+ (part.startsWith(UPLOAD_SESSION_TEMP_PREFIX) && part.endsWith('.tmp')),
179
+ )
180
+ ) {
181
+ return false;
182
+ }
183
+ if (parts.includes('.env')) {
184
+ return false;
185
+ }
186
+ if (excludeCozeFile && parts.includes('.coze')) {
187
+ return false;
188
+ }
189
+ if (parts.some(part => part.startsWith('.git'))) {
190
+ return false;
191
+ }
192
+ return !parts.some(part => EXCLUDED_PARTS.has(part));
193
+ };
194
+
195
+ const createTarGzWithoutRoot = async (sourceDir, outputPath, excludeCozeFile = false) => {
196
+ if (!existsSync(sourceDir) || !statSync(sourceDir).isDirectory()) {
197
+ throw new Error(`source directory does not exist: ${sourceDir}`);
198
+ }
199
+ mkdirSync(path.dirname(outputPath), { recursive: true });
200
+ process.stderr.write(`[package] Creating archive from: ${sourceDir}\n`);
201
+ const resolvedOutputPath = path.resolve(outputPath);
202
+ const entries = readdirSync(sourceDir)
203
+ .filter(entry =>
204
+ entry !== '.codegraph' &&
205
+ path.resolve(sourceDir, entry) !== resolvedOutputPath,
206
+ )
207
+ .sort();
208
+ await tar.create(
209
+ {
210
+ cwd: sourceDir,
211
+ file: outputPath,
212
+ filter: archivePath => shouldInclude(archivePath, excludeCozeFile),
213
+ gzip: true,
214
+ mtime: REPRODUCIBLE_ARCHIVE_MTIME,
215
+ onWriteEntry: entry => {
216
+ if (entry.path === '.coze' && entry.stat?.isFile()) {
217
+ entry.stat.mode = PROJECT_METADATA_FILE_MODE;
218
+ }
219
+ },
220
+ portable: true,
221
+ },
222
+ entries,
223
+ );
224
+ const size = statSync(outputPath).size;
225
+ if (size <= 0) {
226
+ throw new Error('source archive is empty');
227
+ }
228
+ process.stderr.write(`[package] Archive ready: ${String(size)} bytes\n`);
229
+ return size;
230
+ };
231
+
232
+ const main = async () => {
233
+ const { sourceDir, outputPath, pages } = parseArguments(process.argv.slice(2));
234
+ const packageDir = pages
235
+ ? preparePagesOutput(sourceDir)
236
+ : (runPackCommand(sourceDir), sourceDir);
237
+ const size = await createTarGzWithoutRoot(packageDir, outputPath, pages);
238
+ process.stdout.write(`${String(size)}\n`);
239
+ };
240
+
241
+ main().catch(error => {
242
+ process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`);
243
+ process.exitCode = 1;
244
+ });
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env python3
2
+ """Create the source tar.gz consumed by CreateDeployHistoryV2.
3
+
4
+ This is the standalone subset of coze-coding's devbox package.py. It keeps the
5
+ same archive layout and deployment exclusions without importing the devbox
6
+ service runtime (FastAPI, S3FS, project managers, and background tasks).
7
+ """
8
+
9
+ import argparse
10
+ import os
11
+ import subprocess
12
+ import sys
13
+ import tarfile
14
+ from pathlib import Path
15
+
16
+ try:
17
+ import tomllib
18
+ except ModuleNotFoundError as error: # pragma: no cover - depends on host Python
19
+ raise RuntimeError("Python 3.11 or newer is required to read .coze") from error
20
+
21
+
22
+ EXCLUDED_PARTS = {
23
+ ".codegraph",
24
+ ".next",
25
+ ".venv",
26
+ "__pycache__",
27
+ "node_modules",
28
+ "site-packages",
29
+ }
30
+ UPLOAD_SESSION_FILENAME = ".coze-deploy-upload.json"
31
+ UPLOAD_SESSION_TEMP_PREFIX = ".coze-deploy-upload-"
32
+ HOST_LINK_FILENAME = ".host.json"
33
+ PROJECT_METADATA_FILE_MODE = 0o644
34
+
35
+
36
+ def build_command_env():
37
+ env = os.environ.copy()
38
+ cli_bin_dir = Path(__file__).resolve().parent.parent / "node_modules" / ".bin"
39
+ path = env.get("PATH", "")
40
+ env["PATH"] = f"{cli_bin_dir}{os.pathsep}{path}" if path else str(cli_bin_dir)
41
+ return env
42
+
43
+
44
+ def read_coze_config(source_dir: Path):
45
+ coze_path = source_dir / ".coze"
46
+ if not coze_path.is_file():
47
+ return {}
48
+ with coze_path.open("rb") as file:
49
+ return tomllib.load(file)
50
+
51
+
52
+ def read_command(config, section: str, key: str):
53
+ command = (config.get(section) or {}).get(key)
54
+ if command is None:
55
+ return None
56
+ if not isinstance(command, list) or not all(
57
+ isinstance(part, str) and part for part in command
58
+ ):
59
+ raise ValueError(f".coze {section}.{key} must be a string array")
60
+ return command
61
+
62
+
63
+ def read_platform_command(config, section: str, key: str, platform=sys.platform):
64
+ default_command = read_command(config, section, key)
65
+ windows_command = (
66
+ read_command(config, section, f"{key}_win")
67
+ if platform.startswith("win")
68
+ else None
69
+ )
70
+ if windows_command:
71
+ return f"{key}_win", windows_command
72
+ return (key, default_command) if default_command else None
73
+
74
+
75
+ def run_pack_command(source_dir: Path):
76
+ configured_command = read_platform_command(
77
+ read_coze_config(source_dir), "dev", "pack"
78
+ )
79
+ if configured_command:
80
+ key, command = configured_command
81
+ print(f"[package] Running dev.{key}: {' '.join(command)}", file=sys.stderr)
82
+ subprocess.run(command, cwd=source_dir, env=build_command_env(), check=True)
83
+
84
+
85
+ def has_index_html(directory: Path):
86
+ return (directory / "index.html").is_file()
87
+
88
+
89
+ def prepare_pages_output(source_dir: Path):
90
+ if has_index_html(source_dir):
91
+ print(f"[package] Pages static source: {source_dir}", file=sys.stderr)
92
+ return source_dir
93
+
94
+ coze_path = source_dir / ".coze"
95
+ if not coze_path.is_file():
96
+ raise ValueError(
97
+ "Pages source contains neither index.html nor a .coze file. "
98
+ "Add index.html or configure a Next.js build in .coze."
99
+ )
100
+
101
+ config = read_coze_config(source_dir)
102
+ template = (config.get("project") or {}).get("template")
103
+ if not isinstance(template, str) or template.strip().lower() != "nextjs":
104
+ raise ValueError(
105
+ 'Pages source contains no index.html; .coze project.template must be "nextjs"'
106
+ )
107
+ if not read_command(config, "deploy", "build"):
108
+ raise ValueError(
109
+ "Pages source contains no index.html; .coze deploy.build is required"
110
+ )
111
+ print(f"[package] Pages Next.js source: {source_dir}", file=sys.stderr)
112
+ return source_dir
113
+
114
+
115
+ def should_exclude(tar_info: tarfile.TarInfo, exclude_coze_file=False):
116
+ parts = Path(tar_info.name).parts
117
+
118
+ # Local deployment metadata is not application source. Including it can change
119
+ # the package digest on the next CLI invocation and make the resumable PreDeploy
120
+ # session look like a different artifact.
121
+ if any(
122
+ part in {UPLOAD_SESSION_FILENAME, HOST_LINK_FILENAME}
123
+ or (part.startswith(UPLOAD_SESSION_TEMP_PREFIX) and part.endswith(".tmp"))
124
+ for part in parts
125
+ ):
126
+ return None
127
+ if ".env" in parts:
128
+ return None
129
+ if exclude_coze_file and ".coze" in parts:
130
+ return None
131
+ if any(part.startswith(".git") for part in parts):
132
+ return None
133
+ if any(part in EXCLUDED_PARTS for part in parts):
134
+ return None
135
+ if parts == (".coze",) and tar_info.isfile():
136
+ tar_info.mode = PROJECT_METADATA_FILE_MODE
137
+ return tar_info
138
+
139
+
140
+ def create_tar_gz_without_root(source_dir: Path, output_path: Path, exclude_coze_file=False):
141
+ if not source_dir.is_dir():
142
+ raise ValueError(f"source directory does not exist: {source_dir}")
143
+ output_path.parent.mkdir(parents=True, exist_ok=True)
144
+ print(f"[package] Creating archive from: {source_dir}", file=sys.stderr)
145
+ with tarfile.open(output_path, "w:gz") as archive:
146
+ for entry in sorted(os.listdir(source_dir)):
147
+ if entry == ".codegraph":
148
+ continue
149
+ if (source_dir / entry).resolve() == output_path.resolve():
150
+ continue
151
+ archive.add(
152
+ source_dir / entry,
153
+ arcname=entry,
154
+ filter=lambda info: should_exclude(info, exclude_coze_file),
155
+ )
156
+ if not output_path.is_file() or output_path.stat().st_size <= 0:
157
+ raise RuntimeError("source archive is empty")
158
+ print(
159
+ f"[package] Archive ready: {output_path.stat().st_size} bytes",
160
+ file=sys.stderr,
161
+ )
162
+
163
+
164
+ def main():
165
+ parser = argparse.ArgumentParser()
166
+ parser.add_argument("source_dir")
167
+ parser.add_argument("output_path")
168
+ parser.add_argument("--pages", action="store_true")
169
+ args = parser.parse_args()
170
+
171
+ source_dir = Path(args.source_dir).resolve()
172
+ output_path = Path(args.output_path).resolve()
173
+ if args.pages:
174
+ package_dir = prepare_pages_output(source_dir)
175
+ else:
176
+ run_pack_command(source_dir)
177
+ package_dir = source_dir
178
+ create_tar_gz_without_root(package_dir, output_path, args.pages)
179
+ print(output_path.stat().st_size)
180
+
181
+
182
+ if __name__ == "__main__":
183
+ main()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coze-arch/cli",
3
- "version": "0.0.37",
3
+ "version": "0.0.39-alpha.bdff32",
4
4
  "private": false,
5
5
  "description": "coze coding devtools cli",
6
6
  "license": "MIT",
@@ -12,6 +12,7 @@
12
12
  "files": [
13
13
  "lib",
14
14
  "bin",
15
+ "docs/deploy.md",
15
16
  "lib/**/.npmrc",
16
17
  "!**/*.tsbuildinfo",
17
18
  "!**/*.map",
@@ -48,7 +49,9 @@
48
49
  "fast-glob": "^3.3.3",
49
50
  "js-yaml": "^4.2.0",
50
51
  "minimist": "^1.2.8",
51
- "shelljs": "^0.10.0"
52
+ "pnpm": "9.0.0",
53
+ "shelljs": "^0.10.0",
54
+ "tar": "^7.5.22"
52
55
  },
53
56
  "devDependencies": {
54
57
  "@coze-arch/cli-logger": "workspace:*",