@coze-arch/cli 0.0.1-beta.5

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 (126) hide show
  1. package/README.md +142 -0
  2. package/bin/main +2 -0
  3. package/lib/__templates__/expo/.coze +7 -0
  4. package/lib/__templates__/expo/.cozeproj/scripts/deploy_build.sh +109 -0
  5. package/lib/__templates__/expo/.cozeproj/scripts/deploy_run.sh +257 -0
  6. package/lib/__templates__/expo/README.md +13 -0
  7. package/lib/__templates__/expo/_gitignore +11 -0
  8. package/lib/__templates__/expo/app.json +63 -0
  9. package/lib/__templates__/expo/babel.config.js +9 -0
  10. package/lib/__templates__/expo/client/app/(tabs)/_layout.tsx +43 -0
  11. package/lib/__templates__/expo/client/app/(tabs)/home.tsx +1 -0
  12. package/lib/__templates__/expo/client/app/(tabs)/index.tsx +7 -0
  13. package/lib/__templates__/expo/client/app/+not-found.tsx +79 -0
  14. package/lib/__templates__/expo/client/app/_layout.tsx +33 -0
  15. package/lib/__templates__/expo/client/assets/fonts/SpaceMono-Regular.ttf +0 -0
  16. package/lib/__templates__/expo/client/assets/images/adaptive-icon.png +0 -0
  17. package/lib/__templates__/expo/client/assets/images/default-avatar.png +0 -0
  18. package/lib/__templates__/expo/client/assets/images/favicon.png +0 -0
  19. package/lib/__templates__/expo/client/assets/images/icon.png +0 -0
  20. package/lib/__templates__/expo/client/assets/images/partial-react-logo.png +0 -0
  21. package/lib/__templates__/expo/client/assets/images/react-logo.png +0 -0
  22. package/lib/__templates__/expo/client/assets/images/react-logo@2x.png +0 -0
  23. package/lib/__templates__/expo/client/assets/images/react-logo@3x.png +0 -0
  24. package/lib/__templates__/expo/client/assets/images/splash-icon.png +0 -0
  25. package/lib/__templates__/expo/client/components/Screen.tsx +330 -0
  26. package/lib/__templates__/expo/client/components/SmartDateInput.tsx +238 -0
  27. package/lib/__templates__/expo/client/constants/theme.ts +118 -0
  28. package/lib/__templates__/expo/client/contexts/AuthContext.tsx +142 -0
  29. package/lib/__templates__/expo/client/hooks/useColorScheme.ts +1 -0
  30. package/lib/__templates__/expo/client/hooks/useTheme.ts +13 -0
  31. package/lib/__templates__/expo/client/index.js +11 -0
  32. package/lib/__templates__/expo/client/screens/home/index.tsx +54 -0
  33. package/lib/__templates__/expo/client/screens/home/styles.ts +332 -0
  34. package/lib/__templates__/expo/client/scripts/install-missing-deps.js +80 -0
  35. package/lib/__templates__/expo/client/utils/index.ts +55 -0
  36. package/lib/__templates__/expo/eslint-formatter-simple.mjs +49 -0
  37. package/lib/__templates__/expo/eslint.config.mjs +98 -0
  38. package/lib/__templates__/expo/metro.config.js +53 -0
  39. package/lib/__templates__/expo/package.json +100 -0
  40. package/lib/__templates__/expo/pnpm-lock.yaml +13978 -0
  41. package/lib/__templates__/expo/src/index.ts +12 -0
  42. package/lib/__templates__/expo/template.config.js +49 -0
  43. package/lib/__templates__/expo/tsconfig.json +24 -0
  44. package/lib/__templates__/nextjs/.coze +11 -0
  45. package/lib/__templates__/nextjs/.vscode/settings.json +121 -0
  46. package/lib/__templates__/nextjs/README.md +36 -0
  47. package/lib/__templates__/nextjs/_gitignore +99 -0
  48. package/lib/__templates__/nextjs/_npmrc +22 -0
  49. package/lib/__templates__/nextjs/eslint.config.mjs +18 -0
  50. package/lib/__templates__/nextjs/next-env.d.ts +6 -0
  51. package/lib/__templates__/nextjs/next.config.ts +7 -0
  52. package/lib/__templates__/nextjs/package.json +32 -0
  53. package/lib/__templates__/nextjs/pnpm-lock.yaml +4061 -0
  54. package/lib/__templates__/nextjs/postcss.config.mjs +7 -0
  55. package/lib/__templates__/nextjs/public/file.svg +1 -0
  56. package/lib/__templates__/nextjs/public/globe.svg +1 -0
  57. package/lib/__templates__/nextjs/public/next.svg +1 -0
  58. package/lib/__templates__/nextjs/public/vercel.svg +1 -0
  59. package/lib/__templates__/nextjs/public/window.svg +1 -0
  60. package/lib/__templates__/nextjs/scripts/build.sh +14 -0
  61. package/lib/__templates__/nextjs/scripts/dev.sh +51 -0
  62. package/lib/__templates__/nextjs/scripts/start.sh +15 -0
  63. package/lib/__templates__/nextjs/src/app/favicon.ico +0 -0
  64. package/lib/__templates__/nextjs/src/app/globals.css +26 -0
  65. package/lib/__templates__/nextjs/src/app/layout.tsx +34 -0
  66. package/lib/__templates__/nextjs/src/app/page.tsx +66 -0
  67. package/lib/__templates__/nextjs/template.config.js +55 -0
  68. package/lib/__templates__/nextjs/tsconfig.json +34 -0
  69. package/lib/__templates__/react-rsbuild/.coze +11 -0
  70. package/lib/__templates__/react-rsbuild/.vscode/settings.json +121 -0
  71. package/lib/__templates__/react-rsbuild/README.md +61 -0
  72. package/lib/__templates__/react-rsbuild/_gitignore +97 -0
  73. package/lib/__templates__/react-rsbuild/_npmrc +22 -0
  74. package/lib/__templates__/react-rsbuild/package.json +31 -0
  75. package/lib/__templates__/react-rsbuild/pnpm-lock.yaml +997 -0
  76. package/lib/__templates__/react-rsbuild/rsbuild.config.ts +13 -0
  77. package/lib/__templates__/react-rsbuild/scripts/build.sh +14 -0
  78. package/lib/__templates__/react-rsbuild/scripts/dev.sh +51 -0
  79. package/lib/__templates__/react-rsbuild/scripts/start.sh +15 -0
  80. package/lib/__templates__/react-rsbuild/src/App.tsx +60 -0
  81. package/lib/__templates__/react-rsbuild/src/index.css +21 -0
  82. package/lib/__templates__/react-rsbuild/src/index.html +12 -0
  83. package/lib/__templates__/react-rsbuild/src/index.tsx +16 -0
  84. package/lib/__templates__/react-rsbuild/tailwind.config.js +9 -0
  85. package/lib/__templates__/react-rsbuild/template.config.js +54 -0
  86. package/lib/__templates__/react-rsbuild/tsconfig.json +17 -0
  87. package/lib/__templates__/rsbuild/.coze +11 -0
  88. package/lib/__templates__/rsbuild/.vscode/settings.json +7 -0
  89. package/lib/__templates__/rsbuild/README.md +61 -0
  90. package/lib/__templates__/rsbuild/_gitignore +97 -0
  91. package/lib/__templates__/rsbuild/_npmrc +22 -0
  92. package/lib/__templates__/rsbuild/package.json +24 -0
  93. package/lib/__templates__/rsbuild/pnpm-lock.yaml +888 -0
  94. package/lib/__templates__/rsbuild/rsbuild.config.ts +12 -0
  95. package/lib/__templates__/rsbuild/scripts/build.sh +14 -0
  96. package/lib/__templates__/rsbuild/scripts/dev.sh +51 -0
  97. package/lib/__templates__/rsbuild/scripts/start.sh +15 -0
  98. package/lib/__templates__/rsbuild/src/index.css +21 -0
  99. package/lib/__templates__/rsbuild/src/index.html +12 -0
  100. package/lib/__templates__/rsbuild/src/index.ts +5 -0
  101. package/lib/__templates__/rsbuild/src/main.ts +65 -0
  102. package/lib/__templates__/rsbuild/tailwind.config.js +9 -0
  103. package/lib/__templates__/rsbuild/template.config.js +54 -0
  104. package/lib/__templates__/rsbuild/tsconfig.json +16 -0
  105. package/lib/__templates__/templates.json +100 -0
  106. package/lib/__templates__/vite/.coze +11 -0
  107. package/lib/__templates__/vite/.vscode/settings.json +7 -0
  108. package/lib/__templates__/vite/README.md +61 -0
  109. package/lib/__templates__/vite/_gitignore +66 -0
  110. package/lib/__templates__/vite/_npmrc +22 -0
  111. package/lib/__templates__/vite/index.html +13 -0
  112. package/lib/__templates__/vite/package.json +24 -0
  113. package/lib/__templates__/vite/pnpm-lock.yaml +1249 -0
  114. package/lib/__templates__/vite/postcss.config.js +6 -0
  115. package/lib/__templates__/vite/scripts/build.sh +14 -0
  116. package/lib/__templates__/vite/scripts/dev.sh +51 -0
  117. package/lib/__templates__/vite/scripts/start.sh +15 -0
  118. package/lib/__templates__/vite/src/index.css +21 -0
  119. package/lib/__templates__/vite/src/index.ts +5 -0
  120. package/lib/__templates__/vite/src/main.ts +65 -0
  121. package/lib/__templates__/vite/tailwind.config.js +9 -0
  122. package/lib/__templates__/vite/template.config.js +55 -0
  123. package/lib/__templates__/vite/tsconfig.json +16 -0
  124. package/lib/__templates__/vite/vite.config.ts +15 -0
  125. package/lib/cli.js +1575 -0
  126. package/package.json +70 -0
