@coze-arch/cli 0.0.1-alpha.1d232d → 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 (241) 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/contexts/AuthContext.tsx +14 -107
  20. package/lib/__templates__/expo/client/declarations.d.ts +5 -0
  21. package/lib/__templates__/expo/{eslint.config.mjs → client/eslint.config.mjs} +40 -10
  22. package/lib/__templates__/expo/client/hooks/useColorScheme.tsx +48 -0
  23. package/lib/__templates__/expo/client/hooks/useSafeRouter.ts +152 -0
  24. package/lib/__templates__/expo/client/hooks/useTheme.ts +26 -6
  25. package/lib/__templates__/expo/client/metro.config.js +124 -0
  26. package/lib/__templates__/expo/client/package.json +95 -0
  27. package/lib/__templates__/expo/client/screens/demo/index.tsx +25 -0
  28. package/lib/__templates__/expo/client/screens/demo/styles.ts +28 -0
  29. package/lib/__templates__/expo/client/scripts/install-missing-deps.js +11 -10
  30. package/lib/__templates__/expo/client/tsconfig.json +24 -0
  31. package/lib/__templates__/expo/client/utils/index.ts +23 -2
  32. package/lib/__templates__/expo/eslint-plugins/fontawesome6/index.js +9 -0
  33. package/lib/__templates__/expo/eslint-plugins/fontawesome6/names.js +1889 -0
  34. package/lib/__templates__/expo/eslint-plugins/fontawesome6/rule.js +174 -0
  35. package/lib/__templates__/expo/eslint-plugins/fontawesome6/v5-only-names.js +388 -0
  36. package/lib/__templates__/expo/eslint-plugins/forbid-emoji/index.js +9 -0
  37. package/lib/__templates__/expo/eslint-plugins/forbid-emoji/rule.js +112 -0
  38. package/lib/__templates__/expo/eslint-plugins/forbid-emoji/tech.md +94 -0
  39. package/lib/__templates__/expo/eslint-plugins/react-native/index.js +9 -0
  40. package/lib/__templates__/expo/eslint-plugins/react-native/rule.js +64 -0
  41. package/lib/__templates__/expo/eslint-plugins/reanimated/index.js +9 -0
  42. package/lib/__templates__/expo/eslint-plugins/reanimated/rule.js +88 -0
  43. package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/index.js +9 -0
  44. package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/rule.js +120 -0
  45. package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/tech.md +58 -0
  46. package/lib/__templates__/expo/package.json +16 -103
  47. package/lib/__templates__/expo/patches/expo@54.0.33.patch +45 -0
  48. package/lib/__templates__/expo/pnpm-lock.yaml +1437 -3171
  49. package/lib/__templates__/expo/pnpm-workspace.yaml +3 -0
  50. package/lib/__templates__/expo/server/build.js +21 -0
  51. package/lib/__templates__/expo/server/package.json +34 -0
  52. package/lib/__templates__/expo/server/src/index.ts +20 -0
  53. package/lib/__templates__/expo/server/tsconfig.json +24 -0
  54. package/lib/__templates__/expo/template.config.js +58 -1
  55. package/lib/__templates__/expo/tsconfig.json +1 -24
  56. package/lib/__templates__/native-static/.coze +11 -0
  57. package/lib/__templates__/native-static/index.html +33 -0
  58. package/lib/__templates__/native-static/styles/main.css +136 -0
  59. package/lib/__templates__/native-static/template.config.js +22 -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 +7 -2
  67. package/lib/__templates__/nextjs/pnpm-lock.yaml +2480 -1581
  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 +20 -33
  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 +68 -3
  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 +79 -9
  221. package/lib/__templates__/vite/tsconfig.json +4 -3
  222. package/lib/__templates__/vite/vite.config.ts +8 -3
  223. package/lib/cli.js +1768 -333
  224. package/package.json +17 -6
  225. package/lib/__templates__/expo/.cozeproj/scripts/deploy_build.sh +0 -115
  226. package/lib/__templates__/expo/.cozeproj/scripts/deploy_run.sh +0 -271
  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 -54
  235. package/lib/__templates__/expo/client/screens/home/styles.ts +0 -332
  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__/nextjs/server.mjs +0 -50
  240. package/lib/__templates__/vite/.vscode/settings.json +0 -7
  241. /package/lib/__templates__/expo/{eslint-formatter-simple.mjs → client/eslint-formatter-simple.mjs} +0 -0
package/lib/cli.js CHANGED
@@ -4,11 +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');
17
+ var child_process = require('child_process');
12
18
  var addFormats = require('ajv-formats');
13
19
  var Ajv = require('ajv');
14
20
  var minimist = require('minimist');
@@ -125,7 +131,236 @@ const generateTemplatesHelpText = () => {
125
131
  return lines.join('\n');
126
132
  };
127
133
 
