neo-cmp-cli 1.5.0-beta.12 → 1.5.0-beta.15
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
|
@@ -27,15 +27,17 @@ module.exports = function (cmpName) {
|
|
|
27
27
|
const finalCmpName = cmpName || 'neoCustomCmp';
|
|
28
28
|
const finalCmpPath = path.resolve(process.cwd(), finalCmpName);
|
|
29
29
|
|
|
30
|
-
if (hasCmpTypeByDir(
|
|
31
|
-
console.error(`${consoleTag}创建自定义组件失败,当前已经存在${
|
|
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(
|
|
38
|
-
const cmpType = _.kebabCase(
|
|
37
|
+
const curCmpName = _.camelCase(finalCmpName);
|
|
38
|
+
const cmpType = _.kebabCase(finalCmpName);
|
|
39
|
+
|
|
40
|
+
// 替换文件中的内容
|
|
39
41
|
replaceInFilesByMap(finalCmpPath, {
|
|
40
42
|
[curCmpTemplate.widgetInfo.cmpName]: curCmpName,
|
|
41
43
|
[curCmpTemplate.widgetInfo.modelName]: `${curCmpName}Model`,
|
|
@@ -3,32 +3,25 @@ const fs = require('fs');
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 自动进入指定项目的根目录
|
|
6
|
-
*
|
|
6
|
+
*
|
|
7
7
|
* ⚠️ 重要说明:子进程无法直接改变父 shell 的工作目录
|
|
8
|
-
*
|
|
8
|
+
*
|
|
9
9
|
* 原因:
|
|
10
10
|
* 1. 进程隔离:每个进程都有独立的工作目录,子进程无法修改父进程的工作目录
|
|
11
11
|
* 2. Shell 限制:cd 是 shell 的内置命令,必须在父 shell 进程中执行
|
|
12
12
|
* 3. 安全机制:操作系统禁止子进程修改父进程的工作目录,防止安全问题
|
|
13
|
-
*
|
|
13
|
+
*
|
|
14
14
|
* 解决方案:
|
|
15
15
|
* 1. 使用 process.chdir() 改变 Node.js 进程的工作目录(仅对后续 Node.js 操作有效)
|
|
16
16
|
* 2. 输出 shell 命令让用户执行以改变 shell 的工作目录
|
|
17
|
-
*
|
|
18
|
-
*
|
|
17
|
+
*
|
|
19
18
|
* @param {string} projectPath 项目路径(可以是相对路径或绝对路径)
|
|
20
19
|
* @param {object} options 配置选项
|
|
21
|
-
* @param {boolean} options.changeProcessDir 是否改变 Node.js 进程的工作目录(默认:true)
|
|
22
20
|
* @param {boolean} options.outputShellCommand 是否输出 shell 命令让用户执行(默认:true)
|
|
23
|
-
* @param {boolean} options.autoExecute 是否尝试自动执行切换命令(默认:false,需要 shell 支持)
|
|
24
21
|
* @returns {boolean} 是否成功切换目录
|
|
25
22
|
*/
|
|
26
23
|
module.exports = function (projectPath, options = {}) {
|
|
27
|
-
const {
|
|
28
|
-
changeProcessDir = true,
|
|
29
|
-
outputShellCommand = true,
|
|
30
|
-
autoExecute = false
|
|
31
|
-
} = options;
|
|
24
|
+
const { outputShellCommand = true } = options;
|
|
32
25
|
|
|
33
26
|
if (!projectPath) {
|
|
34
27
|
console.error('运行异常:未找到可用的项目路径。');
|
|
@@ -36,8 +29,8 @@ module.exports = function (projectPath, options = {}) {
|
|
|
36
29
|
}
|
|
37
30
|
|
|
38
31
|
// 将路径解析为绝对路径
|
|
39
|
-
const absolutePath = path.isAbsolute(projectPath)
|
|
40
|
-
? projectPath
|
|
32
|
+
const absolutePath = path.isAbsolute(projectPath)
|
|
33
|
+
? projectPath
|
|
41
34
|
: path.resolve(process.cwd(), projectPath);
|
|
42
35
|
|
|
43
36
|
// 检查目录是否存在
|
|
@@ -53,76 +46,29 @@ module.exports = function (projectPath, options = {}) {
|
|
|
53
46
|
return false;
|
|
54
47
|
}
|
|
55
48
|
|
|
49
|
+
const projectName = path.relative(process.cwd(), absolutePath);
|
|
50
|
+
|
|
56
51
|
let success = false;
|
|
57
52
|
|
|
58
53
|
// 改变 Node.js 进程的工作目录(仅对后续 Node.js 操作有效)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
54
|
+
try {
|
|
55
|
+
process.chdir(absolutePath);
|
|
56
|
+
success = true;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error(`错误:无法切换到目录 ${projectName}: ${error.message}`);
|
|
59
|
+
return false;
|
|
67
60
|
}
|
|
68
61
|
|
|
69
62
|
// 输出 shell 命令让用户执行(这样可以真正改变 shell 的工作目录)
|
|
70
63
|
if (outputShellCommand) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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) {
|
|
125
|
-
console.log(`已自动切换到 ${projectPath} 目录。`);
|
|
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`);
|
|
126
72
|
}
|
|
127
73
|
|
|
128
74
|
return success;
|