package/lib/cli.js ADDED
@@ -0,0 +1,1575 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ var commander = require('commander');
5
+ var path = require('path');
6
+ var fs = require('fs');
7
+ var shelljs = require('shelljs');
8
+ var fs$1 = require('fs/promises');
9
+ var toml = require('@iarna/toml');
10
+ var jsYaml = require('js-yaml');
11
+ var perf_hooks = require('perf_hooks');
12
+ var addFormats = require('ajv-formats');
13
+ var Ajv = require('ajv');
14
+ var changeCase = require('change-case');
15
+ var ejs = require('ejs');
16
+
17
+ /**
18
+ * 读取 templates.json 配置
19
+ */
20
+ const loadTemplatesConfig$1 = () => {
21
+ // 尝试多个可能的路径
22
+ const possiblePaths = [
23
+ // 生产模式:lib/cli.js 打包后
24
+ path.join(__dirname, '__templates__/templates.json'),
25
+ // 开发模式:src/utils/template-help.ts
26
+ path.join(__dirname, '../__templates__/templates.json'),
27
+ ];
28
+
29
+ for (const templatesPath of possiblePaths) {
30
+ try {
31
+ const content = fs.readFileSync(templatesPath, 'utf-8');
32
+ // eslint-disable-next-line no-restricted-syntax
33
+ return JSON.parse(content) ;
34
+ } catch (error) {
35
+ // 继续尝试下一个路径
36
+ continue;
37
+ }
38
+ }
39
+
40
+ // 所有路径都失败,返回 null
41
+ return null;
42
+ };
43
+
44
+ /**
45
+ * 格式化单个参数信息
46
+ */
47
+ const formatParam = (paramName, param) => {
48
+ const parts = [];
49
+
50
+ // 参数名和类型
51
+ parts.push(` --${paramName} <${param.type}>`);
52
+
53
+ // 描述
54
+ if (param.description) {
55
+ parts.push(` ${param.description}`);
56
+ }
57
+
58
+ // 默认值
59
+ if (param.default !== undefined) {
60
+ parts.push(` Default: ${param.default}`);
61
+ }
62
+
63
+ // 范围限制
64
+ if (param.minimum !== undefined || param.maximum !== undefined) {
65
+ const range = [];
66
+ if (param.minimum !== undefined) {
67
+ range.push(`min: ${param.minimum}`);
68
+ }
69
+ if (param.maximum !== undefined) {
70
+ range.push(`max: ${param.maximum}`);
71
+ }
72
+ parts.push(` Range: ${range.join(', ')}`);
73
+ }
74
+
75
+ // 模式限制
76
+ if (param.pattern) {
77
+ parts.push(` Pattern: ${param.pattern}`);
78
+ }
79
+
80
+ return parts.join('\n');
81
+ };
82
+
83
+ /**
84
+ * 生成模板帮助文本
85
+ */
86
+ const generateTemplatesHelpText = () => {
87
+ const config = loadTemplatesConfig$1();
88
+
89
+ if (!config || config.templates.length === 0) {
90
+ return '';
91
+ }
92
+
93
+ const lines = [];
94
+
95
+ lines.push('\n📦 Available Templates:\n');
96
+ lines.push(` ${'='.repeat(76)}`);
97
+
98
+ config.templates.forEach((template, index) => {
99
+ if (index > 0) {
100
+ lines.push(` ${'-'.repeat(76)}`);
101
+ }
102
+
103
+ lines.push(`\n Template: ${template.name}`);
104
+ lines.push(` Description: ${template.description}`);
105
+
106
+ const params = Object.entries(template.paramsSchema);
107
+ if (params.length > 0) {
108
+ lines.push(' Parameters:');
109
+ params.forEach(([paramName, param]) => {
110
+ lines.push(formatParam(paramName, param));
111
+ });
112
+ }
113
+ });
114
+
115
+ lines.push(`\n ${'='.repeat(76)}`);
116
+ lines.push('\n Usage:');
117
+ lines.push(
118
+ ' coze init <directory> -t <template-name> [--param value ...]',
119
+ );
120
+ lines.push('\n Example:');
121
+ lines.push(' coze init my-app -t nextjs --appName my-app --port 3000');
122
+ lines.push('');
123
+
124
+ return lines.join('\n');
125
+ };
126
+
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) {
128
+ const ERROR = 0; LogLevel[LogLevel["ERROR"] = ERROR] = "ERROR";
129
+ const WARN = 1; LogLevel[LogLevel["WARN"] = WARN] = "WARN";
130
+ const SUCCESS = 2; LogLevel[LogLevel["SUCCESS"] = SUCCESS] = "SUCCESS";
131
+ const INFO = 3; LogLevel[LogLevel["INFO"] = INFO] = "INFO";
132
+ const VERBOSE = 4; LogLevel[LogLevel["VERBOSE"] = VERBOSE] = "VERBOSE";
133
+ })(LogLevel || (LogLevel = {}));
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+ const LOG_LEVEL_MAP = {
142
+ error: LogLevel.ERROR,
143
+ warn: LogLevel.WARN,
144
+ success: LogLevel.SUCCESS,
145
+ info: LogLevel.INFO,
146
+ verbose: LogLevel.VERBOSE,
147
+ };
148
+
149
+ const COLOR_CODES = {
150
+ reset: '\x1b[0m',
151
+ red: '\x1b[31m',
152
+ yellow: '\x1b[33m',
153
+ green: '\x1b[32m',
154
+ cyan: '\x1b[36m',
155
+ gray: '\x1b[90m',
156
+ };
157
+
158
+ class Logger {
159
+
160
+
161
+
162
+
163
+ constructor(options = {}) {
164
+ this.level = this.parseLogLevel(options.level);
165
+ this.useColor = _nullishCoalesce$2(options.useColor, () => ( this.isColorSupported()));
166
+ this.prefix = options.prefix;
167
+ }
168
+
169
+ parseLogLevel(level) {
170
+ if (level !== undefined) {
171
+ return level;
172
+ }
173
+
174
+ const envLevel = _optionalChain$4([process, 'access', _ => _.env, 'access', _2 => _2.LOG_LEVEL, 'optionalAccess', _3 => _3.toLowerCase, 'call', _4 => _4()]);
175
+ if (envLevel && envLevel in LOG_LEVEL_MAP) {
176
+ return LOG_LEVEL_MAP[envLevel];
177
+ }
178
+
179
+ return LogLevel.INFO;
180
+ }
181
+
182
+ isColorSupported() {
183
+ // 简单检测:Node.js 环境且支持 TTY
184
+ return (
185
+ typeof process !== 'undefined' &&
186
+ _optionalChain$4([process, 'access', _5 => _5.stdout, 'optionalAccess', _6 => _6.isTTY]) === true &&
187
+ process.env.NO_COLOR === undefined
188
+ );
189
+ }
190
+
191
+ colorize(text, color) {
192
+ if (!this.useColor) {
193
+ return text;
194
+ }
195
+ return `${COLOR_CODES[color]}${text}${COLOR_CODES.reset}`;
196
+ }
197
+
198
+ log(options
199
+
200
+
201
+
202
+
203
+
204
+ ) {
205
+ if (options.level > this.level) {
206
+ return;
207
+ }
208
+
209
+ const icon = this.colorize(options.icon, options.color);
210
+ const prefix = this.prefix ? `${icon} ${this.prefix}` : icon;
211
+ console.log(prefix, options.message, ...(_nullishCoalesce$2(options.args, () => ( []))));
212
+ }
213
+
214
+ error(message, ...args) {
215
+ this.log({
216
+ level: LogLevel.ERROR,
217
+ icon: '✖',
218
+ color: 'red',
219
+ message,
220
+ args,
221
+ });
222
+ }
223
+
224
+ warn(message, ...args) {
225
+ this.log({
226
+ level: LogLevel.WARN,
227
+ icon: '⚠',
228
+ color: 'yellow',
229
+ message,
230
+ args,
231
+ });
232
+ }
233
+
234
+ success(message, ...args) {
235
+ this.log({
236
+ level: LogLevel.SUCCESS,
237
+ icon: '✓',
238
+ color: 'green',
239
+ message,
240
+ args,
241
+ });
242
+ }
243
+
244
+ info(message, ...args) {
245
+ this.log({
246
+ level: LogLevel.INFO,
247
+ icon: 'ℹ',
248
+ color: 'cyan',
249
+ message,
250
+ args,
251
+ });
252
+ }
253
+
254
+ verbose(message, ...args) {
255
+ this.log({
256
+ level: LogLevel.VERBOSE,
257
+ icon: '→',
258
+ color: 'gray',
259
+ message,
260
+ args,
261
+ });
262
+ }
263
+ }
264
+
265
+ // 创建 logger 实例的工厂函数
266
+ const createLogger = (options = {}) =>
267
+ new Logger(options);
268
+
269
+ // 导出默认实例
270
+ const logger = createLogger();
271
+
272
+ function _optionalChain$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/* eslint-disable @typescript-eslint/no-explicit-any */
273
+ // Safe JSON parsing utilities with type safety and error handling
274
+ // Provides fallback values, validation, and error monitoring capabilities
275
+
276
+ /**
277
+ * Options for safe JSON parsing
278
+ */
279
+
280
+
281
+
282
+
283
+
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+ /**
312
+ * Safely parse JSON with error handling and type safety
313
+ *
314
+ * @example
315
+ * ```ts
316
+ * // Basic usage - returns unknown | undefined
317
+ * const data = safeJsonParse('{"a":1}'); // { a: 1 }
318
+ *
319
+ * // With default value - always returns T
320
+ * const config = safeJsonParse(str, {});
321
+ * const user = safeJsonParse(str, null);
322
+ *
323
+ * // With error reporting
324
+ * const data = safeJsonParse(input, null, {
325
+ * onError: (error, input) => logger.error('Parse failed', { error, input })
326
+ * });
327
+ *
328
+ * // With validation
329
+ * const isUser = (data: unknown): data is User => ...;
330
+ * const user = safeJsonParse<User>(input, null, { validate: isUser });
331
+ * ```
332
+ */
333
+ function safeJsonParse(
334
+ input,
335
+ defaultValueOrOptions,
336
+ optionsArg,
337
+ ) {
338
+ // Parse arguments
339
+ let defaultValue;
340
+ let options;
341
+
342
+ if (arguments.length === 2) {
343
+ // safeJsonParse(input, options) or safeJsonParse(input, defaultValue)
344
+ {
345
+ defaultValue = defaultValueOrOptions ;
346
+ options = undefined;
347
+ }
348
+ } else if (arguments.length === 3) {
349
+ // safeJsonParse(input, defaultValue, options)
350
+ defaultValue = defaultValueOrOptions ;
351
+ options = optionsArg;
352
+ }
353
+
354
+ // If input is already an object (and not null), return it directly
355
+ if (typeof input === 'object' && input !== null) {
356
+ return input ;
357
+ }
358
+
359
+ try {
360
+ const parsed = JSON.parse(String(input));
361
+
362
+ // Optional validation
363
+ if (_optionalChain$3([options, 'optionalAccess', _ => _.validate])) {
364
+ if (options.validate(parsed)) {
365
+ return parsed;
366
+ } else {
367
+ const validationError = new Error('JSON validation failed');
368
+ _optionalChain$3([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
369
+
370
+ if (options.throwOnValidationError) {
371
+ throw validationError;
372
+ }
373
+ return defaultValue;
374
+ }
375
+ }
376
+
377
+ return parsed;
378
+ } catch (error) {
379
+ // 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])) {
381
+ throw error;
382
+ }
383
+ _optionalChain$3([options, 'optionalAccess', _5 => _5.onError, 'optionalCall', _6 => _6(error , input)]);
384
+ return defaultValue;
385
+ }
386
+ }
387
+
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; }
389
+
390
+
391
+ /**
392
+ * 检测并解析配置文件内容
393
+ *
394
+ * @param content - 文件内容
395
+ * @returns 解析后的配置对象
396
+ */
397
+ const parseConfigContent = (content) => {
398
+ // 1. 尝试 TOML 格式 (默认)
399
+ try {
400
+ const config = toml.parse(content);
401
+ return config ;
402
+ } catch (error) {
403
+ // TOML 解析失败,继续尝试其他格式
404
+ // eslint-disable-next-line no-console
405
+ console.debug('TOML parse failed:', error);
406
+ }
407
+
408
+ // 2. 尝试 YAML 格式
409
+ try {
410
+ const config = jsYaml.load(content);
411
+ if (config && typeof config === 'object') {
412
+ return config ;
413
+ }
414
+ } catch (error) {
415
+ // YAML 解析失败,继续尝试其他格式
416
+ // eslint-disable-next-line no-console
417
+ console.debug('YAML parse failed:', error);
418
+ }
419
+
420
+ // 3. 尝试 JSON 格式
421
+ const config = safeJsonParse(content);
422
+ if (config) {
423
+ return config;
424
+ }
425
+
426
+ return null;
427
+ };
428
+
429
+ /**
430
+ * 加载 .coze 配置文件
431
+ *
432
+ * @param projectPath - 项目目录路径
433
+ * @returns .coze 配置对象
434
+ */
435
+ const loadCozeConfig = async (
436
+ projectPath = process.cwd(),
437
+ ) => {
438
+ const cozeConfigPath = path.join(projectPath, '.coze');
439
+
440
+ let content;
441
+ try {
442
+ content = await fs$1.readFile(cozeConfigPath, 'utf-8');
443
+ } catch (error) {
444
+ throw new Error(
445
+ `.coze config file not found in ${projectPath}\n` +
446
+ 'Please ensure you are in a project directory initialized with coze-coding.\n' +
447
+ `Error: ${error instanceof Error ? error.message : String(error)}`,
448
+ );
449
+ }
450
+
451
+ const config = parseConfigContent(content);
452
+ if (!config) {
453
+ throw new Error(
454
+ 'Failed to parse .coze config file.\n' +
455
+ 'Please ensure the file is in valid TOML, YAML, or JSON format.',
456
+ );
457
+ }
458
+
459
+ return config;
460
+ };
461
+
462
+ /**
463
+ * 获取指定命令的配置
464
+ *
465
+ * @param config - .coze 配置对象
466
+ * @param commandName - 命令名称 (dev/build/start)
467
+ * @returns 命令配置数组
468
+ */
469
+ const getCommandConfig = (
470
+ config,
471
+ commandName,
472
+ ) => {
473
+ let commandConfig;
474
+
475
+ // 根据命令名称映射到配置路径
476
+ switch (commandName) {
477
+ case 'dev':
478
+ commandConfig = _optionalChain$2([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
479
+ break;
480
+ case 'build':
481
+ commandConfig = _optionalChain$2([config, 'access', _3 => _3.deploy, 'optionalAccess', _4 => _4.build]);
482
+ break;
483
+ case 'start':
484
+ commandConfig = _optionalChain$2([config, 'access', _5 => _5.deploy, 'optionalAccess', _6 => _6.run]);
485
+ break;
486
+ default:
487
+ throw new Error(`Unknown command: ${commandName}`);
488
+ }
489
+
490
+ if (!commandConfig || commandConfig.length === 0) {
491
+ throw new Error(
492
+ `Command '${commandName}' is not configured in .coze file.\n` +
493
+ 'Please add the corresponding configuration to your .coze file.',
494
+ );
495
+ }
496
+
497
+ return commandConfig;
498
+ };
499
+
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; }
501
+
502
+ /**
503
+ * 创建日志管理器
504
+ */
505
+ const createLogManager = (logDir = '.coze-logs') => {
506
+ const ensureLogDir = () => {
507
+ if (!fs.existsSync(logDir)) {
508
+ fs.mkdirSync(logDir, { recursive: true });
509
+ }
510
+ };
511
+
512
+ const getLogPath = (logFile) => path.join(logDir, logFile);
513
+
514
+ return {
515
+ createWriteStream: (logFile) => {
516
+ ensureLogDir();
517
+ return fs.createWriteStream(getLogPath(logFile), { flags: 'a' });
518
+ },
519
+ };
520
+ };
521
+
522
+ /**
523
+ * 执行命令的内部实现
524
+ */
525
+ const executeRun = async (
526
+ commandName,
527
+ options = {},
528
+ ) => {
529
+ try {
530
+ logger.info(`Running ${commandName} command...`);
531
+
532
+ // 1. 加载 .coze 配置
533
+ const config = await loadCozeConfig();
534
+ const commandArgs = getCommandConfig(config, commandName);
535
+
536
+ // 2. 准备日志
537
+ const logManager = createLogManager();
538
+ const logFile = options.logFile || `${commandName}.log`;
539
+ const logStream = logManager.createWriteStream(logFile);
540
+
541
+ // 3. 执行命令
542
+ const commandString = commandArgs.join(' ');
543
+
544
+ logger.info(`Executing: ${commandString}`);
545
+ logger.info(`Working directory: ${process.cwd()}`);
546
+ logger.info(`Log file: ${logFile}`);
547
+
548
+ const childProcess = shelljs.exec(commandString, {
549
+ async: true,
550
+ silent: true, // 不自动输出,我们手动处理
551
+ });
552
+
553
+ if (!childProcess) {
554
+ throw new Error('Failed to create child process');
555
+ }
556
+
557
+ // 将输出同时写入控制台和日志文件
558
+ _optionalChain$1([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
559
+ process.stdout.write(data);
560
+ logStream.write(data);
561
+ })]);
562
+
563
+ _optionalChain$1([childProcess, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
564
+ process.stderr.write(data);
565
+ logStream.write(data);
566
+ })]);
567
+
568
+ childProcess.on('close', (code, signal) => {
569
+ logStream.end();
570
+
571
+ if (code !== 0) {
572
+ logger.error(
573
+ `Command exited with code ${_nullishCoalesce$1(code, () => ( 'unknown'))}${signal ? ` and signal ${signal}` : ''}`,
574
+ );
575
+ logger.error(`Check log file for details: ${logFile}`);
576
+ process.exit(code || 1);
577
+ } else {
578
+ logger.success('Command completed successfully');
579
+ logger.info(`Log file: ${logFile}`);
580
+ }
581
+ });
582
+
583
+ childProcess.on('error', (error) => {
584
+ logger.error('Failed to execute command:');
585
+ logger.error(`Error: ${error.message}`);
586
+ if (error.stack) {
587
+ logger.error(`Stack trace:\n${error.stack}`);
588
+ }
589
+ logStream.end();
590
+ process.exit(1);
591
+ });
592
+ } catch (error) {
593
+ logger.error(`Failed to run ${commandName} command:`);
594
+ logger.error(error instanceof Error ? error.message : String(error));
595
+ process.exit(1);
596
+ }
597
+ };
598
+
599
+ /**
600
+ * 注册 dev/build/start 命令到 program
601
+ */
602
+ const registerCommand$1 = program => {
603
+ // dev 命令
604
+ program
605
+ .command('dev')
606
+ .description('Start development server')
607
+ .option('--log-file <path>', 'Log file path')
608
+ .action(async options => {
609
+ await executeRun('dev', options);
610
+ });
611
+
612
+ // build 命令
613
+ program
614
+ .command('build')
615
+ .description('Build for production')
616
+ .option('--log-file <path>', 'Log file path')
617
+ .action(async options => {
618
+ await executeRun('build', options);
619
+ });
620
+
621
+ // start 命令
622
+ program
623
+ .command('start')
624
+ .description('Start production server')
625
+ .option('--log-file <path>', 'Log file path')
626
+ .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;
681
+ };
682
+
683
+ /**
684
+ * 获取模板目录路径
685
+ * @returns __templates__ 目录的绝对路径
686
+ */
687
+ const getTemplatesDir = () => {
688
+ const templatesDir = path.resolve(__dirname, './__templates__');
689
+ logger.verbose(`Templates directory: ${templatesDir}`);
690
+ logger.verbose(`Templates directory exists: ${fs.existsSync(templatesDir)}`);
691
+ return templatesDir;
692
+ };
693
+
694
+ /**
695
+ * 创建 AJV 验证器实例
696
+ */
697
+ const createAjvInstance = () => {
698
+ const ajv = new Ajv({
699
+ useDefaults: true, // 自动应用默认值
700
+ coerceTypes: true, // 类型强制转换
701
+ removeAdditional: false, // 保留额外属性以便后续报错
702
+ allErrors: true, // 收集所有错误
703
+ });
704
+
705
+ addFormats(ajv);
706
+ return ajv;
707
+ };
708
+
709
+ /**
710
+ * 验证参数
711
+ *
712
+ * @param schema - JSON Schema 定义
713
+ * @param params - 待验证的参数
714
+ * @returns 验证后的参数(应用了默认值)
715
+ * @throws 验证失败时抛出错误
716
+ */
717
+ const validateParams = (
718
+ schema,
719
+ params,
720
+ ) => {
721
+ const ajv = createAjvInstance();
722
+ const validate = ajv.compile(schema);
723
+
724
+ const isValid = validate(params);
725
+
726
+ if (!isValid) {
727
+ const errors = validate.errors || [];
728
+ const errorMessages = errors.map(err => {
729
+ const path = err.instancePath || '/';
730
+ const message = err.message || 'validation failed';
731
+ return ` - ${path}: ${message}`;
732
+ });
733
+
734
+ throw new Error(
735
+ `Parameter validation failed:\n${errorMessages.join('\n')}\n\nPlease check your parameters and try again.`,
736
+ );
737
+ }
738
+
739
+ return params ;
740
+ };
741
+
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
+ /**
882
+ * 从 Commander 解析透传参数
883
+ * 将 kebab-case 的 CLI 参数转换为 camelCase
884
+ *
885
+ * @param command - Commander 命令实例
886
+ * @param knownOptions - 已知的选项集合(不需要透传的选项)
887
+ * @returns 参数对象
888
+ */
889
+ const parsePassThroughParams = (
890
+ command,
891
+ knownOptions = new Set(),
892
+ ) => {
893
+ const rawOptions = command.opts();
894
+
895
+ const initial = {};
896
+ return Object.entries(rawOptions).reduce(
897
+ (params, [key, value]) => {
898
+ if (knownOptions.has(key)) {
899
+ return params;
900
+ }
901
+
902
+ const camelKey = changeCase.camelCase(key);
903
+ return { ...params, [camelKey]: value };
904
+ },
905
+ initial,
906
+ );
907
+ };
908
+
909
+ /**
910
+ * 渲染 EJS 模板文件
911
+ *
912
+ * @param filePath - 模板文件路径
913
+ * @param context - 模板上下文
914
+ * @returns 渲染后的内容
915
+ */
916
+ const renderTemplate = async (
917
+ filePath,
918
+ context,
919
+ ) => {
920
+ const content = await fs$1.readFile(filePath, 'utf-8');
921
+
922
+ try {
923
+ return ejs.render(content, context, {
924
+ filename: filePath,
925
+ });
926
+ } catch (error) {
927
+ throw new Error(
928
+ `Failed to render template ${filePath}:\n${error instanceof Error ? error.message : String(error)}`,
929
+ );
930
+ }
931
+ };
932
+
933
+ /**
934
+ * 判断文件是否需要渲染(是否为文本文件)
935
+ *
936
+ * @param filePath - 文件路径
937
+ * @returns 是否需要渲染
938
+ */
939
+ const shouldRenderFile = (filePath) => {
940
+ const textExtensions = new Set([
941
+ '.js',
942
+ '.ts',
943
+ '.jsx',
944
+ '.tsx',
945
+ '.json',
946
+ '.css',
947
+ '.scss',
948
+ '.sass',
949
+ '.less',
950
+ '.html',
951
+ '.md',
952
+ '.txt',
953
+ '.yaml',
954
+ '.yml',
955
+ '.xml',
956
+ '.svg',
957
+ '.env',
958
+ '.gitignore',
959
+ '.npmrc',
960
+ '.eslintrc',
961
+ '.prettierrc',
962
+ '.babelrc',
963
+ '.sh',
964
+ ]);
965
+
966
+ const ext = path.extname(filePath).toLowerCase();
967
+
968
+ // 没有扩展名的文件(如 .coze)也尝试渲染
969
+ if (ext === '') {
970
+ return true;
971
+ }
972
+
973
+ return textExtensions.has(ext);
974
+ };
975
+
976
+ /**
977
+ * 判断文件是否应该被忽略(不复制到目标目录)
978
+ *
979
+ * @param filePath - 文件路径
980
+ * @returns 是否应该忽略
981
+ */
982
+ const shouldIgnoreFile = (filePath) => {
983
+ const fileName = path.basename(filePath);
984
+ const pathParts = filePath.split(path.sep);
985
+
986
+ // 精确匹配的文件名
987
+ const exactMatch = ['template.config.ts', 'template.config.js', '.DS_Store'];
988
+
989
+ // 目录名(需要检查路径中是否包含)
990
+ const directoryPatterns = ['node_modules'];
991
+
992
+ // 检查精确匹配
993
+ if (exactMatch.includes(fileName)) {
994
+ return true;
995
+ }
996
+
997
+ // 检查路径中是否包含需要忽略的目录
998
+ return directoryPatterns.some(dir => pathParts.includes(dir));
999
+ };
1000
+
1001
+ /**
1002
+ * 递归获取目录中的所有文件
1003
+ *
1004
+ * @param dir - 目录路径
1005
+ * @param baseDir - 基础目录(用于计算相对路径)
1006
+ * @returns 文件相对路径数组
1007
+ */
1008
+ const getAllFiles = async (
1009
+ dir,
1010
+ baseDir = dir,
1011
+ ) => {
1012
+ logger.verbose(`Scanning directory: ${dir}`);
1013
+
1014
+ const entries = await fs$1.readdir(dir, { withFileTypes: true });
1015
+
1016
+ logger.verbose(`Found ${entries.length} entries in ${path.basename(dir)}`);
1017
+
1018
+ const results = await Promise.all(
1019
+ entries.map(entry => {
1020
+ const fullPath = path.join(dir, entry.name);
1021
+ const relativePath = path.relative(baseDir, fullPath);
1022
+
1023
+ if (shouldIgnoreFile(relativePath)) {
1024
+ logger.verbose(` - Ignoring: ${entry.name}`);
1025
+ return [];
1026
+ }
1027
+
1028
+ if (entry.isDirectory()) {
1029
+ logger.verbose(` - Entering directory: ${entry.name}`);
1030
+ return getAllFiles(fullPath, baseDir);
1031
+ }
1032
+
1033
+ logger.verbose(` - Found file: ${entry.name}`);
1034
+ return [relativePath];
1035
+ }),
1036
+ );
1037
+
1038
+ return results.flat();
1039
+ };
1040
+
1041
+ /**
1042
+ * 确保目录存在
1043
+ *
1044
+ * @param dir - 目录路径
1045
+ */
1046
+ const ensureDir = async (dir) => {
1047
+ await fs$1.mkdir(dir, { recursive: true });
1048
+ };
1049
+
1050
+ /**
1051
+ * 复制并处理模板文件到目标目录
1052
+ *
1053
+ * @param templatePath - 模板目录路径
1054
+ * @param outputPath - 输出目录路径
1055
+ * @param context - 模板上下文
1056
+ */
1057
+ const processTemplateFiles = async (
1058
+ templatePath,
1059
+ outputPath,
1060
+ context,
1061
+ ) => {
1062
+ logger.verbose('Processing template files:');
1063
+ logger.verbose(` - Template path: ${templatePath}`);
1064
+ logger.verbose(` - Output path: ${outputPath}`);
1065
+
1066
+ // 验证模板目录是否存在
1067
+ try {
1068
+ const stat = await fs$1.stat(templatePath);
1069
+ logger.verbose(
1070
+ ` - Template path exists: ${stat.isDirectory() ? 'directory' : 'file'}`,
1071
+ );
1072
+ if (!stat.isDirectory()) {
1073
+ throw new Error(`Template path is not a directory: ${templatePath}`);
1074
+ }
1075
+ } catch (error) {
1076
+ logger.error(
1077
+ ` - Failed to access template path: ${error instanceof Error ? error.message : String(error)}`,
1078
+ );
1079
+ throw error;
1080
+ }
1081
+
1082
+ const files = await getAllFiles(templatePath);
1083
+
1084
+ logger.verbose(` - Found ${files.length} files to process`);
1085
+
1086
+ if (files.length === 0) {
1087
+ logger.warn(' - No files found in template directory!');
1088
+ return;
1089
+ }
1090
+
1091
+ await Promise.all(
1092
+ files.map(async file => {
1093
+ const srcPath = path.join(templatePath, file);
1094
+ // 将模板中 _ 开头的文件转换为 . 开头(如 _gitignore -> .gitignore)
1095
+ const destFile = file.replace(/(^|\/|\\)_([^/\\]+)$/g, '$1.$2');
1096
+ const destPath = path.join(outputPath, destFile);
1097
+
1098
+ logger.verbose(
1099
+ ` - Processing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1100
+ );
1101
+
1102
+ // 确保目标目录存在
1103
+ await ensureDir(path.dirname(destPath));
1104
+
1105
+ if (shouldRenderFile(srcPath)) {
1106
+ // 渲染文本文件
1107
+ const rendered = await renderTemplate(srcPath, context);
1108
+ await fs$1.writeFile(destPath, rendered, 'utf-8');
1109
+ logger.verbose(' ✓ Rendered and written');
1110
+ } else {
1111
+ // 直接复制二进制文件
1112
+ await fs$1.copyFile(srcPath, destPath);
1113
+ logger.verbose(' ✓ Copied');
1114
+ }
1115
+ }),
1116
+ );
1117
+
1118
+ logger.verbose('✓ All files processed successfully');
1119
+
1120
+ // 单独处理 node_modules 目录(如果存在)
1121
+ const sourceNodeModules = path.join(templatePath, 'node_modules');
1122
+ const targetNodeModules = path.join(outputPath, 'node_modules');
1123
+
1124
+ if (fs.existsSync(sourceNodeModules)) {
1125
+ logger.info('\nCopying node_modules from pre-warmed template...');
1126
+ logger.verbose(` From: ${sourceNodeModules}`);
1127
+ logger.verbose(` To: ${targetNodeModules}`);
1128
+
1129
+ const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1130
+ silent: true,
1131
+ });
1132
+
1133
+ if (result.stdout) {
1134
+ process.stdout.write(result.stdout);
1135
+ }
1136
+
1137
+ if (result.stderr) {
1138
+ process.stderr.write(result.stderr);
1139
+ }
1140
+
1141
+ if (result.code === 0) {
1142
+ logger.success('✓ node_modules copied successfully');
1143
+ } else {
1144
+ logger.warn(
1145
+ `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1146
+ );
1147
+ logger.info('Will need to run pnpm install manually');
1148
+ }
1149
+ }
1150
+ };
1151
+
1152
+ /**
1153
+ * 检查输出目录是否为空
1154
+ *
1155
+ * @param outputPath - 输出目录路径
1156
+ * @returns 是否为空
1157
+ */
1158
+ const isOutputDirEmpty = async (
1159
+ outputPath,
1160
+ ) => {
1161
+ try {
1162
+ const entries = await fs$1.readdir(outputPath);
1163
+ return entries.length === 0;
1164
+ } catch (e) {
1165
+ // 目录不存在,视为空
1166
+ return true;
1167
+ }
1168
+ };
1169
+
1170
+ /**
1171
+ * 验证输出目录
1172
+ *
1173
+ * @param outputPath - 输出目录路径
1174
+ * @throws 如果目录不为空则抛出错误
1175
+ */
1176
+ const validateOutputDir = async (outputPath) => {
1177
+ const isEmpty = await isOutputDirEmpty(outputPath);
1178
+ if (!isEmpty) {
1179
+ throw new Error(
1180
+ `Output directory is not empty: ${outputPath}\n` +
1181
+ 'Please use an empty directory or remove existing files.',
1182
+ );
1183
+ }
1184
+ };
1185
+
1186
+ /**
1187
+ * 模板引擎执行选项
1188
+ */
1189
+
1190
+
1191
+
1192
+
1193
+
1194
+
1195
+ /**
1196
+ * 加载模板元数据和路径
1197
+ */
1198
+ const loadTemplateMetadata = async (templateName) => {
1199
+ const templatesConfigPath = getTemplatesConfigPath();
1200
+ const templatesConfig = await loadTemplatesConfig(templatesConfigPath);
1201
+ const templateMetadata = findTemplate(templatesConfig, templateName);
1202
+ const templatesDir = getTemplatesDir();
1203
+ const templatePath = await getTemplatePath(templatesDir, templateMetadata);
1204
+
1205
+ return { templatePath, templateMetadata };
1206
+ };
1207
+
1208
+ /**
1209
+ * 解析并验证模板参数
1210
+ */
1211
+ const parseAndValidateParams = (
1212
+ command,
1213
+ templateConfig,
1214
+ ) => {
1215
+ const knownOptions = new Set([
1216
+ 'template',
1217
+ 't',
1218
+ 'output',
1219
+ 'o',
1220
+ 'skipInstall',
1221
+ 'skip-install',
1222
+ 'skipGit',
1223
+ 'skip-git',
1224
+ 'skipDev',
1225
+ 'skip-dev',
1226
+ ]);
1227
+ const userParams = parsePassThroughParams(command, knownOptions);
1228
+
1229
+ // 合并默认参数:defaultParams < schema defaults < userParams
1230
+ const paramsWithDefaults = {
1231
+ ...(templateConfig.defaultParams || {}),
1232
+ ...userParams,
1233
+ };
1234
+
1235
+ return validateParams(templateConfig.paramsSchema, paramsWithDefaults);
1236
+ };
1237
+
1238
+ /**
1239
+ * 执行生命周期钩子
1240
+ */
1241
+ const executeBeforeRenderHook = async (
1242
+ templateConfig,
1243
+ context,
1244
+ ) => {
1245
+ if (!templateConfig.onBeforeRender) {
1246
+ return context;
1247
+ }
1248
+ return (await templateConfig.onBeforeRender(context)) ;
1249
+ };
1250
+
1251
+ /**
1252
+ * 执行渲染后钩子
1253
+ */
1254
+ const executeAfterRenderHook = async (
1255
+ templateConfig,
1256
+ context,
1257
+ outputPath,
1258
+ ) => {
1259
+ if (templateConfig.onAfterRender) {
1260
+ await templateConfig.onAfterRender(context, outputPath);
1261
+ }
1262
+ };
1263
+
1264
+ /**
1265
+ * 准备输出目录
1266
+ */
1267
+ const prepareOutputDirectory = async (outputPath) => {
1268
+ const absolutePath = path.resolve(process.cwd(), outputPath);
1269
+ await validateOutputDir(absolutePath);
1270
+ return absolutePath;
1271
+ };
1272
+
1273
+ /**
1274
+ * 执行完整的模板渲染流程
1275
+ */
1276
+ const execute = async (
1277
+ options,
1278
+ ) => {
1279
+ const { templateName, outputPath, command } = options;
1280
+
1281
+ // 1. 加载模板
1282
+ const { templatePath } = await loadTemplateMetadata(templateName);
1283
+
1284
+ // 2. 加载模板配置
1285
+ const templateConfig = await loadTemplateConfig(templatePath);
1286
+
1287
+ // 3. 解析并验证参数
1288
+ const validatedParams = parseAndValidateParams(command, templateConfig);
1289
+
1290
+ // 4. 执行 onBeforeRender 钩子
1291
+ const context = await executeBeforeRenderHook(templateConfig, {
1292
+ ...validatedParams,
1293
+ });
1294
+
1295
+ // 5. 准备输出目录
1296
+ const absoluteOutputPath = await prepareOutputDirectory(outputPath);
1297
+
1298
+ // 6. 处理模板文件
1299
+ await processTemplateFiles(templatePath, absoluteOutputPath, context);
1300
+
1301
+ // 7. 执行 onAfterRender 钩子
1302
+ await executeAfterRenderHook(templateConfig, context, absoluteOutputPath);
1303
+
1304
+ return absoluteOutputPath;
1305
+ };
1306
+
1307
+ 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; }
1308
+ /**
1309
+ * 运行 pnpm install
1310
+ */
1311
+ const runPnpmInstall = (projectPath) => {
1312
+ logger.info('\nInstalling dependencies with pnpm...');
1313
+ logger.info(`Executing: pnpm install in ${projectPath}`);
1314
+
1315
+ const result = shelljs.exec('pnpm install', {
1316
+ cwd: projectPath,
1317
+ silent: true, // 使用 silent 来捕获输出
1318
+ });
1319
+
1320
+ // 输出 stdout
1321
+ if (result.stdout) {
1322
+ process.stdout.write(result.stdout);
1323
+ }
1324
+
1325
+ // 输出 stderr
1326
+ if (result.stderr) {
1327
+ process.stderr.write(result.stderr);
1328
+ }
1329
+
1330
+ if (result.code === 0) {
1331
+ logger.success('Dependencies installed successfully!');
1332
+ } else {
1333
+ const errorMessage = [
1334
+ `pnpm install failed with exit code ${result.code}`,
1335
+ result.stderr ? `\nStderr:\n${result.stderr}` : '',
1336
+ result.stdout ? `\nStdout:\n${result.stdout}` : '',
1337
+ ]
1338
+ .filter(Boolean)
1339
+ .join('');
1340
+
1341
+ throw new Error(errorMessage);
1342
+ }
1343
+ };
1344
+
1345
+ /**
1346
+ * 初始化 git 仓库并创建初始提交
1347
+ */
1348
+ const runGitInit = (projectPath) => {
1349
+ const runGitCommand = (command) => {
1350
+ logger.info(`Executing: ${command}`);
1351
+
1352
+ const result = shelljs.exec(command, {
1353
+ cwd: projectPath,
1354
+ silent: true,
1355
+ });
1356
+
1357
+ // 输出命令的结果
1358
+ if (result.stdout) {
1359
+ process.stdout.write(result.stdout);
1360
+ }
1361
+
1362
+ if (result.stderr) {
1363
+ process.stderr.write(result.stderr);
1364
+ }
1365
+
1366
+ if (result.code !== 0) {
1367
+ const errorMessage = [
1368
+ `${command} failed with exit code ${result.code}`,
1369
+ result.stderr ? `\nStderr:\n${result.stderr}` : '',
1370
+ result.stdout ? `\nStdout:\n${result.stdout}` : '',
1371
+ ]
1372
+ .filter(Boolean)
1373
+ .join('');
1374
+
1375
+ throw new Error(errorMessage);
1376
+ }
1377
+ };
1378
+
1379
+ try {
1380
+ logger.info('\nInitializing git repository...');
1381
+ runGitCommand('git init');
1382
+
1383
+ logger.info('Adding files to git...');
1384
+ runGitCommand('git add .');
1385
+
1386
+ logger.info('Creating initial commit...');
1387
+ runGitCommand('git commit -m "chore: initial commit"');
1388
+
1389
+ logger.success('Git repository initialized successfully!');
1390
+ } catch (error) {
1391
+ // Git 初始化失败不应该导致整个流程失败
1392
+ logger.warn(
1393
+ `Git initialization failed: ${error instanceof Error ? error.message : String(error)}`,
1394
+ );
1395
+ logger.info('You can manually initialize git later with: git init');
1396
+ }
1397
+ };
1398
+
1399
+ /**
1400
+ * 运行开发服务器
1401
+ */
1402
+ const runNpmDev = (projectPath) => {
1403
+ logger.info('\nStarting development server...');
1404
+ logger.info(`Executing: npm run dev in ${projectPath}`);
1405
+ logger.info('Press Ctrl+C to stop the server\n');
1406
+
1407
+ // 使用 async: true 异步执行,不阻塞进程
1408
+ const child = shelljs.exec('npm run dev', {
1409
+ cwd: projectPath,
1410
+ async: true,
1411
+ silent: true, // 手动处理输出以便显示详细信息
1412
+ });
1413
+
1414
+ if (child) {
1415
+ // 输出 stdout
1416
+ _optionalChain([child, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
1417
+ process.stdout.write(data);
1418
+ })]);
1419
+
1420
+ // 输出 stderr
1421
+ _optionalChain([child, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
1422
+ process.stderr.write(data);
1423
+ })]);
1424
+
1425
+ // 监听错误
1426
+ child.on('error', (error) => {
1427
+ logger.error(`Failed to start dev server: ${error.message}`);
1428
+ logger.error(`Error stack: ${error.stack}`);
1429
+ });
1430
+
1431
+ // 监听退出
1432
+ child.on('exit', (code, signal) => {
1433
+ if (code !== 0 && code !== null) {
1434
+ logger.error(
1435
+ `Dev server exited with code ${code}${signal ? ` and signal ${signal}` : ''}`,
1436
+ );
1437
+ }
1438
+ });
1439
+ }
1440
+ };
1441
+
1442
+ /**
1443
+ * 执行 init 命令的内部实现
1444
+ */
1445
+ const executeInit = async (
1446
+ options
1447
+
1448
+
1449
+
1450
+
1451
+
1452
+ ,
1453
+ command,
1454
+ ) => {
1455
+ const timer = new TimeTracker();
1456
+
1457
+ try {
1458
+ const {
1459
+ template: templateName,
1460
+ output: outputPath,
1461
+ skipInstall,
1462
+ skipGit,
1463
+ skipDev,
1464
+ } = options;
1465
+
1466
+ logger.info(`Initializing project with template: ${templateName}`);
1467
+ timer.logPhase('Initialization');
1468
+
1469
+ // 执行模板引擎,返回绝对路径
1470
+ const absoluteOutputPath = await execute({
1471
+ templateName,
1472
+ outputPath,
1473
+ command,
1474
+ });
1475
+
1476
+ timer.logPhase('Template engine execution');
1477
+ logger.success('Project created successfully!');
1478
+
1479
+ // 如果没有跳过安装,检查是否需要运行 pnpm install
1480
+ if (!skipInstall) {
1481
+ const nodeModulesPath = path.join(absoluteOutputPath, 'node_modules');
1482
+ const hasNodeModules = fs.existsSync(nodeModulesPath);
1483
+
1484
+ if (hasNodeModules) {
1485
+ logger.info(
1486
+ '\n💡 Using pre-warmed node_modules, skipping pnpm install',
1487
+ );
1488
+ timer.logPhase('Node modules (pre-warmed)');
1489
+ } else {
1490
+ runPnpmInstall(absoluteOutputPath);
1491
+ timer.logPhase('Dependencies installation');
1492
+ }
1493
+ }
1494
+
1495
+ // 如果没有跳过 git,则初始化 git 仓库
1496
+ if (!skipGit) {
1497
+ runGitInit(absoluteOutputPath);
1498
+ timer.logPhase('Git initialization');
1499
+ }
1500
+
1501
+ // 如果没有跳过 dev,则启动开发服务器
1502
+ if (!skipDev) {
1503
+ runNpmDev(absoluteOutputPath);
1504
+ timer.logPhase('Dev server startup');
1505
+ } else {
1506
+ // 只有跳过 dev 时才显示 Next steps
1507
+ logger.info('\nNext steps:');
1508
+ logger.info(` cd ${outputPath}`);
1509
+ if (skipInstall) {
1510
+ logger.info(' pnpm install');
1511
+ }
1512
+ if (skipGit) {
1513
+ logger.info(
1514
+ ' git init && git add . && git commit -m "initial commit"',
1515
+ );
1516
+ }
1517
+ logger.info(' npm run dev');
1518
+ }
1519
+
1520
+ // 输出总耗时
1521
+ timer.logTotal();
1522
+ } catch (error) {
1523
+ timer.logTotal();
1524
+ logger.error('Failed to initialize project:');
1525
+ logger.error(error instanceof Error ? error.message : String(error));
1526
+ process.exit(1);
1527
+ }
1528
+ };
1529
+
1530
+ /**
1531
+ * 注册 init 命令到 program
1532
+ */
1533
+ const registerCommand = program => {
1534
+ program
1535
+ .command('init')
1536
+ .description('Initialize a new project from a template')
1537
+ .argument('[directory]', 'Output directory for the project')
1538
+ .requiredOption('-t, --template <name>', 'Template name')
1539
+ .option('-o, --output <path>', 'Output directory', process.cwd())
1540
+ .option('--skip-install', 'Skip automatic pnpm install', false)
1541
+ .option('--skip-git', 'Skip automatic git initialization', false)
1542
+ .option('--skip-dev', 'Skip automatic dev server start', false)
1543
+ .allowUnknownOption() // 允许透传参数
1544
+ .action(async (directory, options, command) => {
1545
+ // 位置参数优先级高于 --output 选项
1546
+ const outputPath = _nullishCoalesce(directory, () => ( options.output));
1547
+ await executeInit({ ...options, output: outputPath }, command);
1548
+ });
1549
+ };
1550
+
1551
+ const commands = [
1552
+ registerCommand,
1553
+ registerCommand$1,
1554
+ // registerWarmupCommand,
1555
+ ];
1556
+
1557
+ const main = () => {
1558
+ const program = new commander.Command();
1559
+
1560
+ program
1561
+ .name('coze')
1562
+ .description(
1563
+ 'Coze Coding CLI - Project template engine for frontend stacks',
1564
+ )
1565
+ .version('1.0.0');
1566
+
1567
+ commands.forEach(initCmd => initCmd(program));
1568
+
1569
+ // 在 help 输出中添加模板信息
1570
+ program.addHelpText('after', generateTemplatesHelpText());
1571
+
1572
+ program.parse(process.argv);
1573
+ };
1574
+
1575
+ main();