neo-cmp-cli 1.5.0-beta.10 → 1.5.0-beta.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neo-cmp-cli",
3
- "version": "1.5.0-beta.10",
3
+ "version": "1.5.0-beta.12",
4
4
  "description": "前端脚手架:自定义组件开发工具,支持react 和 vue2.0技术栈。",
5
5
  "keywords": [
6
6
  "neo-cli",
@@ -115,9 +115,10 @@ const buildComponentData = async (assetsRoot, cmpInfo) => {
115
115
  try {
116
116
  // 加载自定义组件模型资源文件
117
117
  if (fs.existsSync(modelFile)) {
118
+ // 加载自定义组件模型资源文件
118
119
  let modelModule = require(modelFile);
119
120
  // 获取导出的模型类(可能是 default 导出或命名导出)
120
- CatchCustomCmpModelClass = modelModule.default || modelModule;
121
+ // const CatchCustomCmpModelClass = modelModule.default || modelModule;
121
122
  }
122
123
  else {
123
124
  console.error(`未找到自定义组件模型文件,请检查以下路径是否存在:`, modelFile);
@@ -169,11 +169,11 @@ yargs
169
169
  if (argv.name) {
170
170
  mainAction.createCmpProjectByTemplate(argv.name);
171
171
  } else {
172
- questions.push({
172
+ const questions = [{
173
173
  name: 'name',
174
174
  type: 'input',
175
175
  message: '请设置自定义组件项目名称:',
176
- });
176
+ }];
177
177
 
178
178
  inquirer.prompt(questions).then((ans) => {
179
179
  // 验证项目名称是否合法
@@ -187,7 +187,7 @@ yargs
187
187
  console.error(errors.join('\n'));
188
188
  process.exit(1);
189
189
  } else {
190
- mainAction.createCmpProjectByTemplate(argv.name);
190
+ mainAction.createCmpProjectByTemplate(ans.name);
191
191
  }
192
192
  });
193
193
  }
@@ -210,11 +210,11 @@ yargs
210
210
  if (argv.name) {
211
211
  mainAction.createCmpByTemplate(argv.name);
212
212
  } else {
213
- questions.push({
213
+ const questions = [{
214
214
  name: 'name',
215
215
  type: 'input',
216
216
  message: '请设置自定义组件名称:',
217
- });
217
+ }];
218
218
 
219
219
  inquirer.prompt(questions).then((ans) => {
220
220
  if (!ans.name) {
@@ -3,10 +3,33 @@ const fs = require('fs');
3
3
 
4
4
  /**
5
5
  * 自动进入指定项目的根目录
6
+ *
7
+ * ⚠️ 重要说明:子进程无法直接改变父 shell 的工作目录
8
+ *
9
+ * 原因:
10
+ * 1. 进程隔离:每个进程都有独立的工作目录,子进程无法修改父进程的工作目录
11
+ * 2. Shell 限制:cd 是 shell 的内置命令,必须在父 shell 进程中执行
12
+ * 3. 安全机制:操作系统禁止子进程修改父进程的工作目录,防止安全问题
13
+ *
14
+ * 解决方案:
15
+ * 1. 使用 process.chdir() 改变 Node.js 进程的工作目录(仅对后续 Node.js 操作有效)
16
+ * 2. 输出 shell 命令让用户执行以改变 shell 的工作目录
17
+ * 3. 使用 eval 命令(需要用户在 shell 中配置函数)
18
+ *
6
19
  * @param {string} projectPath 项目路径(可以是相对路径或绝对路径)
20
+ * @param {object} options 配置选项
21
+ * @param {boolean} options.changeProcessDir 是否改变 Node.js 进程的工作目录(默认:true)
22
+ * @param {boolean} options.outputShellCommand 是否输出 shell 命令让用户执行(默认:true)
23
+ * @param {boolean} options.autoExecute 是否尝试自动执行切换命令(默认:false,需要 shell 支持)
7
24
  * @returns {boolean} 是否成功切换目录
8
25
  */
9
- module.exports = function (projectPath) {
26
+ module.exports = function (projectPath, options = {}) {
27
+ const {
28
+ changeProcessDir = true,
29
+ outputShellCommand = true,
30
+ autoExecute = false
31
+ } = options;
32
+
10
33
  if (!projectPath) {
11
34
  console.error('运行异常:未找到可用的项目路径。');
12
35
  return false;
@@ -30,13 +53,77 @@ module.exports = function (projectPath) {
30
53
  return false;
31
54
  }
32
55
 
33
- try {
34
- // 切换到指定目录
35
- process.chdir(absolutePath);
56
+ let success = false;
57
+
58
+ // 改变 Node.js 进程的工作目录(仅对后续 Node.js 操作有效)
59
+ if (changeProcessDir) {
60
+ try {
61
+ process.chdir(absolutePath);
62
+ success = true;
63
+ } catch (error) {
64
+ console.error(`错误:无法切换到目录 ${projectPath}: ${error.message}`);
65
+ return false;
66
+ }
67
+ }
68
+
69
+ // 输出 shell 命令让用户执行(这样可以真正改变 shell 的工作目录)
70
+ if (outputShellCommand) {
71
+ // 检测 shell 类型
72
+ const shell = process.env.SHELL || '/bin/sh';
73
+ const shellName = path.basename(shell);
74
+
75
+ // 转义路径中的特殊字符
76
+ const escapedPath = absolutePath.replace(/'/g, "'\"'\"'");
77
+ const command = `cd '${escapedPath}'`;
78
+
79
+ // 尝试自动执行(仅在某些情况下有效)
80
+ if (autoExecute) {
81
+ // 对于 zsh/bash,可以尝试通过 eval 执行
82
+ // 但这通常不会成功,因为子进程的限制
83
+ // 这里我们只是提供一个尝试,大多数情况下不会生效
84
+ try {
85
+ // 注意:这个尝试通常不会成功,因为子进程无法改变父 shell
86
+ // 但我们可以输出一个可以 eval 的命令
87
+ const evalCommand = `eval "cd '${escapedPath}'"`;
88
+ console.log('\n⚠️ 注意:自动执行切换目录通常无法生效(子进程限制)');
89
+ console.log(' 如果配置了 shell 函数,可以尝试执行:\n');
90
+ console.log(` ${evalCommand}\n`);
91
+ } catch (error) {
92
+ // 忽略错误,继续输出手动命令
93
+ }
94
+ }
95
+
96
+ // 输出友好的提示信息
97
+ if (shellName === 'zsh' || shellName === 'bash') {
98
+ console.log('\n' + '='.repeat(60));
99
+ console.log('💡 提示:要切换到项目目录,请执行以下命令:');
100
+ console.log('='.repeat(60));
101
+ console.log(`\n ${command}\n`);
102
+ console.log('='.repeat(60));
103
+ console.log('\n📝 说明:由于子进程无法改变父 shell 的工作目录,');
104
+ console.log(' 您需要手动执行上述命令来切换目录。\n');
105
+ } else if (shellName === 'fish') {
106
+ const fishEscapedPath = absolutePath.replace(/'/g, "\\'");
107
+ const fishCommand = `cd '${fishEscapedPath}'`;
108
+ console.log('\n' + '='.repeat(60));
109
+ console.log('💡 提示:要切换到项目目录,请执行以下命令:');
110
+ console.log('='.repeat(60));
111
+ console.log(`\n ${fishCommand}\n`);
112
+ console.log('='.repeat(60));
113
+ console.log('\n📝 说明:由于子进程无法改变父 shell 的工作目录,');
114
+ console.log(' 您需要手动执行上述命令来切换目录。\n');
115
+ } else {
116
+ console.log('\n' + '='.repeat(60));
117
+ console.log('💡 提示:要切换到项目目录,请执行以下命令:');
118
+ console.log('='.repeat(60));
119
+ console.log(`\n ${command}\n`);
120
+ console.log('='.repeat(60));
121
+ console.log('\n📝 说明:由于子进程无法改变父 shell 的工作目录,');
122
+ console.log(' 您需要手动执行上述命令来切换目录。\n');
123
+ }
124
+ } else if (changeProcessDir && success) {
36
125
  console.log(`已自动切换到 ${projectPath} 目录。`);
37
- return true;
38
- } catch (error) {
39
- console.error(`错误:无法切换到目录 ${projectPath}: ${error.message}`);
40
- return false;
41
126
  }
127
+
128
+ return success;
42
129
  };
@@ -31,8 +31,8 @@ function replaceInFilesByMap(
31
31
  // 检查文件扩展名
32
32
  const ext = path.extname(file);
33
33
  if (fileExtensions.includes(ext)) {
34
-
35
34
  if (needReplaceStrMap &&Object.keys(needReplaceStrMap).length > 0) {
35
+ let content = fs.readFileSync(filePath, 'utf8');
36
36
  Object.keys(needReplaceStrMap).forEach((key) => {
37
37
  const oldStr = key;
38
38
  const newStr = needReplaceStrMap[key];
@@ -51,4 +51,4 @@ function replaceInFilesByMap(
51
51
  }
52
52
  }
53
53
 
54
- module.exports = replaceInFiles;
54
+ module.exports = replaceInFilesByMap;