@coze-arch/cli 0.0.1-beta.6 → 0.0.3
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.
- package/README.md +1 -0
- package/lib/__templates__/expo/.coze +7 -2
- package/lib/__templates__/expo/.cozeproj/scripts/dev_build.sh +46 -0
- package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +229 -0
- package/lib/__templates__/expo/.cozeproj/scripts/prod_build.sh +47 -0
- package/lib/__templates__/expo/.cozeproj/scripts/prod_run.sh +34 -0
- package/lib/__templates__/expo/.cozeproj/scripts/server_dev_run.sh +46 -0
- package/lib/__templates__/expo/README.md +68 -7
- package/lib/__templates__/expo/_gitignore +2 -1
- package/lib/__templates__/expo/_npmrc +4 -5
- package/lib/__templates__/expo/client/app/+not-found.tsx +15 -64
- package/lib/__templates__/expo/client/app/_layout.tsx +15 -12
- package/lib/__templates__/expo/client/app/index.tsx +1 -0
- package/lib/__templates__/expo/client/app.config.ts +76 -0
- package/lib/__templates__/expo/client/components/Screen.tsx +3 -19
- package/lib/__templates__/expo/client/components/ThemedText.tsx +33 -0
- package/lib/__templates__/expo/client/components/ThemedView.tsx +37 -0
- package/lib/__templates__/expo/client/constants/theme.ts +117 -58
- package/lib/__templates__/expo/client/contexts/AuthContext.tsx +14 -107
- package/lib/__templates__/expo/client/declarations.d.ts +5 -0
- package/lib/__templates__/expo/{eslint.config.mjs → client/eslint.config.mjs} +40 -10
- package/lib/__templates__/expo/client/hooks/useColorScheme.tsx +48 -0
- package/lib/__templates__/expo/client/hooks/useSafeRouter.ts +152 -0
- package/lib/__templates__/expo/client/hooks/useTheme.ts +26 -6
- package/lib/__templates__/expo/client/metro.config.js +124 -0
- package/lib/__templates__/expo/client/package.json +95 -0
- package/lib/__templates__/expo/client/screens/demo/index.tsx +25 -0
- package/lib/__templates__/expo/client/screens/demo/styles.ts +28 -0
- package/lib/__templates__/expo/client/scripts/install-missing-deps.js +11 -10
- package/lib/__templates__/expo/client/tsconfig.json +24 -0
- package/lib/__templates__/expo/client/utils/index.ts +23 -2
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/names.js +1889 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/rule.js +174 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/v5-only-names.js +388 -0
- package/lib/__templates__/expo/eslint-plugins/forbid-emoji/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/forbid-emoji/rule.js +112 -0
- package/lib/__templates__/expo/eslint-plugins/forbid-emoji/tech.md +94 -0
- package/lib/__templates__/expo/eslint-plugins/react-native/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/react-native/rule.js +64 -0
- package/lib/__templates__/expo/eslint-plugins/reanimated/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/reanimated/rule.js +88 -0
- package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/rule.js +120 -0
- package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/tech.md +58 -0
- package/lib/__templates__/expo/package.json +16 -101
- package/lib/__templates__/expo/patches/expo@54.0.33.patch +45 -0
- package/lib/__templates__/expo/pnpm-lock.yaml +1622 -3274
- package/lib/__templates__/expo/pnpm-workspace.yaml +3 -0
- package/lib/__templates__/expo/server/build.js +21 -0
- package/lib/__templates__/expo/server/package.json +34 -0
- package/lib/__templates__/expo/server/src/index.ts +20 -0
- package/lib/__templates__/expo/server/tsconfig.json +24 -0
- package/lib/__templates__/expo/template.config.js +58 -1
- package/lib/__templates__/expo/tsconfig.json +1 -24
- package/lib/__templates__/native-static/.coze +11 -0
- package/lib/__templates__/native-static/index.html +33 -0
- package/lib/__templates__/native-static/styles/main.css +136 -0
- package/lib/__templates__/native-static/template.config.js +22 -0
- package/lib/__templates__/nextjs/.coze +4 -3
- package/lib/__templates__/nextjs/README.md +5 -0
- package/lib/__templates__/nextjs/_gitignore +1 -0
- package/lib/__templates__/nextjs/_npmrc +2 -1
- package/lib/__templates__/nextjs/eslint.config.mjs +5 -0
- package/lib/__templates__/nextjs/next.config.ts +11 -0
- package/lib/__templates__/nextjs/package.json +15 -1
- package/lib/__templates__/nextjs/pnpm-lock.yaml +7694 -4394
- package/lib/__templates__/nextjs/scripts/build.sh +4 -1
- package/lib/__templates__/nextjs/scripts/dev.sh +15 -28
- package/lib/__templates__/nextjs/scripts/prepare.sh +9 -0
- package/lib/__templates__/nextjs/scripts/start.sh +7 -1
- package/lib/__templates__/nextjs/src/app/globals.css +109 -89
- package/lib/__templates__/nextjs/src/app/layout.tsx +20 -33
- package/lib/__templates__/nextjs/src/app/page.tsx +18 -49
- package/lib/__templates__/nextjs/src/components/ui/resizable.tsx +29 -22
- package/lib/__templates__/nextjs/src/components/ui/sidebar.tsx +228 -230
- package/lib/__templates__/nextjs/src/server.ts +35 -0
- package/lib/__templates__/nextjs/template.config.js +68 -3
- package/lib/__templates__/nextjs/tsconfig.json +1 -1
- package/lib/__templates__/nuxt-vue/.coze +12 -0
- package/lib/__templates__/nuxt-vue/README.md +73 -0
- package/lib/__templates__/nuxt-vue/_gitignore +25 -0
- package/lib/__templates__/nuxt-vue/_npmrc +23 -0
- package/lib/__templates__/nuxt-vue/app/app.vue +6 -0
- package/lib/__templates__/nuxt-vue/app/pages/index.vue +23 -0
- package/lib/__templates__/nuxt-vue/assets/css/main.css +24 -0
- package/lib/__templates__/nuxt-vue/nuxt.config.ts +126 -0
- package/lib/__templates__/nuxt-vue/package.json +35 -0
- package/lib/__templates__/nuxt-vue/pnpm-lock.yaml +8759 -0
- package/lib/__templates__/nuxt-vue/postcss.config.mjs +8 -0
- package/lib/__templates__/nuxt-vue/public/favicon.ico +0 -0
- package/lib/__templates__/nuxt-vue/public/robots.txt +2 -0
- package/lib/__templates__/nuxt-vue/scripts/build.sh +14 -0
- package/lib/__templates__/nuxt-vue/scripts/dev.sh +39 -0
- package/lib/__templates__/nuxt-vue/scripts/prepare.sh +14 -0
- package/lib/__templates__/nuxt-vue/scripts/start.sh +21 -0
- package/lib/__templates__/nuxt-vue/server/api/hello.ts +10 -0
- package/lib/__templates__/nuxt-vue/server/middleware/logger.ts +10 -0
- package/lib/__templates__/nuxt-vue/server/routes/health.ts +10 -0
- package/lib/__templates__/nuxt-vue/tailwind.config.js +13 -0
- package/lib/__templates__/nuxt-vue/template.config.js +87 -0
- package/lib/__templates__/nuxt-vue/tsconfig.json +18 -0
- package/lib/__templates__/taro/.coze +14 -0
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +19 -0
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +14 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +2 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +151 -0
- package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +5 -0
- package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +24 -0
- package/lib/__templates__/taro/README.md +763 -0
- package/lib/__templates__/taro/_gitignore +41 -0
- package/lib/__templates__/taro/_npmrc +18 -0
- package/lib/__templates__/taro/babel.config.js +12 -0
- package/lib/__templates__/taro/config/dev.ts +9 -0
- package/lib/__templates__/taro/config/index.ts +238 -0
- package/lib/__templates__/taro/config/prod.ts +34 -0
- package/lib/__templates__/taro/eslint.config.mjs +135 -0
- package/lib/__templates__/taro/key/private.appid.key +0 -0
- package/lib/__templates__/taro/package.json +112 -0
- package/lib/__templates__/taro/patches/@tarojs__plugin-mini-ci@4.1.9.patch +30 -0
- package/lib/__templates__/taro/pnpm-lock.yaml +23412 -0
- package/lib/__templates__/taro/pnpm-workspace.yaml +2 -0
- package/lib/__templates__/taro/project.config.json +15 -0
- package/lib/__templates__/taro/server/nest-cli.json +10 -0
- package/lib/__templates__/taro/server/package.json +40 -0
- package/lib/__templates__/taro/server/src/app.controller.ts +23 -0
- package/lib/__templates__/taro/server/src/app.module.ts +10 -0
- package/lib/__templates__/taro/server/src/app.service.ts +8 -0
- package/lib/__templates__/taro/server/src/interceptors/http-status.interceptor.ts +23 -0
- package/lib/__templates__/taro/server/src/main.ts +49 -0
- package/lib/__templates__/taro/server/tsconfig.json +24 -0
- package/lib/__templates__/taro/src/app.config.ts +11 -0
- package/lib/__templates__/taro/src/app.css +156 -0
- package/lib/__templates__/taro/src/app.tsx +9 -0
- package/lib/__templates__/taro/src/components/ui/accordion.tsx +159 -0
- package/lib/__templates__/taro/src/components/ui/alert-dialog.tsx +260 -0
- package/lib/__templates__/taro/src/components/ui/alert.tsx +60 -0
- package/lib/__templates__/taro/src/components/ui/aspect-ratio.tsx +36 -0
- package/lib/__templates__/taro/src/components/ui/avatar.tsx +84 -0
- package/lib/__templates__/taro/src/components/ui/badge.tsx +37 -0
- package/lib/__templates__/taro/src/components/ui/breadcrumb.tsx +117 -0
- package/lib/__templates__/taro/src/components/ui/button-group.tsx +83 -0
- package/lib/__templates__/taro/src/components/ui/button.tsx +67 -0
- package/lib/__templates__/taro/src/components/ui/calendar.tsx +394 -0
- package/lib/__templates__/taro/src/components/ui/card.tsx +108 -0
- package/lib/__templates__/taro/src/components/ui/carousel.tsx +228 -0
- package/lib/__templates__/taro/src/components/ui/checkbox.tsx +58 -0
- package/lib/__templates__/taro/src/components/ui/code-block.tsx +169 -0
- package/lib/__templates__/taro/src/components/ui/collapsible.tsx +71 -0
- package/lib/__templates__/taro/src/components/ui/command.tsx +385 -0
- package/lib/__templates__/taro/src/components/ui/context-menu.tsx +614 -0
- package/lib/__templates__/taro/src/components/ui/dialog.tsx +256 -0
- package/lib/__templates__/taro/src/components/ui/drawer.tsx +192 -0
- package/lib/__templates__/taro/src/components/ui/dropdown-menu.tsx +561 -0
- package/lib/__templates__/taro/src/components/ui/field.tsx +228 -0
- package/lib/__templates__/taro/src/components/ui/hover-card.tsx +282 -0
- package/lib/__templates__/taro/src/components/ui/input-group.tsx +197 -0
- package/lib/__templates__/taro/src/components/ui/input-otp.tsx +136 -0
- package/lib/__templates__/taro/src/components/ui/input.tsx +56 -0
- package/lib/__templates__/taro/src/components/ui/label.tsx +24 -0
- package/lib/__templates__/taro/src/components/ui/menubar.tsx +595 -0
- package/lib/__templates__/taro/src/components/ui/navigation-menu.tsx +264 -0
- package/lib/__templates__/taro/src/components/ui/pagination.tsx +118 -0
- package/lib/__templates__/taro/src/components/ui/popover.tsx +291 -0
- package/lib/__templates__/taro/src/components/ui/portal.tsx +19 -0
- package/lib/__templates__/taro/src/components/ui/progress.tsx +28 -0
- package/lib/__templates__/taro/src/components/ui/radio-group.tsx +64 -0
- package/lib/__templates__/taro/src/components/ui/resizable.tsx +346 -0
- package/lib/__templates__/taro/src/components/ui/scroll-area.tsx +34 -0
- package/lib/__templates__/taro/src/components/ui/select.tsx +438 -0
- package/lib/__templates__/taro/src/components/ui/separator.tsx +30 -0
- package/lib/__templates__/taro/src/components/ui/sheet.tsx +262 -0
- package/lib/__templates__/taro/src/components/ui/skeleton.tsx +17 -0
- package/lib/__templates__/taro/src/components/ui/slider.tsx +203 -0
- package/lib/__templates__/taro/src/components/ui/sonner.tsx +1 -0
- package/lib/__templates__/taro/src/components/ui/switch.tsx +55 -0
- package/lib/__templates__/taro/src/components/ui/table.tsx +142 -0
- package/lib/__templates__/taro/src/components/ui/tabs.tsx +114 -0
- package/lib/__templates__/taro/src/components/ui/textarea.tsx +54 -0
- package/lib/__templates__/taro/src/components/ui/toast.tsx +517 -0
- package/lib/__templates__/taro/src/components/ui/toggle-group.tsx +120 -0
- package/lib/__templates__/taro/src/components/ui/toggle.tsx +77 -0
- package/lib/__templates__/taro/src/components/ui/tooltip.tsx +455 -0
- package/lib/__templates__/taro/src/index.html +39 -0
- package/lib/__templates__/taro/src/lib/hooks/use-keyboard-offset.ts +37 -0
- package/lib/__templates__/taro/src/lib/measure.ts +115 -0
- package/lib/__templates__/taro/src/lib/platform.ts +12 -0
- package/lib/__templates__/taro/src/lib/utils.ts +6 -0
- package/lib/__templates__/taro/src/network.ts +39 -0
- package/lib/__templates__/taro/src/pages/index/index.config.ts +3 -0
- package/lib/__templates__/taro/src/pages/index/index.css +1 -0
- package/lib/__templates__/taro/src/pages/index/index.tsx +33 -0
- package/lib/__templates__/taro/src/presets/dev-debug.ts +23 -0
- package/lib/__templates__/taro/src/presets/h5-container.tsx +15 -0
- package/lib/__templates__/taro/src/presets/h5-navbar.tsx +238 -0
- package/lib/__templates__/taro/src/presets/h5-styles.ts +220 -0
- package/lib/__templates__/taro/src/presets/index.tsx +18 -0
- package/lib/__templates__/taro/stylelint.config.mjs +4 -0
- package/lib/__templates__/taro/template.config.js +68 -0
- package/lib/__templates__/taro/tsconfig.json +29 -0
- package/lib/__templates__/taro/types/global.d.ts +32 -0
- package/lib/__templates__/templates.json +173 -36
- package/lib/__templates__/vite/.coze +4 -3
- package/lib/__templates__/vite/README.md +383 -26
- package/lib/__templates__/vite/_gitignore +2 -0
- package/lib/__templates__/vite/_npmrc +2 -1
- package/lib/__templates__/vite/eslint.config.mjs +14 -0
- package/lib/__templates__/vite/package.json +23 -3
- package/lib/__templates__/vite/pnpm-lock.yaml +2509 -293
- package/lib/__templates__/vite/scripts/build.sh +4 -1
- package/lib/__templates__/vite/scripts/dev.sh +16 -28
- package/lib/__templates__/vite/scripts/prepare.sh +9 -0
- package/lib/__templates__/vite/scripts/start.sh +9 -3
- package/lib/__templates__/vite/server/routes/index.ts +31 -0
- package/lib/__templates__/vite/server/server.ts +65 -0
- package/lib/__templates__/vite/server/vite.ts +67 -0
- package/lib/__templates__/vite/src/main.ts +17 -48
- package/lib/__templates__/vite/template.config.js +77 -7
- package/lib/__templates__/vite/tsconfig.json +4 -3
- package/lib/__templates__/vite/vite.config.ts +8 -3
- package/lib/cli.js +1545 -526
- package/package.json +20 -7
- package/lib/__templates__/expo/.cozeproj/scripts/deploy_build.sh +0 -109
- package/lib/__templates__/expo/.cozeproj/scripts/deploy_run.sh +0 -257
- package/lib/__templates__/expo/app.json +0 -63
- package/lib/__templates__/expo/babel.config.js +0 -9
- package/lib/__templates__/expo/client/app/(tabs)/_layout.tsx +0 -43
- package/lib/__templates__/expo/client/app/(tabs)/home.tsx +0 -1
- package/lib/__templates__/expo/client/app/(tabs)/index.tsx +0 -7
- package/lib/__templates__/expo/client/hooks/useColorScheme.ts +0 -1
- package/lib/__templates__/expo/client/index.js +0 -12
- package/lib/__templates__/expo/client/screens/home/index.tsx +0 -54
- package/lib/__templates__/expo/client/screens/home/styles.ts +0 -332
- package/lib/__templates__/expo/metro.config.js +0 -53
- package/lib/__templates__/expo/src/index.ts +0 -12
- package/lib/__templates__/nextjs/.vscode/settings.json +0 -121
- package/lib/__templates__/nextjs/server.mjs +0 -50
- package/lib/__templates__/vite/.vscode/settings.json +0 -7
- /package/lib/__templates__/expo/{eslint-formatter-simple.mjs → client/eslint-formatter-simple.mjs} +0 -0
package/lib/cli.js
CHANGED
|
@@ -4,13 +4,18 @@
|
|
|
4
4
|
var commander = require('commander');
|
|
5
5
|
var path = require('path');
|
|
6
6
|
var fs = require('fs');
|
|
7
|
+
var node_path = require('node:path');
|
|
8
|
+
var node_fs = require('node:fs');
|
|
7
9
|
var shelljs = require('shelljs');
|
|
10
|
+
var perf_hooks = require('perf_hooks');
|
|
8
11
|
var fs$1 = require('fs/promises');
|
|
9
|
-
var
|
|
12
|
+
var os = require('os');
|
|
10
13
|
var jsYaml = require('js-yaml');
|
|
11
|
-
var
|
|
14
|
+
var toml = require('@iarna/toml');
|
|
15
|
+
var child_process = require('child_process');
|
|
12
16
|
var addFormats = require('ajv-formats');
|
|
13
17
|
var Ajv = require('ajv');
|
|
18
|
+
var minimist = require('minimist');
|
|
14
19
|
var changeCase = require('change-case');
|
|
15
20
|
var ejs = require('ejs');
|
|
16
21
|
|
|
@@ -124,7 +129,7 @@ const generateTemplatesHelpText = () => {
|
|
|
124
129
|
return lines.join('\n');
|
|
125
130
|
};
|
|
126
131
|
|
|
127
|
-
function _nullishCoalesce$2(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain$
|
|
132
|
+
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) {
|
|
128
133
|
const ERROR = 0; LogLevel[LogLevel["ERROR"] = ERROR] = "ERROR";
|
|
129
134
|
const WARN = 1; LogLevel[LogLevel["WARN"] = WARN] = "WARN";
|
|
130
135
|
const SUCCESS = 2; LogLevel[LogLevel["SUCCESS"] = SUCCESS] = "SUCCESS";
|
|
@@ -171,7 +176,7 @@ class Logger {
|
|
|
171
176
|
return level;
|
|
172
177
|
}
|
|
173
178
|
|
|
174
|
-
const envLevel = _optionalChain$
|
|
179
|
+
const envLevel = _optionalChain$3([process, 'access', _ => _.env, 'access', _2 => _2.LOG_LEVEL, 'optionalAccess', _3 => _3.toLowerCase, 'call', _4 => _4()]);
|
|
175
180
|
if (envLevel && envLevel in LOG_LEVEL_MAP) {
|
|
176
181
|
return LOG_LEVEL_MAP[envLevel];
|
|
177
182
|
}
|
|
@@ -183,7 +188,7 @@ class Logger {
|
|
|
183
188
|
// 简单检测:Node.js 环境且支持 TTY
|
|
184
189
|
return (
|
|
185
190
|
typeof process !== 'undefined' &&
|
|
186
|
-
_optionalChain$
|
|
191
|
+
_optionalChain$3([process, 'access', _5 => _5.stdout, 'optionalAccess', _6 => _6.isTTY]) === true &&
|
|
187
192
|
process.env.NO_COLOR === undefined
|
|
188
193
|
);
|
|
189
194
|
}
|
|
@@ -269,123 +274,448 @@ const createLogger = (options = {}) =>
|
|
|
269
274
|
// 导出默认实例
|
|
270
275
|
const logger = createLogger();
|
|
271
276
|
|
|
272
|
-
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 */
|
|
273
|
-
// Safe JSON parsing utilities with type safety and error handling
|
|
274
|
-
// Provides fallback values, validation, and error monitoring capabilities
|
|
275
|
-
|
|
276
277
|
/**
|
|
277
|
-
*
|
|
278
|
+
* 时间统计工具
|
|
278
279
|
*/
|
|
280
|
+
class TimeTracker {
|
|
281
|
+
|
|
282
|
+
|
|
279
283
|
|
|
284
|
+
constructor() {
|
|
285
|
+
this.startTime = perf_hooks.performance.now();
|
|
286
|
+
this.lastTime = this.startTime;
|
|
287
|
+
}
|
|
280
288
|
|
|
289
|
+
/**
|
|
290
|
+
* 记录阶段耗时
|
|
291
|
+
* @param phaseName 阶段名称
|
|
292
|
+
*/
|
|
293
|
+
logPhase(phaseName) {
|
|
294
|
+
const now = perf_hooks.performance.now();
|
|
295
|
+
const phaseTime = now - this.lastTime;
|
|
296
|
+
this.lastTime = now;
|
|
281
297
|
|
|
298
|
+
logger.verbose(`⏱ ${phaseName}: ${phaseTime.toFixed(2)}ms`);
|
|
299
|
+
}
|
|
282
300
|
|
|
301
|
+
/**
|
|
302
|
+
* 记录总耗时
|
|
303
|
+
*/
|
|
304
|
+
logTotal() {
|
|
305
|
+
const totalTime = perf_hooks.performance.now() - this.startTime;
|
|
306
|
+
logger.verbose(`⏱ Total time: ${totalTime.toFixed(2)}ms`);
|
|
307
|
+
}
|
|
283
308
|
|
|
309
|
+
/**
|
|
310
|
+
* 获取当前耗时(不输出日志)
|
|
311
|
+
* @returns 从开始到现在的总耗时(毫秒)
|
|
312
|
+
*/
|
|
313
|
+
getElapsedTime() {
|
|
314
|
+
return perf_hooks.performance.now() - this.startTime;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
284
317
|
|
|
318
|
+
/**
|
|
319
|
+
* 获取模板配置文件路径
|
|
320
|
+
* @returns templates.json 的绝对路径
|
|
321
|
+
*/
|
|
322
|
+
const getTemplatesConfigPath = () => {
|
|
323
|
+
const configPath = path.resolve(getTemplatesDir(), 'templates.json');
|
|
324
|
+
logger.verbose(`Templates config path: ${configPath}`);
|
|
325
|
+
logger.verbose(`Config file exists: ${fs.existsSync(configPath)}`);
|
|
326
|
+
return configPath;
|
|
327
|
+
};
|
|
285
328
|
|
|
329
|
+
/**
|
|
330
|
+
* 获取模板目录路径
|
|
331
|
+
* @returns __templates__ 目录的绝对路径
|
|
332
|
+
*/
|
|
333
|
+
const getTemplatesDir = () => {
|
|
334
|
+
const templatesDir = path.resolve(__dirname, './__templates__');
|
|
335
|
+
logger.verbose(`Templates directory: ${templatesDir}`);
|
|
336
|
+
logger.verbose(`Templates directory exists: ${fs.existsSync(templatesDir)}`);
|
|
337
|
+
return templatesDir;
|
|
338
|
+
};
|
|
286
339
|
|
|
340
|
+
/**
|
|
341
|
+
* 加载模板配置文件
|
|
342
|
+
* 支持 .ts 和 .js 文件(通过 sucrase 注册)
|
|
343
|
+
*
|
|
344
|
+
* @param templatePath - 模板目录路径
|
|
345
|
+
* @returns 模板配置对象
|
|
346
|
+
*/
|
|
287
347
|
|
|
348
|
+
const loadTemplateConfig = async (
|
|
349
|
+
templatePath,
|
|
350
|
+
) => {
|
|
351
|
+
logger.verbose(`Loading template config from: ${templatePath}`);
|
|
288
352
|
|
|
353
|
+
const tsConfigPath = path.join(templatePath, 'template.config.ts');
|
|
354
|
+
const jsConfigPath = path.join(templatePath, 'template.config.js');
|
|
289
355
|
|
|
356
|
+
logger.verbose('Checking for config files:');
|
|
357
|
+
logger.verbose(` - TypeScript: ${tsConfigPath}`);
|
|
358
|
+
logger.verbose(` - JavaScript: ${jsConfigPath}`);
|
|
290
359
|
|
|
360
|
+
let configPath;
|
|
291
361
|
|
|
362
|
+
const [tsExists, jsExists] = await Promise.all([
|
|
363
|
+
fs$1.access(tsConfigPath).then(
|
|
364
|
+
() => true,
|
|
365
|
+
() => false,
|
|
366
|
+
),
|
|
367
|
+
fs$1.access(jsConfigPath).then(
|
|
368
|
+
() => true,
|
|
369
|
+
() => false,
|
|
370
|
+
),
|
|
371
|
+
]);
|
|
292
372
|
|
|
373
|
+
logger.verbose('Config file existence check:');
|
|
374
|
+
logger.verbose(` - template.config.ts: ${tsExists}`);
|
|
375
|
+
logger.verbose(` - template.config.js: ${jsExists}`);
|
|
293
376
|
|
|
377
|
+
if (tsExists) {
|
|
378
|
+
configPath = tsConfigPath;
|
|
379
|
+
} else if (jsExists) {
|
|
380
|
+
configPath = jsConfigPath;
|
|
381
|
+
} else {
|
|
382
|
+
throw new Error(
|
|
383
|
+
`Template config not found in ${templatePath}.\n` +
|
|
384
|
+
'Expected: template.config.ts or template.config.js',
|
|
385
|
+
);
|
|
386
|
+
}
|
|
294
387
|
|
|
388
|
+
logger.verbose(`Using config file: ${configPath}`);
|
|
295
389
|
|
|
390
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports, security/detect-non-literal-require -- Sucrase handles .ts files at runtime, path is validated above
|
|
391
|
+
const config = require(configPath);
|
|
296
392
|
|
|
393
|
+
logger.verbose('Template config loaded successfully');
|
|
297
394
|
|
|
395
|
+
return config.default || config;
|
|
396
|
+
};
|
|
298
397
|
|
|
398
|
+
/**
|
|
399
|
+
* 加载模板列表配置
|
|
400
|
+
*
|
|
401
|
+
* @param configPath - templates.json 配置文件路径
|
|
402
|
+
* @returns 模板列表配置
|
|
403
|
+
*/
|
|
404
|
+
const loadTemplatesConfig = async (
|
|
405
|
+
configPath,
|
|
406
|
+
) => {
|
|
407
|
+
logger.verbose(`Loading templates config from: ${configPath}`);
|
|
299
408
|
|
|
409
|
+
const content = await fs$1.readFile(configPath, 'utf-8');
|
|
410
|
+
// eslint-disable-next-line no-restricted-syntax -- Static config file loaded at build time, safeJsonParse not needed
|
|
411
|
+
const config = JSON.parse(content) ;
|
|
300
412
|
|
|
413
|
+
logger.verbose(
|
|
414
|
+
`Found ${config.templates.length} templates: ${config.templates.map(t => t.name).join(', ')}`,
|
|
415
|
+
);
|
|
301
416
|
|
|
417
|
+
return config;
|
|
418
|
+
};
|
|
302
419
|
|
|
420
|
+
/**
|
|
421
|
+
* 根据模板名称查找模板元信息
|
|
422
|
+
*
|
|
423
|
+
* @param templatesConfig - 模板列表配置
|
|
424
|
+
* @param templateName - 模板名称
|
|
425
|
+
* @returns 模板元信息
|
|
426
|
+
*/
|
|
427
|
+
const findTemplate = (
|
|
428
|
+
templatesConfig,
|
|
429
|
+
templateName,
|
|
430
|
+
) => {
|
|
431
|
+
const template = templatesConfig.templates.find(t => t.name === templateName);
|
|
303
432
|
|
|
433
|
+
if (!template) {
|
|
434
|
+
const availableTemplates = templatesConfig.templates
|
|
435
|
+
.map(t => t.name)
|
|
436
|
+
.join(', ');
|
|
437
|
+
throw new Error(
|
|
438
|
+
`Template "${templateName}" not found.\n` +
|
|
439
|
+
`Available templates: ${availableTemplates}\n` +
|
|
440
|
+
'Use --template <name> to specify a template.',
|
|
441
|
+
);
|
|
442
|
+
}
|
|
304
443
|
|
|
444
|
+
return template;
|
|
445
|
+
};
|
|
305
446
|
|
|
447
|
+
/**
|
|
448
|
+
* 获取模板的完整路径
|
|
449
|
+
*
|
|
450
|
+
* @param basePath - 模板目录(templates.json 所在目录)
|
|
451
|
+
* @param templateMetadata - 模板元信息
|
|
452
|
+
* @returns 模板完整路径
|
|
453
|
+
*/
|
|
454
|
+
const getTemplatePath = async (
|
|
455
|
+
basePath,
|
|
456
|
+
templateMetadata,
|
|
457
|
+
) => {
|
|
458
|
+
logger.verbose('Resolving template path:');
|
|
459
|
+
logger.verbose(` - Base path: ${basePath}`);
|
|
460
|
+
logger.verbose(` - Template location: ${templateMetadata.location}`);
|
|
306
461
|
|
|
462
|
+
// location 是相对于 templates.json 文件的路径
|
|
463
|
+
const templatePath = path.join(basePath, templateMetadata.location);
|
|
307
464
|
|
|
465
|
+
logger.verbose(` - Resolved path: ${templatePath}`);
|
|
308
466
|
|
|
467
|
+
try {
|
|
468
|
+
await fs$1.access(templatePath);
|
|
469
|
+
logger.verbose(' - Template directory exists: ✓');
|
|
470
|
+
// eslint-disable-next-line @coze-arch/use-error-in-catch -- Error handling done in throw statement
|
|
471
|
+
} catch (e) {
|
|
472
|
+
logger.error(' - Template directory does not exist: ✗');
|
|
473
|
+
throw new Error(`Template directory not found: ${templatePath}`);
|
|
474
|
+
}
|
|
309
475
|
|
|
476
|
+
return templatePath;
|
|
477
|
+
};
|
|
310
478
|
|
|
311
479
|
/**
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
* @example
|
|
315
|
-
* ```ts
|
|
316
|
-
* // Basic usage - returns unknown | undefined
|
|
317
|
-
* const data = safeJsonParse('{"a":1}'); // { a: 1 }
|
|
318
|
-
*
|
|
319
|
-
* // With default value - always returns T
|
|
320
|
-
* const config = safeJsonParse(str, {});
|
|
321
|
-
* const user = safeJsonParse(str, null);
|
|
322
|
-
*
|
|
323
|
-
* // With error reporting
|
|
324
|
-
* const data = safeJsonParse(input, null, {
|
|
325
|
-
* onError: (error, input) => logger.error('Parse failed', { error, input })
|
|
326
|
-
* });
|
|
327
|
-
*
|
|
328
|
-
* // With validation
|
|
329
|
-
* const isUser = (data: unknown): data is User => ...;
|
|
330
|
-
* const user = safeJsonParse<User>(input, null, { validate: isUser });
|
|
331
|
-
* ```
|
|
480
|
+
* 对单个模板执行 pnpm install
|
|
332
481
|
*/
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
482
|
+
const warmupTemplate = (templatePath, templateName) => {
|
|
483
|
+
logger.info(`\nWarming up template: ${templateName}`);
|
|
484
|
+
logger.info(` Path: ${templatePath}`);
|
|
485
|
+
|
|
486
|
+
// 检查是否存在 package.json
|
|
487
|
+
const packageJsonPath = node_path.join(templatePath, 'package.json');
|
|
488
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
489
|
+
if (!node_fs.existsSync(packageJsonPath)) {
|
|
490
|
+
logger.info(` ⊘ Skipping ${templateName} (no package.json found)`);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
341
493
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
defaultValue = defaultValueOrOptions ;
|
|
351
|
-
options = optionsArg;
|
|
494
|
+
const result = shelljs.exec('pnpm install', {
|
|
495
|
+
cwd: templatePath,
|
|
496
|
+
silent: true,
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
// 输出 stdout
|
|
500
|
+
if (result.stdout) {
|
|
501
|
+
process.stdout.write(result.stdout);
|
|
352
502
|
}
|
|
353
503
|
|
|
354
|
-
//
|
|
355
|
-
if (
|
|
356
|
-
|
|
504
|
+
// 输出 stderr
|
|
505
|
+
if (result.stderr) {
|
|
506
|
+
process.stderr.write(result.stderr);
|
|
357
507
|
}
|
|
358
508
|
|
|
359
|
-
|
|
360
|
-
|
|
509
|
+
if (result.code === 0) {
|
|
510
|
+
logger.success(` ✓ ${templateName} warmed up successfully`);
|
|
511
|
+
} else {
|
|
512
|
+
const errorMessage = [
|
|
513
|
+
`pnpm install failed for ${templateName} with exit code ${result.code}`,
|
|
514
|
+
result.stderr ? `\nStderr:\n${result.stderr}` : '',
|
|
515
|
+
result.stdout ? `\nStdout:\n${result.stdout}` : '',
|
|
516
|
+
]
|
|
517
|
+
.filter(Boolean)
|
|
518
|
+
.join('');
|
|
361
519
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
return parsed;
|
|
366
|
-
} else {
|
|
367
|
-
const validationError = new Error('JSON validation failed');
|
|
368
|
-
_optionalChain$3([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
|
|
520
|
+
throw new Error(errorMessage);
|
|
521
|
+
}
|
|
522
|
+
};
|
|
369
523
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
524
|
+
/**
|
|
525
|
+
* 执行 warmup 命令的内部实现
|
|
526
|
+
*/
|
|
527
|
+
const executeWarmup = async (options) => {
|
|
528
|
+
const timer = new TimeTracker();
|
|
376
529
|
|
|
377
|
-
|
|
530
|
+
try {
|
|
531
|
+
const { template: templateFilter } = options;
|
|
532
|
+
|
|
533
|
+
logger.info('Starting template warmup...');
|
|
534
|
+
timer.logPhase('Initialization');
|
|
535
|
+
|
|
536
|
+
// 加载模板配置
|
|
537
|
+
const configPath = getTemplatesConfigPath();
|
|
538
|
+
const templatesConfig = await loadTemplatesConfig(configPath);
|
|
539
|
+
|
|
540
|
+
logger.verbose(
|
|
541
|
+
`Found ${templatesConfig.templates.length} templates in config`,
|
|
542
|
+
);
|
|
543
|
+
|
|
544
|
+
// 过滤模板
|
|
545
|
+
const templatesToWarmup = templateFilter
|
|
546
|
+
? templatesConfig.templates.filter(t => t.name === templateFilter)
|
|
547
|
+
: templatesConfig.templates;
|
|
548
|
+
|
|
549
|
+
if (templatesToWarmup.length === 0) {
|
|
550
|
+
if (templateFilter) {
|
|
551
|
+
logger.warn(`Template "${templateFilter}" not found`);
|
|
552
|
+
logger.info(
|
|
553
|
+
`Available templates: ${templatesConfig.templates.map(t => t.name).join(', ')}`,
|
|
554
|
+
);
|
|
555
|
+
} else {
|
|
556
|
+
logger.warn('No templates found');
|
|
557
|
+
}
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
logger.info(
|
|
562
|
+
`\nWill warm up ${templatesToWarmup.length} template(s): ${templatesToWarmup.map(t => t.name).join(', ')}`,
|
|
563
|
+
);
|
|
564
|
+
|
|
565
|
+
// 获取模板基础路径
|
|
566
|
+
const basePath = configPath.replace(/\/templates\.json$/, '');
|
|
567
|
+
|
|
568
|
+
// 对每个模板执行 pnpm install
|
|
569
|
+
for (const templateMetadata of templatesToWarmup) {
|
|
570
|
+
const templatePath = await getTemplatePath(basePath, templateMetadata);
|
|
571
|
+
warmupTemplate(templatePath, templateMetadata.name);
|
|
572
|
+
timer.logPhase(`Warmup ${templateMetadata.name}`);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
logger.success('\n✅ All templates warmed up successfully!');
|
|
576
|
+
logger.info(
|
|
577
|
+
'\nNext time you run `coze init`, it will be much faster as node_modules are pre-installed.',
|
|
578
|
+
);
|
|
579
|
+
|
|
580
|
+
timer.logTotal();
|
|
581
|
+
} catch (error) {
|
|
582
|
+
timer.logTotal();
|
|
583
|
+
logger.error('Failed to warmup templates:');
|
|
584
|
+
logger.error(error instanceof Error ? error.message : String(error));
|
|
585
|
+
process.exit(1);
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* 注册 warmup 命令到 program
|
|
591
|
+
*/
|
|
592
|
+
const registerCommand$4 = program => {
|
|
593
|
+
program
|
|
594
|
+
.command('warmup')
|
|
595
|
+
.description('Pre-install dependencies for templates to speed up init')
|
|
596
|
+
.option('-t, --template <name>', 'Warmup a specific template only')
|
|
597
|
+
.action(async options => {
|
|
598
|
+
await executeWarmup(options);
|
|
599
|
+
});
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
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 */
|
|
603
|
+
// Safe JSON parsing utilities with type safety and error handling
|
|
604
|
+
// Provides fallback values, validation, and error monitoring capabilities
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Options for safe JSON parsing
|
|
608
|
+
*/
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Safely parse JSON with error handling and type safety
|
|
643
|
+
*
|
|
644
|
+
* @example
|
|
645
|
+
* ```ts
|
|
646
|
+
* // Basic usage - returns unknown | undefined
|
|
647
|
+
* const data = safeJsonParse('{"a":1}'); // { a: 1 }
|
|
648
|
+
*
|
|
649
|
+
* // With default value - always returns T
|
|
650
|
+
* const config = safeJsonParse(str, {});
|
|
651
|
+
* const user = safeJsonParse(str, null);
|
|
652
|
+
*
|
|
653
|
+
* // With error reporting
|
|
654
|
+
* const data = safeJsonParse(input, null, {
|
|
655
|
+
* onError: (error, input) => logger.error('Parse failed', { error, input })
|
|
656
|
+
* });
|
|
657
|
+
*
|
|
658
|
+
* // With validation
|
|
659
|
+
* const isUser = (data: unknown): data is User => ...;
|
|
660
|
+
* const user = safeJsonParse<User>(input, null, { validate: isUser });
|
|
661
|
+
* ```
|
|
662
|
+
*/
|
|
663
|
+
function safeJsonParse(
|
|
664
|
+
input,
|
|
665
|
+
defaultValueOrOptions,
|
|
666
|
+
optionsArg,
|
|
667
|
+
) {
|
|
668
|
+
// Parse arguments
|
|
669
|
+
let defaultValue;
|
|
670
|
+
let options;
|
|
671
|
+
|
|
672
|
+
if (arguments.length === 2) {
|
|
673
|
+
// safeJsonParse(input, options) or safeJsonParse(input, defaultValue)
|
|
674
|
+
{
|
|
675
|
+
defaultValue = defaultValueOrOptions ;
|
|
676
|
+
options = undefined;
|
|
677
|
+
}
|
|
678
|
+
} else if (arguments.length === 3) {
|
|
679
|
+
// safeJsonParse(input, defaultValue, options)
|
|
680
|
+
defaultValue = defaultValueOrOptions ;
|
|
681
|
+
options = optionsArg;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// If input is already an object (and not null), return it directly
|
|
685
|
+
if (typeof input === 'object' && input !== null) {
|
|
686
|
+
return input ;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
try {
|
|
690
|
+
const parsed = JSON.parse(String(input));
|
|
691
|
+
|
|
692
|
+
// Optional validation
|
|
693
|
+
if (_optionalChain$2([options, 'optionalAccess', _ => _.validate])) {
|
|
694
|
+
if (options.validate(parsed)) {
|
|
695
|
+
return parsed;
|
|
696
|
+
} else {
|
|
697
|
+
const validationError = new Error('JSON validation failed');
|
|
698
|
+
_optionalChain$2([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
|
|
699
|
+
|
|
700
|
+
if (options.throwOnValidationError) {
|
|
701
|
+
throw validationError;
|
|
702
|
+
}
|
|
703
|
+
return defaultValue;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
return parsed;
|
|
378
708
|
} catch (error) {
|
|
379
709
|
// Re-throw validation errors when throwOnValidationError is true
|
|
380
|
-
if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$
|
|
710
|
+
if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$2([options, 'optionalAccess', _4 => _4.throwOnValidationError])) {
|
|
381
711
|
throw error;
|
|
382
712
|
}
|
|
383
|
-
_optionalChain$
|
|
713
|
+
_optionalChain$2([options, 'optionalAccess', _5 => _5.onError, 'optionalCall', _6 => _6(error , input)]);
|
|
384
714
|
return defaultValue;
|
|
385
715
|
}
|
|
386
716
|
}
|
|
387
717
|
|
|
388
|
-
function _optionalChain$
|
|
718
|
+
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; }
|
|
389
719
|
|
|
390
720
|
|
|
391
721
|
/**
|
|
@@ -401,7 +731,7 @@ const parseConfigContent = (content) => {
|
|
|
401
731
|
return config ;
|
|
402
732
|
} catch (error) {
|
|
403
733
|
// TOML 解析失败,继续尝试其他格式
|
|
404
|
-
|
|
734
|
+
|
|
405
735
|
console.debug('TOML parse failed:', error);
|
|
406
736
|
}
|
|
407
737
|
|
|
@@ -413,7 +743,7 @@ const parseConfigContent = (content) => {
|
|
|
413
743
|
}
|
|
414
744
|
} catch (error) {
|
|
415
745
|
// YAML 解析失败,继续尝试其他格式
|
|
416
|
-
|
|
746
|
+
|
|
417
747
|
console.debug('YAML parse failed:', error);
|
|
418
748
|
}
|
|
419
749
|
|
|
@@ -475,13 +805,13 @@ const getCommandConfig = (
|
|
|
475
805
|
// 根据命令名称映射到配置路径
|
|
476
806
|
switch (commandName) {
|
|
477
807
|
case 'dev':
|
|
478
|
-
commandConfig = _optionalChain$
|
|
808
|
+
commandConfig = _optionalChain$1([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
|
|
479
809
|
break;
|
|
480
810
|
case 'build':
|
|
481
|
-
commandConfig = _optionalChain$
|
|
811
|
+
commandConfig = _optionalChain$1([config, 'access', _3 => _3.deploy, 'optionalAccess', _4 => _4.build]);
|
|
482
812
|
break;
|
|
483
813
|
case 'start':
|
|
484
|
-
commandConfig = _optionalChain$
|
|
814
|
+
commandConfig = _optionalChain$1([config, 'access', _5 => _5.deploy, 'optionalAccess', _6 => _6.run]);
|
|
485
815
|
break;
|
|
486
816
|
default:
|
|
487
817
|
throw new Error(`Unknown command: ${commandName}`);
|
|
@@ -497,27 +827,276 @@ const getCommandConfig = (
|
|
|
497
827
|
return commandConfig;
|
|
498
828
|
};
|
|
499
829
|
|
|
500
|
-
|
|
830
|
+
// ABOUTME: Fix rule to comment out problematic outputFileTracingRoot config in Next.js projects
|
|
831
|
+
// ABOUTME: This config can cause issues in monorepo environments and should be removed
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
501
835
|
|
|
502
836
|
/**
|
|
503
|
-
*
|
|
837
|
+
* 检查是否为 Next.js 项目
|
|
504
838
|
*/
|
|
505
|
-
const
|
|
506
|
-
const
|
|
507
|
-
|
|
508
|
-
|
|
839
|
+
const isNextProject = (projectFolder) => {
|
|
840
|
+
const packageJsonPath = path.join(projectFolder, 'package.json');
|
|
841
|
+
|
|
842
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
843
|
+
return false;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
try {
|
|
847
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
848
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
849
|
+
const deps = {
|
|
850
|
+
...packageJson.dependencies,
|
|
851
|
+
...packageJson.devDependencies,
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
return 'next' in deps;
|
|
855
|
+
} catch (e) {
|
|
856
|
+
return false;
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* 查找 Next.js 配置文件
|
|
862
|
+
*/
|
|
863
|
+
const findNextConfigFile = (projectFolder) => {
|
|
864
|
+
const possibleConfigs = [
|
|
865
|
+
'next.config.ts',
|
|
866
|
+
'next.config.js',
|
|
867
|
+
'next.config.mjs',
|
|
868
|
+
];
|
|
869
|
+
|
|
870
|
+
for (const configFile of possibleConfigs) {
|
|
871
|
+
const configPath = path.join(projectFolder, configFile);
|
|
872
|
+
if (fs.existsSync(configPath)) {
|
|
873
|
+
return configPath;
|
|
509
874
|
}
|
|
510
|
-
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
return null;
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
/**
|
|
881
|
+
* 注释掉 outputFileTracingRoot 配置
|
|
882
|
+
*/
|
|
883
|
+
const commentOutOutputTracingRoot = (
|
|
884
|
+
configPath,
|
|
885
|
+
) => {
|
|
886
|
+
let content = fs.readFileSync(configPath, 'utf-8');
|
|
887
|
+
let modified = false;
|
|
888
|
+
let originalLine = null;
|
|
511
889
|
|
|
512
|
-
|
|
890
|
+
// 匹配包含 outputFileTracingRoot 的行(尚未被注释的)
|
|
891
|
+
// 支持 path.resolve(...) 后面有空格,然后可选逗号
|
|
892
|
+
// 只匹配单行配置,不匹配跨多行的配置
|
|
893
|
+
const pattern =
|
|
894
|
+
/^(\s*)(outputFileTracingRoot:\s*path\.resolve\([^\n\r)]+\)\s*,?)\s*$/gm;
|
|
895
|
+
|
|
896
|
+
const matches = content.match(pattern);
|
|
897
|
+
|
|
898
|
+
if (matches && matches.length > 0) {
|
|
899
|
+
originalLine = matches[0].trim();
|
|
900
|
+
|
|
901
|
+
// 在匹配的行前添加 // 注释
|
|
902
|
+
content = content.replace(pattern, '$1// $2');
|
|
903
|
+
modified = true;
|
|
904
|
+
|
|
905
|
+
fs.writeFileSync(configPath, content, 'utf-8');
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
return { modified, originalLine };
|
|
909
|
+
};
|
|
910
|
+
|
|
911
|
+
// start_aigc
|
|
912
|
+
/**
|
|
913
|
+
* Fix 规则:注释掉 Next.js 项目中的 outputFileTracingRoot 配置
|
|
914
|
+
* 这个配置在 monorepo 环境中可能会导致问题
|
|
915
|
+
*/
|
|
916
|
+
const fixNextOutputTracingRoot = context => {
|
|
917
|
+
const ruleName = 'next-output-tracing-root';
|
|
918
|
+
|
|
919
|
+
// 1. 检查是否为 Next.js 项目
|
|
920
|
+
if (!isNextProject(context.projectFolder)) {
|
|
921
|
+
return {
|
|
922
|
+
ruleName,
|
|
923
|
+
applied: false,
|
|
924
|
+
message: 'Not a Next.js project, skipping',
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
// 2. 查找 Next.js 配置文件
|
|
929
|
+
const configPath = findNextConfigFile(context.projectFolder);
|
|
930
|
+
|
|
931
|
+
if (!configPath) {
|
|
932
|
+
return {
|
|
933
|
+
ruleName,
|
|
934
|
+
applied: false,
|
|
935
|
+
message: 'Next.js config file not found, skipping',
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
// 3. 注释掉 outputFileTracingRoot 配置
|
|
940
|
+
const { modified, originalLine } = commentOutOutputTracingRoot(configPath);
|
|
941
|
+
|
|
942
|
+
if (modified && originalLine) {
|
|
943
|
+
logger.success(
|
|
944
|
+
`Commented out outputFileTracingRoot in ${configPath.split('/').pop()}`,
|
|
945
|
+
);
|
|
946
|
+
logger.info(` Original: ${originalLine}`);
|
|
947
|
+
|
|
948
|
+
return {
|
|
949
|
+
ruleName,
|
|
950
|
+
applied: true,
|
|
951
|
+
message: `Successfully commented out: ${originalLine}`,
|
|
952
|
+
};
|
|
953
|
+
}
|
|
513
954
|
|
|
514
955
|
return {
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
},
|
|
956
|
+
ruleName,
|
|
957
|
+
applied: false,
|
|
958
|
+
message: 'No outputFileTracingRoot config found, skipping',
|
|
519
959
|
};
|
|
520
960
|
};
|
|
961
|
+
// end_aigc
|
|
962
|
+
|
|
963
|
+
/**
|
|
964
|
+
* 所有修复规则的数组
|
|
965
|
+
* 按顺序执行,新增规则直接添加到数组中
|
|
966
|
+
*/
|
|
967
|
+
const rules = [
|
|
968
|
+
// Next.js related fixes
|
|
969
|
+
fixNextOutputTracingRoot,
|
|
970
|
+
|
|
971
|
+
// Add more rules here
|
|
972
|
+
] ;
|
|
973
|
+
|
|
974
|
+
// ABOUTME: Fix command for resolving legacy issues from previous project versions
|
|
975
|
+
// ABOUTME: Applies a series of fix rules defined in the fix-rules directory
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
// start_aigc
|
|
979
|
+
/**
|
|
980
|
+
* 执行 fix 命令的内部实现
|
|
981
|
+
*/
|
|
982
|
+
const executeFix = async (options = {}) => {
|
|
983
|
+
try {
|
|
984
|
+
const cwd = process.cwd();
|
|
985
|
+
const projectFolder = options.directory
|
|
986
|
+
? path.resolve(cwd, options.directory)
|
|
987
|
+
: cwd;
|
|
988
|
+
|
|
989
|
+
logger.info(`Running fix command on: ${projectFolder}`);
|
|
990
|
+
logger.info(`Found ${rules.length} fix rule(s) to apply\n`);
|
|
991
|
+
|
|
992
|
+
const context = {
|
|
993
|
+
cwd,
|
|
994
|
+
projectFolder,
|
|
995
|
+
};
|
|
996
|
+
|
|
997
|
+
let appliedCount = 0;
|
|
998
|
+
let skippedCount = 0;
|
|
999
|
+
|
|
1000
|
+
// 依次执行所有修复规则
|
|
1001
|
+
for (const rule of rules) {
|
|
1002
|
+
try {
|
|
1003
|
+
const result = await Promise.resolve(rule(context));
|
|
1004
|
+
|
|
1005
|
+
if (result.applied) {
|
|
1006
|
+
appliedCount++;
|
|
1007
|
+
logger.success(`✓ ${result.ruleName}: ${result.message}`);
|
|
1008
|
+
} else {
|
|
1009
|
+
skippedCount++;
|
|
1010
|
+
logger.info(`○ ${result.ruleName}: ${result.message}`);
|
|
1011
|
+
}
|
|
1012
|
+
} catch (error) {
|
|
1013
|
+
logger.error(
|
|
1014
|
+
`✗ Rule execution failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1015
|
+
);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
// 输出汇总信息
|
|
1020
|
+
logger.info(
|
|
1021
|
+
`\nSummary: ${appliedCount} fixed, ${skippedCount} skipped, ${rules.length} total`,
|
|
1022
|
+
);
|
|
1023
|
+
|
|
1024
|
+
if (appliedCount > 0) {
|
|
1025
|
+
logger.success('\nFixes applied successfully!');
|
|
1026
|
+
} else {
|
|
1027
|
+
logger.info('\nNo fixes needed');
|
|
1028
|
+
}
|
|
1029
|
+
} catch (error) {
|
|
1030
|
+
logger.error('Failed to run fix command:');
|
|
1031
|
+
logger.error(error instanceof Error ? error.message : String(error));
|
|
1032
|
+
process.exit(1);
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
// end_aigc
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* 注册 fix 命令到 program
|
|
1039
|
+
*/
|
|
1040
|
+
const registerCommand$3 = program => {
|
|
1041
|
+
program
|
|
1042
|
+
.command('fix')
|
|
1043
|
+
.description(
|
|
1044
|
+
'Fix legacy issues from previous versions (e.g., problematic configs)',
|
|
1045
|
+
)
|
|
1046
|
+
.argument(
|
|
1047
|
+
'[directory]',
|
|
1048
|
+
'Target directory to fix (defaults to current directory)',
|
|
1049
|
+
)
|
|
1050
|
+
.action(async (directory) => {
|
|
1051
|
+
await executeFix({ directory });
|
|
1052
|
+
});
|
|
1053
|
+
};
|
|
1054
|
+
|
|
1055
|
+
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; }
|
|
1056
|
+
/**
|
|
1057
|
+
* 日志文件名常量
|
|
1058
|
+
*/
|
|
1059
|
+
const LOG_FILE_NAME$1 = 'dev.log';
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* 获取日志目录
|
|
1063
|
+
* 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
|
|
1064
|
+
*/
|
|
1065
|
+
const getLogDir$1 = () =>
|
|
1066
|
+
process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* 解析日志文件路径
|
|
1070
|
+
* - 如果是绝对路径,直接使用
|
|
1071
|
+
* - 如果是相对路径,基于 getLogDir() + 相对路径
|
|
1072
|
+
* - 如果为空,使用 getLogDir() + LOG_FILE_NAME
|
|
1073
|
+
*/
|
|
1074
|
+
const resolveLogFilePath$1 = (logFile) => {
|
|
1075
|
+
if (!logFile) {
|
|
1076
|
+
return path.join(getLogDir$1(), LOG_FILE_NAME$1);
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
if (path.isAbsolute(logFile)) {
|
|
1080
|
+
return logFile;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
return path.join(getLogDir$1(), logFile);
|
|
1084
|
+
};
|
|
1085
|
+
|
|
1086
|
+
/**
|
|
1087
|
+
* 创建日志写入流
|
|
1088
|
+
*/
|
|
1089
|
+
const createLogStream$1 = (logFilePath) => {
|
|
1090
|
+
const logDir = path.dirname(logFilePath);
|
|
1091
|
+
|
|
1092
|
+
// 确保日志目录存在
|
|
1093
|
+
if (!fs.existsSync(logDir)) {
|
|
1094
|
+
fs.mkdirSync(logDir, { recursive: true });
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
// 使用 'w' 标志覆盖之前的日志
|
|
1098
|
+
return fs.createWriteStream(logFilePath, { flags: 'w' });
|
|
1099
|
+
};
|
|
521
1100
|
|
|
522
1101
|
/**
|
|
523
1102
|
* 执行命令的内部实现
|
|
@@ -529,21 +1108,32 @@ const executeRun = async (
|
|
|
529
1108
|
try {
|
|
530
1109
|
logger.info(`Running ${commandName} command...`);
|
|
531
1110
|
|
|
532
|
-
// 1.
|
|
1111
|
+
// 1. 对于 build 命令,先执行 fix 以确保项目配置正确
|
|
1112
|
+
if (['dev', 'build'].includes(commandName)) {
|
|
1113
|
+
logger.info('\n🔧 Running fix command before build...\n');
|
|
1114
|
+
try {
|
|
1115
|
+
await executeFix();
|
|
1116
|
+
// eslint-disable-next-line @coze-arch/no-empty-catch
|
|
1117
|
+
} catch (e) {
|
|
1118
|
+
// just ignore
|
|
1119
|
+
}
|
|
1120
|
+
logger.info('');
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
// 2. 加载 .coze 配置
|
|
533
1124
|
const config = await loadCozeConfig();
|
|
534
1125
|
const commandArgs = getCommandConfig(config, commandName);
|
|
535
1126
|
|
|
536
|
-
//
|
|
537
|
-
const
|
|
538
|
-
const
|
|
539
|
-
const logStream = logManager.createWriteStream(logFile);
|
|
1127
|
+
// 3. 准备日志
|
|
1128
|
+
const logFilePath = resolveLogFilePath$1(options.logFile);
|
|
1129
|
+
const logStream = createLogStream$1(logFilePath);
|
|
540
1130
|
|
|
541
|
-
//
|
|
1131
|
+
// 4. 执行命令
|
|
542
1132
|
const commandString = commandArgs.join(' ');
|
|
543
1133
|
|
|
544
1134
|
logger.info(`Executing: ${commandString}`);
|
|
545
1135
|
logger.info(`Working directory: ${process.cwd()}`);
|
|
546
|
-
logger.info(`Log file: ${
|
|
1136
|
+
logger.info(`Log file: ${logFilePath}`);
|
|
547
1137
|
|
|
548
1138
|
const childProcess = shelljs.exec(commandString, {
|
|
549
1139
|
async: true,
|
|
@@ -555,12 +1145,12 @@ const executeRun = async (
|
|
|
555
1145
|
}
|
|
556
1146
|
|
|
557
1147
|
// 将输出同时写入控制台和日志文件
|
|
558
|
-
_optionalChain
|
|
1148
|
+
_optionalChain([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
|
|
559
1149
|
process.stdout.write(data);
|
|
560
1150
|
logStream.write(data);
|
|
561
1151
|
})]);
|
|
562
1152
|
|
|
563
|
-
_optionalChain
|
|
1153
|
+
_optionalChain([childProcess, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
|
|
564
1154
|
process.stderr.write(data);
|
|
565
1155
|
logStream.write(data);
|
|
566
1156
|
})]);
|
|
@@ -572,11 +1162,11 @@ const executeRun = async (
|
|
|
572
1162
|
logger.error(
|
|
573
1163
|
`Command exited with code ${_nullishCoalesce$1(code, () => ( 'unknown'))}${signal ? ` and signal ${signal}` : ''}`,
|
|
574
1164
|
);
|
|
575
|
-
logger.error(`Check log file for details: ${
|
|
1165
|
+
logger.error(`Check log file for details: ${logFilePath}`);
|
|
576
1166
|
process.exit(code || 1);
|
|
577
1167
|
} else {
|
|
578
1168
|
logger.success('Command completed successfully');
|
|
579
|
-
logger.info(`Log file: ${
|
|
1169
|
+
logger.info(`Log file: ${logFilePath}`);
|
|
580
1170
|
}
|
|
581
1171
|
});
|
|
582
1172
|
|
|
@@ -599,7 +1189,7 @@ const executeRun = async (
|
|
|
599
1189
|
/**
|
|
600
1190
|
* 注册 dev/build/start 命令到 program
|
|
601
1191
|
*/
|
|
602
|
-
const registerCommand$
|
|
1192
|
+
const registerCommand$2 = program => {
|
|
603
1193
|
// dev 命令
|
|
604
1194
|
program
|
|
605
1195
|
.command('dev')
|
|
@@ -629,259 +1219,98 @@ const registerCommand$1 = program => {
|
|
|
629
1219
|
};
|
|
630
1220
|
|
|
631
1221
|
/**
|
|
632
|
-
*
|
|
633
|
-
|
|
634
|
-
class TimeTracker {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
constructor() {
|
|
639
|
-
this.startTime = perf_hooks.performance.now();
|
|
640
|
-
this.lastTime = this.startTime;
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
/**
|
|
644
|
-
* 记录阶段耗时
|
|
645
|
-
* @param phaseName 阶段名称
|
|
646
|
-
*/
|
|
647
|
-
logPhase(phaseName) {
|
|
648
|
-
const now = perf_hooks.performance.now();
|
|
649
|
-
const phaseTime = now - this.lastTime;
|
|
650
|
-
this.lastTime = now;
|
|
651
|
-
|
|
652
|
-
logger.verbose(`⏱ ${phaseName}: ${phaseTime.toFixed(2)}ms`);
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
/**
|
|
656
|
-
* 记录总耗时
|
|
657
|
-
*/
|
|
658
|
-
logTotal() {
|
|
659
|
-
const totalTime = perf_hooks.performance.now() - this.startTime;
|
|
660
|
-
logger.verbose(`⏱ Total time: ${totalTime.toFixed(2)}ms`);
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
/**
|
|
664
|
-
* 获取当前耗时(不输出日志)
|
|
665
|
-
* @returns 从开始到现在的总耗时(毫秒)
|
|
666
|
-
*/
|
|
667
|
-
getElapsedTime() {
|
|
668
|
-
return perf_hooks.performance.now() - this.startTime;
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
/**
|
|
673
|
-
* 获取模板配置文件路径
|
|
674
|
-
* @returns templates.json 的绝对路径
|
|
675
|
-
*/
|
|
676
|
-
const getTemplatesConfigPath = () => {
|
|
677
|
-
const configPath = path.resolve(getTemplatesDir(), 'templates.json');
|
|
678
|
-
logger.verbose(`Templates config path: ${configPath}`);
|
|
679
|
-
logger.verbose(`Config file exists: ${fs.existsSync(configPath)}`);
|
|
680
|
-
return configPath;
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
/**
|
|
684
|
-
* 获取模板目录路径
|
|
685
|
-
* @returns __templates__ 目录的绝对路径
|
|
686
|
-
*/
|
|
687
|
-
const getTemplatesDir = () => {
|
|
688
|
-
const templatesDir = path.resolve(__dirname, './__templates__');
|
|
689
|
-
logger.verbose(`Templates directory: ${templatesDir}`);
|
|
690
|
-
logger.verbose(`Templates directory exists: ${fs.existsSync(templatesDir)}`);
|
|
691
|
-
return templatesDir;
|
|
692
|
-
};
|
|
693
|
-
|
|
694
|
-
/**
|
|
695
|
-
* 创建 AJV 验证器实例
|
|
696
|
-
*/
|
|
697
|
-
const createAjvInstance = () => {
|
|
698
|
-
const ajv = new Ajv({
|
|
699
|
-
useDefaults: true, // 自动应用默认值
|
|
700
|
-
coerceTypes: true, // 类型强制转换
|
|
701
|
-
removeAdditional: false, // 保留额外属性以便后续报错
|
|
702
|
-
allErrors: true, // 收集所有错误
|
|
703
|
-
});
|
|
704
|
-
|
|
705
|
-
addFormats(ajv);
|
|
706
|
-
return ajv;
|
|
707
|
-
};
|
|
708
|
-
|
|
709
|
-
/**
|
|
710
|
-
* 验证参数
|
|
1222
|
+
* 在后台启动一个独立的子进程
|
|
1223
|
+
* 类似于 `setsid command args >/dev/null 2>&1 &`
|
|
711
1224
|
*
|
|
712
|
-
* @param
|
|
713
|
-
* @param
|
|
714
|
-
* @
|
|
715
|
-
* @
|
|
1225
|
+
* @param command - 要执行的命令 (例如: 'npm', 'node', 'bash')
|
|
1226
|
+
* @param args - 命令参数数组 (例如: ['run', 'dev'])
|
|
1227
|
+
* @param options - 配置选项
|
|
1228
|
+
* @returns 子进程的 PID
|
|
716
1229
|
*/
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
const
|
|
723
|
-
|
|
724
|
-
const isValid = validate(params);
|
|
725
|
-
|
|
726
|
-
if (!isValid) {
|
|
727
|
-
const errors = validate.errors || [];
|
|
728
|
-
const errorMessages = errors.map(err => {
|
|
729
|
-
const path = err.instancePath || '/';
|
|
730
|
-
const message = err.message || 'validation failed';
|
|
731
|
-
return ` - ${path}: ${message}`;
|
|
732
|
-
});
|
|
1230
|
+
function spawnDetached(
|
|
1231
|
+
command,
|
|
1232
|
+
args,
|
|
1233
|
+
options,
|
|
1234
|
+
) {
|
|
1235
|
+
const { cwd, verbose = true } = options;
|
|
1236
|
+
const isWindows = os.platform() === 'win32';
|
|
733
1237
|
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
);
|
|
1238
|
+
if (verbose) {
|
|
1239
|
+
console.log(`Spawning detached process: ${command} ${args.join(' ')}`);
|
|
1240
|
+
console.log(`Working directory: ${cwd}`);
|
|
737
1241
|
}
|
|
738
1242
|
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
*
|
|
746
|
-
* @param templatePath - 模板目录路径
|
|
747
|
-
* @returns 模板配置对象
|
|
748
|
-
*/
|
|
749
|
-
|
|
750
|
-
const loadTemplateConfig = async (
|
|
751
|
-
templatePath,
|
|
752
|
-
) => {
|
|
753
|
-
logger.verbose(`Loading template config from: ${templatePath}`);
|
|
754
|
-
|
|
755
|
-
const tsConfigPath = path.join(templatePath, 'template.config.ts');
|
|
756
|
-
const jsConfigPath = path.join(templatePath, 'template.config.js');
|
|
757
|
-
|
|
758
|
-
logger.verbose('Checking for config files:');
|
|
759
|
-
logger.verbose(` - TypeScript: ${tsConfigPath}`);
|
|
760
|
-
logger.verbose(` - JavaScript: ${jsConfigPath}`);
|
|
761
|
-
|
|
762
|
-
let configPath;
|
|
763
|
-
|
|
764
|
-
const [tsExists, jsExists] = await Promise.all([
|
|
765
|
-
fs$1.access(tsConfigPath).then(
|
|
766
|
-
() => true,
|
|
767
|
-
() => false,
|
|
768
|
-
),
|
|
769
|
-
fs$1.access(jsConfigPath).then(
|
|
770
|
-
() => true,
|
|
771
|
-
() => false,
|
|
772
|
-
),
|
|
773
|
-
]);
|
|
1243
|
+
// 使用 spawn 创建后台子进程
|
|
1244
|
+
const child = child_process.spawn(command, args, {
|
|
1245
|
+
cwd,
|
|
1246
|
+
detached: !isWindows, // Windows 不完全支持 detached,但仍可以使用
|
|
1247
|
+
stdio: 'ignore', // 忽略所有输入输出,让进程完全独立运行
|
|
1248
|
+
});
|
|
774
1249
|
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
logger.verbose(` - template.config.js: ${jsExists}`);
|
|
1250
|
+
// 分离父子进程引用,允许父进程退出而不等待子进程
|
|
1251
|
+
child.unref();
|
|
778
1252
|
|
|
779
|
-
if (
|
|
780
|
-
|
|
781
|
-
} else if (jsExists) {
|
|
782
|
-
configPath = jsConfigPath;
|
|
783
|
-
} else {
|
|
784
|
-
throw new Error(
|
|
785
|
-
`Template config not found in ${templatePath}.\n` +
|
|
786
|
-
'Expected: template.config.ts or template.config.js',
|
|
787
|
-
);
|
|
1253
|
+
if (verbose && child.pid) {
|
|
1254
|
+
console.log(`Process started with PID: ${child.pid}`);
|
|
788
1255
|
}
|
|
789
1256
|
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports, security/detect-non-literal-require -- Sucrase handles .ts files at runtime, path is validated above
|
|
793
|
-
const config = require(configPath);
|
|
794
|
-
|
|
795
|
-
logger.verbose('Template config loaded successfully');
|
|
796
|
-
|
|
797
|
-
return config.default || config;
|
|
798
|
-
};
|
|
799
|
-
|
|
800
|
-
/**
|
|
801
|
-
* 加载模板列表配置
|
|
802
|
-
*
|
|
803
|
-
* @param configPath - templates.json 配置文件路径
|
|
804
|
-
* @returns 模板列表配置
|
|
805
|
-
*/
|
|
806
|
-
const loadTemplatesConfig = async (
|
|
807
|
-
configPath,
|
|
808
|
-
) => {
|
|
809
|
-
logger.verbose(`Loading templates config from: ${configPath}`);
|
|
810
|
-
|
|
811
|
-
const content = await fs$1.readFile(configPath, 'utf-8');
|
|
812
|
-
// eslint-disable-next-line no-restricted-syntax -- Static config file loaded at build time, safeJsonParse not needed
|
|
813
|
-
const config = JSON.parse(content) ;
|
|
814
|
-
|
|
815
|
-
logger.verbose(
|
|
816
|
-
`Found ${config.templates.length} templates: ${config.templates.map(t => t.name).join(', ')}`,
|
|
817
|
-
);
|
|
818
|
-
|
|
819
|
-
return config;
|
|
820
|
-
};
|
|
1257
|
+
return child.pid;
|
|
1258
|
+
}
|
|
821
1259
|
|
|
822
1260
|
/**
|
|
823
|
-
*
|
|
824
|
-
*
|
|
825
|
-
* @param templatesConfig - 模板列表配置
|
|
826
|
-
* @param templateName - 模板名称
|
|
827
|
-
* @returns 模板元信息
|
|
1261
|
+
* 创建 AJV 验证器实例
|
|
828
1262
|
*/
|
|
829
|
-
const
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
const availableTemplates = templatesConfig.templates
|
|
837
|
-
.map(t => t.name)
|
|
838
|
-
.join(', ');
|
|
839
|
-
throw new Error(
|
|
840
|
-
`Template "${templateName}" not found.\n` +
|
|
841
|
-
`Available templates: ${availableTemplates}\n` +
|
|
842
|
-
'Use --template <name> to specify a template.',
|
|
843
|
-
);
|
|
844
|
-
}
|
|
1263
|
+
const createAjvInstance = () => {
|
|
1264
|
+
const ajv = new Ajv({
|
|
1265
|
+
useDefaults: true, // 自动应用默认值
|
|
1266
|
+
coerceTypes: true, // 类型强制转换
|
|
1267
|
+
removeAdditional: false, // 保留额外属性以便后续报错
|
|
1268
|
+
allErrors: true, // 收集所有错误
|
|
1269
|
+
});
|
|
845
1270
|
|
|
846
|
-
|
|
1271
|
+
addFormats(ajv);
|
|
1272
|
+
return ajv;
|
|
847
1273
|
};
|
|
848
1274
|
|
|
849
1275
|
/**
|
|
850
|
-
*
|
|
1276
|
+
* 验证参数
|
|
851
1277
|
*
|
|
852
|
-
* @param
|
|
853
|
-
* @param
|
|
854
|
-
* @returns
|
|
1278
|
+
* @param schema - JSON Schema 定义
|
|
1279
|
+
* @param params - 待验证的参数
|
|
1280
|
+
* @returns 验证后的参数(应用了默认值)
|
|
1281
|
+
* @throws 验证失败时抛出错误
|
|
855
1282
|
*/
|
|
856
|
-
const
|
|
857
|
-
|
|
858
|
-
|
|
1283
|
+
const validateParams = (
|
|
1284
|
+
schema,
|
|
1285
|
+
params,
|
|
859
1286
|
) => {
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
logger.verbose(` - Template location: ${templateMetadata.location}`);
|
|
1287
|
+
const ajv = createAjvInstance();
|
|
1288
|
+
const validate = ajv.compile(schema);
|
|
863
1289
|
|
|
864
|
-
|
|
865
|
-
const templatePath = path.join(basePath, templateMetadata.location);
|
|
1290
|
+
const isValid = validate(params);
|
|
866
1291
|
|
|
867
|
-
|
|
1292
|
+
if (!isValid) {
|
|
1293
|
+
const errors = validate.errors || [];
|
|
1294
|
+
const errorMessages = errors.map(err => {
|
|
1295
|
+
const path = err.instancePath || '/';
|
|
1296
|
+
const message = err.message || 'validation failed';
|
|
1297
|
+
return ` - ${path}: ${message}`;
|
|
1298
|
+
});
|
|
868
1299
|
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
// eslint-disable-next-line @coze-arch/use-error-in-catch -- Error handling done in throw statement
|
|
873
|
-
} catch (e) {
|
|
874
|
-
logger.error(' - Template directory does not exist: ✗');
|
|
875
|
-
throw new Error(`Template directory not found: ${templatePath}`);
|
|
1300
|
+
throw new Error(
|
|
1301
|
+
`Parameter validation failed:\n${errorMessages.join('\n')}\n\nPlease check your parameters and try again.`,
|
|
1302
|
+
);
|
|
876
1303
|
}
|
|
877
1304
|
|
|
878
|
-
return
|
|
1305
|
+
return params ;
|
|
879
1306
|
};
|
|
880
1307
|
|
|
881
1308
|
/**
|
|
882
1309
|
* 从 Commander 解析透传参数
|
|
883
1310
|
* 将 kebab-case 的 CLI 参数转换为 camelCase
|
|
884
1311
|
*
|
|
1312
|
+
* 使用 minimist 解析 process.argv,自动处理类型转换
|
|
1313
|
+
*
|
|
885
1314
|
* @param command - Commander 命令实例
|
|
886
1315
|
* @param knownOptions - 已知的选项集合(不需要透传的选项)
|
|
887
1316
|
* @returns 参数对象
|
|
@@ -890,20 +1319,34 @@ const parsePassThroughParams = (
|
|
|
890
1319
|
command,
|
|
891
1320
|
knownOptions = new Set(),
|
|
892
1321
|
) => {
|
|
893
|
-
|
|
1322
|
+
// 使用 minimist 解析所有参数
|
|
1323
|
+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers -- slice(2) to skip node and script path
|
|
1324
|
+
const parsed = minimist(process.argv.slice(2));
|
|
894
1325
|
|
|
895
|
-
|
|
896
|
-
|
|
1326
|
+
// 过滤掉已知选项和位置参数(_)
|
|
1327
|
+
const filtered = Object.entries(parsed).reduce(
|
|
897
1328
|
(params, [key, value]) => {
|
|
898
|
-
|
|
1329
|
+
// 跳过 minimist 的位置参数数组
|
|
1330
|
+
if (key === '_') {
|
|
1331
|
+
return params;
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
// 跳过已知选项(支持原始格式和 camelCase 格式)
|
|
1335
|
+
if (knownOptions.has(key) || knownOptions.has(changeCase.camelCase(key))) {
|
|
899
1336
|
return params;
|
|
900
1337
|
}
|
|
901
1338
|
|
|
1339
|
+
// 将 kebab-case 转换为 camelCase
|
|
902
1340
|
const camelKey = changeCase.camelCase(key);
|
|
903
|
-
|
|
1341
|
+
// eslint-disable-next-line security/detect-object-injection -- camelKey is sanitized by camelCase
|
|
1342
|
+
params[camelKey] = value;
|
|
1343
|
+
|
|
1344
|
+
return params;
|
|
904
1345
|
},
|
|
905
|
-
|
|
1346
|
+
{},
|
|
906
1347
|
);
|
|
1348
|
+
|
|
1349
|
+
return filtered;
|
|
907
1350
|
};
|
|
908
1351
|
|
|
909
1352
|
/**
|
|
@@ -998,6 +1441,11 @@ const shouldIgnoreFile = (filePath) => {
|
|
|
998
1441
|
return directoryPatterns.some(dir => pathParts.includes(dir));
|
|
999
1442
|
};
|
|
1000
1443
|
|
|
1444
|
+
// ABOUTME: File system utilities for template file processing
|
|
1445
|
+
// ABOUTME: Provides directory scanning, file path conversion, and node_modules copying
|
|
1446
|
+
|
|
1447
|
+
|
|
1448
|
+
// start_aigc
|
|
1001
1449
|
/**
|
|
1002
1450
|
* 递归获取目录中的所有文件
|
|
1003
1451
|
*
|
|
@@ -1067,144 +1515,346 @@ const convertDotfileName = (filePath) => {
|
|
|
1067
1515
|
|
|
1068
1516
|
return filePath;
|
|
1069
1517
|
};
|
|
1518
|
+
// end_aigc
|
|
1519
|
+
|
|
1520
|
+
// ABOUTME: File rendering utilities for template processing
|
|
1521
|
+
// ABOUTME: Handles file content rendering, hook execution, and file writing
|
|
1522
|
+
|
|
1523
|
+
|
|
1524
|
+
|
|
1070
1525
|
|
|
1526
|
+
|
|
1527
|
+
|
|
1528
|
+
|
|
1529
|
+
|
|
1530
|
+
// start_aigc
|
|
1071
1531
|
/**
|
|
1072
|
-
*
|
|
1532
|
+
* 执行文件渲染钩子
|
|
1073
1533
|
*
|
|
1074
|
-
* @param
|
|
1075
|
-
* @param
|
|
1534
|
+
* @param templateConfig - 模板配置
|
|
1535
|
+
* @param fileInfo - 文件渲染信息
|
|
1076
1536
|
* @param context - 模板上下文
|
|
1537
|
+
* @returns 处理后的文件信息,或 null 表示跳过该文件
|
|
1077
1538
|
*/
|
|
1078
|
-
const
|
|
1079
|
-
|
|
1080
|
-
|
|
1539
|
+
const executeFileRenderHook = async (
|
|
1540
|
+
templateConfig,
|
|
1541
|
+
fileInfo,
|
|
1081
1542
|
context,
|
|
1082
1543
|
) => {
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
logger.verbose(` - Output path: ${outputPath}`);
|
|
1086
|
-
|
|
1087
|
-
// 验证模板目录是否存在
|
|
1088
|
-
try {
|
|
1089
|
-
const stat = await fs$1.stat(templatePath);
|
|
1090
|
-
logger.verbose(
|
|
1091
|
-
` - Template path exists: ${stat.isDirectory() ? 'directory' : 'file'}`,
|
|
1092
|
-
);
|
|
1093
|
-
if (!stat.isDirectory()) {
|
|
1094
|
-
throw new Error(`Template path is not a directory: ${templatePath}`);
|
|
1095
|
-
}
|
|
1096
|
-
} catch (error) {
|
|
1097
|
-
logger.error(
|
|
1098
|
-
` - Failed to access template path: ${error instanceof Error ? error.message : String(error)}`,
|
|
1099
|
-
);
|
|
1100
|
-
throw error;
|
|
1544
|
+
if (!templateConfig.onFileRender) {
|
|
1545
|
+
return fileInfo;
|
|
1101
1546
|
}
|
|
1102
1547
|
|
|
1103
|
-
const
|
|
1548
|
+
const result = await templateConfig.onFileRender(
|
|
1549
|
+
fileInfo,
|
|
1550
|
+
context,
|
|
1551
|
+
);
|
|
1104
1552
|
|
|
1105
|
-
|
|
1553
|
+
// false: 跳过文件
|
|
1554
|
+
if (result === false) {
|
|
1555
|
+
return null;
|
|
1556
|
+
}
|
|
1106
1557
|
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
return;
|
|
1558
|
+
// undefined/void: 使用默认内容
|
|
1559
|
+
if (result === undefined || result === null) {
|
|
1560
|
+
return fileInfo;
|
|
1110
1561
|
}
|
|
1111
1562
|
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1563
|
+
// string: 作为 content,其他不变
|
|
1564
|
+
if (typeof result === 'string') {
|
|
1565
|
+
return {
|
|
1566
|
+
...fileInfo,
|
|
1567
|
+
content: result,
|
|
1568
|
+
};
|
|
1569
|
+
}
|
|
1117
1570
|
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1571
|
+
// FileRenderInfo: 使用新对象的信息
|
|
1572
|
+
return result;
|
|
1573
|
+
};
|
|
1574
|
+
|
|
1575
|
+
/**
|
|
1576
|
+
* 准备单个文件的渲染信息(不实际写入)
|
|
1577
|
+
* 用于 dry-run 阶段,收集所有将要写入的文件信息
|
|
1578
|
+
*
|
|
1579
|
+
* @param options - 准备选项
|
|
1580
|
+
* @returns 文件渲染信息,或 null 表示该文件被跳过
|
|
1581
|
+
*/
|
|
1582
|
+
const prepareFileInfo = async (options
|
|
1121
1583
|
|
|
1122
|
-
// 确保目标目录存在
|
|
1123
|
-
await ensureDir(path.dirname(destPath));
|
|
1124
1584
|
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
}),
|
|
1585
|
+
|
|
1586
|
+
|
|
1587
|
+
) => {
|
|
1588
|
+
const { file, templatePath, context, templateConfig } = options;
|
|
1589
|
+
|
|
1590
|
+
const srcPath = path.join(templatePath, file);
|
|
1591
|
+
const destFile = convertDotfileName(file);
|
|
1592
|
+
|
|
1593
|
+
logger.verbose(
|
|
1594
|
+
` - Preparing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
|
|
1136
1595
|
);
|
|
1137
1596
|
|
|
1138
|
-
|
|
1597
|
+
// 判断是否为二进制文件
|
|
1598
|
+
const isBinary = !shouldRenderFile(srcPath);
|
|
1599
|
+
let content;
|
|
1600
|
+
let wasRendered = false;
|
|
1139
1601
|
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1602
|
+
if (isBinary) {
|
|
1603
|
+
// 二进制文件,读取为 buffer 然后转为 base64
|
|
1604
|
+
const buffer = await fs$1.readFile(srcPath);
|
|
1605
|
+
content = buffer.toString('base64');
|
|
1606
|
+
} else {
|
|
1607
|
+
// 文本文件,渲染后的内容
|
|
1608
|
+
content = await renderTemplate(srcPath, context);
|
|
1609
|
+
wasRendered = true;
|
|
1610
|
+
}
|
|
1143
1611
|
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1612
|
+
// 构造文件信息对象
|
|
1613
|
+
const fileInfo = {
|
|
1614
|
+
path: file,
|
|
1615
|
+
destPath: destFile,
|
|
1616
|
+
content,
|
|
1617
|
+
isBinary,
|
|
1618
|
+
wasRendered,
|
|
1619
|
+
};
|
|
1148
1620
|
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1621
|
+
// 执行文件渲染钩子
|
|
1622
|
+
const processedFileInfo = await executeFileRenderHook(
|
|
1623
|
+
templateConfig,
|
|
1624
|
+
fileInfo,
|
|
1625
|
+
context,
|
|
1626
|
+
);
|
|
1152
1627
|
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1628
|
+
// 如果返回 null,表示该文件被 hook 跳过
|
|
1629
|
+
if (processedFileInfo === null) {
|
|
1630
|
+
logger.verbose(' ⊘ Skipped by onFileRender hook');
|
|
1631
|
+
return null;
|
|
1632
|
+
}
|
|
1156
1633
|
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
}
|
|
1634
|
+
return processedFileInfo;
|
|
1635
|
+
};
|
|
1160
1636
|
|
|
1161
|
-
|
|
1162
|
-
|
|
1637
|
+
/**
|
|
1638
|
+
* 写入渲染后的文件到目标路径
|
|
1639
|
+
*
|
|
1640
|
+
* @param options - 写入选项
|
|
1641
|
+
*/
|
|
1642
|
+
const writeRenderedFile = async (options
|
|
1643
|
+
|
|
1644
|
+
|
|
1645
|
+
|
|
1646
|
+
) => {
|
|
1647
|
+
const { fileInfo, srcPath, destPath } = options;
|
|
1648
|
+
|
|
1649
|
+
logger.verbose(` - Writing: ${fileInfo.destPath}`);
|
|
1650
|
+
|
|
1651
|
+
// 确保目标目录存在
|
|
1652
|
+
await ensureDir(path.dirname(destPath));
|
|
1653
|
+
|
|
1654
|
+
// 写入文件
|
|
1655
|
+
if (fileInfo.isBinary) {
|
|
1656
|
+
// 二进制文件:如果内容是原始 base64(未被 hook 修改),直接复制;否则从 base64 解码写入
|
|
1657
|
+
const buffer = await fs$1.readFile(srcPath);
|
|
1658
|
+
const originalContent = buffer.toString('base64');
|
|
1659
|
+
|
|
1660
|
+
if (fileInfo.content === originalContent) {
|
|
1661
|
+
await fs$1.copyFile(srcPath, destPath);
|
|
1662
|
+
logger.verbose(' ✓ Copied (binary)');
|
|
1163
1663
|
} else {
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
);
|
|
1167
|
-
logger.info('Will need to run pnpm install manually');
|
|
1664
|
+
const modifiedBuffer = Buffer.from(fileInfo.content, 'base64');
|
|
1665
|
+
await fs$1.writeFile(destPath, modifiedBuffer);
|
|
1666
|
+
logger.verbose(' ✓ Written (binary, modified by hook)');
|
|
1168
1667
|
}
|
|
1668
|
+
} else {
|
|
1669
|
+
// 文本文件
|
|
1670
|
+
await fs$1.writeFile(destPath, fileInfo.content, 'utf-8');
|
|
1671
|
+
logger.verbose(' ✓ Rendered and written');
|
|
1169
1672
|
}
|
|
1170
1673
|
};
|
|
1674
|
+
// end_aigc
|
|
1675
|
+
|
|
1676
|
+
// ABOUTME: File conflict detection utilities for template processing
|
|
1677
|
+
// ABOUTME: Provides dry-run file collection and conflict checking
|
|
1171
1678
|
|
|
1679
|
+
|
|
1680
|
+
|
|
1681
|
+
|
|
1682
|
+
|
|
1683
|
+
|
|
1684
|
+
|
|
1685
|
+
// start_aigc
|
|
1172
1686
|
/**
|
|
1173
|
-
*
|
|
1174
|
-
* 注意:.git 目录会被忽略,允许在已初始化 git 的目录中创建项目
|
|
1687
|
+
* 收集所有将要写入的文件路径(dry-run 阶段)
|
|
1175
1688
|
*
|
|
1176
|
-
* @param
|
|
1177
|
-
* @returns
|
|
1689
|
+
* @param options - 收集选项
|
|
1690
|
+
* @returns 将要写入的文件路径列表
|
|
1178
1691
|
*/
|
|
1179
|
-
const
|
|
1180
|
-
|
|
1692
|
+
const collectFilesToRender = async (options
|
|
1693
|
+
|
|
1694
|
+
|
|
1695
|
+
|
|
1696
|
+
|
|
1181
1697
|
) => {
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1698
|
+
const { files, templatePath, context, templateConfig } = options;
|
|
1699
|
+
|
|
1700
|
+
logger.verbose('\nDry-run: Collecting files to render...');
|
|
1701
|
+
|
|
1702
|
+
const fileInfos = await Promise.all(
|
|
1703
|
+
files.map(file =>
|
|
1704
|
+
prepareFileInfo({
|
|
1705
|
+
file,
|
|
1706
|
+
templatePath,
|
|
1707
|
+
context,
|
|
1708
|
+
templateConfig,
|
|
1709
|
+
}),
|
|
1710
|
+
),
|
|
1711
|
+
);
|
|
1712
|
+
|
|
1713
|
+
// 过滤掉被 hook 跳过的文件,收集 destPath
|
|
1714
|
+
const filesToWrite = fileInfos
|
|
1715
|
+
.filter((info) => info !== null)
|
|
1716
|
+
.map(info => info.destPath);
|
|
1717
|
+
|
|
1718
|
+
logger.verbose(` - ${filesToWrite.length} files will be written`);
|
|
1719
|
+
logger.verbose(
|
|
1720
|
+
` - ${fileInfos.length - filesToWrite.length} files skipped by hooks`,
|
|
1721
|
+
);
|
|
1722
|
+
|
|
1723
|
+
return filesToWrite;
|
|
1724
|
+
};
|
|
1725
|
+
// end_aigc
|
|
1726
|
+
|
|
1727
|
+
// ABOUTME: Main file processing orchestration for template rendering
|
|
1728
|
+
// ABOUTME: Coordinates file system, rendering, and conflict detection layers
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
|
|
1732
|
+
// start_aigc
|
|
1733
|
+
/**
|
|
1734
|
+
* 处理单个文件(准备 + 写入)
|
|
1735
|
+
*
|
|
1736
|
+
* @param options - 处理选项
|
|
1737
|
+
*/
|
|
1738
|
+
const processSingleFile = async (options
|
|
1739
|
+
|
|
1740
|
+
|
|
1741
|
+
|
|
1742
|
+
|
|
1743
|
+
|
|
1744
|
+
) => {
|
|
1745
|
+
const { file, templatePath, outputPath, context, templateConfig } = options;
|
|
1746
|
+
|
|
1747
|
+
const srcPath = path.join(templatePath, file);
|
|
1748
|
+
|
|
1749
|
+
// 准备文件信息
|
|
1750
|
+
const processedFileInfo = await prepareFileInfo({
|
|
1751
|
+
file,
|
|
1752
|
+
templatePath,
|
|
1753
|
+
context,
|
|
1754
|
+
templateConfig,
|
|
1755
|
+
});
|
|
1756
|
+
|
|
1757
|
+
// 如果返回 null,跳过该文件
|
|
1758
|
+
if (processedFileInfo === null) {
|
|
1759
|
+
return;
|
|
1190
1760
|
}
|
|
1761
|
+
|
|
1762
|
+
// 使用处理后的目标路径
|
|
1763
|
+
const finalDestPath = path.join(outputPath, processedFileInfo.destPath);
|
|
1764
|
+
|
|
1765
|
+
// 写入文件
|
|
1766
|
+
await writeRenderedFile({
|
|
1767
|
+
fileInfo: processedFileInfo,
|
|
1768
|
+
srcPath,
|
|
1769
|
+
destPath: finalDestPath,
|
|
1770
|
+
});
|
|
1191
1771
|
};
|
|
1192
1772
|
|
|
1193
1773
|
/**
|
|
1194
|
-
*
|
|
1774
|
+
* 复制并处理模板文件到目标目录
|
|
1195
1775
|
*
|
|
1196
|
-
*
|
|
1197
|
-
*
|
|
1776
|
+
* 流程:
|
|
1777
|
+
* 1. 验证模板目录
|
|
1778
|
+
* 2. 扫描所有模板文件
|
|
1779
|
+
* 3. Dry-run:收集将要写入的文件列表(考虑 hooks 影响)
|
|
1780
|
+
* 4. 冲突检测:检查是否有文件会被覆盖(可通过 force 跳过)
|
|
1781
|
+
* 5. 实际写入:渲染并写入所有文件
|
|
1782
|
+
* 6. 复制 node_modules(如果存在)
|
|
1783
|
+
*
|
|
1784
|
+
* @param options - 处理选项
|
|
1198
1785
|
*/
|
|
1199
|
-
const
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1786
|
+
const processTemplateFiles = async (options
|
|
1787
|
+
|
|
1788
|
+
|
|
1789
|
+
|
|
1790
|
+
|
|
1791
|
+
|
|
1792
|
+
) => {
|
|
1793
|
+
const { templatePath, outputPath, context, templateConfig} = options;
|
|
1794
|
+
logger.verbose('Processing template files:');
|
|
1795
|
+
logger.verbose(` - Template path: ${templatePath}`);
|
|
1796
|
+
logger.verbose(` - Output path: ${outputPath}`);
|
|
1797
|
+
|
|
1798
|
+
// 阶段 0: 验证模板目录是否存在
|
|
1799
|
+
try {
|
|
1800
|
+
const stat = await fs$1.stat(templatePath);
|
|
1801
|
+
logger.verbose(
|
|
1802
|
+
` - Template path exists: ${stat.isDirectory() ? 'directory' : 'file'}`,
|
|
1803
|
+
);
|
|
1804
|
+
if (!stat.isDirectory()) {
|
|
1805
|
+
throw new Error(`Template path is not a directory: ${templatePath}`);
|
|
1806
|
+
}
|
|
1807
|
+
} catch (error) {
|
|
1808
|
+
logger.error(
|
|
1809
|
+
` - Failed to access template path: ${error instanceof Error ? error.message : String(error)}`,
|
|
1810
|
+
);
|
|
1811
|
+
throw error;
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
// 阶段 1: 扫描所有模板文件
|
|
1815
|
+
const files = await getAllFiles(templatePath);
|
|
1816
|
+
|
|
1817
|
+
logger.verbose(` - Found ${files.length} files to process`);
|
|
1818
|
+
|
|
1819
|
+
if (files.length === 0) {
|
|
1820
|
+
logger.warn(' - No files found in template directory!');
|
|
1821
|
+
return;
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
// 阶段 2: Dry-run - 收集所有将要写入的文件
|
|
1825
|
+
await collectFilesToRender({
|
|
1826
|
+
files,
|
|
1827
|
+
templatePath,
|
|
1828
|
+
context,
|
|
1829
|
+
templateConfig,
|
|
1830
|
+
});
|
|
1831
|
+
|
|
1832
|
+
// 阶段 3: 冲突检测(force 为 true 时跳过)
|
|
1833
|
+
{
|
|
1834
|
+
logger.verbose(
|
|
1835
|
+
' - Force mode enabled, skipping conflict detection. Existing files will be overwritten.',
|
|
1205
1836
|
);
|
|
1206
1837
|
}
|
|
1838
|
+
|
|
1839
|
+
// 阶段 4: 实际写入文件
|
|
1840
|
+
logger.verbose('\nWriting files...');
|
|
1841
|
+
await Promise.all(
|
|
1842
|
+
files.map(file =>
|
|
1843
|
+
processSingleFile({
|
|
1844
|
+
file,
|
|
1845
|
+
templatePath,
|
|
1846
|
+
outputPath,
|
|
1847
|
+
context,
|
|
1848
|
+
templateConfig,
|
|
1849
|
+
}),
|
|
1850
|
+
),
|
|
1851
|
+
);
|
|
1852
|
+
|
|
1853
|
+
logger.verbose('✓ All files processed successfully');
|
|
1854
|
+
|
|
1855
|
+
// node_modules 将由 pnpm install 处理(利用缓存和硬链接机制)
|
|
1207
1856
|
};
|
|
1857
|
+
// end_aigc
|
|
1208
1858
|
|
|
1209
1859
|
/**
|
|
1210
1860
|
* 模板引擎执行选项
|
|
@@ -1215,6 +1865,7 @@ const validateOutputDir = async (outputPath) => {
|
|
|
1215
1865
|
|
|
1216
1866
|
|
|
1217
1867
|
|
|
1868
|
+
|
|
1218
1869
|
/**
|
|
1219
1870
|
* 加载模板元数据和路径
|
|
1220
1871
|
*/
|
|
@@ -1284,22 +1935,47 @@ const executeAfterRenderHook = async (
|
|
|
1284
1935
|
}
|
|
1285
1936
|
};
|
|
1286
1937
|
|
|
1938
|
+
/**
|
|
1939
|
+
* 执行完成钩子
|
|
1940
|
+
*/
|
|
1941
|
+
const executeCompleteHook = async (
|
|
1942
|
+
templateConfig,
|
|
1943
|
+
context,
|
|
1944
|
+
outputPath,
|
|
1945
|
+
) => {
|
|
1946
|
+
if (templateConfig.onComplete) {
|
|
1947
|
+
await templateConfig.onComplete(context, outputPath);
|
|
1948
|
+
}
|
|
1949
|
+
};
|
|
1950
|
+
|
|
1287
1951
|
/**
|
|
1288
1952
|
* 准备输出目录
|
|
1289
1953
|
*/
|
|
1290
|
-
const prepareOutputDirectory =
|
|
1954
|
+
const prepareOutputDirectory = (outputPath) => {
|
|
1291
1955
|
const absolutePath = path.resolve(process.cwd(), outputPath);
|
|
1292
|
-
|
|
1956
|
+
// 不再在这里验证目录是否为空,冲突检测已移至 processTemplateFiles 中
|
|
1293
1957
|
return absolutePath;
|
|
1294
1958
|
};
|
|
1295
1959
|
|
|
1960
|
+
/**
|
|
1961
|
+
* 模板引擎执行结果
|
|
1962
|
+
*/
|
|
1963
|
+
|
|
1964
|
+
|
|
1965
|
+
|
|
1966
|
+
|
|
1967
|
+
|
|
1968
|
+
|
|
1969
|
+
|
|
1970
|
+
|
|
1971
|
+
|
|
1296
1972
|
/**
|
|
1297
1973
|
* 执行完整的模板渲染流程
|
|
1298
1974
|
*/
|
|
1299
1975
|
const execute = async (
|
|
1300
1976
|
options,
|
|
1301
1977
|
) => {
|
|
1302
|
-
const { templateName, outputPath, command
|
|
1978
|
+
const { templateName, outputPath, command} = options;
|
|
1303
1979
|
|
|
1304
1980
|
// 1. 加载模板
|
|
1305
1981
|
const { templatePath } = await loadTemplateMetadata(templateName);
|
|
@@ -1316,18 +1992,26 @@ const execute = async (
|
|
|
1316
1992
|
});
|
|
1317
1993
|
|
|
1318
1994
|
// 5. 准备输出目录
|
|
1319
|
-
const absoluteOutputPath =
|
|
1995
|
+
const absoluteOutputPath = prepareOutputDirectory(outputPath);
|
|
1320
1996
|
|
|
1321
1997
|
// 6. 处理模板文件
|
|
1322
|
-
await processTemplateFiles(
|
|
1998
|
+
await processTemplateFiles({
|
|
1999
|
+
templatePath,
|
|
2000
|
+
outputPath: absoluteOutputPath,
|
|
2001
|
+
context,
|
|
2002
|
+
templateConfig});
|
|
1323
2003
|
|
|
1324
2004
|
// 7. 执行 onAfterRender 钩子
|
|
1325
2005
|
await executeAfterRenderHook(templateConfig, context, absoluteOutputPath);
|
|
1326
2006
|
|
|
1327
|
-
return
|
|
2007
|
+
return {
|
|
2008
|
+
outputPath: absoluteOutputPath,
|
|
2009
|
+
templateConfig,
|
|
2010
|
+
context,
|
|
2011
|
+
};
|
|
1328
2012
|
};
|
|
1329
2013
|
|
|
1330
|
-
function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2014
|
+
function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
1331
2015
|
/**
|
|
1332
2016
|
* 运行 pnpm install
|
|
1333
2017
|
*/
|
|
@@ -1366,7 +2050,40 @@ const runPnpmInstall = (projectPath) => {
|
|
|
1366
2050
|
};
|
|
1367
2051
|
|
|
1368
2052
|
/**
|
|
1369
|
-
*
|
|
2053
|
+
* 运行 git 命令的辅助函数
|
|
2054
|
+
*/
|
|
2055
|
+
const runGitCommand = (command, projectPath) => {
|
|
2056
|
+
logger.info(`Executing: ${command}`);
|
|
2057
|
+
|
|
2058
|
+
const result = shelljs.exec(command, {
|
|
2059
|
+
cwd: projectPath,
|
|
2060
|
+
silent: true,
|
|
2061
|
+
});
|
|
2062
|
+
|
|
2063
|
+
// 输出命令的结果
|
|
2064
|
+
if (result.stdout) {
|
|
2065
|
+
process.stdout.write(result.stdout);
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
if (result.stderr) {
|
|
2069
|
+
process.stderr.write(result.stderr);
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
if (result.code !== 0) {
|
|
2073
|
+
const errorMessage = [
|
|
2074
|
+
`${command} failed with exit code ${result.code}`,
|
|
2075
|
+
result.stderr ? `\nStderr:\n${result.stderr}` : '',
|
|
2076
|
+
result.stdout ? `\nStdout:\n${result.stdout}` : '',
|
|
2077
|
+
]
|
|
2078
|
+
.filter(Boolean)
|
|
2079
|
+
.join('');
|
|
2080
|
+
|
|
2081
|
+
throw new Error(errorMessage);
|
|
2082
|
+
}
|
|
2083
|
+
};
|
|
2084
|
+
|
|
2085
|
+
/**
|
|
2086
|
+
* 初始化 git 仓库
|
|
1370
2087
|
* 如果目录中已存在 .git,则跳过初始化
|
|
1371
2088
|
*/
|
|
1372
2089
|
const runGitInit = (projectPath) => {
|
|
@@ -1379,46 +2096,9 @@ const runGitInit = (projectPath) => {
|
|
|
1379
2096
|
return;
|
|
1380
2097
|
}
|
|
1381
2098
|
|
|
1382
|
-
const runGitCommand = (command) => {
|
|
1383
|
-
logger.info(`Executing: ${command}`);
|
|
1384
|
-
|
|
1385
|
-
const result = shelljs.exec(command, {
|
|
1386
|
-
cwd: projectPath,
|
|
1387
|
-
silent: true,
|
|
1388
|
-
});
|
|
1389
|
-
|
|
1390
|
-
// 输出命令的结果
|
|
1391
|
-
if (result.stdout) {
|
|
1392
|
-
process.stdout.write(result.stdout);
|
|
1393
|
-
}
|
|
1394
|
-
|
|
1395
|
-
if (result.stderr) {
|
|
1396
|
-
process.stderr.write(result.stderr);
|
|
1397
|
-
}
|
|
1398
|
-
|
|
1399
|
-
if (result.code !== 0) {
|
|
1400
|
-
const errorMessage = [
|
|
1401
|
-
`${command} failed with exit code ${result.code}`,
|
|
1402
|
-
result.stderr ? `\nStderr:\n${result.stderr}` : '',
|
|
1403
|
-
result.stdout ? `\nStdout:\n${result.stdout}` : '',
|
|
1404
|
-
]
|
|
1405
|
-
.filter(Boolean)
|
|
1406
|
-
.join('');
|
|
1407
|
-
|
|
1408
|
-
throw new Error(errorMessage);
|
|
1409
|
-
}
|
|
1410
|
-
};
|
|
1411
|
-
|
|
1412
2099
|
try {
|
|
1413
2100
|
logger.info('\nInitializing git repository...');
|
|
1414
|
-
runGitCommand('git init');
|
|
1415
|
-
|
|
1416
|
-
logger.info('Adding files to git...');
|
|
1417
|
-
runGitCommand('git add .');
|
|
1418
|
-
|
|
1419
|
-
logger.info('Creating initial commit...');
|
|
1420
|
-
runGitCommand('git commit -m "chore: initial commit"');
|
|
1421
|
-
|
|
2101
|
+
runGitCommand('git init', projectPath);
|
|
1422
2102
|
logger.success('Git repository initialized successfully!');
|
|
1423
2103
|
} catch (error) {
|
|
1424
2104
|
// Git 初始化失败不应该导致整个流程失败
|
|
@@ -1430,45 +2110,62 @@ const runGitInit = (projectPath) => {
|
|
|
1430
2110
|
};
|
|
1431
2111
|
|
|
1432
2112
|
/**
|
|
1433
|
-
*
|
|
2113
|
+
* 提交初始化生成的所有文件
|
|
1434
2114
|
*/
|
|
1435
|
-
const
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
2115
|
+
const commitChanges = (projectPath) => {
|
|
2116
|
+
// 检查是否存在 .git 目录
|
|
2117
|
+
const gitDir = path.join(projectPath, '.git');
|
|
2118
|
+
if (!fs.existsSync(gitDir)) {
|
|
2119
|
+
logger.warn(
|
|
2120
|
+
'\n⚠️ Git repository does not exist, skipping commit. Run git init first.',
|
|
2121
|
+
);
|
|
2122
|
+
return;
|
|
2123
|
+
}
|
|
1439
2124
|
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
})
|
|
2125
|
+
try {
|
|
2126
|
+
logger.info('\nCommitting initialized files...');
|
|
2127
|
+
runGitCommand('git add --all', projectPath);
|
|
2128
|
+
runGitCommand('git commit -m "chore: init env"', projectPath);
|
|
2129
|
+
logger.success('Changes committed successfully!');
|
|
2130
|
+
} catch (error) {
|
|
2131
|
+
// Commit 失败不应该导致整个流程失败
|
|
2132
|
+
logger.warn(
|
|
2133
|
+
`Git commit failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
2134
|
+
);
|
|
2135
|
+
logger.info(
|
|
2136
|
+
'You can manually commit later with: git add --all && git commit -m "chore: init env"',
|
|
2137
|
+
);
|
|
2138
|
+
}
|
|
2139
|
+
};
|
|
1446
2140
|
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
2141
|
+
/**
|
|
2142
|
+
* 运行开发服务器(后台模式)
|
|
2143
|
+
* 启动后台子进程运行开发服务器,父进程可以直接退出
|
|
2144
|
+
* 使用 CLI 自己的 dev 命令(定义在 run.ts)而不是直接运行 npm run dev
|
|
2145
|
+
*/
|
|
2146
|
+
const runDev = (projectPath) => {
|
|
2147
|
+
logger.info('\nStarting development server in background...');
|
|
1452
2148
|
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
})]);
|
|
2149
|
+
// 获取当前 CLI 的可执行文件路径
|
|
2150
|
+
// process.argv[0] 是 node,process.argv[1] 是 CLI 入口文件
|
|
2151
|
+
const cliPath = process.argv[1];
|
|
1457
2152
|
|
|
1458
|
-
|
|
1459
|
-
child.on('error', (error) => {
|
|
1460
|
-
logger.error(`Failed to start dev server: ${error.message}`);
|
|
1461
|
-
logger.error(`Error stack: ${error.stack}`);
|
|
1462
|
-
});
|
|
2153
|
+
logger.info(`Executing: ${cliPath} dev in ${projectPath}`);
|
|
1463
2154
|
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
2155
|
+
// 使用通用的后台执行函数启动开发服务器
|
|
2156
|
+
// 调用 CLI 自己的 dev 命令
|
|
2157
|
+
const pid = spawnDetached(process.argv[0], [cliPath, 'dev'], {
|
|
2158
|
+
cwd: projectPath,
|
|
2159
|
+
verbose: false, // 不输出额外的进程信息,由 logger 统一处理
|
|
2160
|
+
});
|
|
2161
|
+
|
|
2162
|
+
logger.success('Development server started in background!');
|
|
2163
|
+
if (pid) {
|
|
2164
|
+
logger.info(`Process ID: ${pid}`);
|
|
2165
|
+
logger.info(
|
|
2166
|
+
'\nThe dev server is running independently. You can close this terminal.',
|
|
2167
|
+
);
|
|
2168
|
+
logger.info(`To stop the server later, use: kill ${pid}`);
|
|
1472
2169
|
}
|
|
1473
2170
|
};
|
|
1474
2171
|
|
|
@@ -1482,6 +2179,8 @@ const executeInit = async (
|
|
|
1482
2179
|
|
|
1483
2180
|
|
|
1484
2181
|
|
|
2182
|
+
|
|
2183
|
+
|
|
1485
2184
|
,
|
|
1486
2185
|
command,
|
|
1487
2186
|
) => {
|
|
@@ -1493,61 +2192,76 @@ const executeInit = async (
|
|
|
1493
2192
|
output: outputPath,
|
|
1494
2193
|
skipInstall,
|
|
1495
2194
|
skipGit,
|
|
2195
|
+
skipCommit,
|
|
1496
2196
|
skipDev,
|
|
2197
|
+
force,
|
|
1497
2198
|
} = options;
|
|
1498
2199
|
|
|
1499
2200
|
logger.info(`Initializing project with template: ${templateName}`);
|
|
1500
2201
|
timer.logPhase('Initialization');
|
|
1501
2202
|
|
|
1502
|
-
//
|
|
1503
|
-
const
|
|
2203
|
+
// 执行模板引擎,返回结果对象
|
|
2204
|
+
const result = await execute({
|
|
1504
2205
|
templateName,
|
|
1505
2206
|
outputPath,
|
|
1506
2207
|
command,
|
|
2208
|
+
force,
|
|
1507
2209
|
});
|
|
2210
|
+
const { outputPath: absoluteOutputPath, templateConfig, context } = result;
|
|
1508
2211
|
|
|
1509
2212
|
timer.logPhase('Template engine execution');
|
|
1510
2213
|
logger.success('Project created successfully!');
|
|
1511
2214
|
|
|
1512
|
-
//
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
const hasNodeModules = fs.existsSync(nodeModulesPath);
|
|
2215
|
+
// 检查是否存在 package.json
|
|
2216
|
+
const packageJsonPath = path.join(absoluteOutputPath, 'package.json');
|
|
2217
|
+
const hasPackageJson = fs.existsSync(packageJsonPath);
|
|
1516
2218
|
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
);
|
|
1521
|
-
timer.logPhase('Node modules (pre-warmed)');
|
|
1522
|
-
} else {
|
|
2219
|
+
// 安装依赖(始终使用 pnpm install,利用缓存机制)
|
|
2220
|
+
if (!skipInstall) {
|
|
2221
|
+
if (hasPackageJson) {
|
|
1523
2222
|
runPnpmInstall(absoluteOutputPath);
|
|
1524
2223
|
timer.logPhase('Dependencies installation');
|
|
2224
|
+
} else {
|
|
2225
|
+
logger.info(
|
|
2226
|
+
'\n💡 No package.json found, skipping dependency installation',
|
|
2227
|
+
);
|
|
1525
2228
|
}
|
|
1526
2229
|
}
|
|
1527
2230
|
|
|
2231
|
+
// 执行 onComplete 钩子(在 pnpm install 之后)
|
|
2232
|
+
await executeCompleteHook(templateConfig, context, absoluteOutputPath);
|
|
2233
|
+
timer.logPhase('Complete hook execution');
|
|
2234
|
+
|
|
1528
2235
|
// 如果没有跳过 git,则初始化 git 仓库
|
|
1529
2236
|
if (!skipGit) {
|
|
1530
2237
|
runGitInit(absoluteOutputPath);
|
|
1531
2238
|
timer.logPhase('Git initialization');
|
|
1532
2239
|
}
|
|
1533
2240
|
|
|
2241
|
+
// 如果没有跳过 commit,则提交初始化生成的文件
|
|
2242
|
+
if (!skipCommit) {
|
|
2243
|
+
commitChanges(absoluteOutputPath);
|
|
2244
|
+
timer.logPhase('Git commit');
|
|
2245
|
+
}
|
|
2246
|
+
|
|
1534
2247
|
// 如果没有跳过 dev,则启动开发服务器
|
|
1535
2248
|
if (!skipDev) {
|
|
1536
|
-
|
|
2249
|
+
runDev(absoluteOutputPath);
|
|
1537
2250
|
timer.logPhase('Dev server startup');
|
|
1538
2251
|
} else {
|
|
1539
2252
|
// 只有跳过 dev 时才显示 Next steps
|
|
1540
2253
|
logger.info('\nNext steps:');
|
|
1541
2254
|
logger.info(` cd ${outputPath}`);
|
|
1542
|
-
if (skipInstall) {
|
|
2255
|
+
if (skipInstall && hasPackageJson) {
|
|
1543
2256
|
logger.info(' pnpm install');
|
|
1544
2257
|
}
|
|
1545
2258
|
if (skipGit) {
|
|
1546
|
-
logger.info(
|
|
1547
|
-
|
|
1548
|
-
|
|
2259
|
+
logger.info(' git init');
|
|
2260
|
+
}
|
|
2261
|
+
if (skipCommit) {
|
|
2262
|
+
logger.info(' git add --all && git commit -m "chore: init env"');
|
|
1549
2263
|
}
|
|
1550
|
-
logger.info('
|
|
2264
|
+
logger.info(' coze dev');
|
|
1551
2265
|
}
|
|
1552
2266
|
|
|
1553
2267
|
// 输出总耗时
|
|
@@ -1563,7 +2277,7 @@ const executeInit = async (
|
|
|
1563
2277
|
/**
|
|
1564
2278
|
* 注册 init 命令到 program
|
|
1565
2279
|
*/
|
|
1566
|
-
const registerCommand = program => {
|
|
2280
|
+
const registerCommand$1 = program => {
|
|
1567
2281
|
program
|
|
1568
2282
|
.command('init')
|
|
1569
2283
|
.description('Initialize a new project from a template')
|
|
@@ -1572,23 +2286,328 @@ const registerCommand = program => {
|
|
|
1572
2286
|
.option('-o, --output <path>', 'Output directory', process.cwd())
|
|
1573
2287
|
.option('--skip-install', 'Skip automatic pnpm install', false)
|
|
1574
2288
|
.option('--skip-git', 'Skip automatic git initialization', false)
|
|
2289
|
+
.option(
|
|
2290
|
+
'--skip-commit',
|
|
2291
|
+
'Skip automatic git commit after initialization',
|
|
2292
|
+
false,
|
|
2293
|
+
)
|
|
1575
2294
|
.option('--skip-dev', 'Skip automatic dev server start', false)
|
|
1576
2295
|
.allowUnknownOption() // 允许透传参数
|
|
1577
2296
|
.action(async (directory, options, command) => {
|
|
1578
2297
|
// 位置参数优先级高于 --output 选项
|
|
1579
2298
|
const outputPath = _nullishCoalesce(directory, () => ( options.output));
|
|
1580
|
-
|
|
2299
|
+
// Always use force mode - overwrite existing files without conflict check
|
|
2300
|
+
const force = true;
|
|
2301
|
+
await executeInit({ ...options, output: outputPath, force }, command);
|
|
2302
|
+
});
|
|
2303
|
+
};
|
|
2304
|
+
|
|
2305
|
+
// ABOUTME: This file implements the update command for coze CLI
|
|
2306
|
+
// ABOUTME: It wraps pnpm update/install to update package dependencies with logging support
|
|
2307
|
+
|
|
2308
|
+
|
|
2309
|
+
|
|
2310
|
+
|
|
2311
|
+
/**
|
|
2312
|
+
* 日志文件名常量
|
|
2313
|
+
*/
|
|
2314
|
+
const LOG_FILE_NAME = 'update.log';
|
|
2315
|
+
|
|
2316
|
+
/**
|
|
2317
|
+
* 获取日志目录
|
|
2318
|
+
* 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
|
|
2319
|
+
*/
|
|
2320
|
+
const getLogDir = () =>
|
|
2321
|
+
process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
|
|
2322
|
+
|
|
2323
|
+
/**
|
|
2324
|
+
* 解析日志文件路径
|
|
2325
|
+
* - 如果是绝对路径,直接使用
|
|
2326
|
+
* - 如果是相对路径,基于 getLogDir() + 相对路径
|
|
2327
|
+
* - 如果为空,使用 getLogDir() + LOG_FILE_NAME
|
|
2328
|
+
*/
|
|
2329
|
+
const resolveLogFilePath = (logFile) => {
|
|
2330
|
+
if (!logFile) {
|
|
2331
|
+
return path.join(getLogDir(), LOG_FILE_NAME);
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
if (path.isAbsolute(logFile)) {
|
|
2335
|
+
return logFile;
|
|
2336
|
+
}
|
|
2337
|
+
|
|
2338
|
+
return path.join(getLogDir(), logFile);
|
|
2339
|
+
};
|
|
2340
|
+
|
|
2341
|
+
/**
|
|
2342
|
+
* 创建日志写入流
|
|
2343
|
+
*/
|
|
2344
|
+
const createLogStream = (logFilePath) => {
|
|
2345
|
+
const logDir = path.dirname(logFilePath);
|
|
2346
|
+
|
|
2347
|
+
// 确保日志目录存在
|
|
2348
|
+
if (!fs.existsSync(logDir)) {
|
|
2349
|
+
fs.mkdirSync(logDir, { recursive: true });
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
// 使用 'w' 标志覆盖之前的日志
|
|
2353
|
+
return fs.createWriteStream(logFilePath, { flags: 'w' });
|
|
2354
|
+
};
|
|
2355
|
+
|
|
2356
|
+
/**
|
|
2357
|
+
* 格式化时间戳
|
|
2358
|
+
*/
|
|
2359
|
+
const formatTimestamp = () => {
|
|
2360
|
+
const now = new Date();
|
|
2361
|
+
return now.toISOString();
|
|
2362
|
+
};
|
|
2363
|
+
|
|
2364
|
+
/**
|
|
2365
|
+
* 写入带时间戳的日志
|
|
2366
|
+
*/
|
|
2367
|
+
const writeLogWithTimestamp = (stream, message) => {
|
|
2368
|
+
const timestamp = formatTimestamp();
|
|
2369
|
+
const lines = message.split('\n');
|
|
2370
|
+
lines.forEach(line => {
|
|
2371
|
+
if (line) {
|
|
2372
|
+
stream.write(`[${timestamp}] ${line}\n`);
|
|
2373
|
+
} else {
|
|
2374
|
+
stream.write('\n');
|
|
2375
|
+
}
|
|
2376
|
+
});
|
|
2377
|
+
// 确保数据写入磁盘
|
|
2378
|
+
stream.uncork();
|
|
2379
|
+
};
|
|
2380
|
+
|
|
2381
|
+
/**
|
|
2382
|
+
* 同时输出到控制台和日志文件
|
|
2383
|
+
*/
|
|
2384
|
+
const logWithFile = (
|
|
2385
|
+
stream,
|
|
2386
|
+
level,
|
|
2387
|
+
message,
|
|
2388
|
+
) => {
|
|
2389
|
+
// 输出到控制台
|
|
2390
|
+
switch (level) {
|
|
2391
|
+
case 'info':
|
|
2392
|
+
logger.info(message);
|
|
2393
|
+
break;
|
|
2394
|
+
case 'success':
|
|
2395
|
+
logger.success(message);
|
|
2396
|
+
break;
|
|
2397
|
+
case 'error':
|
|
2398
|
+
logger.error(message);
|
|
2399
|
+
break;
|
|
2400
|
+
default:
|
|
2401
|
+
logger.info(message);
|
|
2402
|
+
break;
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
// 写入日志文件(带时间戳)
|
|
2406
|
+
writeLogWithTimestamp(stream, `[${level.toUpperCase()}] ${message}`);
|
|
2407
|
+
};
|
|
2408
|
+
|
|
2409
|
+
// start_aigc
|
|
2410
|
+
/**
|
|
2411
|
+
* 构建 pnpm add 命令
|
|
2412
|
+
*/
|
|
2413
|
+
const buildPnpmCommand = (
|
|
2414
|
+
packageName,
|
|
2415
|
+
options
|
|
2416
|
+
|
|
2417
|
+
|
|
2418
|
+
|
|
2419
|
+
|
|
2420
|
+
,
|
|
2421
|
+
) => {
|
|
2422
|
+
const { global, version, registry, extraArgs } = options;
|
|
2423
|
+
|
|
2424
|
+
const parts = ['pnpm', 'add'];
|
|
2425
|
+
|
|
2426
|
+
// 添加全局标记
|
|
2427
|
+
if (global) {
|
|
2428
|
+
parts.push('-g');
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
// 添加包名和版本
|
|
2432
|
+
if (version && version !== 'latest') {
|
|
2433
|
+
parts.push(`${packageName}@${version}`);
|
|
2434
|
+
} else {
|
|
2435
|
+
parts.push(`${packageName}@latest`);
|
|
2436
|
+
}
|
|
2437
|
+
|
|
2438
|
+
// 添加 registry
|
|
2439
|
+
if (registry) {
|
|
2440
|
+
parts.push(`--registry=${registry}`);
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
// 添加额外参数
|
|
2444
|
+
if (extraArgs.length > 0) {
|
|
2445
|
+
parts.push(...extraArgs);
|
|
2446
|
+
}
|
|
2447
|
+
|
|
2448
|
+
return parts.join(' ');
|
|
2449
|
+
};
|
|
2450
|
+
// end_aigc
|
|
2451
|
+
|
|
2452
|
+
// start_aigc
|
|
2453
|
+
/**
|
|
2454
|
+
* 执行 update 命令的内部实现
|
|
2455
|
+
*/
|
|
2456
|
+
const executeUpdate = (
|
|
2457
|
+
packageName,
|
|
2458
|
+
options
|
|
2459
|
+
|
|
2460
|
+
|
|
2461
|
+
|
|
2462
|
+
|
|
2463
|
+
|
|
2464
|
+
|
|
2465
|
+
,
|
|
2466
|
+
) => {
|
|
2467
|
+
let logStream = null;
|
|
2468
|
+
|
|
2469
|
+
try {
|
|
2470
|
+
const { global, cwd, version, registry, logFile, extraArgs } = options;
|
|
2471
|
+
|
|
2472
|
+
// 准备日志
|
|
2473
|
+
const logFilePath = resolveLogFilePath(logFile);
|
|
2474
|
+
|
|
2475
|
+
// 调试:确认日志路径
|
|
2476
|
+
logger.info(`Log file path resolved to: ${logFilePath}`);
|
|
2477
|
+
|
|
2478
|
+
logStream = createLogStream(logFilePath);
|
|
2479
|
+
|
|
2480
|
+
// 调试:确认流已创建
|
|
2481
|
+
logger.info('Log stream created successfully');
|
|
2482
|
+
|
|
2483
|
+
logWithFile(logStream, 'info', `Updating package: ${packageName}`);
|
|
2484
|
+
|
|
2485
|
+
// 构建命令
|
|
2486
|
+
const command = buildPnpmCommand(packageName, {
|
|
2487
|
+
global,
|
|
2488
|
+
version,
|
|
2489
|
+
registry,
|
|
2490
|
+
extraArgs,
|
|
2491
|
+
});
|
|
2492
|
+
|
|
2493
|
+
// 确定工作目录
|
|
2494
|
+
const workingDir = cwd
|
|
2495
|
+
? path.isAbsolute(cwd)
|
|
2496
|
+
? cwd
|
|
2497
|
+
: path.join(process.cwd(), cwd)
|
|
2498
|
+
: process.cwd();
|
|
2499
|
+
|
|
2500
|
+
logWithFile(logStream, 'info', `Executing: ${command}`);
|
|
2501
|
+
logWithFile(logStream, 'info', `Working directory: ${workingDir}`);
|
|
2502
|
+
logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
|
|
2503
|
+
|
|
2504
|
+
// 记录命令开始时间
|
|
2505
|
+
writeLogWithTimestamp(logStream, '--- Command execution started ---');
|
|
2506
|
+
|
|
2507
|
+
// 同步执行命令
|
|
2508
|
+
const result = shelljs.exec(command, {
|
|
2509
|
+
cwd: workingDir,
|
|
2510
|
+
silent: true, // 使用 silent 来捕获输出
|
|
2511
|
+
});
|
|
2512
|
+
|
|
2513
|
+
// 将输出写入控制台和日志文件(带时间戳)
|
|
2514
|
+
if (result.stdout) {
|
|
2515
|
+
process.stdout.write(result.stdout);
|
|
2516
|
+
writeLogWithTimestamp(logStream, result.stdout.trim());
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2519
|
+
if (result.stderr) {
|
|
2520
|
+
process.stderr.write(result.stderr);
|
|
2521
|
+
writeLogWithTimestamp(logStream, result.stderr.trim());
|
|
2522
|
+
}
|
|
2523
|
+
|
|
2524
|
+
// 记录命令结束时间
|
|
2525
|
+
writeLogWithTimestamp(logStream, '--- Command execution ended ---');
|
|
2526
|
+
|
|
2527
|
+
// 检查执行结果并记录到日志
|
|
2528
|
+
if (result.code === 0) {
|
|
2529
|
+
logWithFile(logStream, 'success', 'Package updated successfully');
|
|
2530
|
+
logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
|
|
2531
|
+
} else {
|
|
2532
|
+
logWithFile(
|
|
2533
|
+
logStream,
|
|
2534
|
+
'error',
|
|
2535
|
+
`Command exited with code ${result.code}`,
|
|
2536
|
+
);
|
|
2537
|
+
logWithFile(
|
|
2538
|
+
logStream,
|
|
2539
|
+
'error',
|
|
2540
|
+
`Check log file for details: ${logFilePath}`,
|
|
2541
|
+
);
|
|
2542
|
+
}
|
|
2543
|
+
|
|
2544
|
+
// 关闭日志流并等待写入完成
|
|
2545
|
+
logStream.end(() => {
|
|
2546
|
+
// 流关闭后再退出进程
|
|
2547
|
+
if (result.code !== 0) {
|
|
2548
|
+
process.exit(result.code || 1);
|
|
2549
|
+
}
|
|
2550
|
+
});
|
|
2551
|
+
} catch (error) {
|
|
2552
|
+
logger.error('Failed to update package:');
|
|
2553
|
+
logger.error(error instanceof Error ? error.message : String(error));
|
|
2554
|
+
|
|
2555
|
+
// 写入错误到日志文件
|
|
2556
|
+
if (logStream) {
|
|
2557
|
+
writeLogWithTimestamp(
|
|
2558
|
+
logStream,
|
|
2559
|
+
`[ERROR] ${error instanceof Error ? error.message : String(error)}`,
|
|
2560
|
+
);
|
|
2561
|
+
// 等待流关闭后再退出
|
|
2562
|
+
logStream.end(() => {
|
|
2563
|
+
process.exit(1);
|
|
2564
|
+
});
|
|
2565
|
+
} else {
|
|
2566
|
+
process.exit(1);
|
|
2567
|
+
}
|
|
2568
|
+
}
|
|
2569
|
+
};
|
|
2570
|
+
// end_aigc
|
|
2571
|
+
|
|
2572
|
+
/**
|
|
2573
|
+
* 注册 update 命令到 program
|
|
2574
|
+
*/
|
|
2575
|
+
const registerCommand = program => {
|
|
2576
|
+
program
|
|
2577
|
+
.command('update <package>')
|
|
2578
|
+
.description('Update a package dependency')
|
|
2579
|
+
.option('-g, --global', 'Update package globally', false)
|
|
2580
|
+
.option('-c, --cwd <path>', 'Working directory for the update')
|
|
2581
|
+
.option(
|
|
2582
|
+
'--to <version>',
|
|
2583
|
+
'Version to update to (default: latest)',
|
|
2584
|
+
'latest',
|
|
2585
|
+
)
|
|
2586
|
+
.option('--registry <url>', 'Registry URL to use for the update')
|
|
2587
|
+
.option('--log-file <path>', 'Log file path')
|
|
2588
|
+
.allowUnknownOption() // 允许透传参数给 pnpm
|
|
2589
|
+
.action((packageName, options, command) => {
|
|
2590
|
+
// 收集所有未知选项作为额外参数
|
|
2591
|
+
const extraArgs = command.args.slice(1);
|
|
2592
|
+
|
|
2593
|
+
executeUpdate(packageName, {
|
|
2594
|
+
...options,
|
|
2595
|
+
version: options.to, // 将 --to 映射到 version
|
|
2596
|
+
extraArgs,
|
|
2597
|
+
});
|
|
1581
2598
|
});
|
|
1582
2599
|
};
|
|
1583
2600
|
|
|
1584
|
-
var version = "0.0.
|
|
2601
|
+
var version = "0.0.3";
|
|
1585
2602
|
var packageJson = {
|
|
1586
2603
|
version: version};
|
|
1587
2604
|
|
|
1588
2605
|
const commands = [
|
|
1589
|
-
registerCommand,
|
|
1590
2606
|
registerCommand$1,
|
|
1591
|
-
|
|
2607
|
+
registerCommand$2,
|
|
2608
|
+
registerCommand$4,
|
|
2609
|
+
registerCommand$3,
|
|
2610
|
+
registerCommand,
|
|
1592
2611
|
];
|
|
1593
2612
|
|
|
1594
2613
|
const main = () => {
|