generator-mico-cli 0.1.2 → 0.1.6

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/bin/mico.js CHANGED
@@ -52,6 +52,7 @@ function printVersion() {
52
52
  async function checkForUpdate() {
53
53
  try {
54
54
  const updateNotifier = (await import('update-notifier')).default;
55
+ const semver = (await import('semver')).default;
55
56
  const notifier = updateNotifier({
56
57
  pkg,
57
58
  updateCheckInterval: 0 // 每次都检查
@@ -60,7 +61,8 @@ async function checkForUpdate() {
60
61
  // 等待检查完成
61
62
  await notifier.fetchInfo();
62
63
 
63
- if (notifier.update && notifier.update.latest !== pkg.version) {
64
+ // 使用 semver 比较,确保 latest > current
65
+ if (notifier.update && semver.gt(notifier.update.latest, pkg.version)) {
64
66
  return {
65
67
  current: pkg.version,
66
68
  latest: notifier.update.latest,
@@ -7,6 +7,33 @@ const { toKebab, toPascal } = require('../../lib/utils');
7
7
 
8
8
  const IGNORE_LIST = require('./ignore-list.json');
9
9
 
10
+ // 全局未捕获异常处理
11
+ process.on('uncaughtException', (error) => {
12
+ console.error('');
13
+ console.error('❌ Unexpected error:');
14
+ console.error(` ${error.message}`);
15
+ if (error.stack) {
16
+ console.error('');
17
+ console.error(' Stack trace:');
18
+ console.error(error.stack.split('\n').map(line => ` ${line}`).join('\n'));
19
+ }
20
+ console.error('');
21
+ process.exit(1);
22
+ });
23
+
24
+ process.on('unhandledRejection', (reason) => {
25
+ console.error('');
26
+ console.error('❌ Unhandled promise rejection:');
27
+ console.error(` ${reason instanceof Error ? reason.message : String(reason)}`);
28
+ if (reason instanceof Error && reason.stack) {
29
+ console.error('');
30
+ console.error(' Stack trace:');
31
+ console.error(reason.stack.split('\n').map(line => ` ${line}`).join('\n'));
32
+ }
33
+ console.error('');
34
+ process.exit(1);
35
+ });
36
+
10
37
  /**
11
38
  * 检查路径是否应该被忽略
12
39
  */
@@ -48,74 +75,143 @@ const TEMPLATE_EXTENSIONS = new Set([
48
75
  ]);
49
76
 
50
77
  module.exports = class extends Generator {
51
- async prompting() {
52
- this.answers = await this.prompt([
53
- {
54
- type: 'input',
55
- name: 'appName',
56
- message: 'Sub app name',
57
- default: 'subapp',
58
- filter: (input) => toKebab(input),
59
- validate: (input) => {
60
- const value = toKebab(input);
61
- if (!value) return 'App name is required';
62
- return true;
63
- }
64
- }
65
- ]);
66
-
67
- this.monorepoRoot = this.destinationRoot();
68
- this.appName = toKebab(this.answers.appName);
69
- this.appNamePascal = toPascal(this.appName);
70
- this.templateDir = this.templatePath('homepage');
71
- this.destDir = path.join(this.monorepoRoot, 'apps', this.appName);
72
- }
73
-
74
- writing() {
78
+ /**
79
+ * 初始化阶段:检查运行环境
80
+ */
81
+ initializing() {
82
+ // 使用当前工作目录,而不是 Yeoman 的 destinationRoot
83
+ this.monorepoRoot = process.cwd();
75
84
  const appsDir = path.join(this.monorepoRoot, 'apps');
76
85
  const workspaceFile = path.join(this.monorepoRoot, 'pnpm-workspace.yaml');
77
86
 
78
87
  // 检查是否在 monorepo 中
79
88
  if (!fs.existsSync(appsDir)) {
80
- this.env.error(
81
- `apps directory not found in ${this.monorepoRoot}. Run this generator from the monorepo root.`
82
- );
83
- return;
89
+ // 使用 console.error 确保错误信息总是被输出
90
+ console.error('');
91
+ console.error('❌ Error: apps directory not found.');
92
+ console.error('');
93
+ console.error(' This generator must be run from a monorepo root that contains an "apps" directory.');
94
+ console.error(` Current directory: ${this.monorepoRoot}`);
95
+ console.error('');
96
+ console.error(' Usage:');
97
+ console.error(' cd /path/to/your-monorepo');
98
+ console.error(' mico create subapp-react');
99
+ console.error('');
100
+ process.exit(1);
84
101
  }
85
102
 
86
103
  // 警告:检查是否是 pnpm workspace
87
104
  if (!fs.existsSync(workspaceFile)) {
88
- this.log.warning(
89
- 'pnpm-workspace.yaml not found. Make sure you are in the correct monorepo.'
90
- );
105
+ console.warn('');
106
+ console.warn('⚠️ Warning: pnpm-workspace.yaml not found.');
107
+ console.warn(' Make sure you are in the correct monorepo.');
108
+ console.warn('');
91
109
  }
110
+ }
111
+
112
+ async prompting() {
113
+ try {
114
+ // 确保 monorepoRoot 已设置
115
+ if (!this.monorepoRoot) {
116
+ console.error('');
117
+ console.error('❌ Error: Internal error - monorepoRoot not initialized.');
118
+ console.error('');
119
+ process.exit(1);
120
+ }
92
121
 
93
- if (fs.existsSync(this.destDir)) {
94
- this.env.error(`Target already exists: ${this.destDir}`);
95
- return;
122
+ this.answers = await this.prompt([
123
+ {
124
+ type: 'input',
125
+ name: 'appName',
126
+ message: 'Sub app name',
127
+ default: 'subapp',
128
+ filter: (input) => toKebab(input),
129
+ validate: (input) => {
130
+ const value = toKebab(input);
131
+ if (!value) return 'App name is required';
132
+ // 检查目标目录是否已存在
133
+ const destDir = path.join(this.monorepoRoot, 'apps', value);
134
+ if (fs.existsSync(destDir)) {
135
+ return `Target already exists: apps/${value}`;
136
+ }
137
+ return true;
138
+ }
139
+ }
140
+ ]);
141
+
142
+ this.appName = toKebab(this.answers.appName);
143
+ this.appNamePascal = toPascal(this.appName);
144
+ this.templateDir = this.templatePath('homepage');
145
+ this.destDir = path.join(this.monorepoRoot, 'apps', this.appName);
146
+ } catch (error) {
147
+ console.error('');
148
+ console.error('❌ Error during prompting:');
149
+ console.error(` ${error.message}`);
150
+ if (error.stack) {
151
+ console.error('');
152
+ console.error(' Stack trace:');
153
+ console.error(error.stack.split('\n').map(line => ` ${line}`).join('\n'));
154
+ }
155
+ console.error('');
156
+ process.exit(1);
96
157
  }
158
+ }
159
+
160
+ writing() {
161
+ try {
162
+ // 检查模板目录是否存在
163
+ if (!fs.existsSync(this.templateDir)) {
164
+ console.error('');
165
+ console.error('❌ Error: Template directory not found.');
166
+ console.error(` Expected: ${this.templateDir}`);
167
+ console.error('');
168
+ console.error(' This may indicate a corrupted installation.');
169
+ console.error(' Try reinstalling: npm install -g mico-cli');
170
+ console.error('');
171
+ process.exit(1);
172
+ }
173
+
174
+ // 模板数据
175
+ const templateData = {
176
+ appName: this.appName,
177
+ AppName: this.appNamePascal
178
+ };
179
+
180
+ // 收集所有文件
181
+ const files = collectFiles(this.templateDir, this.templateDir);
182
+
183
+ if (files.length === 0) {
184
+ console.error('');
185
+ console.error('❌ Error: No template files found.');
186
+ console.error(` Template directory: ${this.templateDir}`);
187
+ console.error('');
188
+ process.exit(1);
189
+ }
97
190
 
98
- // 模板数据
99
- const templateData = {
100
- appName: this.appName,
101
- AppName: this.appNamePascal
102
- };
103
-
104
- // 收集所有文件
105
- const files = collectFiles(this.templateDir, this.templateDir);
106
-
107
- for (const relPath of files) {
108
- const srcPath = path.join(this.templateDir, relPath);
109
- const destPath = path.join(this.destDir, relPath);
110
- const ext = path.extname(relPath);
111
-
112
- if (TEMPLATE_EXTENSIONS.has(ext)) {
113
- // 使用 EJS 模板处理
114
- this.fs.copyTpl(srcPath, destPath, templateData);
115
- } else {
116
- // 直接复制(图片等二进制文件)
117
- this.fs.copy(srcPath, destPath);
191
+ for (const relPath of files) {
192
+ const srcPath = path.join(this.templateDir, relPath);
193
+ const destPath = path.join(this.destDir, relPath);
194
+ const ext = path.extname(relPath);
195
+
196
+ if (TEMPLATE_EXTENSIONS.has(ext)) {
197
+ // 使用 EJS 模板处理
198
+ this.fs.copyTpl(srcPath, destPath, templateData);
199
+ } else {
200
+ // 直接复制(图片等二进制文件)
201
+ this.fs.copy(srcPath, destPath);
202
+ }
203
+ }
204
+ } catch (error) {
205
+ console.error('');
206
+ console.error('❌ Error during file generation:');
207
+ console.error(` ${error.message}`);
208
+ if (error.stack) {
209
+ console.error('');
210
+ console.error(' Stack trace:');
211
+ console.error(error.stack.split('\n').map(line => ` ${line}`).join('\n'));
118
212
  }
213
+ console.error('');
214
+ process.exit(1);
119
215
  }
120
216
  }
121
217
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generator-mico-cli",
3
- "version": "0.1.2",
3
+ "version": "0.1.6",
4
4
  "description": "Yeoman generator for Mico CLI projects",
5
5
  "keywords": [
6
6
  "yeoman-generator",
@@ -8,7 +8,7 @@
8
8
  "cli"
9
9
  ],
10
10
  "license": "MIT",
11
- "main": "generators/app/index.js",
11
+ "main": "generators/subapp-react/index.js",
12
12
  "bin": {
13
13
  "mico": "bin/mico.js"
14
14
  },
@@ -24,6 +24,7 @@
24
24
  "node": ">=18"
25
25
  },
26
26
  "dependencies": {
27
+ "semver": "^7.6.3",
27
28
  "update-notifier": "^7.3.1",
28
29
  "yeoman-generator": "^5.9.0"
29
30
  }