@coze-arch/cli 0.0.1-alpha.1fb1dc → 0.0.1-alpha.209402

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 (240) hide show
  1. package/README.md +1 -0
  2. package/lib/__templates__/expo/.coze +7 -2
  3. package/lib/__templates__/expo/.cozeproj/scripts/dev_build.sh +46 -0
  4. package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +229 -0
  5. package/lib/__templates__/expo/.cozeproj/scripts/prod_build.sh +47 -0
  6. package/lib/__templates__/expo/.cozeproj/scripts/prod_run.sh +34 -0
  7. package/lib/__templates__/expo/.cozeproj/scripts/server_dev_run.sh +46 -0
  8. package/lib/__templates__/expo/README.md +68 -7
  9. package/lib/__templates__/expo/_gitignore +1 -1
  10. package/lib/__templates__/expo/_npmrc +3 -4
  11. package/lib/__templates__/expo/client/app/+not-found.tsx +15 -64
  12. package/lib/__templates__/expo/client/app/_layout.tsx +15 -12
  13. package/lib/__templates__/expo/client/app/index.tsx +1 -0
  14. package/lib/__templates__/expo/client/app.config.ts +76 -0
  15. package/lib/__templates__/expo/client/components/Screen.tsx +3 -19
  16. package/lib/__templates__/expo/client/components/ThemedText.tsx +33 -0
  17. package/lib/__templates__/expo/client/components/ThemedView.tsx +37 -0
  18. package/lib/__templates__/expo/client/constants/theme.ts +117 -58
  19. package/lib/__templates__/expo/client/declarations.d.ts +5 -0
  20. package/lib/__templates__/expo/{eslint.config.mjs → client/eslint.config.mjs} +40 -10
  21. package/lib/__templates__/expo/client/hooks/useColorScheme.tsx +48 -0
  22. package/lib/__templates__/expo/client/hooks/useSafeRouter.ts +152 -0
  23. package/lib/__templates__/expo/client/hooks/useTheme.ts +26 -6
  24. package/lib/__templates__/expo/client/metro.config.js +124 -0
  25. package/lib/__templates__/expo/client/package.json +95 -0
  26. package/lib/__templates__/expo/client/screens/demo/index.tsx +25 -0
  27. package/lib/__templates__/expo/client/screens/demo/styles.ts +28 -0
  28. package/lib/__templates__/expo/client/scripts/install-missing-deps.js +11 -10
  29. package/lib/__templates__/expo/client/tsconfig.json +24 -0
  30. package/lib/__templates__/expo/client/utils/index.ts +22 -0
  31. package/lib/__templates__/expo/eslint-plugins/fontawesome6/index.js +9 -0
  32. package/lib/__templates__/expo/eslint-plugins/fontawesome6/names.js +1889 -0
  33. package/lib/__templates__/expo/eslint-plugins/fontawesome6/rule.js +174 -0
  34. package/lib/__templates__/expo/eslint-plugins/fontawesome6/v5-only-names.js +388 -0
  35. package/lib/__templates__/expo/eslint-plugins/forbid-emoji/index.js +9 -0
  36. package/lib/__templates__/expo/eslint-plugins/forbid-emoji/rule.js +112 -0
  37. package/lib/__templates__/expo/eslint-plugins/forbid-emoji/tech.md +94 -0
  38. package/lib/__templates__/expo/eslint-plugins/react-native/index.js +9 -0
  39. package/lib/__templates__/expo/eslint-plugins/react-native/rule.js +64 -0
  40. package/lib/__templates__/expo/eslint-plugins/reanimated/index.js +9 -0
  41. package/lib/__templates__/expo/eslint-plugins/reanimated/rule.js +88 -0
  42. package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/index.js +9 -0
  43. package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/rule.js +120 -0
  44. package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/tech.md +58 -0
  45. package/lib/__templates__/expo/package.json +16 -103
  46. package/lib/__templates__/expo/patches/expo@54.0.33.patch +45 -0
  47. package/lib/__templates__/expo/pnpm-lock.yaml +1437 -3171
  48. package/lib/__templates__/expo/pnpm-workspace.yaml +3 -0
  49. package/lib/__templates__/expo/server/build.js +21 -0
  50. package/lib/__templates__/expo/server/package.json +34 -0
  51. package/lib/__templates__/expo/server/src/index.ts +20 -0
  52. package/lib/__templates__/expo/server/tsconfig.json +24 -0
  53. package/lib/__templates__/expo/template.config.js +57 -0
  54. package/lib/__templates__/expo/tsconfig.json +1 -24
  55. package/lib/__templates__/native-static/.coze +11 -0
  56. package/lib/__templates__/native-static/index.html +33 -0
  57. package/lib/__templates__/native-static/styles/main.css +136 -0
  58. package/lib/__templates__/native-static/template.config.js +22 -0
  59. package/lib/__templates__/nextjs/.babelrc +15 -0
  60. package/lib/__templates__/nextjs/.coze +1 -0
  61. package/lib/__templates__/nextjs/AGENTS.md +54 -0
  62. package/lib/__templates__/nextjs/README.md +5 -0
  63. package/lib/__templates__/nextjs/_npmrc +1 -0
  64. package/lib/__templates__/nextjs/eslint.config.mjs +5 -0
  65. package/lib/__templates__/nextjs/next.config.ts +11 -0
  66. package/lib/__templates__/nextjs/package.json +10 -2
  67. package/lib/__templates__/nextjs/pnpm-lock.yaml +3488 -1569
  68. package/lib/__templates__/nextjs/scripts/build.sh +4 -1
  69. package/lib/__templates__/nextjs/scripts/dev.sh +15 -28
  70. package/lib/__templates__/nextjs/scripts/prepare.sh +9 -0
  71. package/lib/__templates__/nextjs/scripts/start.sh +7 -1
  72. package/lib/__templates__/nextjs/src/app/globals.css +109 -89
  73. package/lib/__templates__/nextjs/src/app/layout.tsx +23 -32
  74. package/lib/__templates__/nextjs/src/app/page.tsx +18 -49
  75. package/lib/__templates__/nextjs/src/components/ui/resizable.tsx +29 -22
  76. package/lib/__templates__/nextjs/src/components/ui/sidebar.tsx +228 -230
  77. package/lib/__templates__/nextjs/src/server.ts +35 -0
  78. package/lib/__templates__/nextjs/template.config.js +67 -2
  79. package/lib/__templates__/nextjs/tsconfig.json +1 -1
  80. package/lib/__templates__/nuxt-vue/.coze +12 -0
  81. package/lib/__templates__/nuxt-vue/AGENTS.md +42 -0
  82. package/lib/__templates__/nuxt-vue/README.md +73 -0
  83. package/lib/__templates__/nuxt-vue/_gitignore +24 -0
  84. package/lib/__templates__/nuxt-vue/_npmrc +23 -0
  85. package/lib/__templates__/nuxt-vue/app/app.vue +6 -0
  86. package/lib/__templates__/nuxt-vue/app/pages/index.vue +23 -0
  87. package/lib/__templates__/nuxt-vue/assets/css/main.css +24 -0
  88. package/lib/__templates__/nuxt-vue/nuxt.config.ts +116 -0
  89. package/lib/__templates__/nuxt-vue/package.json +35 -0
  90. package/lib/__templates__/nuxt-vue/pnpm-lock.yaml +8759 -0
  91. package/lib/__templates__/nuxt-vue/postcss.config.mjs +8 -0
  92. package/lib/__templates__/nuxt-vue/public/favicon.ico +0 -0
  93. package/lib/__templates__/nuxt-vue/public/robots.txt +2 -0
  94. package/lib/__templates__/nuxt-vue/scripts/build.sh +14 -0
  95. package/lib/__templates__/nuxt-vue/scripts/dev.sh +39 -0
  96. package/lib/__templates__/nuxt-vue/scripts/prepare.sh +14 -0
  97. package/lib/__templates__/nuxt-vue/scripts/start.sh +21 -0
  98. package/lib/__templates__/nuxt-vue/server/api/hello.ts +10 -0
  99. package/lib/__templates__/nuxt-vue/server/middleware/logger.ts +10 -0
  100. package/lib/__templates__/nuxt-vue/server/routes/health.ts +10 -0
  101. package/lib/__templates__/nuxt-vue/tailwind.config.js +13 -0
  102. package/lib/__templates__/nuxt-vue/template.config.js +87 -0
  103. package/lib/__templates__/nuxt-vue/tsconfig.json +18 -0
  104. package/lib/__templates__/taro/.coze +14 -0
  105. package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +19 -0
  106. package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +14 -0
  107. package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +2 -0
  108. package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +151 -0
  109. package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +5 -0
  110. package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +24 -0
  111. package/lib/__templates__/taro/README.md +763 -0
  112. package/lib/__templates__/taro/_gitignore +40 -0
  113. package/lib/__templates__/taro/_npmrc +18 -0
  114. package/lib/__templates__/taro/babel.config.js +12 -0
  115. package/lib/__templates__/taro/config/dev.ts +9 -0
  116. package/lib/__templates__/taro/config/index.ts +238 -0
  117. package/lib/__templates__/taro/config/prod.ts +34 -0
  118. package/lib/__templates__/taro/eslint.config.mjs +135 -0
  119. package/lib/__templates__/taro/key/private.appid.key +0 -0
  120. package/lib/__templates__/taro/package.json +112 -0
  121. package/lib/__templates__/taro/patches/@tarojs__plugin-mini-ci@4.1.9.patch +30 -0
  122. package/lib/__templates__/taro/pnpm-lock.yaml +23412 -0
  123. package/lib/__templates__/taro/pnpm-workspace.yaml +2 -0
  124. package/lib/__templates__/taro/project.config.json +15 -0
  125. package/lib/__templates__/taro/server/nest-cli.json +10 -0
  126. package/lib/__templates__/taro/server/package.json +40 -0
  127. package/lib/__templates__/taro/server/src/app.controller.ts +23 -0
  128. package/lib/__templates__/taro/server/src/app.module.ts +10 -0
  129. package/lib/__templates__/taro/server/src/app.service.ts +8 -0
  130. package/lib/__templates__/taro/server/src/interceptors/http-status.interceptor.ts +23 -0
  131. package/lib/__templates__/taro/server/src/main.ts +49 -0
  132. package/lib/__templates__/taro/server/tsconfig.json +24 -0
  133. package/lib/__templates__/taro/src/app.config.ts +11 -0
  134. package/lib/__templates__/taro/src/app.css +156 -0
  135. package/lib/__templates__/taro/src/app.tsx +9 -0
  136. package/lib/__templates__/taro/src/components/ui/accordion.tsx +159 -0
  137. package/lib/__templates__/taro/src/components/ui/alert-dialog.tsx +260 -0
  138. package/lib/__templates__/taro/src/components/ui/alert.tsx +60 -0
  139. package/lib/__templates__/taro/src/components/ui/aspect-ratio.tsx +36 -0
  140. package/lib/__templates__/taro/src/components/ui/avatar.tsx +84 -0
  141. package/lib/__templates__/taro/src/components/ui/badge.tsx +37 -0
  142. package/lib/__templates__/taro/src/components/ui/breadcrumb.tsx +117 -0
  143. package/lib/__templates__/taro/src/components/ui/button-group.tsx +83 -0
  144. package/lib/__templates__/taro/src/components/ui/button.tsx +67 -0
  145. package/lib/__templates__/taro/src/components/ui/calendar.tsx +394 -0
  146. package/lib/__templates__/taro/src/components/ui/card.tsx +108 -0
  147. package/lib/__templates__/taro/src/components/ui/carousel.tsx +228 -0
  148. package/lib/__templates__/taro/src/components/ui/checkbox.tsx +58 -0
  149. package/lib/__templates__/taro/src/components/ui/code-block.tsx +169 -0
  150. package/lib/__templates__/taro/src/components/ui/collapsible.tsx +71 -0
  151. package/lib/__templates__/taro/src/components/ui/command.tsx +385 -0
  152. package/lib/__templates__/taro/src/components/ui/context-menu.tsx +614 -0
  153. package/lib/__templates__/taro/src/components/ui/dialog.tsx +256 -0
  154. package/lib/__templates__/taro/src/components/ui/drawer.tsx +192 -0
  155. package/lib/__templates__/taro/src/components/ui/dropdown-menu.tsx +561 -0
  156. package/lib/__templates__/taro/src/components/ui/field.tsx +228 -0
  157. package/lib/__templates__/taro/src/components/ui/hover-card.tsx +282 -0
  158. package/lib/__templates__/taro/src/components/ui/input-group.tsx +197 -0
  159. package/lib/__templates__/taro/src/components/ui/input-otp.tsx +136 -0
  160. package/lib/__templates__/taro/src/components/ui/input.tsx +56 -0
  161. package/lib/__templates__/taro/src/components/ui/label.tsx +24 -0
  162. package/lib/__templates__/taro/src/components/ui/menubar.tsx +595 -0
  163. package/lib/__templates__/taro/src/components/ui/navigation-menu.tsx +264 -0
  164. package/lib/__templates__/taro/src/components/ui/pagination.tsx +118 -0
  165. package/lib/__templates__/taro/src/components/ui/popover.tsx +291 -0
  166. package/lib/__templates__/taro/src/components/ui/portal.tsx +19 -0
  167. package/lib/__templates__/taro/src/components/ui/progress.tsx +28 -0
  168. package/lib/__templates__/taro/src/components/ui/radio-group.tsx +64 -0
  169. package/lib/__templates__/taro/src/components/ui/resizable.tsx +346 -0
  170. package/lib/__templates__/taro/src/components/ui/scroll-area.tsx +34 -0
  171. package/lib/__templates__/taro/src/components/ui/select.tsx +438 -0
  172. package/lib/__templates__/taro/src/components/ui/separator.tsx +30 -0
  173. package/lib/__templates__/taro/src/components/ui/sheet.tsx +262 -0
  174. package/lib/__templates__/taro/src/components/ui/skeleton.tsx +17 -0
  175. package/lib/__templates__/taro/src/components/ui/slider.tsx +203 -0
  176. package/lib/__templates__/taro/src/components/ui/sonner.tsx +1 -0
  177. package/lib/__templates__/taro/src/components/ui/switch.tsx +55 -0
  178. package/lib/__templates__/taro/src/components/ui/table.tsx +142 -0
  179. package/lib/__templates__/taro/src/components/ui/tabs.tsx +114 -0
  180. package/lib/__templates__/taro/src/components/ui/textarea.tsx +54 -0
  181. package/lib/__templates__/taro/src/components/ui/toast.tsx +517 -0
  182. package/lib/__templates__/taro/src/components/ui/toggle-group.tsx +120 -0
  183. package/lib/__templates__/taro/src/components/ui/toggle.tsx +77 -0
  184. package/lib/__templates__/taro/src/components/ui/tooltip.tsx +455 -0
  185. package/lib/__templates__/taro/src/index.html +39 -0
  186. package/lib/__templates__/taro/src/lib/hooks/use-keyboard-offset.ts +37 -0
  187. package/lib/__templates__/taro/src/lib/measure.ts +115 -0
  188. package/lib/__templates__/taro/src/lib/platform.ts +12 -0
  189. package/lib/__templates__/taro/src/lib/utils.ts +6 -0
  190. package/lib/__templates__/taro/src/network.ts +39 -0
  191. package/lib/__templates__/taro/src/pages/index/index.config.ts +3 -0
  192. package/lib/__templates__/taro/src/pages/index/index.css +1 -0
  193. package/lib/__templates__/taro/src/pages/index/index.tsx +33 -0
  194. package/lib/__templates__/taro/src/presets/dev-debug.ts +23 -0
  195. package/lib/__templates__/taro/src/presets/h5-container.tsx +15 -0
  196. package/lib/__templates__/taro/src/presets/h5-navbar.tsx +238 -0
  197. package/lib/__templates__/taro/src/presets/h5-styles.ts +220 -0
  198. package/lib/__templates__/taro/src/presets/index.tsx +18 -0
  199. package/lib/__templates__/taro/stylelint.config.mjs +4 -0
  200. package/lib/__templates__/taro/template.config.js +68 -0
  201. package/lib/__templates__/taro/tsconfig.json +29 -0
  202. package/lib/__templates__/taro/types/global.d.ts +32 -0
  203. package/lib/__templates__/templates.json +136 -36
  204. package/lib/__templates__/vite/.coze +1 -0
  205. package/lib/__templates__/vite/AGENTS.md +41 -0
  206. package/lib/__templates__/vite/README.md +190 -11
  207. package/lib/__templates__/vite/_gitignore +1 -0
  208. package/lib/__templates__/vite/_npmrc +1 -0
  209. package/lib/__templates__/vite/eslint.config.mjs +14 -0
  210. package/lib/__templates__/vite/package.json +23 -3
  211. package/lib/__templates__/vite/pnpm-lock.yaml +2509 -293
  212. package/lib/__templates__/vite/scripts/build.sh +4 -1
  213. package/lib/__templates__/vite/scripts/dev.sh +16 -28
  214. package/lib/__templates__/vite/scripts/prepare.sh +9 -0
  215. package/lib/__templates__/vite/scripts/start.sh +9 -3
  216. package/lib/__templates__/vite/server/routes/index.ts +31 -0
  217. package/lib/__templates__/vite/server/server.ts +65 -0
  218. package/lib/__templates__/vite/server/vite.ts +67 -0
  219. package/lib/__templates__/vite/src/main.ts +17 -48
  220. package/lib/__templates__/vite/template.config.js +78 -8
  221. package/lib/__templates__/vite/tsconfig.json +4 -3
  222. package/lib/__templates__/vite/vite.config.ts +8 -3
  223. package/lib/cli.js +1711 -298
  224. package/package.json +17 -5
  225. package/lib/__templates__/expo/.cozeproj/scripts/deploy_build.sh +0 -116
  226. package/lib/__templates__/expo/.cozeproj/scripts/deploy_run.sh +0 -239
  227. package/lib/__templates__/expo/app.json +0 -63
  228. package/lib/__templates__/expo/babel.config.js +0 -9
  229. package/lib/__templates__/expo/client/app/(tabs)/_layout.tsx +0 -43
  230. package/lib/__templates__/expo/client/app/(tabs)/home.tsx +0 -1
  231. package/lib/__templates__/expo/client/app/(tabs)/index.tsx +0 -7
  232. package/lib/__templates__/expo/client/hooks/useColorScheme.ts +0 -1
  233. package/lib/__templates__/expo/client/index.js +0 -12
  234. package/lib/__templates__/expo/client/screens/home/index.tsx +0 -51
  235. package/lib/__templates__/expo/client/screens/home/styles.ts +0 -60
  236. package/lib/__templates__/expo/metro.config.js +0 -53
  237. package/lib/__templates__/expo/src/index.ts +0 -12
  238. package/lib/__templates__/nextjs/.vscode/settings.json +0 -121
  239. package/lib/__templates__/vite/.vscode/settings.json +0 -7
  240. /package/lib/__templates__/expo/{eslint-formatter-simple.mjs → client/eslint-formatter-simple.mjs} +0 -0
