neo-cmp-cli 1.5.0-beta.9 → 1.5.2

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.9",
3
+ "version": "1.5.2",
4
4
  "description": "前端脚手架:自定义组件开发工具,支持react 和 vue2.0技术栈。",
5
5
  "keywords": [
6
6
  "neo-cli",
@@ -10,7 +10,7 @@ const curCmpTemplate = {
10
10
  // 需要替换掉的字段信息(模板中的字段)
11
11
  widgetInfo: {
12
12
  cmpName: 'CustomCmp',
13
- modelName: 'CustomCmpModel',
13
+ modelName: 'CmpModel',
14
14
  cmpClassName: 'custom-cmp-container',
15
15
  cmpType: 'xx-custom-cmp',
16
16
  cmpLabel: 'xx组件',
@@ -22,23 +22,25 @@ const curCmpTemplate = {
22
22
  * 创建自定义组件
23
23
  * @param {*} cmpName 自定义组件名称
24
24
  */
25
- module.exports = function (cmpName) {
25
+ module.exports = function (cmpName, componentBaseDir = './src/components') {
26
26
  const currentTemplateDir = curCmpTemplate.dir;
27
27
  const finalCmpName = cmpName || 'neoCustomCmp';
28
- const finalCmpPath = path.resolve(process.cwd(), finalCmpName);
28
+ const finalCmpPath = path.resolve(process.cwd(), componentBaseDir, finalCmpName);
29
29
 
30
- if (hasCmpTypeByDir(cmpType)) {
31
- console.error(`${consoleTag}创建自定义组件失败,当前已经存在${cmpType}自定义组件。`);
30
+ if (hasCmpTypeByDir(finalCmpName)) {
31
+ console.error(`${consoleTag}创建自定义组件失败,当前已经存在${finalCmpName}自定义组件。`);
32
32
  process.exit(1);
33
33
  }
34
34
 
35
35
  fs.copy(currentTemplateDir, finalCmpPath)
36
36
  .then(() => {
37
- const curCmpName = _.camelCase(cmpName);
38
- const cmpType = _.kebabCase(cmpName);
37
+ const curCmpName = _.camelCase(finalCmpName);
38
+ const cmpType = _.kebabCase(finalCmpName);
39
+
40
+ // 替换文件中的内容
39
41
  replaceInFilesByMap(finalCmpPath, {
40
- [curCmpTemplate.widgetInfo.cmpName]: curCmpName,
41
42
  [curCmpTemplate.widgetInfo.modelName]: `${curCmpName}Model`,
43
+ [curCmpTemplate.widgetInfo.cmpName]: curCmpName,
42
44
  [curCmpTemplate.widgetInfo.cmpClassName]: `${cmpType}-container`,
43
45
  [curCmpTemplate.widgetInfo.cmpType]: cmpType,
44
46
  [curCmpTemplate.widgetInfo.cmpLabel]: `${cmpType}组件`,
@@ -47,4 +49,4 @@ module.exports = function (cmpName) {
47
49
  console.log(`${consoleTag}已创建自定义组件(${finalCmpName})!`);
48
50
  })
49
51
  .catch((err) => console.error(`${consoleTag}自定义组件创建失败(${finalCmpName}):`, err));
50
- };;
52
+ };
@@ -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,18 +210,18 @@ 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) {
221
221
  console.error('自定义组件名称不能为空。');
222
222
  process.exit(1);
223
223
  } else {
224
- mainAction.createCmpByTemplate(argv.name);
224
+ mainAction.createCmpByTemplate(ans.name);
225
225
  }
226
226
  });
227
227
  }
@@ -15,8 +15,8 @@ const AddNeoRequirePlugin = require('../plugins/AddNeoRequirePlugin');
15
15
  const { getExternalsByNeoCommonModules } = require('../neo/neoRequire');
16
16
  // const { MFPlugins } = require('../neo/webpack.mf');
17
17
  const createCommonModulesCode = require('../cmpUtils/createCommonModulesCode');
18
+ const createCmpByTemplate = require('../cmpUtils/createCmpByTemplate');
18
19
  const createCmpProjectByTemplate = require('../projectUtils/createCmpProjectByTemplate');
19
- const createCmpByTemplate = require('../projectUtils/createCmpByTemplate');
20
20
 
21
21
  const getValue = (originValue, defaultValue) => {
22
22
  return originValue !== undefined ? originValue : defaultValue;
@@ -47,7 +47,7 @@
47
47
  "@commitlint/config-conventional": "^9.1.1",
48
48
  "@types/react": "^16.9.11",
49
49
  "@types/react-dom": "^16.9.15",
50
- "neo-cmp-cli": "^1.3.8",
50
+ "neo-cmp-cli": "^1.5.2",
51
51
  "husky": "^4.2.5",
52
52
  "lint-staged": "^10.2.9",
53
53
  "prettier": "^2.0.5"
@@ -47,7 +47,7 @@
47
47
  "@commitlint/config-conventional": "^9.1.1",
48
48
  "@types/react": "^16.9.11",
49
49
  "@types/react-dom": "^16.9.15",
50
- "neo-cmp-cli": "^1.3.8",
50
+ "neo-cmp-cli": "^1.5.2",
51
51
  "husky": "^4.2.5",
52
52
  "lint-staged": "^10.2.9",
53
53
  "prettier": "^2.0.5",
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @file 自定义组件对接编辑器的描述文件
3
3
  */
4
- export class CustomCmpModel {
4
+ export class CmpModel {
5
5
  /**
6
6
  * cmpType 为自定义组件名称,用于标识组件的唯一性
7
7
  * 在构建时根据当前组件目录名称自动生成
@@ -45,7 +45,7 @@
45
45
  "@commitlint/config-conventional": "^9.1.1",
46
46
  "@types/react": "^16.9.11",
47
47
  "@types/react-dom": "^16.9.15",
48
- "neo-cmp-cli": "^1.3.8",
48
+ "neo-cmp-cli": "^1.5.2",
49
49
  "husky": "^4.2.5",
50
50
  "lint-staged": "^10.2.9",
51
51
  "prettier": "^2.0.5"
@@ -44,7 +44,7 @@
44
44
  "devDependencies": {
45
45
  "@commitlint/cli": "^8.3.5",
46
46
  "@commitlint/config-conventional": "^9.1.1",
47
- "neo-cmp-cli": "^1.3.8",
47
+ "neo-cmp-cli": "^1.5.2",
48
48
  "husky": "^4.2.5",
49
49
  "lint-staged": "^10.2.9",
50
50
  "prettier": "^2.0.5"
@@ -46,7 +46,7 @@
46
46
  "@commitlint/config-conventional": "^9.1.1",
47
47
  "@types/react": "^16.9.11",
48
48
  "@types/react-dom": "^16.9.15",
49
- "neo-cmp-cli": "^1.3.8",
49
+ "neo-cmp-cli": "^1.5.2",
50
50
  "husky": "^4.2.5",
51
51
  "lint-staged": "^10.2.9",
52
52
  "prettier": "^2.0.5"
@@ -44,7 +44,7 @@
44
44
  "devDependencies": {
45
45
  "@commitlint/cli": "^8.3.5",
46
46
  "@commitlint/config-conventional": "^9.1.1",
47
- "neo-cmp-cli": "^1.3.8",
47
+ "neo-cmp-cli": "^1.5.2",
48
48
  "husky": "^4.2.5",
49
49
  "lint-staged": "^10.2.9",
50
50
  "prettier": "^2.0.5",
@@ -3,18 +3,34 @@ 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
+ *
6
18
  * @param {string} projectPath 项目路径(可以是相对路径或绝对路径)
19
+ * @param {object} options 配置选项
20
+ * @param {boolean} options.outputShellCommand 是否输出 shell 命令让用户执行(默认:true)
7
21
  * @returns {boolean} 是否成功切换目录
8
22
  */
9
- module.exports = function (projectPath) {
23
+ module.exports = function (projectPath, options = {}) {
24
+ const { outputShellCommand = true } = options;
25
+
10
26
  if (!projectPath) {
11
27
  console.error('运行异常:未找到可用的项目路径。');
12
28
  return false;
13
29
  }
14
30
 
15
31
  // 将路径解析为绝对路径
16
- const absolutePath = path.isAbsolute(projectPath)
17
- ? projectPath
32
+ const absolutePath = path.isAbsolute(projectPath)
33
+ ? projectPath
18
34
  : path.resolve(process.cwd(), projectPath);
19
35
 
20
36
  // 检查目录是否存在
@@ -30,13 +46,30 @@ module.exports = function (projectPath) {
30
46
  return false;
31
47
  }
32
48
 
49
+ const projectName = path.relative(process.cwd(), absolutePath);
50
+
51
+ let success = false;
52
+
53
+ // 改变 Node.js 进程的工作目录(仅对后续 Node.js 操作有效)
33
54
  try {
34
- // 切换到指定目录
35
55
  process.chdir(absolutePath);
36
- console.log(`已自动切换到 ${projectPath} 目录。`);
37
- return true;
56
+ success = true;
38
57
  } catch (error) {
39
- console.error(`错误:无法切换到目录 ${projectPath}: ${error.message}`);
58
+ console.error(`错误:无法切换到目录 ${projectName}: ${error.message}`);
40
59
  return false;
41
60
  }
61
+
62
+ // 输出 shell 命令让用户执行(这样可以真正改变 shell 的工作目录)
63
+ if (outputShellCommand) {
64
+ const command = `cd ${projectName}`;
65
+ // 输出提示信息
66
+ console.log('\n' + '='.repeat(60));
67
+ console.log('💡 提示:要切换到项目目录,请执行以下命令:');
68
+ console.log(`\n ${command}\n`);
69
+ console.log('='.repeat(60));
70
+ console.log('\n📝 说明:组件开发工具(neo-cmp-cli)无法直接改变当前命令窗口工作目录,');
71
+ console.log(` 您需要手动执行上述命令进入刚创建的自定义组件项目(${projectName})。\n`);
72
+ }
73
+
74
+ return success;
42
75
  };
@@ -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;