generator-mico-cli 0.1.5 → 0.1.7

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
@@ -98,9 +98,10 @@ function askForUpdate(current, latest) {
98
98
 
99
99
  /**
100
100
  * 执行更新
101
+ * @param {string} latestVersion - 要更新到的版本号
101
102
  * @returns {boolean} 是否更新成功
102
103
  */
103
- function performUpdate() {
104
+ function performUpdate(latestVersion) {
104
105
  console.log('');
105
106
  console.log(' ⏳ Updating mico-cli...');
106
107
 
@@ -113,12 +114,15 @@ function performUpdate() {
113
114
  pm = 'yarn';
114
115
  }
115
116
 
117
+ // 指定具体版本号,避免 npm 缓存问题
118
+ const packageSpec = latestVersion ? `${pkg.name}@${latestVersion}` : `${pkg.name}@latest`;
119
+
116
120
  // 执行全局安装
117
121
  const installCmd = pm === 'yarn' ? 'yarn' : pm;
118
122
  const installArgs =
119
123
  pm === 'yarn'
120
- ? ['global', 'add', pkg.name]
121
- : ['install', '-g', pkg.name];
124
+ ? ['global', 'add', packageSpec]
125
+ : ['install', '-g', packageSpec];
122
126
 
123
127
  const result = spawnSync(installCmd, installArgs, {
124
128
  stdio: 'inherit',
@@ -224,7 +228,7 @@ async function main() {
224
228
  if (command === 'update') {
225
229
  const updateInfo = await checkForUpdate();
226
230
  if (updateInfo && updateInfo.hasUpdate) {
227
- performUpdate();
231
+ performUpdate(updateInfo.latest);
228
232
  } else if (updateInfo) {
229
233
  console.log(` ✅ You are using the latest version (v${updateInfo.current})`);
230
234
  } else {
@@ -250,7 +254,7 @@ async function main() {
250
254
  updateInfo.latest
251
255
  );
252
256
  if (shouldUpdate) {
253
- const success = performUpdate();
257
+ const success = performUpdate(updateInfo.latest);
254
258
  if (success) {
255
259
  process.exit(0);
256
260
  }
@@ -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
  */
@@ -59,78 +86,132 @@ module.exports = class extends Generator {
59
86
 
60
87
  // 检查是否在 monorepo 中
61
88
  if (!fs.existsSync(appsDir)) {
62
- this.log('');
63
- this.log('❌ Error: apps directory not found.');
64
- this.log('');
65
- this.log(' This generator must be run from a monorepo root that contains an "apps" directory.');
66
- this.log(` Current directory: ${this.monorepoRoot}`);
67
- this.log('');
68
- this.log(' Usage:');
69
- this.log(' cd /path/to/your-monorepo');
70
- this.log(' mico create subapp-react');
71
- this.log('');
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('');
72
100
  process.exit(1);
73
101
  }
74
102
 
75
103
  // 警告:检查是否是 pnpm workspace
76
104
  if (!fs.existsSync(workspaceFile)) {
77
- this.log('');
78
- this.log('⚠️ Warning: pnpm-workspace.yaml not found.');
79
- this.log(' Make sure you are in the correct monorepo.');
80
- this.log('');
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('');
81
109
  }
82
110
  }
83
111
 
84
112
  async prompting() {
85
- this.answers = await this.prompt([
86
- {
87
- type: 'input',
88
- name: 'appName',
89
- message: 'Sub app name',
90
- default: 'subapp',
91
- filter: (input) => toKebab(input),
92
- validate: (input) => {
93
- const value = toKebab(input);
94
- if (!value) return 'App name is required';
95
- // 检查目标目录是否已存在
96
- const destDir = path.join(this.monorepoRoot, 'apps', value);
97
- if (fs.existsSync(destDir)) {
98
- return `Target already exists: apps/${value}`;
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
+ }
121
+
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;
99
138
  }
100
- return true;
101
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'));
102
154
  }
103
- ]);
104
-
105
- this.appName = toKebab(this.answers.appName);
106
- this.appNamePascal = toPascal(this.appName);
107
- this.templateDir = this.templatePath('homepage');
108
- this.destDir = path.join(this.monorepoRoot, 'apps', this.appName);
155
+ console.error('');
156
+ process.exit(1);
157
+ }
109
158
  }
110
159
 
111
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
+ }
112
173
 
113
- // 模板数据
114
- const templateData = {
115
- appName: this.appName,
116
- AppName: this.appNamePascal
117
- };
118
-
119
- // 收集所有文件
120
- const files = collectFiles(this.templateDir, this.templateDir);
121
-
122
- for (const relPath of files) {
123
- const srcPath = path.join(this.templateDir, relPath);
124
- const destPath = path.join(this.destDir, relPath);
125
- const ext = path.extname(relPath);
126
-
127
- if (TEMPLATE_EXTENSIONS.has(ext)) {
128
- // 使用 EJS 模板处理
129
- this.fs.copyTpl(srcPath, destPath, templateData);
130
- } else {
131
- // 直接复制(图片等二进制文件)
132
- this.fs.copy(srcPath, destPath);
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);
133
189
  }
190
+
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'));
212
+ }
213
+ console.error('');
214
+ process.exit(1);
134
215
  }
135
216
  }
136
217
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generator-mico-cli",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Yeoman generator for Mico CLI projects",
5
5
  "keywords": [
6
6
  "yeoman-generator",