@coze-arch/cli 0.0.1-alpha.912cd5 → 0.0.1-alpha.9f719c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/lib/__templates__/expo/.coze +5 -0
  2. package/lib/__templates__/expo/.cozeproj/scripts/deploy_build.sh +25 -25
  3. package/lib/__templates__/expo/.cozeproj/scripts/deploy_run.sh +31 -56
  4. package/lib/__templates__/expo/.cozeproj/scripts/prod_build.sh +47 -0
  5. package/lib/__templates__/expo/.cozeproj/scripts/prod_run.sh +35 -0
  6. package/lib/__templates__/expo/_npmrc +1 -1
  7. package/lib/__templates__/expo/babel.config.js +10 -0
  8. package/lib/__templates__/expo/client/constants/theme.ts +10 -0
  9. package/lib/__templates__/expo/client/contexts/AuthContext.tsx +14 -107
  10. package/lib/__templates__/expo/client/hooks/useTheme.ts +1 -1
  11. package/lib/__templates__/expo/client/screens/home/index.tsx +1 -4
  12. package/lib/__templates__/expo/client/screens/home/styles.ts +1 -273
  13. package/lib/__templates__/expo/client/utils/index.ts +1 -2
  14. package/lib/__templates__/expo/metro.config.js +3 -5
  15. package/lib/__templates__/expo/package.json +8 -2
  16. package/lib/__templates__/expo/pnpm-lock.yaml +87 -5
  17. package/lib/__templates__/expo/src/index.ts +2 -2
  18. package/lib/__templates__/expo/template.config.js +1 -1
  19. package/lib/__templates__/nextjs/.coze +3 -3
  20. package/lib/__templates__/nextjs/_npmrc +1 -1
  21. package/lib/__templates__/nextjs/package.json +9 -3
  22. package/lib/__templates__/nextjs/pnpm-lock.yaml +2501 -1120
  23. package/lib/__templates__/nextjs/scripts/dev.sh +8 -27
  24. package/lib/__templates__/nextjs/src/app/layout.tsx +18 -22
  25. package/lib/__templates__/nextjs/template.config.js +1 -1
  26. package/lib/__templates__/templates.json +7 -0
  27. package/lib/__templates__/vite/.coze +3 -3
  28. package/lib/__templates__/vite/README.md +204 -26
  29. package/lib/__templates__/vite/_npmrc +1 -1
  30. package/lib/__templates__/vite/package.json +1 -1
  31. package/lib/__templates__/vite/pnpm-lock.yaml +120 -120
  32. package/lib/__templates__/vite/scripts/dev.sh +7 -26
  33. package/lib/__templates__/vite/template.config.js +10 -1
  34. package/lib/__templates__/vite/vite.config.ts +3 -3
  35. package/lib/cli.js +408 -248
  36. package/package.json +3 -2
  37. package/lib/__templates__/nextjs/.babelrc +0 -15
  38. package/lib/__templates__/nextjs/server.mjs +0 -50
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
10
  var toml = require('@iarna/toml');
10
11
  var jsYaml = require('js-yaml');