128
- function _nullishCoalesce$2(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } 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; }var LogLevel; (function (LogLevel) {
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
+
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) {
129
364
  const ERROR = 0; LogLevel[LogLevel["ERROR"] = ERROR] = "ERROR";
130
365
  const WARN = 1; LogLevel[LogLevel["WARN"] = WARN] = "WARN";
131
366
  const SUCCESS = 2; LogLevel[LogLevel["SUCCESS"] = SUCCESS] = "SUCCESS";
@@ -172,7 +407,7 @@ class Logger {
172
407
  return level;
173
408
  }
174
409
 
175
- const envLevel = _optionalChain$4([process, 'access', _ => _.env, 'access', _2 => _2.LOG_LEVEL, 'optionalAccess', _3 => _3.toLowerCase, 'call', _4 => _4()]);
410
+ const envLevel = _optionalChain$3([process, 'access', _ => _.env, 'access', _2 => _2.LOG_LEVEL, 'optionalAccess', _3 => _3.toLowerCase, 'call', _4 => _4()]);
176
411
  if (envLevel && envLevel in LOG_LEVEL_MAP) {
177
412
  return LOG_LEVEL_MAP[envLevel];
178
413
  }
@@ -184,7 +419,7 @@ class Logger {
184
419
  // 简单检测:Node.js 环境且支持 TTY
185
420
  return (
186
421
  typeof process !== 'undefined' &&
187
- _optionalChain$4([process, 'access', _5 => _5.stdout, 'optionalAccess', _6 => _6.isTTY]) === true &&
422
+ _optionalChain$3([process, 'access', _5 => _5.stdout, 'optionalAccess', _6 => _6.isTTY]) === true &&
188
423
  process.env.NO_COLOR === undefined
189
424
  );
190
425
  }
@@ -479,6 +714,14 @@ const warmupTemplate = (templatePath, templateName) => {
479
714
  logger.info(`\nWarming up template: ${templateName}`);
480
715
  logger.info(` Path: ${templatePath}`);
481
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
+
482
725
  const result = shelljs.exec('pnpm install', {
483
726
  cwd: templatePath,
484
727
  silent: true,
@@ -512,13 +755,7 @@ const warmupTemplate = (templatePath, templateName) => {
512
755
  /**
513
756
  * 执行 warmup 命令的内部实现
514
757
  */
515
- const executeWarmup = async (
516
- options
517
-
518
- ,
519
-
520
- command,
521
- ) => {
758
+ const executeWarmup = async (options) => {
522
759
  const timer = new TimeTracker();
523
760
 
524
761
  try {
@@ -583,17 +820,362 @@ const executeWarmup = async (
583
820
  /**
584
821
  * 注册 warmup 命令到 program
585
822
  */
586
- const registerCommand$2 = program => {
823
+ const registerCommand$4 = program => {
587
824
  program
588
825
  .command('warmup')
589
826
  .description('Pre-install dependencies for templates to speed up init')
590
827
  .option('-t, --template <name>', 'Warmup a specific template only')
591
- .action(async (options, command) => {
828
+ .action(async options => {
592
829
  await executeWarmup(options);
593
830
  });
594
831
  };
595
832
 
596
- 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; }/* eslint-disable @typescript-eslint/no-explicit-any */
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
+
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 */
597
1179
  // Safe JSON parsing utilities with type safety and error handling
598
1180
  // Provides fallback values, validation, and error monitoring capabilities
599
1181
 
@@ -684,12 +1266,12 @@ function safeJsonParse(
684
1266
  const parsed = JSON.parse(String(input));
685
1267
 
686
1268
  // Optional validation
687
- if (_optionalChain$3([options, 'optionalAccess', _ => _.validate])) {
1269
+ if (_optionalChain$2([options, 'optionalAccess', _ => _.validate])) {
688
1270
  if (options.validate(parsed)) {
689
1271
  return parsed;
690
1272
  } else {
691
1273
  const validationError = new Error('JSON validation failed');
692
- _optionalChain$3([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
1274
+ _optionalChain$2([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
693
1275
 
694
1276
  if (options.throwOnValidationError) {
695
1277
  throw validationError;
@@ -701,15 +1283,15 @@ function safeJsonParse(
701
1283
  return parsed;
702
1284
  } catch (error) {
703
1285
  // Re-throw validation errors when throwOnValidationError is true
704
- if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$3([options, 'optionalAccess', _4 => _4.throwOnValidationError])) {
1286
+ if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$2([options, 'optionalAccess', _4 => _4.throwOnValidationError])) {
705
1287
  throw error;
706
1288
  }
707
- _optionalChain$3([options, 'optionalAccess', _5 => _5.onError, 'optionalCall', _6 => _6(error , input)]);
1289
+ _optionalChain$2([options, 'optionalAccess', _5 => _5.onError, 'optionalCall', _6 => _6(error , input)]);
708
1290
  return defaultValue;
709
1291
  }
710
1292
  }
711
1293
 
712
- 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; }
1294
+ function _optionalChain$1(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; }
713
1295
 
714
1296
 
715
1297
  /**
@@ -725,7 +1307,7 @@ const parseConfigContent = (content) => {
725
1307
  return config ;
726
1308
  } catch (error) {
727
1309
  // TOML 解析失败,继续尝试其他格式
728
- // eslint-disable-next-line no-console
1310
+
729
1311
  console.debug('TOML parse failed:', error);
730
1312
  }
731
1313
 
@@ -737,7 +1319,7 @@ const parseConfigContent = (content) => {
737
1319
  }
738
1320
  } catch (error) {
739
1321
  // YAML 解析失败,继续尝试其他格式
740
- // eslint-disable-next-line no-console
1322
+
741
1323
  console.debug('YAML parse failed:', error);
742
1324
  }
743
1325
 
@@ -799,13 +1381,13 @@ const getCommandConfig = (
799
1381
  // 根据命令名称映射到配置路径
800
1382
  switch (commandName) {
801
1383
  case 'dev':
802
- commandConfig = _optionalChain$2([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
1384
+ commandConfig = _optionalChain$1([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
803
1385
  break;
804
1386
  case 'build':
805
- commandConfig = _optionalChain$2([config, 'access', _3 => _3.deploy, 'optionalAccess', _4 => _4.build]);
1387
+ commandConfig = _optionalChain$1([config, 'access', _3 => _3.deploy, 'optionalAccess', _4 => _4.build]);
806
1388
  break;
807
1389
  case 'start':
808
- commandConfig = _optionalChain$2([config, 'access', _5 => _5.deploy, 'optionalAccess', _6 => _6.run]);
1390
+ commandConfig = _optionalChain$1([config, 'access', _5 => _5.deploy, 'optionalAccess', _6 => _6.run]);
809
1391
  break;
810
1392
  default:
811
1393
  throw new Error(`Unknown command: ${commandName}`);
@@ -821,70 +1403,337 @@ const getCommandConfig = (
821
1403
  return commandConfig;
822
1404
  };
823
1405
 
824
- function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain$1(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
+
825
1411
 
826
1412
  /**
827
- * 创建日志管理器
1413
+ * 检查是否为 Next.js 项目
828
1414
  */
829
- const createLogManager = (logDir = '.coze-logs') => {
830
- const ensureLogDir = () => {
831
- if (!fs.existsSync(logDir)) {
832
- fs.mkdirSync(logDir, { recursive: true });
833
- }
834
- };
1415
+ const isNextProject = (projectFolder) => {
1416
+ const packageJsonPath = path.join(projectFolder, 'package.json');
835
1417
 
836
- const getLogPath = (logFile) => path.join(logDir, logFile);
1418
+ if (!fs.existsSync(packageJsonPath)) {
1419
+ return false;
1420
+ }
837
1421
 
838
- return {
839
- createWriteStream: (logFile) => {
840
- ensureLogDir();
841
- return fs.createWriteStream(getLogPath(logFile), { flags: 'a' });
842
- },
843
- };
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
+ }
844
1434
  };
845
1435
 
846
1436
  /**
847
- * 执行命令的内部实现
1437
+ * 查找 Next.js 配置文件
848
1438
  */
849
- const executeRun = async (
850
- commandName,
851
- options = {},
852
- ) => {
853
- try {
854
- 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
+ ];
855
1445
 
856
- // 1. 加载 .coze 配置
857
- const config = await loadCozeConfig();
858
- 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
+ }
859
1452
 
860
- // 2. 准备日志
861
- const logManager = createLogManager();
862
- const logFile = options.logFile || `${commandName}.log`;
863
- const logStream = logManager.createWriteStream(logFile);
1453
+ return null;
1454
+ };
864
1455
 
865
- // 3. 执行命令
866
- 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;
867
1465
 
868
- logger.info(`Executing: ${commandString}`);
869
- logger.info(`Working directory: ${process.cwd()}`);
870
- 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;
871
1471
 
872
- const childProcess = shelljs.exec(commandString, {
873
- async: true,
874
- silent: true, // 不自动输出,我们手动处理
875
- });
1472
+ const matches = content.match(pattern);
876
1473
 
877
- if (!childProcess) {
878
- throw new Error('Failed to create child process');
879
- }
1474
+ if (matches && matches.length > 0) {
1475
+ originalLine = matches[0].trim();
880
1476
 
881
- // 将输出同时写入控制台和日志文件
882
- _optionalChain$1([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
883
- process.stdout.write(data);
884
- logStream.write(data);
885
- })]);
1477
+ // 在匹配的行前添加 // 注释
1478
+ content = content.replace(pattern, '$1// $2');
1479
+ modified = true;
886
1480
 
887
- _optionalChain$1([childProcess, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
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
+ // 将输出同时写入控制台和日志文件
1731
+ _optionalChain([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
1732
+ process.stdout.write(data);
1733
+ logStream.write(data);
1734
+ })]);
1735
+
1736
+ _optionalChain([childProcess, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
888
1737
  process.stderr.write(data);
889
1738
  logStream.write(data);
890
1739
  })]);
@@ -893,14 +1742,34 @@ const executeRun = async (
893
1742
  logStream.end();
894
1743
 
895
1744
  if (code !== 0) {
896
- logger.error(
897
- `Command exited with code ${_nullishCoalesce$1(code, () => ( 'unknown'))}${signal ? ` and signal ${signal}` : ''}`,
898
- );
899
- logger.error(`Check log file for details: ${logFile}`);
900
- 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
+ });
901
1764
  } else {
902
1765
  logger.success('Command completed successfully');
903
- 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 函数统一处理
904
1773
  }
905
1774
  });
906
1775
 
@@ -911,12 +1780,39 @@ const executeRun = async (
911
1780
  logger.error(`Stack trace:\n${error.stack}`);
912
1781
  }
913
1782
  logStream.end();
914
- 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
+ });
915
1797
  });
916
1798
  } catch (error) {
1799
+ const err = error instanceof Error ? error : new Error(String(error));
917
1800
  logger.error(`Failed to run ${commandName} command:`);
918
- logger.error(error instanceof Error ? error.message : String(error));
919
- 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
+ });
920
1816
  }
921
1817
  };
922
1818
 
@@ -952,6 +1848,45 @@ const registerCommand$1 = program => {
952
1848
  });
953
1849
  };
954
1850
 
1851
+ /**
1852
+ * 在后台启动一个独立的子进程
1853
+ * 类似于 `setsid command args >/dev/null 2>&1 &`
1854
+ *
1855
+ * @param command - 要执行的命令 (例如: 'npm', 'node', 'bash')
1856
+ * @param args - 命令参数数组 (例如: ['run', 'dev'])
1857
+ * @param options - 配置选项
1858
+ * @returns 子进程的 PID
1859
+ */
1860
+ function spawnDetached(
1861
+ command,
1862
+ args,
1863
+ options,
1864
+ ) {
1865
+ const { cwd, verbose = true } = options;
1866
+ const isWindows = os.platform() === 'win32';
1867
+
1868
+ if (verbose) {
1869
+ console.log(`Spawning detached process: ${command} ${args.join(' ')}`);
1870
+ console.log(`Working directory: ${cwd}`);
1871
+ }
1872
+
1873
+ // 使用 spawn 创建后台子进程
1874
+ const child = child_process.spawn(command, args, {
1875
+ cwd,
1876
+ detached: !isWindows, // Windows 不完全支持 detached,但仍可以使用
1877
+ stdio: 'ignore', // 忽略所有输入输出,让进程完全独立运行
1878
+ });
1879
+
1880
+ // 分离父子进程引用,允许父进程退出而不等待子进程
1881
+ child.unref();
1882
+
1883
+ if (verbose && child.pid) {
1884
+ console.log(`Process started with PID: ${child.pid}`);
1885
+ }
1886
+
1887
+ return child.pid;
1888
+ }
1889
+
955
1890
  /**
956
1891
  * 创建 AJV 验证器实例
957
1892
  */
@@ -1136,6 +2071,11 @@ const shouldIgnoreFile = (filePath) => {
1136
2071
  return directoryPatterns.some(dir => pathParts.includes(dir));
1137
2072
  };
1138
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
1139
2079
  /**
1140
2080
  * 递归获取目录中的所有文件
1141
2081
  *
@@ -1205,162 +2145,398 @@ const convertDotfileName = (filePath) => {
1205
2145
 
1206
2146
  return filePath;
1207
2147
  };
2148
+ // end_aigc
2149
+
2150
+ // ABOUTME: File rendering utilities for template processing
2151
+ // ABOUTME: Handles file content rendering, hook execution, and file writing
1208
2152
 
2153
+
2154
+ // start_aigc
1209
2155
  /**
1210
- * 复制并处理模板文件到目标目录
2156
+ * 执行文件渲染钩子
1211
2157
  *
1212
- * @param templatePath - 模板目录路径
1213
- * @param outputPath - 输出目录路径
2158
+ * @param templateConfig - 模板配置
2159
+ * @param fileInfo - 文件渲染信息
1214
2160
  * @param context - 模板上下文
2161
+ * @returns 处理后的文件信息,或 null 表示跳过该文件
1215
2162
  */
1216
- const processTemplateFiles = async (
1217
- templatePath,
1218
- outputPath,
2163
+ const executeFileRenderHook = async (
2164
+ templateConfig,
2165
+ fileInfo,
1219
2166
  context,
1220
2167
  ) => {
1221
- logger.verbose('Processing template files:');
1222
- logger.verbose(` - Template path: ${templatePath}`);
1223
- logger.verbose(` - Output path: ${outputPath}`);
1224
-
1225
- // 验证模板目录是否存在
1226
- try {
1227
- const stat = await fs$1.stat(templatePath);
1228
- logger.verbose(
1229
- ` - Template path exists: ${stat.isDirectory() ? 'directory' : 'file'}`,
1230
- );
1231
- if (!stat.isDirectory()) {
1232
- throw new Error(`Template path is not a directory: ${templatePath}`);
1233
- }
1234
- } catch (error) {
1235
- logger.error(
1236
- ` - Failed to access template path: ${error instanceof Error ? error.message : String(error)}`,
1237
- );
1238
- throw error;
2168
+ if (!templateConfig.onFileRender) {
2169
+ return fileInfo;
1239
2170
  }
1240
2171
 
1241
- const files = await getAllFiles(templatePath);
2172
+ const result = await templateConfig.onFileRender(
2173
+ fileInfo,
2174
+ context,
2175
+ );
1242
2176
 
1243
- logger.verbose(` - Found ${files.length} files to process`);
2177
+ // false: 跳过文件
2178
+ if (result === false) {
2179
+ return null;
2180
+ }
1244
2181
 
1245
- if (files.length === 0) {
1246
- logger.warn(' - No files found in template directory!');
1247
- return;
2182
+ // undefined/void: 使用默认内容
2183
+ if (result === undefined || result === null) {
2184
+ return fileInfo;
1248
2185
  }
1249
2186
 
1250
- await Promise.all(
1251
- files.map(async file => {
1252
- const srcPath = path.join(templatePath, file);
1253
- const destFile = convertDotfileName(file);
1254
- const destPath = path.join(outputPath, destFile);
2187
+ // string: 作为 content,其他不变
2188
+ if (typeof result === 'string') {
2189
+ return {
2190
+ ...fileInfo,
2191
+ content: result,
2192
+ };
2193
+ }
1255
2194
 
1256
- logger.verbose(
1257
- ` - Processing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1258
- );
2195
+ // FileRenderInfo: 使用新对象的信息
2196
+ return result;
2197
+ };
1259
2198
 
1260
- // 确保目标目录存在
1261
- await ensureDir(path.dirname(destPath));
2199
+ /**
2200
+ * 准备单个文件的渲染信息(不实际写入)
2201
+ * 用于 dry-run 阶段,收集所有将要写入的文件信息
2202
+ *
2203
+ * @param options - 准备选项
2204
+ * @returns 文件渲染信息,或 null 表示该文件被跳过
2205
+ */
2206
+ const prepareFileInfo = async (options
1262
2207
 
1263
- if (shouldRenderFile(srcPath)) {
1264
- // 渲染文本文件
1265
- const rendered = await renderTemplate(srcPath, context);
1266
- await fs$1.writeFile(destPath, rendered, 'utf-8');
1267
- logger.verbose(' ✓ Rendered and written');
1268
- } else {
1269
- // 直接复制二进制文件
1270
- await fs$1.copyFile(srcPath, destPath);
1271
- logger.verbose(' ✓ Copied');
1272
- }
1273
- }),
1274
- );
1275
2208
 
1276
- logger.verbose('✓ All files processed successfully');
1277
2209
 
1278
- // 单独处理 node_modules 目录(如果存在)
1279
- const sourceNodeModules = path.join(templatePath, 'node_modules');
1280
- const targetNodeModules = path.join(outputPath, 'node_modules');
1281
2210
 
1282
- if (fs.existsSync(sourceNodeModules)) {
1283
- logger.info('\nCopying node_modules from pre-warmed template...');
1284
- logger.verbose(` From: ${sourceNodeModules}`);
1285
- logger.verbose(` To: ${targetNodeModules}`);
2211
+ ) => {
2212
+ const { file, templatePath, context, templateConfig } = options;
1286
2213
 
1287
- const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1288
- silent: true,
1289
- });
2214
+ const srcPath = path.join(templatePath, file);
2215
+ const destFile = convertDotfileName(file);
1290
2216
 
1291
- if (result.stdout) {
1292
- process.stdout.write(result.stdout);
1293
- }
2217
+ logger.verbose(
2218
+ ` - Preparing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
2219
+ );
1294
2220
 
1295
- if (result.stderr) {
1296
- process.stderr.write(result.stderr);
1297
- }
2221
+ // 判断是否为二进制文件
2222
+ const isBinary = !shouldRenderFile(srcPath);
2223
+ let content;
2224
+ let wasRendered = false;
1298
2225
 
1299
- if (result.code === 0) {
1300
- logger.success('✓ node_modules copied successfully');
1301
- } else {
1302
- logger.warn(
1303
- `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1304
- );
1305
- logger.info('Will need to run pnpm install manually');
1306
- }
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
+ }
2235
+
2236
+ // 构造文件信息对象
2237
+ const fileInfo = {
2238
+ path: file,
2239
+ destPath: destFile,
2240
+ content,
2241
+ isBinary,
2242
+ wasRendered,
2243
+ };
2244
+
2245
+ // 执行文件渲染钩子
2246
+ const processedFileInfo = await executeFileRenderHook(
2247
+ templateConfig,
2248
+ fileInfo,
2249
+ context,
2250
+ );
2251
+
2252
+ // 如果返回 null,表示该文件被 hook 跳过
2253
+ if (processedFileInfo === null) {
2254
+ logger.verbose(' ⊘ Skipped by onFileRender hook');
2255
+ return null;
1307
2256
  }
2257
+
2258
+ return processedFileInfo;
1308
2259
  };
1309
2260
 
1310
2261
  /**
1311
- * 检查输出目录是否为空
1312
- * 注意:.git 目录会被忽略,允许在已初始化 git 的目录中创建项目
2262
+ * 写入渲染后的文件到目标路径
1313
2263
  *
1314
- * @param outputPath - 输出目录路径
1315
- * @returns 是否为空
2264
+ * @param options - 写入选项
1316
2265
  */
1317
- const isOutputDirEmpty = async (
1318
- outputPath,
2266
+ const writeRenderedFile = async (options
2267
+
2268
+
2269
+
1319
2270
  ) => {
1320
- try {
1321
- const entries = await fs$1.readdir(outputPath);
1322
- // 过滤掉 .git 目录,允许在已初始化 git 的目录中创建项目
1323
- const filteredEntries = entries.filter(entry => entry !== '.git');
1324
- return filteredEntries.length === 0;
1325
- } catch (e) {
1326
- // 目录不存在,视为空
1327
- return true;
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)');
2287
+ } else {
2288
+ const modifiedBuffer = Buffer.from(fileInfo.content, 'base64');
2289
+ await fs$1.writeFile(destPath, modifiedBuffer);
2290
+ logger.verbose(' ✓ Written (binary, modified by hook)');
2291
+ }
2292
+ } else {
2293
+ // 文本文件
2294
+ await fs$1.writeFile(destPath, fileInfo.content, 'utf-8');
2295
+ logger.verbose(' ✓ Rendered and written');
1328
2296
  }
1329
2297
  };
2298
+ // end_aigc
2299
+
2300
+ // ABOUTME: File conflict detection utilities for template processing
2301
+ // ABOUTME: Provides dry-run file collection and conflict checking
1330
2302
 
2303
+
2304
+ // start_aigc
1331
2305
  /**
1332
- * 验证输出目录
2306
+ * 收集所有将要写入的文件路径(dry-run 阶段)
1333
2307
  *
1334
- * @param outputPath - 输出目录路径
1335
- * @throws 如果目录不为空则抛出错误
2308
+ * @param options - 收集选项
2309
+ * @returns 将要写入的文件路径列表
1336
2310
  */
1337
- const validateOutputDir = async (outputPath) => {
1338
- const isEmpty = await isOutputDirEmpty(outputPath);
1339
- if (!isEmpty) {
1340
- throw new Error(
1341
- `Output directory is not empty: ${outputPath}\n` +
1342
- 'Please use an empty directory or remove existing files.',
1343
- );
1344
- }
1345
- };
2311
+ const collectFilesToRender = async (options
2312
+
1346
2313
 
1347
- /**
1348
- * 模板引擎执行选项
1349
- */
1350
2314
 
1351
2315
 
2316
+ ) => {
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
+ );
1352
2331
 
2332
+ // 过滤掉被 hook 跳过的文件,收集 destPath
2333
+ const filesToWrite = fileInfos
2334
+ .filter((info) => info !== null)
2335
+ .map(info => info.destPath);
1353
2336
 
2337
+ logger.verbose(` - ${filesToWrite.length} files will be written`);
2338
+ logger.verbose(
2339
+ ` - ${fileInfos.length - filesToWrite.length} files skipped by hooks`,
2340
+ );
1354
2341
 
2342
+ return filesToWrite;
2343
+ };
1355
2344
 
1356
2345
  /**
1357
- * 加载模板元数据和路径
2346
+ * 检测文件冲突
2347
+ *
2348
+ * @param outputPath - 输出目录路径
2349
+ * @param filesToWrite - 将要写入的文件路径列表
2350
+ * @returns 冲突的文件路径列表
1358
2351
  */
1359
- const loadTemplateMetadata = async (templateName) => {
1360
- const templatesConfigPath = getTemplatesConfigPath();
1361
- const templatesConfig = await loadTemplatesConfig(templatesConfigPath);
1362
- const templateMetadata = findTemplate(templatesConfig, templateName);
1363
- const templatesDir = getTemplatesDir();
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`);
2372
+ }
2373
+
2374
+ return conflicts;
2375
+ };
2376
+ // end_aigc
2377
+
2378
+ // ABOUTME: Main file processing orchestration for template rendering
2379
+ // ABOUTME: Coordinates file system, rendering, and conflict detection layers
2380
+
2381
+
2382
+ // start_aigc
2383
+ /**
2384
+ * 处理单个文件(准备 + 写入)
2385
+ *
2386
+ * @param options - 处理选项
2387
+ */
2388
+ const processSingleFile = async (options
2389
+
2390
+
2391
+
2392
+
2393
+
2394
+ ) => {
2395
+ const { file, templatePath, outputPath, context, templateConfig } = options;
2396
+
2397
+ const srcPath = path.join(templatePath, file);
2398
+
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();
1364
2540
  const templatePath = await getTemplatePath(templatesDir, templateMetadata);
1365
2541
 
1366
2542
  return { templatePath, templateMetadata };
@@ -1422,22 +2598,47 @@ const executeAfterRenderHook = async (
1422
2598
  }
1423
2599
  };
1424
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
+
1425
2614
  /**
1426
2615
  * 准备输出目录
1427
2616
  */
1428
- const prepareOutputDirectory = async (outputPath) => {
2617
+ const prepareOutputDirectory = (outputPath) => {
1429
2618
  const absolutePath = path.resolve(process.cwd(), outputPath);
1430
- await validateOutputDir(absolutePath);
2619
+ // 不再在这里验证目录是否为空,冲突检测已移至 processTemplateFiles 中
1431
2620
  return absolutePath;
1432
2621
  };
1433
2622
 
2623
+ /**
2624
+ * 模板引擎执行结果
2625
+ */
2626
+
2627
+
2628
+
2629
+
2630
+
2631
+
2632
+
2633
+
2634
+
1434
2635
  /**
1435
2636
  * 执行完整的模板渲染流程
1436
2637
  */
1437
2638
  const execute = async (
1438
2639
  options,
1439
2640
  ) => {
1440
- const { templateName, outputPath, command } = options;
2641
+ const { templateName, outputPath, command, force } = options;
1441
2642
 
1442
2643
  // 1. 加载模板
1443
2644
  const { templatePath } = await loadTemplateMetadata(templateName);
@@ -1454,18 +2655,28 @@ const execute = async (
1454
2655
  });
1455
2656
 
1456
2657
  // 5. 准备输出目录
1457
- const absoluteOutputPath = await prepareOutputDirectory(outputPath);
2658
+ const absoluteOutputPath = prepareOutputDirectory(outputPath);
1458
2659
 
1459
2660
  // 6. 处理模板文件
1460
- await processTemplateFiles(templatePath, absoluteOutputPath, context);
2661
+ await processTemplateFiles({
2662
+ templatePath,
2663
+ outputPath: absoluteOutputPath,
2664
+ context,
2665
+ templateConfig,
2666
+ force,
2667
+ });
1461
2668
 
1462
2669
  // 7. 执行 onAfterRender 钩子
1463
2670
  await executeAfterRenderHook(templateConfig, context, absoluteOutputPath);
1464
2671
 
1465
- return absoluteOutputPath;
2672
+ return {
2673
+ outputPath: absoluteOutputPath,
2674
+ templateConfig,
2675
+ context,
2676
+ };
1466
2677
  };
1467
2678
 
1468
- function _nullishCoalesce(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; }
2679
+ function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
1469
2680
  /**
1470
2681
  * 运行 pnpm install
1471
2682
  */
@@ -1504,12 +2715,46 @@ const runPnpmInstall = (projectPath) => {
1504
2715
  };
1505
2716
 
1506
2717
  /**
1507
- * 初始化 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 仓库
1508
2752
  * 如果目录中已存在 .git,则跳过初始化
1509
2753
  */
1510
2754
  const runGitInit = (projectPath) => {
1511
2755
  // 检查是否已存在 .git 目录
1512
2756
  const gitDir = path.join(projectPath, '.git');
2757
+
1513
2758
  if (fs.existsSync(gitDir)) {
1514
2759
  logger.info(
1515
2760
  '\n💡 Git repository already exists, skipping git initialization',
@@ -1517,99 +2762,267 @@ const runGitInit = (projectPath) => {
1517
2762
  return;
1518
2763
  }
1519
2764
 
1520
- const runGitCommand = (command) => {
1521
- 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
+ };
1522
2776
 
1523
- const result = shelljs.exec(command, {
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
+ };
2804
+
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'], {
1524
2823
  cwd: projectPath,
1525
- silent: true,
2824
+ verbose: false, // 不输出额外的进程信息,由 logger 统一处理
1526
2825
  });
1527
2826
 
1528
- // 输出命令的结果
1529
- if (result.stdout) {
1530
- 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');
1531
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
+
1532
2860
 
1533
- if (result.stderr) {
1534
- process.stderr.write(result.stderr);
1535
- }
1536
2861
 
1537
- if (result.code !== 0) {
1538
- const errorMessage = [
1539
- `${command} failed with exit code ${result.code}`,
1540
- result.stderr ? `\nStderr:\n${result.stderr}` : '',
1541
- result.stdout ? `\nStdout:\n${result.stdout}` : '',
1542
- ]
1543
- .filter(Boolean)
1544
- .join('');
1545
2862
 
1546
- throw new Error(errorMessage);
2863
+
2864
+
2865
+
2866
+
2867
+
2868
+
2869
+
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
+ );
1547
2930
  }
1548
- };
2931
+ }
2932
+ };
1549
2933
 
1550
- try {
1551
- logger.info('\nInitializing git repository...');
1552
- runGitCommand('git init');
2934
+ /**
2935
+ * 步骤4: 执行完成钩子
2936
+ */
2937
+ const executeCompleteHookStep = async ctx => {
2938
+ if (!ctx.absoluteOutputPath || !ctx.templateConfig || !ctx.context) {
2939
+ return;
2940
+ }
1553
2941
 
1554
- logger.info('Adding files to git...');
1555
- runGitCommand('git add .');
2942
+ await executeCompleteHook(
2943
+ ctx.templateConfig,
2944
+ ctx.context,
2945
+ ctx.absoluteOutputPath,
2946
+ );
1556
2947
 
1557
- logger.info('Creating initial commit...');
1558
- runGitCommand('git commit -m "chore: initial commit"');
2948
+ ctx.timer.logPhase('Complete hook execution');
2949
+ };
1559
2950
 
1560
- logger.success('Git repository initialized successfully!');
1561
- } catch (error) {
1562
- // Git 初始化失败不应该导致整个流程失败
1563
- logger.warn(
1564
- `Git initialization failed: ${error instanceof Error ? error.message : String(error)}`,
1565
- );
1566
- logger.info('You can manually initialize git later with: git init');
2951
+ /**
2952
+ * 步骤5: 初始化 Git 仓库
2953
+ */
2954
+ const gitInitStep = ctx => {
2955
+ if (!ctx.absoluteOutputPath) {
2956
+ return;
2957
+ }
2958
+
2959
+ const { skipGit } = ctx.options;
2960
+
2961
+ if (!skipGit) {
2962
+ runGitInit(ctx.absoluteOutputPath);
2963
+ ctx.timer.logPhase('Git initialization');
1567
2964
  }
1568
2965
  };
1569
2966
 
1570
2967
  /**
1571
- * 运行开发服务器
2968
+ * 步骤6: 提交 Git 变更
1572
2969
  */
1573
- const runNpmDev = (projectPath) => {
1574
- logger.info('\nStarting development server...');
1575
- logger.info(`Executing: npm run dev in ${projectPath}`);
1576
- logger.info('Press Ctrl+C to stop the server\n');
2970
+ const gitCommitStep = ctx => {
2971
+ if (!ctx.absoluteOutputPath) {
2972
+ return;
2973
+ }
1577
2974
 
1578
- // 使用 async: true 异步执行,不阻塞进程
1579
- const child = shelljs.exec('npm run dev', {
1580
- cwd: projectPath,
1581
- async: true,
1582
- silent: true, // 手动处理输出以便显示详细信息
1583
- });
2975
+ const { skipCommit } = ctx.options;
1584
2976
 
1585
- if (child) {
1586
- // 输出 stdout
1587
- _optionalChain([child, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
1588
- process.stdout.write(data);
1589
- })]);
2977
+ if (!skipCommit) {
2978
+ commitChanges(ctx.absoluteOutputPath);
2979
+ ctx.timer.logPhase('Git commit');
2980
+ }
2981
+ };
1590
2982
 
1591
- // 输出 stderr
1592
- _optionalChain([child, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
1593
- process.stderr.write(data);
1594
- })]);
2983
+ /**
2984
+ * 步骤7: 启动开发服务器
2985
+ */
2986
+ const startDevServerStep = ctx => {
2987
+ if (!ctx.absoluteOutputPath) {
2988
+ return;
2989
+ }
1595
2990
 
1596
- // 监听错误
1597
- child.on('error', (error) => {
1598
- logger.error(`Failed to start dev server: ${error.message}`);
1599
- logger.error(`Error stack: ${error.stack}`);
1600
- });
2991
+ const { skipDev, skipInstall, skipGit, skipCommit } = ctx.options;
1601
2992
 
1602
- // 监听退出
1603
- child.on('exit', (code, signal) => {
1604
- if (code !== 0 && code !== null) {
1605
- logger.error(
1606
- `Dev server exited with code ${code}${signal ? ` and signal ${signal}` : ''}`,
1607
- );
1608
- }
1609
- });
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');
1610
3010
  }
1611
3011
  };
1612
3012
 
3013
+ /**
3014
+ * Init 命令的所有执行步骤
3015
+ */
3016
+ const initSteps = [
3017
+ executeTemplateEngineStep,
3018
+ checkPackageJsonStep,
3019
+ installDependenciesStep,
3020
+ executeCompleteHookStep,
3021
+ gitInitStep,
3022
+ gitCommitStep,
3023
+ startDevServerStep,
3024
+ ];
3025
+
1613
3026
  /**
1614
3027
  * 执行 init 命令的内部实现
1615
3028
  */
@@ -1620,80 +3033,68 @@ const executeInit = async (
1620
3033
 
1621
3034
 
1622
3035
 
3036
+
3037
+
1623
3038
  ,
1624
3039
  command,
1625
3040
  ) => {
1626
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
+ };
1627
3053
 
1628
3054
  try {
1629
- const {
1630
- template: templateName,
1631
- output: outputPath,
1632
- skipInstall,
1633
- skipGit,
1634
- skipDev,
1635
- } = options;
1636
-
1637
- logger.info(`Initializing project with template: ${templateName}`);
1638
- timer.logPhase('Initialization');
1639
-
1640
- // 执行模板引擎,返回绝对路径
1641
- const absoluteOutputPath = await execute({
1642
- templateName,
1643
- outputPath,
1644
- command,
1645
- });
1646
-
1647
- timer.logPhase('Template engine execution');
1648
- logger.success('Project created successfully!');
1649
-
1650
- // 如果没有跳过安装,检查是否需要运行 pnpm install
1651
- if (!skipInstall) {
1652
- const nodeModulesPath = path.join(absoluteOutputPath, 'node_modules');
1653
- const hasNodeModules = fs.existsSync(nodeModulesPath);
1654
-
1655
- if (hasNodeModules) {
1656
- logger.info(
1657
- '\n💡 Using pre-warmed node_modules, skipping pnpm install',
1658
- );
1659
- timer.logPhase('Node modules (pre-warmed)');
1660
- } else {
1661
- runPnpmInstall(absoluteOutputPath);
1662
- timer.logPhase('Dependencies installation');
1663
- }
1664
- }
1665
-
1666
- // 如果没有跳过 git,则初始化 git 仓库
1667
- if (!skipGit) {
1668
- runGitInit(absoluteOutputPath);
1669
- timer.logPhase('Git initialization');
1670
- }
3055
+ // 上报命令开始
3056
+ reportCommandStart(
3057
+ 'init',
3058
+ JSON.stringify({ template: ctx.templateName, output: ctx.outputPath }),
3059
+ { template: ctx.templateName },
3060
+ );
1671
3061
 
1672
- // 如果没有跳过 dev,则启动开发服务器
1673
- if (!skipDev) {
1674
- runNpmDev(absoluteOutputPath);
1675
- timer.logPhase('Dev server startup');
1676
- } else {
1677
- // 只有跳过 dev 时才显示 Next steps
1678
- logger.info('\nNext steps:');
1679
- logger.info(` cd ${outputPath}`);
1680
- if (skipInstall) {
1681
- logger.info(' pnpm install');
1682
- }
1683
- if (skipGit) {
1684
- logger.info(
1685
- ' git init && git add . && git commit -m "initial commit"',
1686
- );
1687
- }
1688
- logger.info(' npm run dev');
3062
+ // 逐个执行步骤
3063
+ for (const step of initSteps) {
3064
+ await step(ctx);
1689
3065
  }
1690
3066
 
1691
3067
  // 输出总耗时
1692
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 函数统一处理
1693
3076
  } catch (error) {
1694
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
+
1695
3096
  logger.error('Failed to initialize project:');
1696
- logger.error(error instanceof Error ? error.message : String(error));
3097
+ logger.error(err.message);
1697
3098
  process.exit(1);
1698
3099
  }
1699
3100
  };
@@ -1710,26 +3111,55 @@ const registerCommand = program => {
1710
3111
  .option('-o, --output <path>', 'Output directory', process.cwd())
1711
3112
  .option('--skip-install', 'Skip automatic pnpm install', false)
1712
3113
  .option('--skip-git', 'Skip automatic git initialization', false)
3114
+ .option(
3115
+ '--skip-commit',
3116
+ 'Skip automatic git commit after initialization',
3117
+ false,
3118
+ )
1713
3119
  .option('--skip-dev', 'Skip automatic dev server start', false)
1714
3120
  .allowUnknownOption() // 允许透传参数
1715
3121
  .action(async (directory, options, command) => {
1716
3122
  // 位置参数优先级高于 --output 选项
1717
3123
  const outputPath = _nullishCoalesce(directory, () => ( options.output));
1718
- 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);
1719
3127
  });
1720
3128
  };
1721
3129
 
1722
- var version = "0.0.1-alpha.1d232d";
1723
- var packageJson = {
1724
- version: version};
1725
-
1726
3130
  const commands = [
1727
3131
  registerCommand,
1728
3132
  registerCommand$1,
3133
+ registerCommand$4,
1729
3134
  registerCommand$2,
3135
+ registerCommand$3,
1730
3136
  ];
1731
3137
 
1732
- 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
+
1733
3163
  const program = new commander.Command();
1734
3164
 
1735
3165
  program
@@ -1744,7 +3174,12 @@ const main = () => {
1744
3174
  // 在 help 输出中添加模板信息
1745
3175
  program.addHelpText('after', generateTemplatesHelpText());
1746
3176
 
1747
- program.parse(process.argv);
3177
+ // 使用 parseAsync 等待命令执行完成
3178
+ await program.parseAsync(process.argv);
3179
+
3180
+ // 统一在命令执行完成后 flush
3181
+ // 注意:如果命令中调用了 process.exit(),不会执行到这里
3182
+ await flushSlardar();
1748
3183
  };
1749
3184
 
1750
3185
  main();