pty-shell 1.5.0 → 1.5.1
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/dist/index.d.ts +0 -4
- package/dist/index.js +102 -150
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -127,10 +127,6 @@ export declare class PtyShell implements PtyShellUserMethod {
|
|
|
127
127
|
* 支持引号内的分隔符不被拆分
|
|
128
128
|
*/
|
|
129
129
|
private parse_combined_commands;
|
|
130
|
-
/**
|
|
131
|
-
* 同步方式执行子进程(用于组合命令),返回退出码
|
|
132
|
-
*/
|
|
133
|
-
private spawn_sync;
|
|
134
130
|
private multiple_line;
|
|
135
131
|
private spawn_write;
|
|
136
132
|
private spawn;
|
package/dist/index.js
CHANGED
|
@@ -782,14 +782,21 @@ class PtyShell {
|
|
|
782
782
|
// 1. check_exe_cmd(只调一次)
|
|
783
783
|
if (this.check_exe_cmd) {
|
|
784
784
|
const v = yield this.check_exe_cmd(exe_cmd, params);
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
785
|
+
switch (v) {
|
|
786
|
+
case exec_type.not:
|
|
787
|
+
this.send_and_enter(`not have permission to execute ${exe_cmd}`);
|
|
788
|
+
this.clear_line();
|
|
789
|
+
this.exec_end_call(-1);
|
|
790
|
+
return;
|
|
791
|
+
case exec_type.auto_child_process:
|
|
792
|
+
break;
|
|
793
|
+
case exec_type.not_pty:
|
|
794
|
+
use_noe_pty = true;
|
|
795
|
+
break;
|
|
796
|
+
default:
|
|
797
|
+
// 未知的不报错也不执行
|
|
798
|
+
this.exec_end_call(0);
|
|
799
|
+
return;
|
|
793
800
|
}
|
|
794
801
|
}
|
|
795
802
|
// 2. cmd_replace(只调一次)
|
|
@@ -843,12 +850,7 @@ class PtyShell {
|
|
|
843
850
|
}
|
|
844
851
|
else {
|
|
845
852
|
// 子进程:组合命令需要同步等待,单命令走原有异步
|
|
846
|
-
|
|
847
|
-
lastExitCode = yield this.spawn_sync(cmd.exe_cmd, cmd.params, cmd.use_noe_pty);
|
|
848
|
-
}
|
|
849
|
-
else {
|
|
850
|
-
this.spawn(cmd.exe_cmd, cmd.params, cmd.use_noe_pty);
|
|
851
|
-
}
|
|
853
|
+
lastExitCode = yield this.spawn(cmd.exe_cmd, cmd.params, cmd.use_noe_pty);
|
|
852
854
|
}
|
|
853
855
|
}
|
|
854
856
|
catch (e) {
|
|
@@ -857,9 +859,8 @@ class PtyShell {
|
|
|
857
859
|
}
|
|
858
860
|
}
|
|
859
861
|
this.clear_line();
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
}
|
|
862
|
+
this.next_not_enter = false; // 下一次的换行输出 上一次没有换行
|
|
863
|
+
this.close_child(lastExitCode);
|
|
863
864
|
});
|
|
864
865
|
}
|
|
865
866
|
/**
|
|
@@ -928,63 +929,6 @@ class PtyShell {
|
|
|
928
929
|
}
|
|
929
930
|
return result;
|
|
930
931
|
}
|
|
931
|
-
/**
|
|
932
|
-
* 同步方式执行子进程(用于组合命令),返回退出码
|
|
933
|
-
*/
|
|
934
|
-
spawn_sync(exe, params, use_noe_pty = false) {
|
|
935
|
-
return new Promise((resolve) => {
|
|
936
|
-
var _a, _b;
|
|
937
|
-
if ((use_noe_pty || ((_a = this.shell_set) === null || _a === void 0 ? void 0 : _a.has("*")) || ((_b = this.shell_set) === null || _b === void 0 ? void 0 : _b.has(exe))) && this.node_pty) {
|
|
938
|
-
this.on_call(`\n\r`);
|
|
939
|
-
this.node_pty_child = this.node_pty.spawn(exe, params, {
|
|
940
|
-
name: 'xterm-color',
|
|
941
|
-
cols: this.cols,
|
|
942
|
-
rows: this.rows,
|
|
943
|
-
cwd: this.cwd,
|
|
944
|
-
useConptyDll: false,
|
|
945
|
-
useConpty: process.env.NODE_ENV !== "production" ? false : undefined,
|
|
946
|
-
env: Object.assign(Object.assign({}, process.env), this.env),
|
|
947
|
-
});
|
|
948
|
-
this.node_pty_child.onData((data) => {
|
|
949
|
-
this.on_call(data.toString());
|
|
950
|
-
if (this.on_call_child_raw) {
|
|
951
|
-
this.on_call_child_raw(data);
|
|
952
|
-
}
|
|
953
|
-
});
|
|
954
|
-
this.node_pty_child.onExit(({ exitCode, signal }) => {
|
|
955
|
-
this.node_pty_child = null;
|
|
956
|
-
resolve(exitCode !== null && exitCode !== void 0 ? exitCode : -1);
|
|
957
|
-
});
|
|
958
|
-
}
|
|
959
|
-
else {
|
|
960
|
-
const child = child_process.spawn(exe, params, {
|
|
961
|
-
cwd: this.cwd,
|
|
962
|
-
env: Object.assign(Object.assign({}, process.env), this.env),
|
|
963
|
-
});
|
|
964
|
-
child.stdout.setEncoding('utf8');
|
|
965
|
-
child.stdout.on('data', (data) => {
|
|
966
|
-
this.send_and_enter(data.toString());
|
|
967
|
-
if (this.on_call_child_raw) {
|
|
968
|
-
this.on_call_child_raw(data);
|
|
969
|
-
}
|
|
970
|
-
});
|
|
971
|
-
child.stderr.on('data', (data) => {
|
|
972
|
-
this.send_and_enter(data.toString());
|
|
973
|
-
this.next_not_enter = false;
|
|
974
|
-
if (this.on_call_child_raw) {
|
|
975
|
-
this.on_call_child_raw(data);
|
|
976
|
-
}
|
|
977
|
-
});
|
|
978
|
-
child.on('exit', (code) => {
|
|
979
|
-
resolve(code !== null && code !== void 0 ? code : -1);
|
|
980
|
-
});
|
|
981
|
-
child.on('error', (error) => {
|
|
982
|
-
this.send_and_enter(error.message);
|
|
983
|
-
resolve(-1);
|
|
984
|
-
});
|
|
985
|
-
}
|
|
986
|
-
});
|
|
987
|
-
}
|
|
988
932
|
multiple_line(data, enter_index) {
|
|
989
933
|
return __awaiter(this, void 0, void 0, function* () {
|
|
990
934
|
if (!data)
|
|
@@ -1019,86 +963,94 @@ class PtyShell {
|
|
|
1019
963
|
this.js_func_child.write(str);
|
|
1020
964
|
}
|
|
1021
965
|
}
|
|
1022
|
-
spawn(
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
// this.send_and_enter(""); //
|
|
1028
|
-
// if (!this.child) {
|
|
1029
|
-
// this.on_call(`\n\r`); // 先换个行
|
|
1030
|
-
// }
|
|
1031
|
-
if ((use_noe_pty || this.shell_set.has("*") || this.shell_set.has(exe)) && this.node_pty) {
|
|
1032
|
-
// if (!exe.includes('exe') && exe !== 'bash' && exe !== 'sh') {
|
|
1033
|
-
// exe += '.exe';
|
|
966
|
+
spawn(exe_1, params_1) {
|
|
967
|
+
return __awaiter(this, arguments, void 0, function* (exe, params, use_noe_pty = true) {
|
|
968
|
+
// if (this.not_use_node_pre_cmd_exec) {
|
|
969
|
+
// this.send_and_enter(`not_use_node_pre_cmd_exec is true`);
|
|
970
|
+
// return;
|
|
1034
971
|
// }
|
|
1035
|
-
this.
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
// maxBuffer: 1024 * 1024 * 10// 设置缓冲区为 10 MB
|
|
1069
|
-
});
|
|
1070
|
-
// 设置编码为 'utf8',确保输出按 UTF-8 编码解析
|
|
1071
|
-
this.child.stdout.setEncoding('utf8');
|
|
1072
|
-
this.child.stdout.on('data', (data) => {
|
|
1073
|
-
// const v = data.toString(); // 子程序没有换行等符号不会立即输出 有缓冲区
|
|
1074
|
-
this.send_and_enter(data.toString());
|
|
1075
|
-
if (this.on_call_child_raw) {
|
|
1076
|
-
this.on_call_child_raw(data);
|
|
972
|
+
// this.send_and_enter(""); //
|
|
973
|
+
// if (!this.child) {
|
|
974
|
+
// this.on_call(`\n\r`); // 先换个行
|
|
975
|
+
// }
|
|
976
|
+
return new Promise(resolve => {
|
|
977
|
+
if ((use_noe_pty || this.shell_set.has("*") || this.shell_set.has(exe)) && this.node_pty) {
|
|
978
|
+
// if (!exe.includes('exe') && exe !== 'bash' && exe !== 'sh') {
|
|
979
|
+
// exe += '.exe';
|
|
980
|
+
// }
|
|
981
|
+
this.on_call(`\n\r`); // 先换个行
|
|
982
|
+
this.node_pty_child = this.node_pty.spawn(exe, params, {
|
|
983
|
+
name: 'xterm-color',
|
|
984
|
+
cols: this.cols,
|
|
985
|
+
rows: this.rows,
|
|
986
|
+
cwd: this.cwd, // 设置子进程的工作目录
|
|
987
|
+
useConptyDll: false,
|
|
988
|
+
useConpty: process.env.NODE_ENV !== "production" ? false : undefined, // conpty 可以支持 bash 等命令 从 Windows 10 版本 1809 开始提供 , 但是如果使用了 powershell 这个也就没有必要了,而且设置为false才能使用debug模式运行
|
|
989
|
+
env: Object.assign(Object.assign({}, process.env), this.env), // 传递环境变量
|
|
990
|
+
});
|
|
991
|
+
this.node_pty_child.onData((data) => {
|
|
992
|
+
this.on_call(data.toString());
|
|
993
|
+
if (this.on_call_child_raw) {
|
|
994
|
+
this.on_call_child_raw(data);
|
|
995
|
+
}
|
|
996
|
+
});
|
|
997
|
+
this.node_pty_child.onExit(({ exitCode, signal }) => {
|
|
998
|
+
// this.close_child(exitCode);
|
|
999
|
+
// this.send_and_enter("");
|
|
1000
|
+
this.send_and_enter(`pty with ${exitCode}`);
|
|
1001
|
+
// this.next_not_enter = false; // 下一次的换行输出 上一次没有换行
|
|
1002
|
+
resolve(exitCode !== null && exitCode !== void 0 ? exitCode : -1);
|
|
1003
|
+
});
|
|
1004
|
+
// this.is_pty = true;
|
|
1077
1005
|
}
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1006
|
+
else {
|
|
1007
|
+
// this.is_pty = false;
|
|
1008
|
+
// 其他的没有必要再创建一个 tty 都是资源消耗
|
|
1009
|
+
this.child = child_process.spawn(exe, params, {
|
|
1010
|
+
// shell:getShell(),
|
|
1011
|
+
cwd: this.cwd, // 设置子进程的工作目录
|
|
1012
|
+
env: Object.assign(Object.assign(Object.assign({}, process.env), this.env), { LANG: 'en_US.UTF-8' }), // 传递环境变量
|
|
1013
|
+
// stdio: 'inherit' // 让子进程的输入输出与父进程共享 pipe ignore inherit
|
|
1014
|
+
// timeout: 5000, // 设置超时为 5 秒
|
|
1015
|
+
// maxBuffer: 1024 * 1024 * 10// 设置缓冲区为 10 MB
|
|
1016
|
+
});
|
|
1017
|
+
// 设置编码为 'utf8',确保输出按 UTF-8 编码解析
|
|
1018
|
+
this.child.stdout.setEncoding('utf8');
|
|
1019
|
+
this.child.stdout.on('data', (data) => {
|
|
1020
|
+
// const v = data.toString(); // 子程序没有换行等符号不会立即输出 有缓冲区
|
|
1021
|
+
this.send_and_enter(data.toString());
|
|
1022
|
+
if (this.on_call_child_raw) {
|
|
1023
|
+
this.on_call_child_raw(data);
|
|
1024
|
+
}
|
|
1025
|
+
});
|
|
1026
|
+
this.child.stderr.on('data', (data) => {
|
|
1027
|
+
// const v = data.toString();
|
|
1028
|
+
this.send_and_enter(data.toString());
|
|
1029
|
+
this.next_not_enter = false; // 下一次的换行输出 上一次没有换行
|
|
1030
|
+
if (this.on_call_child_raw) {
|
|
1031
|
+
this.on_call_child_raw(data);
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
this.child.on('exit', (code) => {
|
|
1035
|
+
// this.close_child(code);
|
|
1036
|
+
// if (code !== 1) {
|
|
1037
|
+
this.send_and_enter(`process exited with code ${code}`);
|
|
1038
|
+
// }
|
|
1039
|
+
// else {
|
|
1040
|
+
// this.send_and_enter(``);
|
|
1041
|
+
// }
|
|
1042
|
+
// this.next_not_enter = false; // 下一次的换行输出 上一次没有换行
|
|
1043
|
+
resolve(code !== null && code !== void 0 ? code : -1);
|
|
1044
|
+
});
|
|
1045
|
+
this.child.on('error', (error) => {
|
|
1046
|
+
// this.next_not_enter = false; // 下一次的换行输出 上一次没有换行
|
|
1047
|
+
// this.close_child(-1);
|
|
1048
|
+
this.send_and_enter(error.message);
|
|
1049
|
+
resolve(-1);
|
|
1050
|
+
});
|
|
1085
1051
|
}
|
|
1086
1052
|
});
|
|
1087
|
-
|
|
1088
|
-
this.close_child(code);
|
|
1089
|
-
// if (code !== 1) {
|
|
1090
|
-
// this.send_and_enter(`process exited with code ${code}`);
|
|
1091
|
-
// } else {
|
|
1092
|
-
this.send_and_enter(``);
|
|
1093
|
-
// }
|
|
1094
|
-
this.next_not_enter = false; // 下一次的换行输出 上一次没有换行
|
|
1095
|
-
});
|
|
1096
|
-
this.child.on('error', (error) => {
|
|
1097
|
-
this.next_not_enter = false; // 下一次的换行输出 上一次没有换行
|
|
1098
|
-
this.close_child(-1);
|
|
1099
|
-
this.send_and_enter(error.message);
|
|
1100
|
-
});
|
|
1101
|
-
}
|
|
1053
|
+
});
|
|
1102
1054
|
}
|
|
1103
1055
|
exec_cmd(exe, params) {
|
|
1104
1056
|
return __awaiter(this, void 0, void 0, function* () {
|