11
- var perf_hooks = require('perf_hooks');
12
+ var child_process = require('child_process');
13
+ var os = require('os');
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$4(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var LogLevel; (function (LogLevel) {
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$4([process, 'access', _ => _.env, 'access', _2 => _2.LOG_LEVEL, 'optionalAccess', _3 => _3.toLowerCase, 'call', _4 => _4()]);
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$4([process, 'access', _5 => _5.stdout, 'optionalAccess', _6 => _6.isTTY]) === true &&
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
- 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 */
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$2 = 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$3([options, 'optionalAccess', _ => _.validate])) {
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$3([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
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$3([options, 'optionalAccess', _4 => _4.throwOnValidationError])) {
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$3([options, 'optionalAccess', _5 => _5.onError, 'optionalCall', _6 => _6(error , input)]);
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$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; }
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
  /**
@@ -475,13 +801,13 @@ const getCommandConfig = (
475
801
  // 根据命令名称映射到配置路径
476
802
  switch (commandName) {
477
803
  case 'dev':
478
- commandConfig = _optionalChain$2([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
804
+ commandConfig = _optionalChain$1([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
479
805
  break;
480
806
  case 'build':
481
- commandConfig = _optionalChain$2([config, 'access', _3 => _3.deploy, 'optionalAccess', _4 => _4.build]);
807
+ commandConfig = _optionalChain$1([config, 'access', _3 => _3.deploy, 'optionalAccess', _4 => _4.build]);
482
808
  break;
483
809
  case 'start':
484
- commandConfig = _optionalChain$2([config, 'access', _5 => _5.deploy, 'optionalAccess', _6 => _6.run]);
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,7 +823,7 @@ const getCommandConfig = (
497
823
  return commandConfig;
498
824
  };
499
825
 
500
- function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain$1(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
826
+ function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
501
827
 
502
828
  /**
503
829
  * 创建日志管理器
@@ -555,12 +881,12 @@ const executeRun = async (
555
881
  }
556
882
 
557
883
  // 将输出同时写入控制台和日志文件
558
- _optionalChain$1([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
884
+ _optionalChain([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
559
885
  process.stdout.write(data);
560
886
  logStream.write(data);
561
887
  })]);
562
888
 
563
- _optionalChain$1([childProcess, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
889
+ _optionalChain([childProcess, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
564
890
  process.stderr.write(data);
565
891
  logStream.write(data);
566
892
  })]);
@@ -629,68 +955,44 @@ const registerCommand$1 = program => {
629
955
  };
630
956
 
631
957
  /**
632
- * 时间统计工具
958
+ * 在后台启动一个独立的子进程
959
+ * 类似于 `setsid command args >/dev/null 2>&1 &`
960
+ *
961
+ * @param command - 要执行的命令 (例如: 'npm', 'node', 'bash')
962
+ * @param args - 命令参数数组 (例如: ['run', 'dev'])
963
+ * @param options - 配置选项
964
+ * @returns 子进程的 PID
633
965
  */
634
- class TimeTracker {
635
-
636
-
966
+ function spawnDetached(
967
+ command,
968
+ args,
969
+ options,
970
+ ) {
971
+ const { cwd, verbose = true } = options;
972
+ const isWindows = os.platform() === 'win32';
637
973
 
638
- constructor() {
639
- this.startTime = perf_hooks.performance.now();
640
- this.lastTime = this.startTime;
974
+ if (verbose) {
975
+ console.log(`Spawning detached process: ${command} ${args.join(' ')}`);
976
+ console.log(`Working directory: ${cwd}`);
641
977
  }
642
978
 
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;
979
+ // 使用 spawn 创建后台子进程
980
+ const child = child_process.spawn(command, args, {
981
+ cwd,
982
+ detached: !isWindows, // Windows 不完全支持 detached,但仍可以使用
983
+ stdio: 'ignore', // 忽略所有输入输出,让进程完全独立运行
984
+ });
651
985
 
652
- logger.verbose(`⏱ ${phaseName}: ${phaseTime.toFixed(2)}ms`);
653
- }
986
+ // 分离父子进程引用,允许父进程退出而不等待子进程
987
+ child.unref();
654
988
 
655
- /**
656
- * 记录总耗时
657
- */
658
- logTotal() {
659
- const totalTime = perf_hooks.performance.now() - this.startTime;
660
- logger.verbose(`⏱ Total time: ${totalTime.toFixed(2)}ms`);
989
+ if (verbose && child.pid) {
990
+ console.log(`Process started with PID: ${child.pid}`);
661
991
  }
662
992
 
663
- /**
664
- * 获取当前耗时(不输出日志)
665
- * @returns 从开始到现在的总耗时(毫秒)
666
- */
667
- getElapsedTime() {
668
- return perf_hooks.performance.now() - this.startTime;
669
- }
993
+ return child.pid;
670
994
  }
671
995
 
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
996
  /**
695
997
  * 创建 AJV 验证器实例
696
998
  */
@@ -739,149 +1041,12 @@ const validateParams = (
739
1041
  return params ;
740
1042
  };
741
1043
 
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
1044
  /**
882
1045
  * 从 Commander 解析透传参数
883
1046
  * 将 kebab-case 的 CLI 参数转换为 camelCase
884
1047
  *
1048
+ * 使用 minimist 解析 process.argv,自动处理类型转换
1049
+ *
885
1050
  * @param command - Commander 命令实例
886
1051
  * @param knownOptions - 已知的选项集合(不需要透传的选项)
887
1052
  * @returns 参数对象
@@ -890,20 +1055,34 @@ const parsePassThroughParams = (
890
1055
  command,
891
1056
  knownOptions = new Set(),
892
1057
  ) => {
893
- const rawOptions = command.opts();
1058
+ // 使用 minimist 解析所有参数
1059
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers -- slice(2) to skip node and script path
1060
+ const parsed = minimist(process.argv.slice(2));
894
1061
 
895
- const initial = {};
896
- return Object.entries(rawOptions).reduce(
1062
+ // 过滤掉已知选项和位置参数(_)
1063
+ const filtered = Object.entries(parsed).reduce(
897
1064
  (params, [key, value]) => {
898
- if (knownOptions.has(key)) {
1065
+ // 跳过 minimist 的位置参数数组
1066
+ if (key === '_') {
899
1067
  return params;
900
1068
  }
901
1069
 
1070
+ // 跳过已知选项(支持原始格式和 camelCase 格式)
1071
+ if (knownOptions.has(key) || knownOptions.has(changeCase.camelCase(key))) {
1072
+ return params;
1073
+ }
1074
+
1075
+ // 将 kebab-case 转换为 camelCase
902
1076
  const camelKey = changeCase.camelCase(key);
903
- return { ...params, [camelKey]: value };
1077
+ // eslint-disable-next-line security/detect-object-injection -- camelKey is sanitized by camelCase
1078
+ params[camelKey] = value;
1079
+
1080
+ return params;
904
1081
  },
905
- initial,
1082
+ {},
906
1083
  );
1084
+
1085
+ return filtered;
907
1086
  };
908
1087
 
909
1088
  /**
@@ -1327,7 +1506,7 @@ const execute = async (
1327
1506
  return absoluteOutputPath;
1328
1507
  };
1329
1508
 
1330
- function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
1509
+ function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
1331
1510
  /**
1332
1511
  * 运行 pnpm install
1333
1512
  */
@@ -1430,45 +1609,26 @@ const runGitInit = (projectPath) => {
1430
1609
  };
1431
1610
 
1432
1611
  /**
1433
- * 运行开发服务器
1612
+ * 运行开发服务器(后台模式)
1613
+ * 启动后台子进程运行开发服务器,父进程可以直接退出
1434
1614
  */
1435
1615
  const runNpmDev = (projectPath) => {
1436
- logger.info('\nStarting development server...');
1616
+ logger.info('\nStarting development server in background...');
1437
1617
  logger.info(`Executing: npm run dev in ${projectPath}`);
1438
- logger.info('Press Ctrl+C to stop the server\n');
1439
1618
 
1440
- // 使用 async: true 异步执行,不阻塞进程
1441
- const child = shelljs.exec('npm run dev', {
1619
+ // 使用通用的后台执行函数启动开发服务器
1620
+ const pid = spawnDetached('npm', ['run', 'dev'], {
1442
1621
  cwd: projectPath,
1443
- async: true,
1444
- silent: true, // 手动处理输出以便显示详细信息
1622
+ verbose: false, // 不输出额外的进程信息,由 logger 统一处理
1445
1623
  });
1446
1624
 
1447
- if (child) {
1448
- // 输出 stdout
1449
- _optionalChain([child, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
1450
- process.stdout.write(data);
1451
- })]);
1452
-
1453
- // 输出 stderr
1454
- _optionalChain([child, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
1455
- process.stderr.write(data);
1456
- })]);
1457
-
1458
- // 监听错误
1459
- child.on('error', (error) => {
1460
- logger.error(`Failed to start dev server: ${error.message}`);
1461
- logger.error(`Error stack: ${error.stack}`);
1462
- });
1463
-
1464
- // 监听退出
1465
- child.on('exit', (code, signal) => {
1466
- if (code !== 0 && code !== null) {
1467
- logger.error(
1468
- `Dev server exited with code ${code}${signal ? ` and signal ${signal}` : ''}`,
1469
- );
1470
- }
1471
- });
1625
+ logger.success('Development server started in background!');
1626
+ if (pid) {
1627
+ logger.info(`Process ID: ${pid}`);
1628
+ logger.info(
1629
+ '\nThe dev server is running independently. You can close this terminal.',
1630
+ );
1631
+ logger.info(`To stop the server later, use: kill ${pid}`);
1472
1632
  }
1473
1633
  };
1474
1634
 
@@ -1581,14 +1741,14 @@ const registerCommand = program => {
1581
1741
  });
1582
1742
  };
1583
1743
 
1584
- var version = "0.0.1-alpha.912cd5";
1744
+ var version = "0.0.1-alpha.9f719c";
1585
1745
  var packageJson = {
1586
1746
  version: version};
1587
1747
 
1588
1748
  const commands = [
1589
1749
  registerCommand,
1590
1750
  registerCommand$1,
1591
- // registerWarmupCommand,
1751
+ registerCommand$2,
1592
1752
  ];
1593
1753
 
1594
1754
  const main = () => {