@coze-arch/cli 0.0.1-alpha.f74941 → 0.0.1-alpha.f91253
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/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 +220 -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 +45 -0
- package/lib/__templates__/expo/README.md +68 -7
- package/lib/__templates__/expo/_gitignore +1 -1
- package/lib/__templates__/expo/_npmrc +3 -5
- package/lib/__templates__/expo/client/app/_layout.tsx +14 -14
- 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 +1 -17
- package/lib/__templates__/expo/client/components/ThemedText.tsx +33 -0
- package/lib/__templates__/expo/client/components/ThemedView.tsx +38 -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} +14 -10
- package/lib/__templates__/expo/client/hooks/useColorScheme.ts +34 -1
- package/lib/__templates__/expo/client/hooks/useTheme.ts +26 -6
- package/lib/__templates__/expo/client/metro.config.js +121 -0
- package/lib/__templates__/expo/client/package.json +93 -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 +37 -12
- 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 +2486 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/rule.js +155 -0
- package/lib/__templates__/expo/package.json +13 -92
- package/lib/__templates__/expo/pnpm-lock.yaml +675 -678
- 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 +32 -0
- package/lib/__templates__/expo/server/src/index.ts +19 -0
- package/lib/__templates__/expo/server/tsconfig.json +24 -0
- package/lib/__templates__/expo/template.config.js +2 -1
- package/lib/__templates__/expo/tsconfig.json +1 -24
- package/lib/__templates__/nextjs/.babelrc +15 -0
- package/lib/__templates__/nextjs/.coze +4 -3
- package/lib/__templates__/nextjs/README.md +341 -19
- package/lib/__templates__/nextjs/_npmrc +2 -1
- package/lib/__templates__/nextjs/components.json +21 -0
- package/lib/__templates__/nextjs/next.config.ts +12 -0
- package/lib/__templates__/nextjs/package.json +64 -2
- package/lib/__templates__/nextjs/pnpm-lock.yaml +8768 -1565
- package/lib/__templates__/nextjs/scripts/dev.sh +9 -27
- package/lib/__templates__/{react-rsbuild/scripts/build.sh → nextjs/scripts/prepare.sh} +0 -5
- package/lib/__templates__/nextjs/src/app/globals.css +124 -13
- package/lib/__templates__/nextjs/src/app/layout.tsx +23 -32
- package/lib/__templates__/nextjs/src/app/page.tsx +35 -23
- package/lib/__templates__/nextjs/src/components/ui/accordion.tsx +66 -0
- package/lib/__templates__/nextjs/src/components/ui/alert-dialog.tsx +157 -0
- package/lib/__templates__/nextjs/src/components/ui/alert.tsx +66 -0
- package/lib/__templates__/nextjs/src/components/ui/aspect-ratio.tsx +11 -0
- package/lib/__templates__/nextjs/src/components/ui/avatar.tsx +53 -0
- package/lib/__templates__/nextjs/src/components/ui/badge.tsx +46 -0
- package/lib/__templates__/nextjs/src/components/ui/breadcrumb.tsx +109 -0
- package/lib/__templates__/nextjs/src/components/ui/button-group.tsx +83 -0
- package/lib/__templates__/nextjs/src/components/ui/button.tsx +62 -0
- package/lib/__templates__/nextjs/src/components/ui/calendar.tsx +220 -0
- package/lib/__templates__/nextjs/src/components/ui/card.tsx +92 -0
- package/lib/__templates__/nextjs/src/components/ui/carousel.tsx +241 -0
- package/lib/__templates__/nextjs/src/components/ui/chart.tsx +357 -0
- package/lib/__templates__/nextjs/src/components/ui/checkbox.tsx +32 -0
- package/lib/__templates__/nextjs/src/components/ui/collapsible.tsx +33 -0
- package/lib/__templates__/nextjs/src/components/ui/command.tsx +184 -0
- package/lib/__templates__/nextjs/src/components/ui/context-menu.tsx +252 -0
- package/lib/__templates__/nextjs/src/components/ui/dialog.tsx +143 -0
- package/lib/__templates__/nextjs/src/components/ui/drawer.tsx +135 -0
- package/lib/__templates__/nextjs/src/components/ui/dropdown-menu.tsx +257 -0
- package/lib/__templates__/nextjs/src/components/ui/empty.tsx +104 -0
- package/lib/__templates__/nextjs/src/components/ui/field.tsx +248 -0
- package/lib/__templates__/nextjs/src/components/ui/form.tsx +167 -0
- package/lib/__templates__/nextjs/src/components/ui/hover-card.tsx +44 -0
- package/lib/__templates__/nextjs/src/components/ui/input-group.tsx +170 -0
- package/lib/__templates__/nextjs/src/components/ui/input-otp.tsx +77 -0
- package/lib/__templates__/nextjs/src/components/ui/input.tsx +21 -0
- package/lib/__templates__/nextjs/src/components/ui/item.tsx +193 -0
- package/lib/__templates__/nextjs/src/components/ui/kbd.tsx +28 -0
- package/lib/__templates__/nextjs/src/components/ui/label.tsx +24 -0
- package/lib/__templates__/nextjs/src/components/ui/menubar.tsx +276 -0
- package/lib/__templates__/nextjs/src/components/ui/navigation-menu.tsx +168 -0
- package/lib/__templates__/nextjs/src/components/ui/pagination.tsx +127 -0
- package/lib/__templates__/nextjs/src/components/ui/popover.tsx +48 -0
- package/lib/__templates__/nextjs/src/components/ui/progress.tsx +31 -0
- package/lib/__templates__/nextjs/src/components/ui/radio-group.tsx +45 -0
- package/lib/__templates__/nextjs/src/components/ui/resizable.tsx +63 -0
- package/lib/__templates__/nextjs/src/components/ui/scroll-area.tsx +58 -0
- package/lib/__templates__/nextjs/src/components/ui/select.tsx +190 -0
- package/lib/__templates__/nextjs/src/components/ui/separator.tsx +28 -0
- package/lib/__templates__/nextjs/src/components/ui/sheet.tsx +139 -0
- package/lib/__templates__/nextjs/src/components/ui/sidebar.tsx +724 -0
- package/lib/__templates__/nextjs/src/components/ui/skeleton.tsx +13 -0
- package/lib/__templates__/nextjs/src/components/ui/slider.tsx +63 -0
- package/lib/__templates__/nextjs/src/components/ui/sonner.tsx +40 -0
- package/lib/__templates__/nextjs/src/components/ui/spinner.tsx +16 -0
- package/lib/__templates__/nextjs/src/components/ui/switch.tsx +31 -0
- package/lib/__templates__/nextjs/src/components/ui/table.tsx +116 -0
- package/lib/__templates__/nextjs/src/components/ui/tabs.tsx +66 -0
- package/lib/__templates__/nextjs/src/components/ui/textarea.tsx +18 -0
- package/lib/__templates__/nextjs/src/components/ui/toggle-group.tsx +83 -0
- package/lib/__templates__/nextjs/src/components/ui/toggle.tsx +47 -0
- package/lib/__templates__/nextjs/src/components/ui/tooltip.tsx +61 -0
- package/lib/__templates__/nextjs/src/hooks/use-mobile.ts +19 -0
- package/lib/__templates__/nextjs/src/lib/utils.ts +6 -0
- package/lib/__templates__/nextjs/template.config.js +32 -2
- package/lib/__templates__/templates.json +61 -74
- package/lib/__templates__/vite/.coze +4 -3
- package/lib/__templates__/vite/README.md +204 -26
- package/lib/__templates__/vite/_npmrc +2 -1
- package/lib/__templates__/vite/eslint.config.mjs +9 -0
- package/lib/__templates__/vite/package.json +11 -2
- package/lib/__templates__/vite/pnpm-lock.yaml +3232 -243
- package/lib/__templates__/vite/scripts/dev.sh +7 -26
- package/lib/__templates__/{rsbuild/scripts/build.sh → vite/scripts/prepare.sh} +0 -5
- package/lib/__templates__/vite/src/main.ts +1 -2
- package/lib/__templates__/vite/template.config.js +41 -6
- package/lib/__templates__/vite/vite.config.ts +3 -3
- package/lib/cli.js +874 -316
- package/package.json +11 -4
- 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/app/+not-found.tsx +0 -79
- package/lib/__templates__/expo/client/index.js +0 -11
- 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__/react-rsbuild/.coze +0 -11
- package/lib/__templates__/react-rsbuild/.vscode/settings.json +0 -121
- package/lib/__templates__/react-rsbuild/README.md +0 -61
- package/lib/__templates__/react-rsbuild/_gitignore +0 -97
- package/lib/__templates__/react-rsbuild/_npmrc +0 -22
- package/lib/__templates__/react-rsbuild/package.json +0 -31
- package/lib/__templates__/react-rsbuild/pnpm-lock.yaml +0 -997
- package/lib/__templates__/react-rsbuild/rsbuild.config.ts +0 -13
- package/lib/__templates__/react-rsbuild/scripts/dev.sh +0 -51
- package/lib/__templates__/react-rsbuild/scripts/start.sh +0 -15
- package/lib/__templates__/react-rsbuild/src/App.tsx +0 -60
- package/lib/__templates__/react-rsbuild/src/index.css +0 -21
- package/lib/__templates__/react-rsbuild/src/index.html +0 -12
- package/lib/__templates__/react-rsbuild/src/index.tsx +0 -16
- package/lib/__templates__/react-rsbuild/tailwind.config.js +0 -9
- package/lib/__templates__/react-rsbuild/template.config.js +0 -54
- package/lib/__templates__/react-rsbuild/tsconfig.json +0 -17
- package/lib/__templates__/rsbuild/.coze +0 -11
- package/lib/__templates__/rsbuild/.vscode/settings.json +0 -7
- package/lib/__templates__/rsbuild/README.md +0 -61
- package/lib/__templates__/rsbuild/_gitignore +0 -97
- package/lib/__templates__/rsbuild/_npmrc +0 -22
- package/lib/__templates__/rsbuild/package.json +0 -24
- package/lib/__templates__/rsbuild/pnpm-lock.yaml +0 -888
- package/lib/__templates__/rsbuild/rsbuild.config.ts +0 -12
- package/lib/__templates__/rsbuild/scripts/dev.sh +0 -51
- package/lib/__templates__/rsbuild/scripts/start.sh +0 -15
- package/lib/__templates__/rsbuild/src/index.css +0 -21
- package/lib/__templates__/rsbuild/src/index.html +0 -12
- package/lib/__templates__/rsbuild/src/index.ts +0 -5
- package/lib/__templates__/rsbuild/src/main.ts +0 -65
- package/lib/__templates__/rsbuild/tailwind.config.js +0 -9
- package/lib/__templates__/rsbuild/template.config.js +0 -56
- package/lib/__templates__/rsbuild/tsconfig.json +0 -16
- 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
|
@@ -5,12 +5,15 @@ var commander = require('commander');
|
|
|
5
5
|
var path = require('path');
|
|
6
6
|
var fs = require('fs');
|
|
7
7
|
var shelljs = require('shelljs');
|
|
8
|
+
var perf_hooks = require('perf_hooks');
|
|
8
9
|
var fs$1 = require('fs/promises');
|
|
9
|
-
var
|
|
10
|
+
var os = require('os');
|
|
10
11
|
var jsYaml = require('js-yaml');
|
|
11
|
-
var
|
|
12
|
+
var toml = require('@iarna/toml');
|
|
13
|
+
var child_process = require('child_process');
|
|
12
14
|
var addFormats = require('ajv-formats');
|
|
13
15
|
var Ajv = require('ajv');
|
|
16
|
+
var minimist = require('minimist');
|
|
14
17
|
var changeCase = require('change-case');
|
|
15
18
|
var ejs = require('ejs');
|
|
16
19
|
|
|
@@ -124,7 +127,7 @@ const generateTemplatesHelpText = () => {
|
|
|
124
127
|
return lines.join('\n');
|
|
125
128
|
};
|
|
126
129
|
|
|
127
|
-
function _nullishCoalesce$2(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain$
|
|
130
|
+
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
131
|
const ERROR = 0; LogLevel[LogLevel["ERROR"] = ERROR] = "ERROR";
|
|
129
132
|
const WARN = 1; LogLevel[LogLevel["WARN"] = WARN] = "WARN";
|
|
130
133
|
const SUCCESS = 2; LogLevel[LogLevel["SUCCESS"] = SUCCESS] = "SUCCESS";
|
|
@@ -171,7 +174,7 @@ class Logger {
|
|
|
171
174
|
return level;
|
|
172
175
|
}
|
|
173
176
|
|
|
174
|
-
const envLevel = _optionalChain$
|
|
177
|
+
const envLevel = _optionalChain$3([process, 'access', _ => _.env, 'access', _2 => _2.LOG_LEVEL, 'optionalAccess', _3 => _3.toLowerCase, 'call', _4 => _4()]);
|
|
175
178
|
if (envLevel && envLevel in LOG_LEVEL_MAP) {
|
|
176
179
|
return LOG_LEVEL_MAP[envLevel];
|
|
177
180
|
}
|
|
@@ -183,7 +186,7 @@ class Logger {
|
|
|
183
186
|
// 简单检测:Node.js 环境且支持 TTY
|
|
184
187
|
return (
|
|
185
188
|
typeof process !== 'undefined' &&
|
|
186
|
-
_optionalChain$
|
|
189
|
+
_optionalChain$3([process, 'access', _5 => _5.stdout, 'optionalAccess', _6 => _6.isTTY]) === true &&
|
|
187
190
|
process.env.NO_COLOR === undefined
|
|
188
191
|
);
|
|
189
192
|
}
|
|
@@ -269,7 +272,330 @@ const createLogger = (options = {}) =>
|
|
|
269
272
|
// 导出默认实例
|
|
270
273
|
const logger = createLogger();
|
|
271
274
|
|
|
272
|
-
|
|
275
|
+
/**
|
|
276
|
+
* 时间统计工具
|
|
277
|
+
*/
|
|
278
|
+
class TimeTracker {
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
constructor() {
|
|
283
|
+
this.startTime = perf_hooks.performance.now();
|
|
284
|
+
this.lastTime = this.startTime;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 记录阶段耗时
|
|
289
|
+
* @param phaseName 阶段名称
|
|
290
|
+
*/
|
|
291
|
+
logPhase(phaseName) {
|
|
292
|
+
const now = perf_hooks.performance.now();
|
|
293
|
+
const phaseTime = now - this.lastTime;
|
|
294
|
+
this.lastTime = now;
|
|
295
|
+
|
|
296
|
+
logger.verbose(`⏱ ${phaseName}: ${phaseTime.toFixed(2)}ms`);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* 记录总耗时
|
|
301
|
+
*/
|
|
302
|
+
logTotal() {
|
|
303
|
+
const totalTime = perf_hooks.performance.now() - this.startTime;
|
|
304
|
+
logger.verbose(`⏱ Total time: ${totalTime.toFixed(2)}ms`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 获取当前耗时(不输出日志)
|
|
309
|
+
* @returns 从开始到现在的总耗时(毫秒)
|
|
310
|
+
*/
|
|
311
|
+
getElapsedTime() {
|
|
312
|
+
return perf_hooks.performance.now() - this.startTime;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* 获取模板配置文件路径
|
|
318
|
+
* @returns templates.json 的绝对路径
|
|
319
|
+
*/
|
|
320
|
+
const getTemplatesConfigPath = () => {
|
|
321
|
+
const configPath = path.resolve(getTemplatesDir(), 'templates.json');
|
|
322
|
+
logger.verbose(`Templates config path: ${configPath}`);
|
|
323
|
+
logger.verbose(`Config file exists: ${fs.existsSync(configPath)}`);
|
|
324
|
+
return configPath;
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 获取模板目录路径
|
|
329
|
+
* @returns __templates__ 目录的绝对路径
|
|
330
|
+
*/
|
|
331
|
+
const getTemplatesDir = () => {
|
|
332
|
+
const templatesDir = path.resolve(__dirname, './__templates__');
|
|
333
|
+
logger.verbose(`Templates directory: ${templatesDir}`);
|
|
334
|
+
logger.verbose(`Templates directory exists: ${fs.existsSync(templatesDir)}`);
|
|
335
|
+
return templatesDir;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 加载模板配置文件
|
|
340
|
+
* 支持 .ts 和 .js 文件(通过 sucrase 注册)
|
|
341
|
+
*
|
|
342
|
+
* @param templatePath - 模板目录路径
|
|
343
|
+
* @returns 模板配置对象
|
|
344
|
+
*/
|
|
345
|
+
|
|
346
|
+
const loadTemplateConfig = async (
|
|
347
|
+
templatePath,
|
|
348
|
+
) => {
|
|
349
|
+
logger.verbose(`Loading template config from: ${templatePath}`);
|
|
350
|
+
|
|
351
|
+
const tsConfigPath = path.join(templatePath, 'template.config.ts');
|
|
352
|
+
const jsConfigPath = path.join(templatePath, 'template.config.js');
|
|
353
|
+
|
|
354
|
+
logger.verbose('Checking for config files:');
|
|
355
|
+
logger.verbose(` - TypeScript: ${tsConfigPath}`);
|
|
356
|
+
logger.verbose(` - JavaScript: ${jsConfigPath}`);
|
|
357
|
+
|
|
358
|
+
let configPath;
|
|
359
|
+
|
|
360
|
+
const [tsExists, jsExists] = await Promise.all([
|
|
361
|
+
fs$1.access(tsConfigPath).then(
|
|
362
|
+
() => true,
|
|
363
|
+
() => false,
|
|
364
|
+
),
|
|
365
|
+
fs$1.access(jsConfigPath).then(
|
|
366
|
+
() => true,
|
|
367
|
+
() => false,
|
|
368
|
+
),
|
|
369
|
+
]);
|
|
370
|
+
|
|
371
|
+
logger.verbose('Config file existence check:');
|
|
372
|
+
logger.verbose(` - template.config.ts: ${tsExists}`);
|
|
373
|
+
logger.verbose(` - template.config.js: ${jsExists}`);
|
|
374
|
+
|
|
375
|
+
if (tsExists) {
|
|
376
|
+
configPath = tsConfigPath;
|
|
377
|
+
} else if (jsExists) {
|
|
378
|
+
configPath = jsConfigPath;
|
|
379
|
+
} else {
|
|
380
|
+
throw new Error(
|
|
381
|
+
`Template config not found in ${templatePath}.\n` +
|
|
382
|
+
'Expected: template.config.ts or template.config.js',
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
logger.verbose(`Using config file: ${configPath}`);
|
|
387
|
+
|
|
388
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports, security/detect-non-literal-require -- Sucrase handles .ts files at runtime, path is validated above
|
|
389
|
+
const config = require(configPath);
|
|
390
|
+
|
|
391
|
+
logger.verbose('Template config loaded successfully');
|
|
392
|
+
|
|
393
|
+
return config.default || config;
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* 加载模板列表配置
|
|
398
|
+
*
|
|
399
|
+
* @param configPath - templates.json 配置文件路径
|
|
400
|
+
* @returns 模板列表配置
|
|
401
|
+
*/
|
|
402
|
+
const loadTemplatesConfig = async (
|
|
403
|
+
configPath,
|
|
404
|
+
) => {
|
|
405
|
+
logger.verbose(`Loading templates config from: ${configPath}`);
|
|
406
|
+
|
|
407
|
+
const content = await fs$1.readFile(configPath, 'utf-8');
|
|
408
|
+
// eslint-disable-next-line no-restricted-syntax -- Static config file loaded at build time, safeJsonParse not needed
|
|
409
|
+
const config = JSON.parse(content) ;
|
|
410
|
+
|
|
411
|
+
logger.verbose(
|
|
412
|
+
`Found ${config.templates.length} templates: ${config.templates.map(t => t.name).join(', ')}`,
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
return config;
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* 根据模板名称查找模板元信息
|
|
420
|
+
*
|
|
421
|
+
* @param templatesConfig - 模板列表配置
|
|
422
|
+
* @param templateName - 模板名称
|
|
423
|
+
* @returns 模板元信息
|
|
424
|
+
*/
|
|
425
|
+
const findTemplate = (
|
|
426
|
+
templatesConfig,
|
|
427
|
+
templateName,
|
|
428
|
+
) => {
|
|
429
|
+
const template = templatesConfig.templates.find(t => t.name === templateName);
|
|
430
|
+
|
|
431
|
+
if (!template) {
|
|
432
|
+
const availableTemplates = templatesConfig.templates
|
|
433
|
+
.map(t => t.name)
|
|
434
|
+
.join(', ');
|
|
435
|
+
throw new Error(
|
|
436
|
+
`Template "${templateName}" not found.\n` +
|
|
437
|
+
`Available templates: ${availableTemplates}\n` +
|
|
438
|
+
'Use --template <name> to specify a template.',
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return template;
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* 获取模板的完整路径
|
|
447
|
+
*
|
|
448
|
+
* @param basePath - 模板目录(templates.json 所在目录)
|
|
449
|
+
* @param templateMetadata - 模板元信息
|
|
450
|
+
* @returns 模板完整路径
|
|
451
|
+
*/
|
|
452
|
+
const getTemplatePath = async (
|
|
453
|
+
basePath,
|
|
454
|
+
templateMetadata,
|
|
455
|
+
) => {
|
|
456
|
+
logger.verbose('Resolving template path:');
|
|
457
|
+
logger.verbose(` - Base path: ${basePath}`);
|
|
458
|
+
logger.verbose(` - Template location: ${templateMetadata.location}`);
|
|
459
|
+
|
|
460
|
+
// location 是相对于 templates.json 文件的路径
|
|
461
|
+
const templatePath = path.join(basePath, templateMetadata.location);
|
|
462
|
+
|
|
463
|
+
logger.verbose(` - Resolved path: ${templatePath}`);
|
|
464
|
+
|
|
465
|
+
try {
|
|
466
|
+
await fs$1.access(templatePath);
|
|
467
|
+
logger.verbose(' - Template directory exists: ✓');
|
|
468
|
+
// eslint-disable-next-line @coze-arch/use-error-in-catch -- Error handling done in throw statement
|
|
469
|
+
} catch (e) {
|
|
470
|
+
logger.error(' - Template directory does not exist: ✗');
|
|
471
|
+
throw new Error(`Template directory not found: ${templatePath}`);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return templatePath;
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* 对单个模板执行 pnpm install
|
|
479
|
+
*/
|
|
480
|
+
const warmupTemplate = (templatePath, templateName) => {
|
|
481
|
+
logger.info(`\nWarming up template: ${templateName}`);
|
|
482
|
+
logger.info(` Path: ${templatePath}`);
|
|
483
|
+
|
|
484
|
+
const result = shelljs.exec('pnpm install', {
|
|
485
|
+
cwd: templatePath,
|
|
486
|
+
silent: true,
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
// 输出 stdout
|
|
490
|
+
if (result.stdout) {
|
|
491
|
+
process.stdout.write(result.stdout);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// 输出 stderr
|
|
495
|
+
if (result.stderr) {
|
|
496
|
+
process.stderr.write(result.stderr);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if (result.code === 0) {
|
|
500
|
+
logger.success(` ✓ ${templateName} warmed up successfully`);
|
|
501
|
+
} else {
|
|
502
|
+
const errorMessage = [
|
|
503
|
+
`pnpm install failed for ${templateName} with exit code ${result.code}`,
|
|
504
|
+
result.stderr ? `\nStderr:\n${result.stderr}` : '',
|
|
505
|
+
result.stdout ? `\nStdout:\n${result.stdout}` : '',
|
|
506
|
+
]
|
|
507
|
+
.filter(Boolean)
|
|
508
|
+
.join('');
|
|
509
|
+
|
|
510
|
+
throw new Error(errorMessage);
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* 执行 warmup 命令的内部实现
|
|
516
|
+
*/
|
|
517
|
+
const executeWarmup = async (
|
|
518
|
+
options
|
|
519
|
+
|
|
520
|
+
,
|
|
521
|
+
|
|
522
|
+
command,
|
|
523
|
+
) => {
|
|
524
|
+
const timer = new TimeTracker();
|
|
525
|
+
|
|
526
|
+
try {
|
|
527
|
+
const { template: templateFilter } = options;
|
|
528
|
+
|
|
529
|
+
logger.info('Starting template warmup...');
|
|
530
|
+
timer.logPhase('Initialization');
|
|
531
|
+
|
|
532
|
+
// 加载模板配置
|
|
533
|
+
const configPath = getTemplatesConfigPath();
|
|
534
|
+
const templatesConfig = await loadTemplatesConfig(configPath);
|
|
535
|
+
|
|
536
|
+
logger.verbose(
|
|
537
|
+
`Found ${templatesConfig.templates.length} templates in config`,
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
// 过滤模板
|
|
541
|
+
const templatesToWarmup = templateFilter
|
|
542
|
+
? templatesConfig.templates.filter(t => t.name === templateFilter)
|
|
543
|
+
: templatesConfig.templates;
|
|
544
|
+
|
|
545
|
+
if (templatesToWarmup.length === 0) {
|
|
546
|
+
if (templateFilter) {
|
|
547
|
+
logger.warn(`Template "${templateFilter}" not found`);
|
|
548
|
+
logger.info(
|
|
549
|
+
`Available templates: ${templatesConfig.templates.map(t => t.name).join(', ')}`,
|
|
550
|
+
);
|
|
551
|
+
} else {
|
|
552
|
+
logger.warn('No templates found');
|
|
553
|
+
}
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
logger.info(
|
|
558
|
+
`\nWill warm up ${templatesToWarmup.length} template(s): ${templatesToWarmup.map(t => t.name).join(', ')}`,
|
|
559
|
+
);
|
|
560
|
+
|
|
561
|
+
// 获取模板基础路径
|
|
562
|
+
const basePath = configPath.replace(/\/templates\.json$/, '');
|
|
563
|
+
|
|
564
|
+
// 对每个模板执行 pnpm install
|
|
565
|
+
for (const templateMetadata of templatesToWarmup) {
|
|
566
|
+
const templatePath = await getTemplatePath(basePath, templateMetadata);
|
|
567
|
+
warmupTemplate(templatePath, templateMetadata.name);
|
|
568
|
+
timer.logPhase(`Warmup ${templateMetadata.name}`);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
logger.success('\n✅ All templates warmed up successfully!');
|
|
572
|
+
logger.info(
|
|
573
|
+
'\nNext time you run `coze init`, it will be much faster as node_modules are pre-installed.',
|
|
574
|
+
);
|
|
575
|
+
|
|
576
|
+
timer.logTotal();
|
|
577
|
+
} catch (error) {
|
|
578
|
+
timer.logTotal();
|
|
579
|
+
logger.error('Failed to warmup templates:');
|
|
580
|
+
logger.error(error instanceof Error ? error.message : String(error));
|
|
581
|
+
process.exit(1);
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* 注册 warmup 命令到 program
|
|
587
|
+
*/
|
|
588
|
+
const registerCommand$3 = program => {
|
|
589
|
+
program
|
|
590
|
+
.command('warmup')
|
|
591
|
+
.description('Pre-install dependencies for templates to speed up init')
|
|
592
|
+
.option('-t, --template <name>', 'Warmup a specific template only')
|
|
593
|
+
.action(async (options, command) => {
|
|
594
|
+
await executeWarmup(options);
|
|
595
|
+
});
|
|
596
|
+
};
|
|
597
|
+
|
|
598
|
+
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 */
|
|
273
599
|
// Safe JSON parsing utilities with type safety and error handling
|
|
274
600
|
// Provides fallback values, validation, and error monitoring capabilities
|
|
275
601
|
|
|
@@ -360,12 +686,12 @@ function safeJsonParse(
|
|
|
360
686
|
const parsed = JSON.parse(String(input));
|
|
361
687
|
|
|
362
688
|
// Optional validation
|
|
363
|
-
if (_optionalChain$
|
|
689
|
+
if (_optionalChain$2([options, 'optionalAccess', _ => _.validate])) {
|
|
364
690
|
if (options.validate(parsed)) {
|
|
365
691
|
return parsed;
|
|
366
692
|
} else {
|
|
367
693
|
const validationError = new Error('JSON validation failed');
|
|
368
|
-
_optionalChain$
|
|
694
|
+
_optionalChain$2([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
|
|
369
695
|
|
|
370
696
|
if (options.throwOnValidationError) {
|
|
371
697
|
throw validationError;
|
|
@@ -377,15 +703,15 @@ function safeJsonParse(
|
|
|
377
703
|
return parsed;
|
|
378
704
|
} catch (error) {
|
|
379
705
|
// Re-throw validation errors when throwOnValidationError is true
|
|
380
|
-
if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$
|
|
706
|
+
if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$2([options, 'optionalAccess', _4 => _4.throwOnValidationError])) {
|
|
381
707
|
throw error;
|
|
382
708
|
}
|
|
383
|
-
_optionalChain$
|
|
709
|
+
_optionalChain$2([options, 'optionalAccess', _5 => _5.onError, 'optionalCall', _6 => _6(error , input)]);
|
|
384
710
|
return defaultValue;
|
|
385
711
|
}
|
|
386
712
|
}
|
|
387
713
|
|
|
388
|
-
function _optionalChain$
|
|
714
|
+
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
715
|
|
|
390
716
|
|
|
391
717
|
/**
|
|
@@ -401,7 +727,7 @@ const parseConfigContent = (content) => {
|
|
|
401
727
|
return config ;
|
|
402
728
|
} catch (error) {
|
|
403
729
|
// TOML 解析失败,继续尝试其他格式
|
|
404
|
-
|
|
730
|
+
|
|
405
731
|
console.debug('TOML parse failed:', error);
|
|
406
732
|
}
|
|
407
733
|
|
|
@@ -413,7 +739,7 @@ const parseConfigContent = (content) => {
|
|
|
413
739
|
}
|
|
414
740
|
} catch (error) {
|
|
415
741
|
// YAML 解析失败,继续尝试其他格式
|
|
416
|
-
|
|
742
|
+
|
|
417
743
|
console.debug('YAML parse failed:', error);
|
|
418
744
|
}
|
|
419
745
|
|
|
@@ -475,13 +801,13 @@ const getCommandConfig = (
|
|
|
475
801
|
// 根据命令名称映射到配置路径
|
|
476
802
|
switch (commandName) {
|
|
477
803
|
case 'dev':
|
|
478
|
-
commandConfig = _optionalChain$
|
|
804
|
+
commandConfig = _optionalChain$1([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
|
|
479
805
|
break;
|
|
480
806
|
case 'build':
|
|
481
|
-
commandConfig = _optionalChain$
|
|
807
|
+
commandConfig = _optionalChain$1([config, 'access', _3 => _3.deploy, 'optionalAccess', _4 => _4.build]);
|
|
482
808
|
break;
|
|
483
809
|
case 'start':
|
|
484
|
-
commandConfig = _optionalChain$
|
|
810
|
+
commandConfig = _optionalChain$1([config, 'access', _5 => _5.deploy, 'optionalAccess', _6 => _6.run]);
|
|
485
811
|
break;
|
|
486
812
|
default:
|
|
487
813
|
throw new Error(`Unknown command: ${commandName}`);
|
|
@@ -497,27 +823,276 @@ const getCommandConfig = (
|
|
|
497
823
|
return commandConfig;
|
|
498
824
|
};
|
|
499
825
|
|
|
500
|
-
|
|
826
|
+
// ABOUTME: Fix rule to comment out problematic outputFileTracingRoot config in Next.js projects
|
|
827
|
+
// ABOUTME: This config can cause issues in monorepo environments and should be removed
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* 检查是否为 Next.js 项目
|
|
834
|
+
*/
|
|
835
|
+
const isNextProject = (projectFolder) => {
|
|
836
|
+
const packageJsonPath = path.join(projectFolder, 'package.json');
|
|
837
|
+
|
|
838
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
839
|
+
return false;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
try {
|
|
843
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
844
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
845
|
+
const deps = {
|
|
846
|
+
...packageJson.dependencies,
|
|
847
|
+
...packageJson.devDependencies,
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
return 'next' in deps;
|
|
851
|
+
} catch (e) {
|
|
852
|
+
return false;
|
|
853
|
+
}
|
|
854
|
+
};
|
|
501
855
|
|
|
502
856
|
/**
|
|
503
|
-
*
|
|
857
|
+
* 查找 Next.js 配置文件
|
|
504
858
|
*/
|
|
505
|
-
const
|
|
506
|
-
const
|
|
507
|
-
|
|
508
|
-
|
|
859
|
+
const findNextConfigFile = (projectFolder) => {
|
|
860
|
+
const possibleConfigs = [
|
|
861
|
+
'next.config.ts',
|
|
862
|
+
'next.config.js',
|
|
863
|
+
'next.config.mjs',
|
|
864
|
+
];
|
|
865
|
+
|
|
866
|
+
for (const configFile of possibleConfigs) {
|
|
867
|
+
const configPath = path.join(projectFolder, configFile);
|
|
868
|
+
if (fs.existsSync(configPath)) {
|
|
869
|
+
return configPath;
|
|
509
870
|
}
|
|
510
|
-
}
|
|
871
|
+
}
|
|
511
872
|
|
|
512
|
-
|
|
873
|
+
return null;
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* 注释掉 outputFileTracingRoot 配置
|
|
878
|
+
*/
|
|
879
|
+
const commentOutOutputTracingRoot = (
|
|
880
|
+
configPath,
|
|
881
|
+
) => {
|
|
882
|
+
let content = fs.readFileSync(configPath, 'utf-8');
|
|
883
|
+
let modified = false;
|
|
884
|
+
let originalLine = null;
|
|
885
|
+
|
|
886
|
+
// 匹配包含 outputFileTracingRoot 的行(尚未被注释的)
|
|
887
|
+
// 支持 path.resolve(...) 后面有空格,然后可选逗号
|
|
888
|
+
// 只匹配单行配置,不匹配跨多行的配置
|
|
889
|
+
const pattern =
|
|
890
|
+
/^(\s*)(outputFileTracingRoot:\s*path\.resolve\([^\n\r)]+\)\s*,?)\s*$/gm;
|
|
891
|
+
|
|
892
|
+
const matches = content.match(pattern);
|
|
893
|
+
|
|
894
|
+
if (matches && matches.length > 0) {
|
|
895
|
+
originalLine = matches[0].trim();
|
|
896
|
+
|
|
897
|
+
// 在匹配的行前添加 // 注释
|
|
898
|
+
content = content.replace(pattern, '$1// $2');
|
|
899
|
+
modified = true;
|
|
900
|
+
|
|
901
|
+
fs.writeFileSync(configPath, content, 'utf-8');
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
return { modified, originalLine };
|
|
905
|
+
};
|
|
906
|
+
|
|
907
|
+
// start_aigc
|
|
908
|
+
/**
|
|
909
|
+
* Fix 规则:注释掉 Next.js 项目中的 outputFileTracingRoot 配置
|
|
910
|
+
* 这个配置在 monorepo 环境中可能会导致问题
|
|
911
|
+
*/
|
|
912
|
+
const fixNextOutputTracingRoot = context => {
|
|
913
|
+
const ruleName = 'next-output-tracing-root';
|
|
914
|
+
|
|
915
|
+
// 1. 检查是否为 Next.js 项目
|
|
916
|
+
if (!isNextProject(context.projectFolder)) {
|
|
917
|
+
return {
|
|
918
|
+
ruleName,
|
|
919
|
+
applied: false,
|
|
920
|
+
message: 'Not a Next.js project, skipping',
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// 2. 查找 Next.js 配置文件
|
|
925
|
+
const configPath = findNextConfigFile(context.projectFolder);
|
|
926
|
+
|
|
927
|
+
if (!configPath) {
|
|
928
|
+
return {
|
|
929
|
+
ruleName,
|
|
930
|
+
applied: false,
|
|
931
|
+
message: 'Next.js config file not found, skipping',
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// 3. 注释掉 outputFileTracingRoot 配置
|
|
936
|
+
const { modified, originalLine } = commentOutOutputTracingRoot(configPath);
|
|
937
|
+
|
|
938
|
+
if (modified && originalLine) {
|
|
939
|
+
logger.success(
|
|
940
|
+
`Commented out outputFileTracingRoot in ${configPath.split('/').pop()}`,
|
|
941
|
+
);
|
|
942
|
+
logger.info(` Original: ${originalLine}`);
|
|
943
|
+
|
|
944
|
+
return {
|
|
945
|
+
ruleName,
|
|
946
|
+
applied: true,
|
|
947
|
+
message: `Successfully commented out: ${originalLine}`,
|
|
948
|
+
};
|
|
949
|
+
}
|
|
513
950
|
|
|
514
951
|
return {
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
},
|
|
952
|
+
ruleName,
|
|
953
|
+
applied: false,
|
|
954
|
+
message: 'No outputFileTracingRoot config found, skipping',
|
|
519
955
|
};
|
|
520
956
|
};
|
|
957
|
+
// end_aigc
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* 所有修复规则的数组
|
|
961
|
+
* 按顺序执行,新增规则直接添加到数组中
|
|
962
|
+
*/
|
|
963
|
+
const rules = [
|
|
964
|
+
// Next.js related fixes
|
|
965
|
+
fixNextOutputTracingRoot,
|
|
966
|
+
|
|
967
|
+
// Add more rules here
|
|
968
|
+
] ;
|
|
969
|
+
|
|
970
|
+
// ABOUTME: Fix command for resolving legacy issues from previous project versions
|
|
971
|
+
// ABOUTME: Applies a series of fix rules defined in the fix-rules directory
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
// start_aigc
|
|
975
|
+
/**
|
|
976
|
+
* 执行 fix 命令的内部实现
|
|
977
|
+
*/
|
|
978
|
+
const executeFix = async (options = {}) => {
|
|
979
|
+
try {
|
|
980
|
+
const cwd = process.cwd();
|
|
981
|
+
const projectFolder = options.directory
|
|
982
|
+
? path.resolve(cwd, options.directory)
|
|
983
|
+
: cwd;
|
|
984
|
+
|
|
985
|
+
logger.info(`Running fix command on: ${projectFolder}`);
|
|
986
|
+
logger.info(`Found ${rules.length} fix rule(s) to apply\n`);
|
|
987
|
+
|
|
988
|
+
const context = {
|
|
989
|
+
cwd,
|
|
990
|
+
projectFolder,
|
|
991
|
+
};
|
|
992
|
+
|
|
993
|
+
let appliedCount = 0;
|
|
994
|
+
let skippedCount = 0;
|
|
995
|
+
|
|
996
|
+
// 依次执行所有修复规则
|
|
997
|
+
for (const rule of rules) {
|
|
998
|
+
try {
|
|
999
|
+
const result = await Promise.resolve(rule(context));
|
|
1000
|
+
|
|
1001
|
+
if (result.applied) {
|
|
1002
|
+
appliedCount++;
|
|
1003
|
+
logger.success(`✓ ${result.ruleName}: ${result.message}`);
|
|
1004
|
+
} else {
|
|
1005
|
+
skippedCount++;
|
|
1006
|
+
logger.info(`○ ${result.ruleName}: ${result.message}`);
|
|
1007
|
+
}
|
|
1008
|
+
} catch (error) {
|
|
1009
|
+
logger.error(
|
|
1010
|
+
`✗ Rule execution failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
// 输出汇总信息
|
|
1016
|
+
logger.info(
|
|
1017
|
+
`\nSummary: ${appliedCount} fixed, ${skippedCount} skipped, ${rules.length} total`,
|
|
1018
|
+
);
|
|
1019
|
+
|
|
1020
|
+
if (appliedCount > 0) {
|
|
1021
|
+
logger.success('\nFixes applied successfully!');
|
|
1022
|
+
} else {
|
|
1023
|
+
logger.info('\nNo fixes needed');
|
|
1024
|
+
}
|
|
1025
|
+
} catch (error) {
|
|
1026
|
+
logger.error('Failed to run fix command:');
|
|
1027
|
+
logger.error(error instanceof Error ? error.message : String(error));
|
|
1028
|
+
process.exit(1);
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
// end_aigc
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* 注册 fix 命令到 program
|
|
1035
|
+
*/
|
|
1036
|
+
const registerCommand$2 = program => {
|
|
1037
|
+
program
|
|
1038
|
+
.command('fix')
|
|
1039
|
+
.description(
|
|
1040
|
+
'Fix legacy issues from previous versions (e.g., problematic configs)',
|
|
1041
|
+
)
|
|
1042
|
+
.argument(
|
|
1043
|
+
'[directory]',
|
|
1044
|
+
'Target directory to fix (defaults to current directory)',
|
|
1045
|
+
)
|
|
1046
|
+
.action(async (directory) => {
|
|
1047
|
+
await executeFix({ directory });
|
|
1048
|
+
});
|
|
1049
|
+
};
|
|
1050
|
+
|
|
1051
|
+
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; }
|
|
1052
|
+
/**
|
|
1053
|
+
* 日志文件名常量
|
|
1054
|
+
*/
|
|
1055
|
+
const LOG_FILE_NAME = 'dev.log';
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* 获取日志目录
|
|
1059
|
+
* 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
|
|
1060
|
+
*/
|
|
1061
|
+
const getLogDir = () =>
|
|
1062
|
+
process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
|
|
1063
|
+
|
|
1064
|
+
/**
|
|
1065
|
+
* 解析日志文件路径
|
|
1066
|
+
* - 如果是绝对路径,直接使用
|
|
1067
|
+
* - 如果是相对路径,基于 getLogDir() + 相对路径
|
|
1068
|
+
* - 如果为空,使用 getLogDir() + LOG_FILE_NAME
|
|
1069
|
+
*/
|
|
1070
|
+
const resolveLogFilePath = (logFile) => {
|
|
1071
|
+
if (!logFile) {
|
|
1072
|
+
return path.join(getLogDir(), LOG_FILE_NAME);
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
if (path.isAbsolute(logFile)) {
|
|
1076
|
+
return logFile;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
return path.join(getLogDir(), logFile);
|
|
1080
|
+
};
|
|
1081
|
+
|
|
1082
|
+
/**
|
|
1083
|
+
* 创建日志写入流
|
|
1084
|
+
*/
|
|
1085
|
+
const createLogStream = (logFilePath) => {
|
|
1086
|
+
const logDir = path.dirname(logFilePath);
|
|
1087
|
+
|
|
1088
|
+
// 确保日志目录存在
|
|
1089
|
+
if (!fs.existsSync(logDir)) {
|
|
1090
|
+
fs.mkdirSync(logDir, { recursive: true });
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
// 使用 'w' 标志覆盖之前的日志
|
|
1094
|
+
return fs.createWriteStream(logFilePath, { flags: 'w' });
|
|
1095
|
+
};
|
|
521
1096
|
|
|
522
1097
|
/**
|
|
523
1098
|
* 执行命令的内部实现
|
|
@@ -529,21 +1104,32 @@ const executeRun = async (
|
|
|
529
1104
|
try {
|
|
530
1105
|
logger.info(`Running ${commandName} command...`);
|
|
531
1106
|
|
|
532
|
-
// 1.
|
|
1107
|
+
// 1. 对于 build 命令,先执行 fix 以确保项目配置正确
|
|
1108
|
+
if (commandName === 'build') {
|
|
1109
|
+
logger.info('\n🔧 Running fix command before build...\n');
|
|
1110
|
+
try {
|
|
1111
|
+
await executeFix();
|
|
1112
|
+
// eslint-disable-next-line @coze-arch/no-empty-catch
|
|
1113
|
+
} catch (e) {
|
|
1114
|
+
// just ignore
|
|
1115
|
+
}
|
|
1116
|
+
logger.info('');
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
// 2. 加载 .coze 配置
|
|
533
1120
|
const config = await loadCozeConfig();
|
|
534
1121
|
const commandArgs = getCommandConfig(config, commandName);
|
|
535
1122
|
|
|
536
|
-
//
|
|
537
|
-
const
|
|
538
|
-
const
|
|
539
|
-
const logStream = logManager.createWriteStream(logFile);
|
|
1123
|
+
// 3. 准备日志
|
|
1124
|
+
const logFilePath = resolveLogFilePath(options.logFile);
|
|
1125
|
+
const logStream = createLogStream(logFilePath);
|
|
540
1126
|
|
|
541
|
-
//
|
|
1127
|
+
// 4. 执行命令
|
|
542
1128
|
const commandString = commandArgs.join(' ');
|
|
543
1129
|
|
|
544
1130
|
logger.info(`Executing: ${commandString}`);
|
|
545
1131
|
logger.info(`Working directory: ${process.cwd()}`);
|
|
546
|
-
logger.info(`Log file: ${
|
|
1132
|
+
logger.info(`Log file: ${logFilePath}`);
|
|
547
1133
|
|
|
548
1134
|
const childProcess = shelljs.exec(commandString, {
|
|
549
1135
|
async: true,
|
|
@@ -555,12 +1141,12 @@ const executeRun = async (
|
|
|
555
1141
|
}
|
|
556
1142
|
|
|
557
1143
|
// 将输出同时写入控制台和日志文件
|
|
558
|
-
_optionalChain
|
|
1144
|
+
_optionalChain([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
|
|
559
1145
|
process.stdout.write(data);
|
|
560
1146
|
logStream.write(data);
|
|
561
1147
|
})]);
|
|
562
1148
|
|
|
563
|
-
_optionalChain
|
|
1149
|
+
_optionalChain([childProcess, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
|
|
564
1150
|
process.stderr.write(data);
|
|
565
1151
|
logStream.write(data);
|
|
566
1152
|
})]);
|
|
@@ -572,11 +1158,11 @@ const executeRun = async (
|
|
|
572
1158
|
logger.error(
|
|
573
1159
|
`Command exited with code ${_nullishCoalesce$1(code, () => ( 'unknown'))}${signal ? ` and signal ${signal}` : ''}`,
|
|
574
1160
|
);
|
|
575
|
-
logger.error(`Check log file for details: ${
|
|
1161
|
+
logger.error(`Check log file for details: ${logFilePath}`);
|
|
576
1162
|
process.exit(code || 1);
|
|
577
1163
|
} else {
|
|
578
1164
|
logger.success('Command completed successfully');
|
|
579
|
-
logger.info(`Log file: ${
|
|
1165
|
+
logger.info(`Log file: ${logFilePath}`);
|
|
580
1166
|
}
|
|
581
1167
|
});
|
|
582
1168
|
|
|
@@ -624,72 +1210,48 @@ const registerCommand$1 = program => {
|
|
|
624
1210
|
.description('Start production server')
|
|
625
1211
|
.option('--log-file <path>', 'Log file path')
|
|
626
1212
|
.action(async options => {
|
|
627
|
-
await executeRun('start', options);
|
|
628
|
-
});
|
|
629
|
-
};
|
|
630
|
-
|
|
631
|
-
/**
|
|
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;
|
|
1213
|
+
await executeRun('start', options);
|
|
1214
|
+
});
|
|
681
1215
|
};
|
|
682
1216
|
|
|
683
1217
|
/**
|
|
684
|
-
*
|
|
685
|
-
*
|
|
1218
|
+
* 在后台启动一个独立的子进程
|
|
1219
|
+
* 类似于 `setsid command args >/dev/null 2>&1 &`
|
|
1220
|
+
*
|
|
1221
|
+
* @param command - 要执行的命令 (例如: 'npm', 'node', 'bash')
|
|
1222
|
+
* @param args - 命令参数数组 (例如: ['run', 'dev'])
|
|
1223
|
+
* @param options - 配置选项
|
|
1224
|
+
* @returns 子进程的 PID
|
|
686
1225
|
*/
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
};
|
|
1226
|
+
function spawnDetached(
|
|
1227
|
+
command,
|
|
1228
|
+
args,
|
|
1229
|
+
options,
|
|
1230
|
+
) {
|
|
1231
|
+
const { cwd, verbose = true } = options;
|
|
1232
|
+
const isWindows = os.platform() === 'win32';
|
|
1233
|
+
|
|
1234
|
+
if (verbose) {
|
|
1235
|
+
console.log(`Spawning detached process: ${command} ${args.join(' ')}`);
|
|
1236
|
+
console.log(`Working directory: ${cwd}`);
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
// 使用 spawn 创建后台子进程
|
|
1240
|
+
const child = child_process.spawn(command, args, {
|
|
1241
|
+
cwd,
|
|
1242
|
+
detached: !isWindows, // Windows 不完全支持 detached,但仍可以使用
|
|
1243
|
+
stdio: 'ignore', // 忽略所有输入输出,让进程完全独立运行
|
|
1244
|
+
});
|
|
1245
|
+
|
|
1246
|
+
// 分离父子进程引用,允许父进程退出而不等待子进程
|
|
1247
|
+
child.unref();
|
|
1248
|
+
|
|
1249
|
+
if (verbose && child.pid) {
|
|
1250
|
+
console.log(`Process started with PID: ${child.pid}`);
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
return child.pid;
|
|
1254
|
+
}
|
|
693
1255
|
|
|
694
1256
|
/**
|
|
695
1257
|
* 创建 AJV 验证器实例
|
|
@@ -739,149 +1301,12 @@ const validateParams = (
|
|
|
739
1301
|
return params ;
|
|
740
1302
|
};
|
|
741
1303
|
|
|
742
|
-
/**
|
|
743
|
-
* 加载模板配置文件
|
|
744
|
-
* 支持 .ts 和 .js 文件(通过 sucrase 注册)
|
|
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
|
-
]);
|
|
774
|
-
|
|
775
|
-
logger.verbose('Config file existence check:');
|
|
776
|
-
logger.verbose(` - template.config.ts: ${tsExists}`);
|
|
777
|
-
logger.verbose(` - template.config.js: ${jsExists}`);
|
|
778
|
-
|
|
779
|
-
if (tsExists) {
|
|
780
|
-
configPath = tsConfigPath;
|
|
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
|
-
);
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
logger.verbose(`Using config file: ${configPath}`);
|
|
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
|
-
};
|
|
821
|
-
|
|
822
|
-
/**
|
|
823
|
-
* 根据模板名称查找模板元信息
|
|
824
|
-
*
|
|
825
|
-
* @param templatesConfig - 模板列表配置
|
|
826
|
-
* @param templateName - 模板名称
|
|
827
|
-
* @returns 模板元信息
|
|
828
|
-
*/
|
|
829
|
-
const findTemplate = (
|
|
830
|
-
templatesConfig,
|
|
831
|
-
templateName,
|
|
832
|
-
) => {
|
|
833
|
-
const template = templatesConfig.templates.find(t => t.name === templateName);
|
|
834
|
-
|
|
835
|
-
if (!template) {
|
|
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
|
-
}
|
|
845
|
-
|
|
846
|
-
return template;
|
|
847
|
-
};
|
|
848
|
-
|
|
849
|
-
/**
|
|
850
|
-
* 获取模板的完整路径
|
|
851
|
-
*
|
|
852
|
-
* @param basePath - 模板目录(templates.json 所在目录)
|
|
853
|
-
* @param templateMetadata - 模板元信息
|
|
854
|
-
* @returns 模板完整路径
|
|
855
|
-
*/
|
|
856
|
-
const getTemplatePath = async (
|
|
857
|
-
basePath,
|
|
858
|
-
templateMetadata,
|
|
859
|
-
) => {
|
|
860
|
-
logger.verbose('Resolving template path:');
|
|
861
|
-
logger.verbose(` - Base path: ${basePath}`);
|
|
862
|
-
logger.verbose(` - Template location: ${templateMetadata.location}`);
|
|
863
|
-
|
|
864
|
-
// location 是相对于 templates.json 文件的路径
|
|
865
|
-
const templatePath = path.join(basePath, templateMetadata.location);
|
|
866
|
-
|
|
867
|
-
logger.verbose(` - Resolved path: ${templatePath}`);
|
|
868
|
-
|
|
869
|
-
try {
|
|
870
|
-
await fs$1.access(templatePath);
|
|
871
|
-
logger.verbose(' - Template directory exists: ✓');
|
|
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}`);
|
|
876
|
-
}
|
|
877
|
-
|
|
878
|
-
return templatePath;
|
|
879
|
-
};
|
|
880
|
-
|
|
881
1304
|
/**
|
|
882
1305
|
* 从 Commander 解析透传参数
|
|
883
1306
|
* 将 kebab-case 的 CLI 参数转换为 camelCase
|
|
884
1307
|
*
|
|
1308
|
+
* 使用 minimist 解析 process.argv,自动处理类型转换
|
|
1309
|
+
*
|
|
885
1310
|
* @param command - Commander 命令实例
|
|
886
1311
|
* @param knownOptions - 已知的选项集合(不需要透传的选项)
|
|
887
1312
|
* @returns 参数对象
|
|
@@ -890,20 +1315,34 @@ const parsePassThroughParams = (
|
|
|
890
1315
|
command,
|
|
891
1316
|
knownOptions = new Set(),
|
|
892
1317
|
) => {
|
|
893
|
-
|
|
1318
|
+
// 使用 minimist 解析所有参数
|
|
1319
|
+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers -- slice(2) to skip node and script path
|
|
1320
|
+
const parsed = minimist(process.argv.slice(2));
|
|
894
1321
|
|
|
895
|
-
|
|
896
|
-
|
|
1322
|
+
// 过滤掉已知选项和位置参数(_)
|
|
1323
|
+
const filtered = Object.entries(parsed).reduce(
|
|
897
1324
|
(params, [key, value]) => {
|
|
898
|
-
|
|
1325
|
+
// 跳过 minimist 的位置参数数组
|
|
1326
|
+
if (key === '_') {
|
|
1327
|
+
return params;
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
// 跳过已知选项(支持原始格式和 camelCase 格式)
|
|
1331
|
+
if (knownOptions.has(key) || knownOptions.has(changeCase.camelCase(key))) {
|
|
899
1332
|
return params;
|
|
900
1333
|
}
|
|
901
1334
|
|
|
1335
|
+
// 将 kebab-case 转换为 camelCase
|
|
902
1336
|
const camelKey = changeCase.camelCase(key);
|
|
903
|
-
|
|
1337
|
+
// eslint-disable-next-line security/detect-object-injection -- camelKey is sanitized by camelCase
|
|
1338
|
+
params[camelKey] = value;
|
|
1339
|
+
|
|
1340
|
+
return params;
|
|
904
1341
|
},
|
|
905
|
-
|
|
1342
|
+
{},
|
|
906
1343
|
);
|
|
1344
|
+
|
|
1345
|
+
return filtered;
|
|
907
1346
|
};
|
|
908
1347
|
|
|
909
1348
|
/**
|
|
@@ -1069,17 +1508,139 @@ const convertDotfileName = (filePath) => {
|
|
|
1069
1508
|
};
|
|
1070
1509
|
|
|
1071
1510
|
/**
|
|
1072
|
-
*
|
|
1511
|
+
* 执行文件渲染钩子
|
|
1073
1512
|
*
|
|
1074
|
-
* @param
|
|
1075
|
-
* @param
|
|
1513
|
+
* @param templateConfig - 模板配置
|
|
1514
|
+
* @param fileInfo - 文件渲染信息
|
|
1076
1515
|
* @param context - 模板上下文
|
|
1516
|
+
* @returns 处理后的文件信息,或 null 表示跳过该文件
|
|
1077
1517
|
*/
|
|
1078
|
-
const
|
|
1079
|
-
|
|
1080
|
-
|
|
1518
|
+
const executeFileRenderHook = async (
|
|
1519
|
+
templateConfig,
|
|
1520
|
+
fileInfo,
|
|
1081
1521
|
context,
|
|
1082
1522
|
) => {
|
|
1523
|
+
if (!templateConfig.onFileRender) {
|
|
1524
|
+
return fileInfo;
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
const result = await templateConfig.onFileRender(
|
|
1528
|
+
fileInfo,
|
|
1529
|
+
context,
|
|
1530
|
+
);
|
|
1531
|
+
|
|
1532
|
+
// false: 跳过文件
|
|
1533
|
+
if (result === false) {
|
|
1534
|
+
return null;
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
// undefined/void: 使用默认内容
|
|
1538
|
+
if (result === undefined || result === null) {
|
|
1539
|
+
return fileInfo;
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
// string: 作为 content,其他不变
|
|
1543
|
+
if (typeof result === 'string') {
|
|
1544
|
+
return {
|
|
1545
|
+
...fileInfo,
|
|
1546
|
+
content: result,
|
|
1547
|
+
};
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
// FileRenderInfo: 使用新对象的信息
|
|
1551
|
+
return result;
|
|
1552
|
+
};
|
|
1553
|
+
|
|
1554
|
+
/**
|
|
1555
|
+
* 处理单个文件
|
|
1556
|
+
*/
|
|
1557
|
+
const processSingleFile = async (options
|
|
1558
|
+
|
|
1559
|
+
|
|
1560
|
+
|
|
1561
|
+
|
|
1562
|
+
|
|
1563
|
+
) => {
|
|
1564
|
+
const { file, templatePath, outputPath, context, templateConfig } = options;
|
|
1565
|
+
|
|
1566
|
+
const srcPath = path.join(templatePath, file);
|
|
1567
|
+
const destFile = convertDotfileName(file);
|
|
1568
|
+
|
|
1569
|
+
logger.verbose(
|
|
1570
|
+
` - Processing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
|
|
1571
|
+
);
|
|
1572
|
+
|
|
1573
|
+
// 判断是否为二进制文件
|
|
1574
|
+
const isBinary = !shouldRenderFile(srcPath);
|
|
1575
|
+
let content;
|
|
1576
|
+
let wasRendered = false;
|
|
1577
|
+
|
|
1578
|
+
if (isBinary) {
|
|
1579
|
+
// 二进制文件,读取为 buffer 然后转为 base64
|
|
1580
|
+
const buffer = await fs$1.readFile(srcPath);
|
|
1581
|
+
content = buffer.toString('base64');
|
|
1582
|
+
} else {
|
|
1583
|
+
// 文本文件,渲染后的内容
|
|
1584
|
+
content = await renderTemplate(srcPath, context);
|
|
1585
|
+
wasRendered = true;
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
// 构造文件信息对象
|
|
1589
|
+
const fileInfo = {
|
|
1590
|
+
path: file,
|
|
1591
|
+
destPath: destFile,
|
|
1592
|
+
content,
|
|
1593
|
+
isBinary,
|
|
1594
|
+
wasRendered,
|
|
1595
|
+
};
|
|
1596
|
+
|
|
1597
|
+
// 执行文件渲染钩子
|
|
1598
|
+
const processedFileInfo = await executeFileRenderHook(
|
|
1599
|
+
templateConfig,
|
|
1600
|
+
fileInfo,
|
|
1601
|
+
context,
|
|
1602
|
+
);
|
|
1603
|
+
|
|
1604
|
+
// 如果返回 null,跳过该文件
|
|
1605
|
+
if (processedFileInfo === null) {
|
|
1606
|
+
logger.verbose(' ⊘ Skipped by onFileRender hook');
|
|
1607
|
+
return;
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
// 使用处理后的目标路径
|
|
1611
|
+
const finalDestPath = path.join(outputPath, processedFileInfo.destPath);
|
|
1612
|
+
|
|
1613
|
+
// 确保目标目录存在
|
|
1614
|
+
await ensureDir(path.dirname(finalDestPath));
|
|
1615
|
+
|
|
1616
|
+
// 写入文件
|
|
1617
|
+
if (processedFileInfo.isBinary) {
|
|
1618
|
+
// 二进制文件:如果内容没变,直接复制;否则从 base64 解码写入
|
|
1619
|
+
if (processedFileInfo.content === content) {
|
|
1620
|
+
await fs$1.copyFile(srcPath, finalDestPath);
|
|
1621
|
+
logger.verbose(' ✓ Copied (binary)');
|
|
1622
|
+
} else {
|
|
1623
|
+
const buffer = Buffer.from(processedFileInfo.content, 'base64');
|
|
1624
|
+
await fs$1.writeFile(finalDestPath, buffer);
|
|
1625
|
+
logger.verbose(' ✓ Written (binary, modified by hook)');
|
|
1626
|
+
}
|
|
1627
|
+
} else {
|
|
1628
|
+
// 文本文件
|
|
1629
|
+
await fs$1.writeFile(finalDestPath, processedFileInfo.content, 'utf-8');
|
|
1630
|
+
logger.verbose(' ✓ Rendered and written');
|
|
1631
|
+
}
|
|
1632
|
+
};
|
|
1633
|
+
|
|
1634
|
+
/**
|
|
1635
|
+
* 复制并处理模板文件到目标目录
|
|
1636
|
+
*/
|
|
1637
|
+
const processTemplateFiles = async (options
|
|
1638
|
+
|
|
1639
|
+
|
|
1640
|
+
|
|
1641
|
+
|
|
1642
|
+
) => {
|
|
1643
|
+
const { templatePath, outputPath, context, templateConfig } = options;
|
|
1083
1644
|
logger.verbose('Processing template files:');
|
|
1084
1645
|
logger.verbose(` - Template path: ${templatePath}`);
|
|
1085
1646
|
logger.verbose(` - Output path: ${outputPath}`);
|
|
@@ -1110,29 +1671,15 @@ const processTemplateFiles = async (
|
|
|
1110
1671
|
}
|
|
1111
1672
|
|
|
1112
1673
|
await Promise.all(
|
|
1113
|
-
files.map(
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
)
|
|
1121
|
-
|
|
1122
|
-
// 确保目标目录存在
|
|
1123
|
-
await ensureDir(path.dirname(destPath));
|
|
1124
|
-
|
|
1125
|
-
if (shouldRenderFile(srcPath)) {
|
|
1126
|
-
// 渲染文本文件
|
|
1127
|
-
const rendered = await renderTemplate(srcPath, context);
|
|
1128
|
-
await fs$1.writeFile(destPath, rendered, 'utf-8');
|
|
1129
|
-
logger.verbose(' ✓ Rendered and written');
|
|
1130
|
-
} else {
|
|
1131
|
-
// 直接复制二进制文件
|
|
1132
|
-
await fs$1.copyFile(srcPath, destPath);
|
|
1133
|
-
logger.verbose(' ✓ Copied');
|
|
1134
|
-
}
|
|
1135
|
-
}),
|
|
1674
|
+
files.map(file =>
|
|
1675
|
+
processSingleFile({
|
|
1676
|
+
file,
|
|
1677
|
+
templatePath,
|
|
1678
|
+
outputPath,
|
|
1679
|
+
context,
|
|
1680
|
+
templateConfig,
|
|
1681
|
+
}),
|
|
1682
|
+
),
|
|
1136
1683
|
);
|
|
1137
1684
|
|
|
1138
1685
|
logger.verbose('✓ All files processed successfully');
|
|
@@ -1171,6 +1718,7 @@ const processTemplateFiles = async (
|
|
|
1171
1718
|
|
|
1172
1719
|
/**
|
|
1173
1720
|
* 检查输出目录是否为空
|
|
1721
|
+
* 注意:.git 目录会被忽略,允许在已初始化 git 的目录中创建项目
|
|
1174
1722
|
*
|
|
1175
1723
|
* @param outputPath - 输出目录路径
|
|
1176
1724
|
* @returns 是否为空
|
|
@@ -1180,7 +1728,9 @@ const isOutputDirEmpty = async (
|
|
|
1180
1728
|
) => {
|
|
1181
1729
|
try {
|
|
1182
1730
|
const entries = await fs$1.readdir(outputPath);
|
|
1183
|
-
|
|
1731
|
+
// 过滤掉 .git 目录,允许在已初始化 git 的目录中创建项目
|
|
1732
|
+
const filteredEntries = entries.filter(entry => entry !== '.git');
|
|
1733
|
+
return filteredEntries.length === 0;
|
|
1184
1734
|
} catch (e) {
|
|
1185
1735
|
// 目录不存在,视为空
|
|
1186
1736
|
return true;
|
|
@@ -1316,7 +1866,12 @@ const execute = async (
|
|
|
1316
1866
|
const absoluteOutputPath = await prepareOutputDirectory(outputPath);
|
|
1317
1867
|
|
|
1318
1868
|
// 6. 处理模板文件
|
|
1319
|
-
await processTemplateFiles(
|
|
1869
|
+
await processTemplateFiles({
|
|
1870
|
+
templatePath,
|
|
1871
|
+
outputPath: absoluteOutputPath,
|
|
1872
|
+
context,
|
|
1873
|
+
templateConfig,
|
|
1874
|
+
});
|
|
1320
1875
|
|
|
1321
1876
|
// 7. 执行 onAfterRender 钩子
|
|
1322
1877
|
await executeAfterRenderHook(templateConfig, context, absoluteOutputPath);
|
|
@@ -1324,7 +1879,7 @@ const execute = async (
|
|
|
1324
1879
|
return absoluteOutputPath;
|
|
1325
1880
|
};
|
|
1326
1881
|
|
|
1327
|
-
function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
1882
|
+
function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
1328
1883
|
/**
|
|
1329
1884
|
* 运行 pnpm install
|
|
1330
1885
|
*/
|
|
@@ -1364,8 +1919,18 @@ const runPnpmInstall = (projectPath) => {
|
|
|
1364
1919
|
|
|
1365
1920
|
/**
|
|
1366
1921
|
* 初始化 git 仓库并创建初始提交
|
|
1922
|
+
* 如果目录中已存在 .git,则跳过初始化
|
|
1367
1923
|
*/
|
|
1368
1924
|
const runGitInit = (projectPath) => {
|
|
1925
|
+
// 检查是否已存在 .git 目录
|
|
1926
|
+
const gitDir = path.join(projectPath, '.git');
|
|
1927
|
+
if (fs.existsSync(gitDir)) {
|
|
1928
|
+
logger.info(
|
|
1929
|
+
'\n💡 Git repository already exists, skipping git initialization',
|
|
1930
|
+
);
|
|
1931
|
+
return;
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1369
1934
|
const runGitCommand = (command) => {
|
|
1370
1935
|
logger.info(`Executing: ${command}`);
|
|
1371
1936
|
|
|
@@ -1417,45 +1982,33 @@ const runGitInit = (projectPath) => {
|
|
|
1417
1982
|
};
|
|
1418
1983
|
|
|
1419
1984
|
/**
|
|
1420
|
-
* 运行开发服务器
|
|
1985
|
+
* 运行开发服务器(后台模式)
|
|
1986
|
+
* 启动后台子进程运行开发服务器,父进程可以直接退出
|
|
1987
|
+
* 使用 CLI 自己的 dev 命令(定义在 run.ts)而不是直接运行 npm run dev
|
|
1421
1988
|
*/
|
|
1422
|
-
const
|
|
1423
|
-
logger.info('\nStarting development server...');
|
|
1424
|
-
logger.info(`Executing: npm run dev in ${projectPath}`);
|
|
1425
|
-
logger.info('Press Ctrl+C to stop the server\n');
|
|
1989
|
+
const runDev = (projectPath) => {
|
|
1990
|
+
logger.info('\nStarting development server in background...');
|
|
1426
1991
|
|
|
1427
|
-
//
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
async: true,
|
|
1431
|
-
silent: true, // 手动处理输出以便显示详细信息
|
|
1432
|
-
});
|
|
1433
|
-
|
|
1434
|
-
if (child) {
|
|
1435
|
-
// 输出 stdout
|
|
1436
|
-
_optionalChain([child, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
|
|
1437
|
-
process.stdout.write(data);
|
|
1438
|
-
})]);
|
|
1992
|
+
// 获取当前 CLI 的可执行文件路径
|
|
1993
|
+
// process.argv[0] 是 node,process.argv[1] 是 CLI 入口文件
|
|
1994
|
+
const cliPath = process.argv[1];
|
|
1439
1995
|
|
|
1440
|
-
|
|
1441
|
-
_optionalChain([child, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
|
|
1442
|
-
process.stderr.write(data);
|
|
1443
|
-
})]);
|
|
1996
|
+
logger.info(`Executing: ${cliPath} dev in ${projectPath}`);
|
|
1444
1997
|
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1998
|
+
// 使用通用的后台执行函数启动开发服务器
|
|
1999
|
+
// 调用 CLI 自己的 dev 命令
|
|
2000
|
+
const pid = spawnDetached(process.argv[0], [cliPath, 'dev'], {
|
|
2001
|
+
cwd: projectPath,
|
|
2002
|
+
verbose: false, // 不输出额外的进程信息,由 logger 统一处理
|
|
2003
|
+
});
|
|
1450
2004
|
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
});
|
|
2005
|
+
logger.success('Development server started in background!');
|
|
2006
|
+
if (pid) {
|
|
2007
|
+
logger.info(`Process ID: ${pid}`);
|
|
2008
|
+
logger.info(
|
|
2009
|
+
'\nThe dev server is running independently. You can close this terminal.',
|
|
2010
|
+
);
|
|
2011
|
+
logger.info(`To stop the server later, use: kill ${pid}`);
|
|
1459
2012
|
}
|
|
1460
2013
|
};
|
|
1461
2014
|
|
|
@@ -1520,7 +2073,7 @@ const executeInit = async (
|
|
|
1520
2073
|
|
|
1521
2074
|
// 如果没有跳过 dev,则启动开发服务器
|
|
1522
2075
|
if (!skipDev) {
|
|
1523
|
-
|
|
2076
|
+
runDev(absoluteOutputPath);
|
|
1524
2077
|
timer.logPhase('Dev server startup');
|
|
1525
2078
|
} else {
|
|
1526
2079
|
// 只有跳过 dev 时才显示 Next steps
|
|
@@ -1534,7 +2087,7 @@ const executeInit = async (
|
|
|
1534
2087
|
' git init && git add . && git commit -m "initial commit"',
|
|
1535
2088
|
);
|
|
1536
2089
|
}
|
|
1537
|
-
logger.info('
|
|
2090
|
+
logger.info(' coze dev');
|
|
1538
2091
|
}
|
|
1539
2092
|
|
|
1540
2093
|
// 输出总耗时
|
|
@@ -1568,10 +2121,15 @@ const registerCommand = program => {
|
|
|
1568
2121
|
});
|
|
1569
2122
|
};
|
|
1570
2123
|
|
|
2124
|
+
var version = "0.0.1-alpha.f91253";
|
|
2125
|
+
var packageJson = {
|
|
2126
|
+
version: version};
|
|
2127
|
+
|
|
1571
2128
|
const commands = [
|
|
1572
2129
|
registerCommand,
|
|
1573
2130
|
registerCommand$1,
|
|
1574
|
-
|
|
2131
|
+
registerCommand$3,
|
|
2132
|
+
registerCommand$2,
|
|
1575
2133
|
];
|
|
1576
2134
|
|
|
1577
2135
|
const main = () => {
|
|
@@ -1582,7 +2140,7 @@ const main = () => {
|
|
|
1582
2140
|
.description(
|
|
1583
2141
|
'Coze Coding CLI - Project template engine for frontend stacks',
|
|
1584
2142
|
)
|
|
1585
|
-
.version(
|
|
2143
|
+
.version(packageJson.version);
|
|
1586
2144
|
|
|
1587
2145
|
commands.forEach(initCmd => initCmd(program));
|
|
1588
2146
|
|