package/lib/cli.js CHANGED
@@ -4,13 +4,17 @@
4
4
  var commander = require('commander');
5
5
  var path = require('path');
6
6
  var fs = require('fs');
7
+ var debug = require('debug');
8
+ var cliSlardar = require('@coze-arch/cli-slardar');
9
+ var node_path = require('node:path');
10
+ var node_fs = require('node:fs');
7
11
  var shelljs = require('shelljs');
8
12
  var perf_hooks = require('perf_hooks');
9
13
  var fs$1 = require('fs/promises');
10
- var toml = require('@iarna/toml');
14
+ var os = require('os');
11
15
  var jsYaml = require('js-yaml');
16
+ var toml = require('@iarna/toml');
12
17
  var child_process = require('child_process');
13
- var os = require('os');
14
18
  var addFormats = require('ajv-formats');
15
19
  var Ajv = require('ajv');
16
20
  var minimist = require('minimist');
@@ -127,6 +131,235 @@ const generateTemplatesHelpText = () => {
127
131
  return lines.join('\n');
128
132
  };
129
133
 
134
+ var name = "@coze-arch/cli";
135
+ var version = "0.0.1-alpha.209402";
136
+ var description = "coze coding devtools cli";
137
+ var license = "MIT";
138
+ var author = "fanwenjie.fe@bytedance.com";
139
+ var maintainers = [
140
+ ];
141
+ var bin = {
142
+ coze: "bin/main"
143
+ };
144
+ var files = [
145
+ "lib",
146
+ "bin",
147
+ "lib/**/.npmrc",
148
+ "!**/*.tsbuildinfo",
149
+ "!**/*.map"
150
+ ];
151
+ var scripts = {
152
+ prebuild: "tsx scripts/prebuild.ts",
153
+ build: "tsx scripts/build.ts",
154
+ create: "tsx scripts/create-template.ts",
155
+ lint: "eslint ./ --cache",
156
+ postpublish: "bash scripts/sync-npmmirror.sh",
157
+ test: "vitest --run --passWithNoTests",
158
+ "test:all": "bash scripts/test-coverage.sh",
159
+ "test:cov": "vitest --run --passWithNoTests --coverage",
160
+ "test:e2e": "NODE_ENV=test bash scripts/e2e.sh",
161
+ "test:perf": "vitest bench --run --config vitest.perf.config.ts",
162
+ "test:perf:compare": "bash scripts/compare-perf.sh",
163
+ "test:perf:save": "bash scripts/run-perf-with-output.sh"
164
+ };
165
+ var dependencies = {
166
+ "@coze-arch/cli-slardar": "workspace:*",
167
+ "@iarna/toml": "^2.2.5",
168
+ ajv: "^8.17.1",
169
+ "ajv-formats": "^3.0.1",
170
+ "change-case": "^5.4.4",
171
+ commander: "~12.1.0",
172
+ debug: "^4.3.7",
173
+ ejs: "^3.1.10",
174
+ "js-yaml": "^4.1.0",
175
+ minimist: "^1.2.5",
176
+ shelljs: "^0.10.0"
177
+ };
178
+ var devDependencies = {
179
+ "@coze-arch/cli-logger": "workspace:*",
180
+ "@coze-arch/eslint-config": "workspace:*",
181
+ "@coze-arch/monorepo-kits": "workspace:*",
182
+ "@coze-arch/rollup-config": "workspace:*",
183
+ "@coze-arch/ts-config": "workspace:*",
184
+ "@coze-arch/vitest-config": "workspace:*",
185
+ "@coze-coding/lambda": "workspace:*",
186
+ "@inquirer/prompts": "^3.2.0",
187
+ "@playwright/test": "~1.55.0",
188
+ "@types/debug": "^4.1.12",
189
+ "@types/ejs": "^3.1.5",
190
+ "@types/iarna__toml": "^2.0.5",
191
+ "@types/js-yaml": "^4.0.9",
192
+ "@types/minimatch": "^5.1.2",
193
+ "@types/minimist": "^1.2.5",
194
+ "@types/node": "^24",
195
+ "@types/shelljs": "^0.10.0",
196
+ "@vitest/coverage-v8": "~4.0.18",
197
+ "json-schema-to-typescript": "^15.0.3",
198
+ minimatch: "^10.0.1",
199
+ playwright: "~1.55.0",
200
+ rollup: "^4.41.1",
201
+ sucrase: "^3.35.0",
202
+ "tree-kill": "^1.2.2",
203
+ tsx: "^4.20.6",
204
+ "vite-tsconfig-paths": "^4.2.1",
205
+ vitest: "~4.0.18"
206
+ };
207
+ var publishConfig = {
208
+ access: "public",
209
+ registry: "https://registry.npmjs.org"
210
+ };
211
+ var cozePublishConfig = {
212
+ bin: {
213
+ coze: "bin/main"
214
+ }
215
+ };
216
+ var packageJson = {
217
+ name: name,
218
+ version: version,
219
+ "private": false,
220
+ description: description,
221
+ license: license,
222
+ author: author,
223
+ maintainers: maintainers,
224
+ bin: bin,
225
+ files: files,
226
+ scripts: scripts,
227
+ dependencies: dependencies,
228
+ devDependencies: devDependencies,
229
+ publishConfig: publishConfig,
230
+ cozePublishConfig: cozePublishConfig
231
+ };
232
+
233
+ function _optionalChain$4(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/**
234
+ * Slardar 监控初始化和上报
235
+ */
236
+
237
+ const log = debug('slardar:cli');
238
+
239
+ /**
240
+ * 安全执行函数包装器
241
+ * 捕获并静默处理所有错误,确保 Slardar 上报失败不影响 CLI 正常运行
242
+ * 支持同步和异步函数
243
+ */
244
+ function safeRun(
245
+ name,
246
+ fn,
247
+ ) {
248
+ return ((...args) => {
249
+ try {
250
+ log('Calling Slardar function: %s', name);
251
+ const result = fn(...args);
252
+
253
+ // 如果是 Promise,处理异步错误
254
+ if (result instanceof Promise) {
255
+ return result
256
+ .then(res => {
257
+ log('Slardar function %s completed', name);
258
+ return res;
259
+ })
260
+ .catch(error => {
261
+ log(
262
+ 'Slardar function %s failed: %s',
263
+ name,
264
+ (error ).message,
265
+ );
266
+ void error;
267
+ });
268
+ }
269
+
270
+ log('Slardar function %s completed', name);
271
+ return result;
272
+ } catch (error) {
273
+ // Slardar 上报失败不应影响 CLI 正常运行,但要记录错误
274
+ log('Slardar function %s failed: %s', name, (error ).message);
275
+ }
276
+ }) ;
277
+ }
278
+
279
+ /**
280
+ * 初始化 Slardar Reporter
281
+ */
282
+ const initSlardar = safeRun('initSlardar', () => {
283
+ cliSlardar.reporter.setup({
284
+ bid: 'coze_codign_cli',
285
+ release: packageJson.version,
286
+ env: process.env.NODE_ENV || 'production',
287
+ userId: process.env.USER,
288
+ useLocalConfig: false, // 启用服务端采样率配置
289
+ domain: 'mon.zijieapi.com', // Node.js 环境上报域名
290
+ });
291
+
292
+ // 设置全局上下文
293
+ cliSlardar.reporter.mergeContext({
294
+ platform: process.platform,
295
+ nodeVersion: process.version,
296
+ cliVersion: packageJson.version,
297
+ });
298
+ });
299
+
300
+ /**
301
+ * 上报命令执行
302
+ */
303
+ const reportCommandStart = safeRun(
304
+ 'reportCommandStart',
305
+ (command, args, extraCategories) => {
306
+ const event = cliSlardar.EventBuilder.cliCommand(command, {
307
+ args,
308
+ categories: extraCategories,
309
+ });
310
+ cliSlardar.reporter.sendEvent(event.name, event.metrics, event.categories);
311
+ },
312
+ );
313
+
314
+ /**
315
+ * 上报命令完成
316
+ */
317
+ const reportCommandComplete = safeRun(
318
+ 'reportCommandComplete',
319
+ (
320
+ command,
321
+ success,
322
+ duration,
323
+ options
324
+
325
+
326
+
327
+
328
+ ,
329
+ ) => {
330
+ const event = cliSlardar.EventBuilder.cliCommandComplete(command, success, duration, {
331
+ args: _optionalChain$4([options, 'optionalAccess', _ => _.args]),
332
+ errorCode: _optionalChain$4([options, 'optionalAccess', _2 => _2.errorCode]),
333
+ categories: {
334
+ ...(_optionalChain$4([options, 'optionalAccess', _3 => _3.errorMessage]) && { errorMessage: options.errorMessage }),
335
+ ..._optionalChain$4([options, 'optionalAccess', _4 => _4.categories]),
336
+ },
337
+ });
338
+ cliSlardar.reporter.sendEvent(event.name, event.metrics, event.categories);
339
+ },
340
+ );
341
+
342
+ /**
343
+ * 上报错误(使用 JS 错误上报,会显示在 Slardar JS 错误总览页面)
344
+ */
345
+ const reportError = safeRun(
346
+ 'reportError',
347
+ (error, context) => {
348
+ cliSlardar.reporter.reportError(error, context, {
349
+ type: 'cli',
350
+ data: context,
351
+ });
352
+ },
353
+ );
354
+
355
+ /**
356
+ * 立即上报(在 CLI 退出前调用)
357
+ * 等待 500ms 确保设置请求完成和事件发送
358
+ */
359
+ const flushSlardar = safeRun('flushSlardar', async () => {
360
+ await cliSlardar.reporter.flush();
361
+ });
362
+
130
363
  function _nullishCoalesce$2(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var LogLevel; (function (LogLevel) {
131
364
  const ERROR = 0; LogLevel[LogLevel["ERROR"] = ERROR] = "ERROR";
132
365
  const WARN = 1; LogLevel[LogLevel["WARN"] = WARN] = "WARN";
@@ -481,6 +714,14 @@ const warmupTemplate = (templatePath, templateName) => {
481
714
  logger.info(`\nWarming up template: ${templateName}`);
482
715
  logger.info(` Path: ${templatePath}`);
483
716
 
717
+ // 检查是否存在 package.json
718
+ const packageJsonPath = node_path.join(templatePath, 'package.json');
719
+ // eslint-disable-next-line security/detect-non-literal-fs-filename
720
+ if (!node_fs.existsSync(packageJsonPath)) {
721
+ logger.info(` ⊘ Skipping ${templateName} (no package.json found)`);
722
+ return;
723
+ }
724
+
484
725
  const result = shelljs.exec('pnpm install', {
485
726
  cwd: templatePath,
486
727
  silent: true,
@@ -514,13 +755,7 @@ const warmupTemplate = (templatePath, templateName) => {
514
755
  /**
515
756
  * 执行 warmup 命令的内部实现
516
757
  */
517
- const executeWarmup = async (
518
- options
519
-
520
- ,
521
-
522
- command,
523
- ) => {
758
+ const executeWarmup = async (options) => {
524
759
  const timer = new TimeTracker();
525
760
 
526
761
  try {
@@ -585,16 +820,361 @@ const executeWarmup = async (
585
820
  /**
586
821
  * 注册 warmup 命令到 program
587
822
  */
588
- const registerCommand$2 = program => {
823
+ const registerCommand$4 = program => {
589
824
  program
590
825
  .command('warmup')
591
826
  .description('Pre-install dependencies for templates to speed up init')
592
827
  .option('-t, --template <name>', 'Warmup a specific template only')
593
- .action(async (options, command) => {
828
+ .action(async options => {
594
829
  await executeWarmup(options);
595
830
  });
596
831
  };
597
832
 
833
+ // ABOUTME: This file implements the update command for coze CLI
834
+ // ABOUTME: It wraps pnpm update/install to update package dependencies with logging support
835
+
836
+
837
+
838
+ /**
839
+ * 日志文件名常量
840
+ */
841
+ const LOG_FILE_NAME$1 = 'update.log';
842
+
843
+ /**
844
+ * 获取日志目录
845
+ * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
846
+ */
847
+ const getLogDir$1 = () =>
848
+ process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
849
+
850
+ /**
851
+ * 解析日志文件路径
852
+ * - 如果是绝对路径,直接使用
853
+ * - 如果是相对路径,基于 getLogDir() + 相对路径
854
+ * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
855
+ */
856
+ const resolveLogFilePath$1 = (logFile) => {
857
+ if (!logFile) {
858
+ return path.join(getLogDir$1(), LOG_FILE_NAME$1);
859
+ }
860
+
861
+ if (path.isAbsolute(logFile)) {
862
+ return logFile;
863
+ }
864
+
865
+ return path.join(getLogDir$1(), logFile);
866
+ };
867
+
868
+ /**
869
+ * 创建日志写入流
870
+ */
871
+ const createLogStream$1 = (logFilePath) => {
872
+ const logDir = path.dirname(logFilePath);
873
+
874
+ // 确保日志目录存在
875
+ if (!fs.existsSync(logDir)) {
876
+ fs.mkdirSync(logDir, { recursive: true });
877
+ }
878
+
879
+ // 使用 'w' 标志覆盖之前的日志
880
+ return fs.createWriteStream(logFilePath, { flags: 'w' });
881
+ };
882
+
883
+ /**
884
+ * 格式化时间戳
885
+ */
886
+ const formatTimestamp = () => {
887
+ const now = new Date();
888
+ return now.toISOString();
889
+ };
890
+
891
+ /**
892
+ * 写入带时间戳的日志
893
+ */
894
+ const writeLogWithTimestamp = (stream, message) => {
895
+ const timestamp = formatTimestamp();
896
+ const lines = message.split('\n');
897
+ lines.forEach(line => {
898
+ if (line) {
899
+ stream.write(`[${timestamp}] ${line}\n`);
900
+ } else {
901
+ stream.write('\n');
902
+ }
903
+ });
904
+ // 确保数据写入磁盘
905
+ stream.uncork();
906
+ };
907
+
908
+ /**
909
+ * 同时输出到控制台和日志文件
910
+ */
911
+ const logWithFile = (
912
+ stream,
913
+ level,
914
+ message,
915
+ ) => {
916
+ // 输出到控制台
917
+ switch (level) {
918
+ case 'info':
919
+ logger.info(message);
920
+ break;
921
+ case 'success':
922
+ logger.success(message);
923
+ break;
924
+ case 'error':
925
+ logger.error(message);
926
+ break;
927
+ default:
928
+ logger.info(message);
929
+ break;
930
+ }
931
+
932
+ // 写入日志文件(带时间戳)
933
+ writeLogWithTimestamp(stream, `[${level.toUpperCase()}] ${message}`);
934
+ };
935
+
936
+ // start_aigc
937
+ /**
938
+ * 构建 pnpm add 命令
939
+ */
940
+ const buildPnpmCommand = (
941
+ packageName,
942
+ options
943
+
944
+
945
+
946
+
947
+ ,
948
+ ) => {
949
+ const { global, version, registry, extraArgs } = options;
950
+
951
+ const parts = ['pnpm', 'add'];
952
+
953
+ // 添加全局标记
954
+ if (global) {
955
+ parts.push('-g');
956
+ }
957
+
958
+ // 添加包名和版本
959
+ if (version && version !== 'latest') {
960
+ parts.push(`${packageName}@${version}`);
961
+ } else {
962
+ parts.push(`${packageName}@latest`);
963
+ }
964
+
965
+ // 添加 registry
966
+ if (registry) {
967
+ parts.push(`--registry=${registry}`);
968
+ }
969
+
970
+ // 添加额外参数
971
+ if (extraArgs.length > 0) {
972
+ parts.push(...extraArgs);
973
+ }
974
+
975
+ return parts.join(' ');
976
+ };
977
+ // end_aigc
978
+
979
+ /**
980
+ * 处理更新失败的错误
981
+ */
982
+ const handleUpdateError = (
983
+ error,
984
+ packageName,
985
+ options,
986
+ cmdStartTime,
987
+ logStream,
988
+ ) => {
989
+ const err = error instanceof Error ? error : new Error(String(error));
990
+ logger.error('Failed to update package:');
991
+ logger.error(err.message);
992
+
993
+ // 上报错误
994
+ reportError(err, {
995
+ command: 'update',
996
+ packageName,
997
+ type: 'execution_error',
998
+ });
999
+ reportCommandComplete('update', false, Date.now() - cmdStartTime, {
1000
+ args: JSON.stringify({ packageName, ...options }),
1001
+ errorCode: 1,
1002
+ errorMessage: err.message,
1003
+ });
1004
+
1005
+ // 写入错误到日志文件
1006
+ if (logStream) {
1007
+ writeLogWithTimestamp(logStream, `[ERROR] ${err.message}`);
1008
+ // 等待流关闭后再退出
1009
+ logStream.end(() => {
1010
+ flushSlardar().then(() => {
1011
+ process.exit(1);
1012
+ });
1013
+ });
1014
+ } else {
1015
+ flushSlardar().then(() => {
1016
+ process.exit(1);
1017
+ });
1018
+ }
1019
+ };
1020
+
1021
+ // start_aigc
1022
+ /**
1023
+ * 执行 update 命令的内部实现
1024
+ */
1025
+ const executeUpdate = (
1026
+ packageName,
1027
+ options
1028
+
1029
+
1030
+
1031
+
1032
+
1033
+
1034
+ ,
1035
+ ) => {
1036
+ const cmdStartTime = Date.now();
1037
+ let logStream = null;
1038
+
1039
+ try {
1040
+ const { global, cwd, version, registry, logFile, extraArgs } = options;
1041
+
1042
+ // 上报命令开始
1043
+ reportCommandStart('update', JSON.stringify({ packageName, ...options }));
1044
+
1045
+ // 准备日志
1046
+ const logFilePath = resolveLogFilePath$1(logFile);
1047
+
1048
+ // 调试:确认日志路径
1049
+ logger.info(`Log file path resolved to: ${logFilePath}`);
1050
+
1051
+ logStream = createLogStream$1(logFilePath);
1052
+
1053
+ // 调试:确认流已创建
1054
+ logger.info('Log stream created successfully');
1055
+
1056
+ logWithFile(logStream, 'info', `Updating package: ${packageName}`);
1057
+
1058
+ // 构建命令
1059
+ const command = buildPnpmCommand(packageName, {
1060
+ global,
1061
+ version,
1062
+ registry,
1063
+ extraArgs,
1064
+ });
1065
+
1066
+ // 确定工作目录
1067
+ const workingDir = cwd
1068
+ ? path.isAbsolute(cwd)
1069
+ ? cwd
1070
+ : path.join(process.cwd(), cwd)
1071
+ : process.cwd();
1072
+
1073
+ logWithFile(logStream, 'info', `Executing: ${command}`);
1074
+ logWithFile(logStream, 'info', `Working directory: ${workingDir}`);
1075
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
1076
+
1077
+ // 记录命令开始时间
1078
+ writeLogWithTimestamp(logStream, '--- Command execution started ---');
1079
+
1080
+ // 同步执行命令
1081
+ const result = shelljs.exec(command, {
1082
+ cwd: workingDir,
1083
+ silent: true, // 使用 silent 来捕获输出
1084
+ });
1085
+
1086
+ // 将输出写入控制台和日志文件(带时间戳)
1087
+ if (result.stdout) {
1088
+ process.stdout.write(result.stdout);
1089
+ writeLogWithTimestamp(logStream, result.stdout.trim());
1090
+ }
1091
+
1092
+ if (result.stderr) {
1093
+ process.stderr.write(result.stderr);
1094
+ writeLogWithTimestamp(logStream, result.stderr.trim());
1095
+ }
1096
+
1097
+ // 记录命令结束时间
1098
+ writeLogWithTimestamp(logStream, '--- Command execution ended ---');
1099
+
1100
+ // 检查执行结果并记录到日志
1101
+ if (result.code === 0) {
1102
+ logWithFile(logStream, 'success', 'Package updated successfully');
1103
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
1104
+
1105
+ // 上报命令成功
1106
+ reportCommandComplete('update', true, Date.now() - cmdStartTime, {
1107
+ args: JSON.stringify({ packageName, ...options }),
1108
+ });
1109
+ // flush 由 main 函数统一处理
1110
+ } else {
1111
+ const errorMessage = `Command exited with code ${result.code}`;
1112
+ logWithFile(logStream, 'error', errorMessage);
1113
+ logWithFile(
1114
+ logStream,
1115
+ 'error',
1116
+ `Check log file for details: ${logFilePath}`,
1117
+ );
1118
+
1119
+ // 上报命令失败
1120
+ reportError(new Error(errorMessage), {
1121
+ command: 'update',
1122
+ packageName,
1123
+ exitCode: String(result.code),
1124
+ });
1125
+ reportCommandComplete('update', false, Date.now() - cmdStartTime, {
1126
+ args: JSON.stringify({ packageName, ...options }),
1127
+ errorCode: result.code || 1,
1128
+ errorMessage,
1129
+ });
1130
+ }
1131
+
1132
+ // 关闭日志流并等待写入完成
1133
+ logStream.end(() => {
1134
+ // 流关闭后再退出进程
1135
+ if (result.code !== 0) {
1136
+ // flush 后再退出
1137
+ flushSlardar().then(() => {
1138
+ process.exit(result.code || 1);
1139
+ });
1140
+ }
1141
+ // 成功时 flush 由 main 函数统一处理
1142
+ });
1143
+ } catch (error) {
1144
+ handleUpdateError(error, packageName, options, cmdStartTime, logStream);
1145
+ }
1146
+ };
1147
+ // end_aigc
1148
+
1149
+ /**
1150
+ * 注册 update 命令到 program
1151
+ */
1152
+ const registerCommand$3 = program => {
1153
+ program
1154
+ .command('update <package>')
1155
+ .description('Update a package dependency')
1156
+ .option('-g, --global', 'Update package globally', false)
1157
+ .option('-c, --cwd <path>', 'Working directory for the update')
1158
+ .option(
1159
+ '--to <version>',
1160
+ 'Version to update to (default: latest)',
1161
+ 'latest',
1162
+ )
1163
+ .option('--registry <url>', 'Registry URL to use for the update')
1164
+ .option('--log-file <path>', 'Log file path')
1165
+ .allowUnknownOption() // 允许透传参数给 pnpm
1166
+ .action((packageName, options, command) => {
1167
+ // 收集所有未知选项作为额外参数
1168
+ const extraArgs = command.args.slice(1);
1169
+
1170
+ executeUpdate(packageName, {
1171
+ ...options,
1172
+ version: options.to, // 将 --to 映射到 version
1173
+ extraArgs,
1174
+ });
1175
+ });
1176
+ };
1177
+
598
1178
  function _optionalChain$2(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/* eslint-disable @typescript-eslint/no-explicit-any */
599
1179
  // Safe JSON parsing utilities with type safety and error handling
600
1180
  // Provides fallback values, validation, and error monitoring capabilities
@@ -727,7 +1307,7 @@ const parseConfigContent = (content) => {
727
1307
  return config ;
728
1308
  } catch (error) {
729
1309
  // TOML 解析失败,继续尝试其他格式
730
- // eslint-disable-next-line no-console
1310
+
731
1311
  console.debug('TOML parse failed:', error);
732
1312
  }
733
1313
 
@@ -739,7 +1319,7 @@ const parseConfigContent = (content) => {
739
1319
  }
740
1320
  } catch (error) {
741
1321
  // YAML 解析失败,继续尝试其他格式
742
- // eslint-disable-next-line no-console
1322
+
743
1323
  console.debug('YAML parse failed:', error);
744
1324
  }
745
1325
 
@@ -823,64 +1403,331 @@ const getCommandConfig = (
823
1403
  return commandConfig;
824
1404
  };
825
1405
 
826
- function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
1406
+ // ABOUTME: Fix rule to comment out problematic outputFileTracingRoot config in Next.js projects
1407
+ // ABOUTME: This config can cause issues in monorepo environments and should be removed
1408
+
1409
+
1410
+
827
1411
 
828
1412
  /**
829
- * 创建日志管理器
1413
+ * 检查是否为 Next.js 项目
830
1414
  */
831
- const createLogManager = (logDir = '.coze-logs') => {
832
- const ensureLogDir = () => {
833
- if (!fs.existsSync(logDir)) {
834
- fs.mkdirSync(logDir, { recursive: true });
835
- }
836
- };
1415
+ const isNextProject = (projectFolder) => {
1416
+ const packageJsonPath = path.join(projectFolder, 'package.json');
837
1417
 
838
- const getLogPath = (logFile) => path.join(logDir, logFile);
1418
+ if (!fs.existsSync(packageJsonPath)) {
1419
+ return false;
1420
+ }
839
1421
 
840
- return {
841
- createWriteStream: (logFile) => {
842
- ensureLogDir();
843
- return fs.createWriteStream(getLogPath(logFile), { flags: 'a' });
844
- },
845
- };
1422
+ try {
1423
+ // eslint-disable-next-line no-restricted-syntax
1424
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
1425
+ const deps = {
1426
+ ...packageJson.dependencies,
1427
+ ...packageJson.devDependencies,
1428
+ };
1429
+
1430
+ return 'next' in deps;
1431
+ } catch (e) {
1432
+ return false;
1433
+ }
846
1434
  };
847
1435
 
848
1436
  /**
849
- * 执行命令的内部实现
1437
+ * 查找 Next.js 配置文件
850
1438
  */
851
- const executeRun = async (
852
- commandName,
853
- options = {},
854
- ) => {
855
- try {
856
- logger.info(`Running ${commandName} command...`);
1439
+ const findNextConfigFile = (projectFolder) => {
1440
+ const possibleConfigs = [
1441
+ 'next.config.ts',
1442
+ 'next.config.js',
1443
+ 'next.config.mjs',
1444
+ ];
857
1445
 
858
- // 1. 加载 .coze 配置
859
- const config = await loadCozeConfig();
860
- const commandArgs = getCommandConfig(config, commandName);
1446
+ for (const configFile of possibleConfigs) {
1447
+ const configPath = path.join(projectFolder, configFile);
1448
+ if (fs.existsSync(configPath)) {
1449
+ return configPath;
1450
+ }
1451
+ }
861
1452
 
862
- // 2. 准备日志
863
- const logManager = createLogManager();
864
- const logFile = options.logFile || `${commandName}.log`;
865
- const logStream = logManager.createWriteStream(logFile);
1453
+ return null;
1454
+ };
866
1455
 
867
- // 3. 执行命令
868
- const commandString = commandArgs.join(' ');
1456
+ /**
1457
+ * 注释掉 outputFileTracingRoot 配置
1458
+ */
1459
+ const commentOutOutputTracingRoot = (
1460
+ configPath,
1461
+ ) => {
1462
+ let content = fs.readFileSync(configPath, 'utf-8');
1463
+ let modified = false;
1464
+ let originalLine = null;
869
1465
 
870
- logger.info(`Executing: ${commandString}`);
871
- logger.info(`Working directory: ${process.cwd()}`);
872
- logger.info(`Log file: ${logFile}`);
1466
+ // 匹配包含 outputFileTracingRoot 的行(尚未被注释的)
1467
+ // 支持 path.resolve(...) 后面有空格,然后可选逗号
1468
+ // 只匹配单行配置,不匹配跨多行的配置
1469
+ const pattern =
1470
+ /^(\s*)(outputFileTracingRoot:\s*path\.resolve\([^\n\r)]+\)\s*,?)\s*$/gm;
873
1471
 
874
- const childProcess = shelljs.exec(commandString, {
875
- async: true,
876
- silent: true, // 不自动输出,我们手动处理
877
- });
1472
+ const matches = content.match(pattern);
878
1473
 
879
- if (!childProcess) {
880
- throw new Error('Failed to create child process');
881
- }
1474
+ if (matches && matches.length > 0) {
1475
+ originalLine = matches[0].trim();
882
1476
 
883
- // 将输出同时写入控制台和日志文件
1477
+ // 在匹配的行前添加 // 注释
1478
+ content = content.replace(pattern, '$1// $2');
1479
+ modified = true;
1480
+
1481
+ fs.writeFileSync(configPath, content, 'utf-8');
1482
+ }
1483
+
1484
+ return { modified, originalLine };
1485
+ };
1486
+
1487
+ // start_aigc
1488
+ /**
1489
+ * Fix 规则:注释掉 Next.js 项目中的 outputFileTracingRoot 配置
1490
+ * 这个配置在 monorepo 环境中可能会导致问题
1491
+ */
1492
+ const fixNextOutputTracingRoot = context => {
1493
+ const ruleName = 'next-output-tracing-root';
1494
+
1495
+ // 1. 检查是否为 Next.js 项目
1496
+ if (!isNextProject(context.projectFolder)) {
1497
+ return {
1498
+ ruleName,
1499
+ applied: false,
1500
+ message: 'Not a Next.js project, skipping',
1501
+ };
1502
+ }
1503
+
1504
+ // 2. 查找 Next.js 配置文件
1505
+ const configPath = findNextConfigFile(context.projectFolder);
1506
+
1507
+ if (!configPath) {
1508
+ return {
1509
+ ruleName,
1510
+ applied: false,
1511
+ message: 'Next.js config file not found, skipping',
1512
+ };
1513
+ }
1514
+
1515
+ // 3. 注释掉 outputFileTracingRoot 配置
1516
+ const { modified, originalLine } = commentOutOutputTracingRoot(configPath);
1517
+
1518
+ if (modified && originalLine) {
1519
+ logger.success(
1520
+ `Commented out outputFileTracingRoot in ${configPath.split('/').pop()}`,
1521
+ );
1522
+ logger.info(` Original: ${originalLine}`);
1523
+
1524
+ return {
1525
+ ruleName,
1526
+ applied: true,
1527
+ message: `Successfully commented out: ${originalLine}`,
1528
+ };
1529
+ }
1530
+
1531
+ return {
1532
+ ruleName,
1533
+ applied: false,
1534
+ message: 'No outputFileTracingRoot config found, skipping',
1535
+ };
1536
+ };
1537
+ // end_aigc
1538
+
1539
+ /**
1540
+ * 所有修复规则的数组
1541
+ * 按顺序执行,新增规则直接添加到数组中
1542
+ */
1543
+ const rules = [
1544
+ // Next.js related fixes
1545
+ fixNextOutputTracingRoot,
1546
+
1547
+ // Add more rules here
1548
+ ] ;
1549
+
1550
+ // ABOUTME: Fix command for resolving legacy issues from previous project versions
1551
+ // ABOUTME: Applies a series of fix rules defined in the fix-rules directory
1552
+
1553
+
1554
+ // start_aigc
1555
+ /**
1556
+ * 执行 fix 命令的内部实现
1557
+ */
1558
+ const executeFix = async (
1559
+ options = {},
1560
+ ) => {
1561
+ try {
1562
+ const cwd = process.cwd();
1563
+ const projectFolder = options.directory
1564
+ ? path.resolve(cwd, options.directory)
1565
+ : cwd;
1566
+
1567
+ logger.info(`Running fix command on: ${projectFolder}`);
1568
+ logger.info(`Found ${rules.length} fix rule(s) to apply\n`);
1569
+
1570
+ const context = {
1571
+ cwd,
1572
+ projectFolder,
1573
+ };
1574
+
1575
+ let appliedCount = 0;
1576
+ let skippedCount = 0;
1577
+
1578
+ // 依次执行所有修复规则
1579
+ for (const rule of rules) {
1580
+ try {
1581
+ const result = await Promise.resolve(rule(context));
1582
+
1583
+ if (result.applied) {
1584
+ appliedCount++;
1585
+ logger.success(`✓ ${result.ruleName}: ${result.message}`);
1586
+ } else {
1587
+ skippedCount++;
1588
+ logger.info(`○ ${result.ruleName}: ${result.message}`);
1589
+ }
1590
+ } catch (error) {
1591
+ logger.error(
1592
+ `✗ Rule execution failed: ${error instanceof Error ? error.message : String(error)}`,
1593
+ );
1594
+ }
1595
+ }
1596
+
1597
+ // 输出汇总信息
1598
+ logger.info(
1599
+ `\nSummary: ${appliedCount} fixed, ${skippedCount} skipped, ${rules.length} total`,
1600
+ );
1601
+
1602
+ if (appliedCount > 0) {
1603
+ logger.success('\nFixes applied successfully!');
1604
+ } else {
1605
+ logger.info('\nNo fixes needed');
1606
+ }
1607
+ } catch (error) {
1608
+ logger.error('Failed to run fix command:');
1609
+ logger.error(error instanceof Error ? error.message : String(error));
1610
+ process.exit(1);
1611
+ }
1612
+ };
1613
+ // end_aigc
1614
+
1615
+ /**
1616
+ * 注册 fix 命令到 program
1617
+ */
1618
+ const registerCommand$2 = program => {
1619
+ program
1620
+ .command('fix')
1621
+ .description(
1622
+ 'Fix legacy issues from previous versions (e.g., problematic configs)',
1623
+ )
1624
+ .argument(
1625
+ '[directory]',
1626
+ 'Target directory to fix (defaults to current directory)',
1627
+ )
1628
+ .action(async (directory) => {
1629
+ await executeFix({ directory });
1630
+ });
1631
+ };
1632
+
1633
+ function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
1634
+ /**
1635
+ * 日志文件名常量
1636
+ */
1637
+ const LOG_FILE_NAME = 'dev.log';
1638
+
1639
+ /**
1640
+ * 获取日志目录
1641
+ * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
1642
+ */
1643
+ const getLogDir = () =>
1644
+ process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
1645
+
1646
+ /**
1647
+ * 解析日志文件路径
1648
+ * - 如果是绝对路径,直接使用
1649
+ * - 如果是相对路径,基于 getLogDir() + 相对路径
1650
+ * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
1651
+ */
1652
+ const resolveLogFilePath = (logFile) => {
1653
+ if (!logFile) {
1654
+ return path.join(getLogDir(), LOG_FILE_NAME);
1655
+ }
1656
+
1657
+ if (path.isAbsolute(logFile)) {
1658
+ return logFile;
1659
+ }
1660
+
1661
+ return path.join(getLogDir(), logFile);
1662
+ };
1663
+
1664
+ /**
1665
+ * 创建日志写入流
1666
+ */
1667
+ const createLogStream = (logFilePath) => {
1668
+ const logDir = path.dirname(logFilePath);
1669
+
1670
+ // 确保日志目录存在
1671
+ if (!fs.existsSync(logDir)) {
1672
+ fs.mkdirSync(logDir, { recursive: true });
1673
+ }
1674
+
1675
+ // 使用 'w' 标志覆盖之前的日志
1676
+ return fs.createWriteStream(logFilePath, { flags: 'w' });
1677
+ };
1678
+
1679
+ /**
1680
+ * 执行命令的内部实现
1681
+ */
1682
+ const executeRun = async (
1683
+ commandName,
1684
+ options = {},
1685
+ ) => {
1686
+ const cmdStartTime = Date.now();
1687
+
1688
+ try {
1689
+ logger.info(`Running ${commandName} command...`);
1690
+
1691
+ // 上报命令开始
1692
+ reportCommandStart(commandName, JSON.stringify(options));
1693
+
1694
+ // 1. 对于 build 命令,先执行 fix 以确保项目配置正确
1695
+ if (['dev', 'build'].includes(commandName)) {
1696
+ logger.info('\n🔧 Running fix command before build...\n');
1697
+ try {
1698
+ await executeFix();
1699
+ // eslint-disable-next-line @coze-arch/no-empty-catch
1700
+ } catch (e) {
1701
+ // just ignore
1702
+ }
1703
+ logger.info('');
1704
+ }
1705
+
1706
+ // 2. 加载 .coze 配置
1707
+ const config = await loadCozeConfig();
1708
+ const commandArgs = getCommandConfig(config, commandName);
1709
+
1710
+ // 3. 准备日志
1711
+ const logFilePath = resolveLogFilePath(options.logFile);
1712
+ const logStream = createLogStream(logFilePath);
1713
+
1714
+ // 4. 执行命令
1715
+ const commandString = commandArgs.join(' ');
1716
+
1717
+ logger.info(`Executing: ${commandString}`);
1718
+ logger.info(`Working directory: ${process.cwd()}`);
1719
+ logger.info(`Log file: ${logFilePath}`);
1720
+
1721
+ const childProcess = shelljs.exec(commandString, {
1722
+ async: true,
1723
+ silent: true, // 不自动输出,我们手动处理
1724
+ });
1725
+
1726
+ if (!childProcess) {
1727
+ throw new Error('Failed to create child process');
1728
+ }
1729
+
1730
+ // 将输出同时写入控制台和日志文件
884
1731
  _optionalChain([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
885
1732
  process.stdout.write(data);
886
1733
  logStream.write(data);
@@ -895,14 +1742,34 @@ const executeRun = async (
895
1742
  logStream.end();
896
1743
 
897
1744
  if (code !== 0) {
898
- logger.error(
899
- `Command exited with code ${_nullishCoalesce$1(code, () => ( 'unknown'))}${signal ? ` and signal ${signal}` : ''}`,
900
- );
901
- logger.error(`Check log file for details: ${logFile}`);
902
- process.exit(code || 1);
1745
+ const errorMessage = `Command exited with code ${_nullishCoalesce$1(code, () => ( 'unknown'))}${signal ? ` and signal ${signal}` : ''}`;
1746
+ logger.error(errorMessage);
1747
+ logger.error(`Check log file for details: ${logFilePath}`);
1748
+
1749
+ // 上报命令失败
1750
+ reportError(new Error(errorMessage), {
1751
+ command: commandName,
1752
+ exitCode: String(_nullishCoalesce$1(code, () => ( 'unknown'))),
1753
+ signal: _nullishCoalesce$1(signal, () => ( 'none')),
1754
+ logFile: logFilePath,
1755
+ });
1756
+ reportCommandComplete(commandName, false, Date.now() - cmdStartTime, {
1757
+ args: JSON.stringify(options),
1758
+ errorCode: _nullishCoalesce$1(code, () => ( 1)),
1759
+ errorMessage,
1760
+ });
1761
+ flushSlardar().then(() => {
1762
+ process.exit(code || 1);
1763
+ });
903
1764
  } else {
904
1765
  logger.success('Command completed successfully');
905
- logger.info(`Log file: ${logFile}`);
1766
+ logger.info(`Log file: ${logFilePath}`);
1767
+
1768
+ // 上报命令成功
1769
+ reportCommandComplete(commandName, true, Date.now() - cmdStartTime, {
1770
+ args: JSON.stringify(options),
1771
+ });
1772
+ // flush 由 main 函数统一处理
906
1773
  }
907
1774
  });
908
1775
 
@@ -913,12 +1780,39 @@ const executeRun = async (
913
1780
  logger.error(`Stack trace:\n${error.stack}`);
914
1781
  }
915
1782
  logStream.end();
916
- process.exit(1);
1783
+
1784
+ // 上报错误
1785
+ reportError(error, {
1786
+ command: commandName,
1787
+ type: 'child_process_error',
1788
+ });
1789
+ reportCommandComplete(commandName, false, Date.now() - cmdStartTime, {
1790
+ args: JSON.stringify(options),
1791
+ errorCode: 1,
1792
+ errorMessage: error.message,
1793
+ });
1794
+ flushSlardar().then(() => {
1795
+ process.exit(1);
1796
+ });
917
1797
  });
918
1798
  } catch (error) {
1799
+ const err = error instanceof Error ? error : new Error(String(error));
919
1800
  logger.error(`Failed to run ${commandName} command:`);
920
- logger.error(error instanceof Error ? error.message : String(error));
921
- process.exit(1);
1801
+ logger.error(err.message);
1802
+
1803
+ // 上报错误
1804
+ reportError(err, {
1805
+ command: commandName,
1806
+ type: 'execution_error',
1807
+ });
1808
+ reportCommandComplete(commandName, false, Date.now() - cmdStartTime, {
1809
+ args: JSON.stringify(options),
1810
+ errorCode: 1,
1811
+ errorMessage: err.message,
1812
+ });
1813
+ flushSlardar().then(() => {
1814
+ process.exit(1);
1815
+ });
922
1816
  }
923
1817
  };
924
1818
 
@@ -1177,6 +2071,11 @@ const shouldIgnoreFile = (filePath) => {
1177
2071
  return directoryPatterns.some(dir => pathParts.includes(dir));
1178
2072
  };
1179
2073
 
2074
+ // ABOUTME: File system utilities for template file processing
2075
+ // ABOUTME: Provides directory scanning, file path conversion, and node_modules copying
2076
+
2077
+
2078
+ // start_aigc
1180
2079
  /**
1181
2080
  * 递归获取目录中的所有文件
1182
2081
  *
@@ -1246,166 +2145,402 @@ const convertDotfileName = (filePath) => {
1246
2145
 
1247
2146
  return filePath;
1248
2147
  };
2148
+ // end_aigc
2149
+
2150
+ // ABOUTME: File rendering utilities for template processing
2151
+ // ABOUTME: Handles file content rendering, hook execution, and file writing
1249
2152
 
2153
+
2154
+ // start_aigc
1250
2155
  /**
1251
- * 复制并处理模板文件到目标目录
2156
+ * 执行文件渲染钩子
1252
2157
  *
1253
- * @param templatePath - 模板目录路径
1254
- * @param outputPath - 输出目录路径
2158
+ * @param templateConfig - 模板配置
2159
+ * @param fileInfo - 文件渲染信息
1255
2160
  * @param context - 模板上下文
2161
+ * @returns 处理后的文件信息,或 null 表示跳过该文件
1256
2162
  */
1257
- const processTemplateFiles = async (
1258
- templatePath,
1259
- outputPath,
2163
+ const executeFileRenderHook = async (
2164
+ templateConfig,
2165
+ fileInfo,
1260
2166
  context,
1261
2167
  ) => {
1262
- logger.verbose('Processing template files:');
1263
- logger.verbose(` - Template path: ${templatePath}`);
1264
- logger.verbose(` - Output path: ${outputPath}`);
1265
-
1266
- // 验证模板目录是否存在
1267
- try {
1268
- const stat = await fs$1.stat(templatePath);
1269
- logger.verbose(
1270
- ` - Template path exists: ${stat.isDirectory() ? 'directory' : 'file'}`,
1271
- );
1272
- if (!stat.isDirectory()) {
1273
- throw new Error(`Template path is not a directory: ${templatePath}`);
1274
- }
1275
- } catch (error) {
1276
- logger.error(
1277
- ` - Failed to access template path: ${error instanceof Error ? error.message : String(error)}`,
1278
- );
1279
- throw error;
2168
+ if (!templateConfig.onFileRender) {
2169
+ return fileInfo;
1280
2170
  }
1281
2171
 
1282
- const files = await getAllFiles(templatePath);
2172
+ const result = await templateConfig.onFileRender(
2173
+ fileInfo,
2174
+ context,
2175
+ );
1283
2176
 
1284
- logger.verbose(` - Found ${files.length} files to process`);
2177
+ // false: 跳过文件
2178
+ if (result === false) {
2179
+ return null;
2180
+ }
1285
2181
 
1286
- if (files.length === 0) {
1287
- logger.warn(' - No files found in template directory!');
1288
- return;
2182
+ // undefined/void: 使用默认内容
2183
+ if (result === undefined || result === null) {
2184
+ return fileInfo;
1289
2185
  }
1290
2186
 
1291
- await Promise.all(
1292
- files.map(async file => {
1293
- const srcPath = path.join(templatePath, file);
1294
- const destFile = convertDotfileName(file);
1295
- const destPath = path.join(outputPath, destFile);
2187
+ // string: 作为 content,其他不变
2188
+ if (typeof result === 'string') {
2189
+ return {
2190
+ ...fileInfo,
2191
+ content: result,
2192
+ };
2193
+ }
1296
2194
 
1297
- logger.verbose(
1298
- ` - Processing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1299
- );
2195
+ // FileRenderInfo: 使用新对象的信息
2196
+ return result;
2197
+ };
2198
+
2199
+ /**
2200
+ * 准备单个文件的渲染信息(不实际写入)
2201
+ * 用于 dry-run 阶段,收集所有将要写入的文件信息
2202
+ *
2203
+ * @param options - 准备选项
2204
+ * @returns 文件渲染信息,或 null 表示该文件被跳过
2205
+ */
2206
+ const prepareFileInfo = async (options
1300
2207
 
1301
- // 确保目标目录存在
1302
- await ensureDir(path.dirname(destPath));
1303
2208
 
1304
- if (shouldRenderFile(srcPath)) {
1305
- // 渲染文本文件
1306
- const rendered = await renderTemplate(srcPath, context);
1307
- await fs$1.writeFile(destPath, rendered, 'utf-8');
1308
- logger.verbose(' ✓ Rendered and written');
1309
- } else {
1310
- // 直接复制二进制文件
1311
- await fs$1.copyFile(srcPath, destPath);
1312
- logger.verbose(' ✓ Copied');
1313
- }
1314
- }),
2209
+
2210
+
2211
+ ) => {
2212
+ const { file, templatePath, context, templateConfig } = options;
2213
+
2214
+ const srcPath = path.join(templatePath, file);
2215
+ const destFile = convertDotfileName(file);
2216
+
2217
+ logger.verbose(
2218
+ ` - Preparing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1315
2219
  );
1316
2220
 
1317
- logger.verbose('✓ All files processed successfully');
2221
+ // 判断是否为二进制文件
2222
+ const isBinary = !shouldRenderFile(srcPath);
2223
+ let content;
2224
+ let wasRendered = false;
1318
2225
 
1319
- // 单独处理 node_modules 目录(如果存在)
1320
- const sourceNodeModules = path.join(templatePath, 'node_modules');
1321
- const targetNodeModules = path.join(outputPath, 'node_modules');
2226
+ if (isBinary) {
2227
+ // 二进制文件,读取为 buffer 然后转为 base64
2228
+ const buffer = await fs$1.readFile(srcPath);
2229
+ content = buffer.toString('base64');
2230
+ } else {
2231
+ // 文本文件,渲染后的内容
2232
+ content = await renderTemplate(srcPath, context);
2233
+ wasRendered = true;
2234
+ }
1322
2235
 
1323
- if (fs.existsSync(sourceNodeModules)) {
1324
- logger.info('\nCopying node_modules from pre-warmed template...');
1325
- logger.verbose(` From: ${sourceNodeModules}`);
1326
- logger.verbose(` To: ${targetNodeModules}`);
2236
+ // 构造文件信息对象
2237
+ const fileInfo = {
2238
+ path: file,
2239
+ destPath: destFile,
2240
+ content,
2241
+ isBinary,
2242
+ wasRendered,
2243
+ };
1327
2244
 
1328
- const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1329
- silent: true,
1330
- });
2245
+ // 执行文件渲染钩子
2246
+ const processedFileInfo = await executeFileRenderHook(
2247
+ templateConfig,
2248
+ fileInfo,
2249
+ context,
2250
+ );
1331
2251
 
1332
- if (result.stdout) {
1333
- process.stdout.write(result.stdout);
1334
- }
2252
+ // 如果返回 null,表示该文件被 hook 跳过
2253
+ if (processedFileInfo === null) {
2254
+ logger.verbose(' ⊘ Skipped by onFileRender hook');
2255
+ return null;
2256
+ }
1335
2257
 
1336
- if (result.stderr) {
1337
- process.stderr.write(result.stderr);
1338
- }
2258
+ return processedFileInfo;
2259
+ };
1339
2260
 
1340
- if (result.code === 0) {
1341
- logger.success('✓ node_modules copied successfully');
2261
+ /**
2262
+ * 写入渲染后的文件到目标路径
2263
+ *
2264
+ * @param options - 写入选项
2265
+ */
2266
+ const writeRenderedFile = async (options
2267
+
2268
+
2269
+
2270
+ ) => {
2271
+ const { fileInfo, srcPath, destPath } = options;
2272
+
2273
+ logger.verbose(` - Writing: ${fileInfo.destPath}`);
2274
+
2275
+ // 确保目标目录存在
2276
+ await ensureDir(path.dirname(destPath));
2277
+
2278
+ // 写入文件
2279
+ if (fileInfo.isBinary) {
2280
+ // 二进制文件:如果内容是原始 base64(未被 hook 修改),直接复制;否则从 base64 解码写入
2281
+ const buffer = await fs$1.readFile(srcPath);
2282
+ const originalContent = buffer.toString('base64');
2283
+
2284
+ if (fileInfo.content === originalContent) {
2285
+ await fs$1.copyFile(srcPath, destPath);
2286
+ logger.verbose(' ✓ Copied (binary)');
1342
2287
  } else {
1343
- logger.warn(
1344
- `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1345
- );
1346
- logger.info('Will need to run pnpm install manually');
2288
+ const modifiedBuffer = Buffer.from(fileInfo.content, 'base64');
2289
+ await fs$1.writeFile(destPath, modifiedBuffer);
2290
+ logger.verbose(' ✓ Written (binary, modified by hook)');
1347
2291
  }
2292
+ } else {
2293
+ // 文本文件
2294
+ await fs$1.writeFile(destPath, fileInfo.content, 'utf-8');
2295
+ logger.verbose(' ✓ Rendered and written');
1348
2296
  }
1349
2297
  };
2298
+ // end_aigc
2299
+
2300
+ // ABOUTME: File conflict detection utilities for template processing
2301
+ // ABOUTME: Provides dry-run file collection and conflict checking
1350
2302
 
2303
+
2304
+ // start_aigc
1351
2305
  /**
1352
- * 检查输出目录是否为空
1353
- * 注意:.git 目录会被忽略,允许在已初始化 git 的目录中创建项目
2306
+ * 收集所有将要写入的文件路径(dry-run 阶段)
1354
2307
  *
1355
- * @param outputPath - 输出目录路径
1356
- * @returns 是否为空
2308
+ * @param options - 收集选项
2309
+ * @returns 将要写入的文件路径列表
1357
2310
  */
1358
- const isOutputDirEmpty = async (
1359
- outputPath,
2311
+ const collectFilesToRender = async (options
2312
+
2313
+
2314
+
2315
+
1360
2316
  ) => {
1361
- try {
1362
- const entries = await fs$1.readdir(outputPath);
1363
- // 过滤掉 .git 目录,允许在已初始化 git 的目录中创建项目
1364
- const filteredEntries = entries.filter(entry => entry !== '.git');
1365
- return filteredEntries.length === 0;
1366
- } catch (e) {
1367
- // 目录不存在,视为空
1368
- return true;
1369
- }
2317
+ const { files, templatePath, context, templateConfig } = options;
2318
+
2319
+ logger.verbose('\nDry-run: Collecting files to render...');
2320
+
2321
+ const fileInfos = await Promise.all(
2322
+ files.map(file =>
2323
+ prepareFileInfo({
2324
+ file,
2325
+ templatePath,
2326
+ context,
2327
+ templateConfig,
2328
+ }),
2329
+ ),
2330
+ );
2331
+
2332
+ // 过滤掉被 hook 跳过的文件,收集 destPath
2333
+ const filesToWrite = fileInfos
2334
+ .filter((info) => info !== null)
2335
+ .map(info => info.destPath);
2336
+
2337
+ logger.verbose(` - ${filesToWrite.length} files will be written`);
2338
+ logger.verbose(
2339
+ ` - ${fileInfos.length - filesToWrite.length} files skipped by hooks`,
2340
+ );
2341
+
2342
+ return filesToWrite;
1370
2343
  };
1371
2344
 
1372
2345
  /**
1373
- * 验证输出目录
2346
+ * 检测文件冲突
1374
2347
  *
1375
2348
  * @param outputPath - 输出目录路径
1376
- * @throws 如果目录不为空则抛出错误
2349
+ * @param filesToWrite - 将要写入的文件路径列表
2350
+ * @returns 冲突的文件路径列表
1377
2351
  */
1378
- const validateOutputDir = async (outputPath) => {
1379
- const isEmpty = await isOutputDirEmpty(outputPath);
1380
- if (!isEmpty) {
1381
- throw new Error(
1382
- `Output directory is not empty: ${outputPath}\n` +
1383
- 'Please use an empty directory or remove existing files.',
1384
- );
2352
+ const detectFileConflicts = (
2353
+ outputPath,
2354
+ filesToWrite,
2355
+ ) => {
2356
+ logger.verbose('\nChecking for file conflicts...');
2357
+
2358
+ const conflicts = [];
2359
+
2360
+ for (const file of filesToWrite) {
2361
+ const fullPath = path.join(outputPath, file);
2362
+ if (fs.existsSync(fullPath)) {
2363
+ conflicts.push(file);
2364
+ logger.verbose(` ⚠ Conflict detected: ${file}`);
2365
+ }
2366
+ }
2367
+
2368
+ if (conflicts.length === 0) {
2369
+ logger.verbose(' ✓ No conflicts detected');
2370
+ } else {
2371
+ logger.verbose(` ⚠ ${conflicts.length} conflicts detected`);
1385
2372
  }
2373
+
2374
+ return conflicts;
1386
2375
  };
2376
+ // end_aigc
2377
+
2378
+ // ABOUTME: Main file processing orchestration for template rendering
2379
+ // ABOUTME: Coordinates file system, rendering, and conflict detection layers
1387
2380
 
2381
+
2382
+ // start_aigc
1388
2383
  /**
1389
- * 模板引擎执行选项
2384
+ * 处理单个文件(准备 + 写入)
2385
+ *
2386
+ * @param options - 处理选项
1390
2387
  */
2388
+ const processSingleFile = async (options
1391
2389
 
1392
2390
 
1393
2391
 
1394
2392
 
1395
2393
 
2394
+ ) => {
2395
+ const { file, templatePath, outputPath, context, templateConfig } = options;
1396
2396
 
1397
- /**
1398
- * 加载模板元数据和路径
1399
- */
1400
- const loadTemplateMetadata = async (templateName) => {
1401
- const templatesConfigPath = getTemplatesConfigPath();
1402
- const templatesConfig = await loadTemplatesConfig(templatesConfigPath);
1403
- const templateMetadata = findTemplate(templatesConfig, templateName);
1404
- const templatesDir = getTemplatesDir();
1405
- const templatePath = await getTemplatePath(templatesDir, templateMetadata);
2397
+ const srcPath = path.join(templatePath, file);
1406
2398
 
1407
- return { templatePath, templateMetadata };
1408
- };
2399
+ // 准备文件信息
2400
+ const processedFileInfo = await prepareFileInfo({
2401
+ file,
2402
+ templatePath,
2403
+ context,
2404
+ templateConfig,
2405
+ });
2406
+
2407
+ // 如果返回 null,跳过该文件
2408
+ if (processedFileInfo === null) {
2409
+ return;
2410
+ }
2411
+
2412
+ // 使用处理后的目标路径
2413
+ const finalDestPath = path.join(outputPath, processedFileInfo.destPath);
2414
+
2415
+ // 写入文件
2416
+ await writeRenderedFile({
2417
+ fileInfo: processedFileInfo,
2418
+ srcPath,
2419
+ destPath: finalDestPath,
2420
+ });
2421
+ };
2422
+
2423
+ /**
2424
+ * 复制并处理模板文件到目标目录
2425
+ *
2426
+ * 流程:
2427
+ * 1. 验证模板目录
2428
+ * 2. 扫描所有模板文件
2429
+ * 3. Dry-run:收集将要写入的文件列表(考虑 hooks 影响)
2430
+ * 4. 冲突检测:检查是否有文件会被覆盖(可通过 force 跳过)
2431
+ * 5. 实际写入:渲染并写入所有文件
2432
+ * 6. 复制 node_modules(如果存在)
2433
+ *
2434
+ * @param options - 处理选项
2435
+ */
2436
+ const processTemplateFiles = async (options
2437
+
2438
+
2439
+
2440
+
2441
+
2442
+ ) => {
2443
+ const { templatePath, outputPath, context, templateConfig, force } = options;
2444
+ logger.verbose('Processing template files:');
2445
+ logger.verbose(` - Template path: ${templatePath}`);
2446
+ logger.verbose(` - Output path: ${outputPath}`);
2447
+
2448
+ // 阶段 0: 验证模板目录是否存在
2449
+ try {
2450
+ const stat = await fs$1.stat(templatePath);
2451
+ logger.verbose(
2452
+ ` - Template path exists: ${stat.isDirectory() ? 'directory' : 'file'}`,
2453
+ );
2454
+ if (!stat.isDirectory()) {
2455
+ throw new Error(`Template path is not a directory: ${templatePath}`);
2456
+ }
2457
+ } catch (error) {
2458
+ logger.error(
2459
+ ` - Failed to access template path: ${error instanceof Error ? error.message : String(error)}`,
2460
+ );
2461
+ throw error;
2462
+ }
2463
+
2464
+ // 阶段 1: 扫描所有模板文件
2465
+ const files = await getAllFiles(templatePath);
2466
+
2467
+ logger.verbose(` - Found ${files.length} files to process`);
2468
+
2469
+ if (files.length === 0) {
2470
+ logger.warn(' - No files found in template directory!');
2471
+ return;
2472
+ }
2473
+
2474
+ // 阶段 2: Dry-run - 收集所有将要写入的文件
2475
+ const filesToWrite = await collectFilesToRender({
2476
+ files,
2477
+ templatePath,
2478
+ context,
2479
+ templateConfig,
2480
+ });
2481
+
2482
+ // 阶段 3: 冲突检测(force 为 true 时跳过)
2483
+ if (!force) {
2484
+ const conflicts = detectFileConflicts(outputPath, filesToWrite);
2485
+
2486
+ if (conflicts.length > 0) {
2487
+ // 有冲突,抛出详细的错误信息
2488
+ const conflictList = conflicts.map(f => ` - ${f}`).join('\n');
2489
+ throw new Error(
2490
+ `File conflicts detected in output directory: ${outputPath}\n\n` +
2491
+ `The following files already exist and would be overwritten:\n${conflictList}\n\n` +
2492
+ 'Please remove these files or use a different output directory.\n' +
2493
+ 'Or use --force to overwrite existing files.',
2494
+ );
2495
+ }
2496
+ } else {
2497
+ logger.verbose(
2498
+ ' - Force mode enabled, skipping conflict detection. Existing files will be overwritten.',
2499
+ );
2500
+ }
2501
+
2502
+ // 阶段 4: 实际写入文件
2503
+ logger.verbose('\nWriting files...');
2504
+ await Promise.all(
2505
+ files.map(file =>
2506
+ processSingleFile({
2507
+ file,
2508
+ templatePath,
2509
+ outputPath,
2510
+ context,
2511
+ templateConfig,
2512
+ }),
2513
+ ),
2514
+ );
2515
+
2516
+ logger.verbose('✓ All files processed successfully');
2517
+
2518
+ // node_modules 将由 pnpm install 处理(利用缓存和硬链接机制)
2519
+ };
2520
+ // end_aigc
2521
+
2522
+ /**
2523
+ * 模板引擎执行选项
2524
+ */
2525
+
2526
+
2527
+
2528
+
2529
+
2530
+
2531
+
2532
+ /**
2533
+ * 加载模板元数据和路径
2534
+ */
2535
+ const loadTemplateMetadata = async (templateName) => {
2536
+ const templatesConfigPath = getTemplatesConfigPath();
2537
+ const templatesConfig = await loadTemplatesConfig(templatesConfigPath);
2538
+ const templateMetadata = findTemplate(templatesConfig, templateName);
2539
+ const templatesDir = getTemplatesDir();
2540
+ const templatePath = await getTemplatePath(templatesDir, templateMetadata);
2541
+
2542
+ return { templatePath, templateMetadata };
2543
+ };
1409
2544
 
1410
2545
  /**
1411
2546
  * 解析并验证模板参数
@@ -1463,22 +2598,47 @@ const executeAfterRenderHook = async (
1463
2598
  }
1464
2599
  };
1465
2600
 
2601
+ /**
2602
+ * 执行完成钩子
2603
+ */
2604
+ const executeCompleteHook = async (
2605
+ templateConfig,
2606
+ context,
2607
+ outputPath,
2608
+ ) => {
2609
+ if (templateConfig.onComplete) {
2610
+ await templateConfig.onComplete(context, outputPath);
2611
+ }
2612
+ };
2613
+
1466
2614
  /**
1467
2615
  * 准备输出目录
1468
2616
  */
1469
- const prepareOutputDirectory = async (outputPath) => {
2617
+ const prepareOutputDirectory = (outputPath) => {
1470
2618
  const absolutePath = path.resolve(process.cwd(), outputPath);
1471
- await validateOutputDir(absolutePath);
2619
+ // 不再在这里验证目录是否为空,冲突检测已移至 processTemplateFiles 中
1472
2620
  return absolutePath;
1473
2621
  };
1474
2622
 
2623
+ /**
2624
+ * 模板引擎执行结果
2625
+ */
2626
+
2627
+
2628
+
2629
+
2630
+
2631
+
2632
+
2633
+
2634
+
1475
2635
  /**
1476
2636
  * 执行完整的模板渲染流程
1477
2637
  */
1478
2638
  const execute = async (
1479
2639
  options,
1480
2640
  ) => {
1481
- const { templateName, outputPath, command } = options;
2641
+ const { templateName, outputPath, command, force } = options;
1482
2642
 
1483
2643
  // 1. 加载模板
1484
2644
  const { templatePath } = await loadTemplateMetadata(templateName);
@@ -1495,15 +2655,25 @@ const execute = async (
1495
2655
  });
1496
2656
 
1497
2657
  // 5. 准备输出目录
1498
- const absoluteOutputPath = await prepareOutputDirectory(outputPath);
2658
+ const absoluteOutputPath = prepareOutputDirectory(outputPath);
1499
2659
 
1500
2660
  // 6. 处理模板文件
1501
- await processTemplateFiles(templatePath, absoluteOutputPath, context);
2661
+ await processTemplateFiles({
2662
+ templatePath,
2663
+ outputPath: absoluteOutputPath,
2664
+ context,
2665
+ templateConfig,
2666
+ force,
2667
+ });
1502
2668
 
1503
2669
  // 7. 执行 onAfterRender 钩子
1504
2670
  await executeAfterRenderHook(templateConfig, context, absoluteOutputPath);
1505
2671
 
1506
- return absoluteOutputPath;
2672
+ return {
2673
+ outputPath: absoluteOutputPath,
2674
+ templateConfig,
2675
+ context,
2676
+ };
1507
2677
  };
1508
2678
 
1509
2679
  function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
@@ -1545,12 +2715,46 @@ const runPnpmInstall = (projectPath) => {
1545
2715
  };
1546
2716
 
1547
2717
  /**
1548
- * 初始化 git 仓库并创建初始提交
2718
+ * 运行 git 命令的辅助函数
2719
+ */
2720
+ const runGitCommand = (command, projectPath) => {
2721
+ logger.info(`Executing: ${command}`);
2722
+
2723
+ const result = shelljs.exec(command, {
2724
+ cwd: projectPath,
2725
+ silent: true,
2726
+ });
2727
+
2728
+ // 输出命令的结果
2729
+ if (result.stdout) {
2730
+ process.stdout.write(result.stdout);
2731
+ }
2732
+
2733
+ if (result.stderr) {
2734
+ process.stderr.write(result.stderr);
2735
+ }
2736
+
2737
+ if (result.code !== 0) {
2738
+ const errorMessage = [
2739
+ `${command} failed with exit code ${result.code}`,
2740
+ result.stderr ? `\nStderr:\n${result.stderr}` : '',
2741
+ result.stdout ? `\nStdout:\n${result.stdout}` : '',
2742
+ ]
2743
+ .filter(Boolean)
2744
+ .join('');
2745
+
2746
+ throw new Error(errorMessage);
2747
+ }
2748
+ };
2749
+
2750
+ /**
2751
+ * 初始化 git 仓库
1549
2752
  * 如果目录中已存在 .git,则跳过初始化
1550
2753
  */
1551
2754
  const runGitInit = (projectPath) => {
1552
2755
  // 检查是否已存在 .git 目录
1553
2756
  const gitDir = path.join(projectPath, '.git');
2757
+
1554
2758
  if (fs.existsSync(gitDir)) {
1555
2759
  logger.info(
1556
2760
  '\n💡 Git repository already exists, skipping git initialization',
@@ -1558,80 +2762,267 @@ const runGitInit = (projectPath) => {
1558
2762
  return;
1559
2763
  }
1560
2764
 
1561
- const runGitCommand = (command) => {
1562
- logger.info(`Executing: ${command}`);
2765
+ try {
2766
+ logger.info('\nInitializing git repository...');
2767
+ runGitCommand('git init', projectPath);
2768
+ logger.success('Git repository initialized successfully!');
2769
+ } catch (error) {
2770
+ // Git 初始化失败不应该导致整个流程失败
2771
+ const errorMessage = error instanceof Error ? error.message : String(error);
2772
+ logger.warn(`Git initialization failed: ${errorMessage}`);
2773
+ logger.info('You can manually initialize git later with: git init');
2774
+ }
2775
+ };
2776
+
2777
+ /**
2778
+ * 提交初始化生成的所有文件
2779
+ */
2780
+ const commitChanges = (projectPath) => {
2781
+ // 检查是否存在 .git 目录
2782
+ const gitDir = path.join(projectPath, '.git');
2783
+ if (!fs.existsSync(gitDir)) {
2784
+ logger.warn(
2785
+ '\n⚠️ Git repository does not exist, skipping commit. Run git init first.',
2786
+ );
2787
+ return;
2788
+ }
2789
+
2790
+ try {
2791
+ logger.info('\nCommitting initialized files...');
2792
+ runGitCommand('git add --all', projectPath);
2793
+ runGitCommand('git commit -m "chore: init env"', projectPath);
2794
+ logger.success('Changes committed successfully!');
2795
+ } catch (error) {
2796
+ // Commit 失败不应该导致整个流程失败
2797
+ const errorMessage = error instanceof Error ? error.message : String(error);
2798
+ logger.warn(`Git commit failed: ${errorMessage}`);
2799
+ logger.info(
2800
+ 'You can manually commit later with: git add --all && git commit -m "chore: init env"',
2801
+ );
2802
+ }
2803
+ };
1563
2804
 
1564
- const result = shelljs.exec(command, {
2805
+ /**
2806
+ * 运行开发服务器(后台模式)
2807
+ * 启动后台子进程运行开发服务器,父进程可以直接退出
2808
+ * 使用 CLI 自己的 dev 命令(定义在 run.ts)而不是直接运行 npm run dev
2809
+ */
2810
+ const runDev = (projectPath) => {
2811
+ logger.info('\nStarting development server in background...');
2812
+
2813
+ // 获取当前 CLI 的可执行文件路径
2814
+ // process.argv[0] 是 node,process.argv[1] 是 CLI 入口文件
2815
+ const cliPath = process.argv[1];
2816
+
2817
+ logger.info(`Executing: ${cliPath} dev in ${projectPath}`);
2818
+
2819
+ try {
2820
+ // 使用通用的后台执行函数启动开发服务器
2821
+ // 调用 CLI 自己的 dev 命令
2822
+ const pid = spawnDetached(process.argv[0], [cliPath, 'dev'], {
1565
2823
  cwd: projectPath,
1566
- silent: true,
2824
+ verbose: false, // 不输出额外的进程信息,由 logger 统一处理
1567
2825
  });
1568
2826
 
1569
- // 输出命令的结果
1570
- if (result.stdout) {
1571
- process.stdout.write(result.stdout);
2827
+ if (pid) {
2828
+ logger.success('Development server started in background!');
2829
+ logger.info(`Process ID: ${pid}`);
2830
+ logger.info(
2831
+ '\nThe dev server is running independently. You can close this terminal.',
2832
+ );
2833
+ logger.info(`To stop the server later, use: kill ${pid}`);
2834
+ } else {
2835
+ logger.warn('Failed to get process ID for dev server');
1572
2836
  }
2837
+ } catch (error) {
2838
+ const errorMessage = error instanceof Error ? error.message : String(error);
2839
+ logger.error(`Failed to start dev server: ${errorMessage}`);
2840
+ }
2841
+ };
2842
+
2843
+ /**
2844
+ * Init 命令执行上下文
2845
+ */
2846
+
2847
+
2848
+
2849
+
2850
+
2851
+
2852
+
2853
+
2854
+
2855
+
2856
+
2857
+
2858
+
2859
+
2860
+
2861
+
2862
+
2863
+
2864
+
2865
+
2866
+
1573
2867
 
1574
- if (result.stderr) {
1575
- process.stderr.write(result.stderr);
1576
- }
1577
2868
 
1578
- if (result.code !== 0) {
1579
- const errorMessage = [
1580
- `${command} failed with exit code ${result.code}`,
1581
- result.stderr ? `\nStderr:\n${result.stderr}` : '',
1582
- result.stdout ? `\nStdout:\n${result.stdout}` : '',
1583
- ]
1584
- .filter(Boolean)
1585
- .join('');
1586
2869
 
1587
- throw new Error(errorMessage);
2870
+
2871
+
2872
+
2873
+
2874
+
2875
+
2876
+
2877
+ /**
2878
+ * 步骤1: 执行模板引擎
2879
+ */
2880
+ const executeTemplateEngineStep = async ctx => {
2881
+ logger.info(`Initializing project with template: ${ctx.templateName}`);
2882
+ ctx.timer.logPhase('Initialization');
2883
+
2884
+ const result = await execute({
2885
+ templateName: ctx.templateName,
2886
+ outputPath: ctx.outputPath,
2887
+ command: ctx.command,
2888
+ force: _nullishCoalesce(ctx.options.force, () => ( false)),
2889
+ });
2890
+
2891
+ // 保存结果到上下文
2892
+ ctx.absoluteOutputPath = result.outputPath;
2893
+ ctx.templateConfig = result.templateConfig;
2894
+ ctx.context = result.context;
2895
+
2896
+ ctx.timer.logPhase('Template engine execution');
2897
+ logger.success('Project created successfully!');
2898
+ };
2899
+
2900
+ /**
2901
+ * 步骤2: 检查 package.json 是否存在
2902
+ */
2903
+ const checkPackageJsonStep = ctx => {
2904
+ if (!ctx.absoluteOutputPath) {
2905
+ return;
2906
+ }
2907
+
2908
+ const packageJsonPath = path.join(ctx.absoluteOutputPath, 'package.json');
2909
+ ctx.hasPackageJson = fs.existsSync(packageJsonPath);
2910
+ };
2911
+
2912
+ /**
2913
+ * 步骤3: 安装依赖
2914
+ */
2915
+ const installDependenciesStep = ctx => {
2916
+ if (!ctx.absoluteOutputPath) {
2917
+ return;
2918
+ }
2919
+
2920
+ const { skipInstall } = ctx.options;
2921
+
2922
+ if (!skipInstall) {
2923
+ if (ctx.hasPackageJson) {
2924
+ runPnpmInstall(ctx.absoluteOutputPath);
2925
+ ctx.timer.logPhase('Dependencies installation');
2926
+ } else {
2927
+ logger.info(
2928
+ '\n💡 No package.json found, skipping dependency installation',
2929
+ );
1588
2930
  }
1589
- };
2931
+ }
2932
+ };
1590
2933
 
1591
- try {
1592
- logger.info('\nInitializing git repository...');
1593
- runGitCommand('git init');
2934
+ /**
2935
+ * 步骤4: 执行完成钩子
2936
+ */
2937
+ const executeCompleteHookStep = async ctx => {
2938
+ if (!ctx.absoluteOutputPath || !ctx.templateConfig || !ctx.context) {
2939
+ return;
2940
+ }
2941
+
2942
+ await executeCompleteHook(
2943
+ ctx.templateConfig,
2944
+ ctx.context,
2945
+ ctx.absoluteOutputPath,
2946
+ );
1594
2947
 
1595
- logger.info('Adding files to git...');
1596
- runGitCommand('git add .');
2948
+ ctx.timer.logPhase('Complete hook execution');
2949
+ };
1597
2950
 
1598
- logger.info('Creating initial commit...');
1599
- runGitCommand('git commit -m "chore: initial commit"');
2951
+ /**
2952
+ * 步骤5: 初始化 Git 仓库
2953
+ */
2954
+ const gitInitStep = ctx => {
2955
+ if (!ctx.absoluteOutputPath) {
2956
+ return;
2957
+ }
1600
2958
 
1601
- logger.success('Git repository initialized successfully!');
1602
- } catch (error) {
1603
- // Git 初始化失败不应该导致整个流程失败
1604
- logger.warn(
1605
- `Git initialization failed: ${error instanceof Error ? error.message : String(error)}`,
1606
- );
1607
- logger.info('You can manually initialize git later with: git init');
2959
+ const { skipGit } = ctx.options;
2960
+
2961
+ if (!skipGit) {
2962
+ runGitInit(ctx.absoluteOutputPath);
2963
+ ctx.timer.logPhase('Git initialization');
1608
2964
  }
1609
2965
  };
1610
2966
 
1611
2967
  /**
1612
- * 运行开发服务器(后台模式)
1613
- * 启动后台子进程运行开发服务器,父进程可以直接退出
2968
+ * 步骤6: 提交 Git 变更
1614
2969
  */
1615
- const runNpmDev = (projectPath) => {
1616
- logger.info('\nStarting development server in background...');
1617
- logger.info(`Executing: npm run dev in ${projectPath}`);
2970
+ const gitCommitStep = ctx => {
2971
+ if (!ctx.absoluteOutputPath) {
2972
+ return;
2973
+ }
1618
2974
 
1619
- // 使用通用的后台执行函数启动开发服务器
1620
- const pid = spawnDetached('npm', ['run', 'dev'], {
1621
- cwd: projectPath,
1622
- verbose: false, // 不输出额外的进程信息,由 logger 统一处理
1623
- });
2975
+ const { skipCommit } = ctx.options;
1624
2976
 
1625
- logger.success('Development server started in background!');
1626
- if (pid) {
1627
- logger.info(`Process ID: ${pid}`);
1628
- logger.info(
1629
- '\nThe dev server is running independently. You can close this terminal.',
1630
- );
1631
- logger.info(`To stop the server later, use: kill ${pid}`);
2977
+ if (!skipCommit) {
2978
+ commitChanges(ctx.absoluteOutputPath);
2979
+ ctx.timer.logPhase('Git commit');
1632
2980
  }
1633
2981
  };
1634
2982
 
2983
+ /**
2984
+ * 步骤7: 启动开发服务器
2985
+ */
2986
+ const startDevServerStep = ctx => {
2987
+ if (!ctx.absoluteOutputPath) {
2988
+ return;
2989
+ }
2990
+
2991
+ const { skipDev, skipInstall, skipGit, skipCommit } = ctx.options;
2992
+
2993
+ if (!skipDev) {
2994
+ runDev(ctx.absoluteOutputPath);
2995
+ ctx.timer.logPhase('Dev server startup');
2996
+ } else {
2997
+ // 只有跳过 dev 时才显示 Next steps
2998
+ logger.info('\nNext steps:');
2999
+ logger.info(` cd ${ctx.outputPath}`);
3000
+ if (skipInstall && ctx.hasPackageJson) {
3001
+ logger.info(' pnpm install');
3002
+ }
3003
+ if (skipGit) {
3004
+ logger.info(' git init');
3005
+ }
3006
+ if (skipCommit) {
3007
+ logger.info(' git add --all && git commit -m "chore: init env"');
3008
+ }
3009
+ logger.info(' coze dev');
3010
+ }
3011
+ };
3012
+
3013
+ /**
3014
+ * Init 命令的所有执行步骤
3015
+ */
3016
+ const initSteps = [
3017
+ executeTemplateEngineStep,
3018
+ checkPackageJsonStep,
3019
+ installDependenciesStep,
3020
+ executeCompleteHookStep,
3021
+ gitInitStep,
3022
+ gitCommitStep,
3023
+ startDevServerStep,
3024
+ ];
3025
+
1635
3026
  /**
1636
3027
  * 执行 init 命令的内部实现
1637
3028
  */
@@ -1642,80 +3033,68 @@ const executeInit = async (
1642
3033
 
1643
3034
 
1644
3035
 
3036
+
3037
+
1645
3038
  ,
1646
3039
  command,
1647
3040
  ) => {
1648
3041
  const timer = new TimeTracker();
3042
+ const cmdStartTime = Date.now();
3043
+
3044
+ // 初始化执行上下文
3045
+ const ctx = {
3046
+ options,
3047
+ command,
3048
+ timer,
3049
+ cmdStartTime,
3050
+ templateName: options.template,
3051
+ outputPath: options.output,
3052
+ };
1649
3053
 
1650
3054
  try {
1651
- const {
1652
- template: templateName,
1653
- output: outputPath,
1654
- skipInstall,
1655
- skipGit,
1656
- skipDev,
1657
- } = options;
1658
-
1659
- logger.info(`Initializing project with template: ${templateName}`);
1660
- timer.logPhase('Initialization');
1661
-
1662
- // 执行模板引擎,返回绝对路径
1663
- const absoluteOutputPath = await execute({
1664
- templateName,
1665
- outputPath,
1666
- command,
1667
- });
1668
-
1669
- timer.logPhase('Template engine execution');
1670
- logger.success('Project created successfully!');
1671
-
1672
- // 如果没有跳过安装,检查是否需要运行 pnpm install
1673
- if (!skipInstall) {
1674
- const nodeModulesPath = path.join(absoluteOutputPath, 'node_modules');
1675
- const hasNodeModules = fs.existsSync(nodeModulesPath);
1676
-
1677
- if (hasNodeModules) {
1678
- logger.info(
1679
- '\n💡 Using pre-warmed node_modules, skipping pnpm install',
1680
- );
1681
- timer.logPhase('Node modules (pre-warmed)');
1682
- } else {
1683
- runPnpmInstall(absoluteOutputPath);
1684
- timer.logPhase('Dependencies installation');
1685
- }
1686
- }
1687
-
1688
- // 如果没有跳过 git,则初始化 git 仓库
1689
- if (!skipGit) {
1690
- runGitInit(absoluteOutputPath);
1691
- timer.logPhase('Git initialization');
1692
- }
3055
+ // 上报命令开始
3056
+ reportCommandStart(
3057
+ 'init',
3058
+ JSON.stringify({ template: ctx.templateName, output: ctx.outputPath }),
3059
+ { template: ctx.templateName },
3060
+ );
1693
3061
 
1694
- // 如果没有跳过 dev,则启动开发服务器
1695
- if (!skipDev) {
1696
- runNpmDev(absoluteOutputPath);
1697
- timer.logPhase('Dev server startup');
1698
- } else {
1699
- // 只有跳过 dev 时才显示 Next steps
1700
- logger.info('\nNext steps:');
1701
- logger.info(` cd ${outputPath}`);
1702
- if (skipInstall) {
1703
- logger.info(' pnpm install');
1704
- }
1705
- if (skipGit) {
1706
- logger.info(
1707
- ' git init && git add . && git commit -m "initial commit"',
1708
- );
1709
- }
1710
- logger.info(' npm run dev');
3062
+ // 逐个执行步骤
3063
+ for (const step of initSteps) {
3064
+ await step(ctx);
1711
3065
  }
1712
3066
 
1713
3067
  // 输出总耗时
1714
3068
  timer.logTotal();
3069
+
3070
+ // 上报命令成功
3071
+ reportCommandComplete('init', true, Date.now() - cmdStartTime, {
3072
+ args: JSON.stringify(options),
3073
+ categories: { template: ctx.templateName },
3074
+ });
3075
+ // flush 由 main 函数统一处理
1715
3076
  } catch (error) {
1716
3077
  timer.logTotal();
3078
+
3079
+ // 上报错误
3080
+ const err = error instanceof Error ? error : new Error(String(error));
3081
+ reportError(err, {
3082
+ command: 'init',
3083
+ template: options.template,
3084
+ output: options.output,
3085
+ });
3086
+
3087
+ // 上报命令失败
3088
+ reportCommandComplete('init', false, Date.now() - cmdStartTime, {
3089
+ args: JSON.stringify(options),
3090
+ errorCode: 1,
3091
+ errorMessage: err.message,
3092
+ categories: { template: options.template },
3093
+ });
3094
+ await flushSlardar();
3095
+
1717
3096
  logger.error('Failed to initialize project:');
1718
- logger.error(error instanceof Error ? error.message : String(error));
3097
+ logger.error(err.message);
1719
3098
  process.exit(1);
1720
3099
  }
1721
3100
  };
@@ -1732,26 +3111,55 @@ const registerCommand = program => {
1732
3111
  .option('-o, --output <path>', 'Output directory', process.cwd())
1733
3112
  .option('--skip-install', 'Skip automatic pnpm install', false)
1734
3113
  .option('--skip-git', 'Skip automatic git initialization', false)
3114
+ .option(
3115
+ '--skip-commit',
3116
+ 'Skip automatic git commit after initialization',
3117
+ false,
3118
+ )
1735
3119
  .option('--skip-dev', 'Skip automatic dev server start', false)
1736
3120
  .allowUnknownOption() // 允许透传参数
1737
3121
  .action(async (directory, options, command) => {
1738
3122
  // 位置参数优先级高于 --output 选项
1739
3123
  const outputPath = _nullishCoalesce(directory, () => ( options.output));
1740
- await executeInit({ ...options, output: outputPath }, command);
3124
+ // Always use force mode - overwrite existing files without conflict check
3125
+ const force = true;
3126
+ await executeInit({ ...options, output: outputPath, force }, command);
1741
3127
  });
1742
3128
  };
1743
3129
 
1744
- var version = "0.0.1-alpha.1fb1dc";
1745
- var packageJson = {
1746
- version: version};
1747
-
1748
3130
  const commands = [
1749
3131
  registerCommand,
1750
3132
  registerCommand$1,
3133
+ registerCommand$4,
1751
3134
  registerCommand$2,
3135
+ registerCommand$3,
1752
3136
  ];
1753
3137
 
1754
- const main = () => {
3138
+ const main = async () => {
3139
+ // 初始化 Slardar 监控
3140
+ initSlardar();
3141
+
3142
+ // 监听未捕获的异常
3143
+ process.on('uncaughtException', async (error) => {
3144
+ reportError(error, {
3145
+ type: 'uncaughtException',
3146
+ context: 'global',
3147
+ });
3148
+ await flushSlardar();
3149
+ process.exit(1);
3150
+ });
3151
+
3152
+ // 监听未处理的 Promise rejection
3153
+ process.on('unhandledRejection', async (reason) => {
3154
+ const error = reason instanceof Error ? reason : new Error(String(reason));
3155
+ reportError(error, {
3156
+ type: 'unhandledRejection',
3157
+ context: 'global',
3158
+ });
3159
+ await flushSlardar();
3160
+ process.exit(1);
3161
+ });
3162
+
1755
3163
  const program = new commander.Command();
1756
3164
 
1757
3165
  program
@@ -1766,7 +3174,12 @@ const main = () => {
1766
3174
  // 在 help 输出中添加模板信息
1767
3175
  program.addHelpText('after', generateTemplatesHelpText());
1768
3176
 
1769
- program.parse(process.argv);
3177
+ // 使用 parseAsync 等待命令执行完成
3178
+ await program.parseAsync(process.argv);
3179
+
3180
+ // 统一在命令执行完成后 flush
3181
+ // 注意:如果命令中调用了 process.exit(),不会执行到这里
3182
+ await flushSlardar();
1770
3183
  };
1771
3184
 
1772
3185
  main();