pty-shell 1.1.0 → 1.2.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 +7 -2
- package/dist/index.js +68 -54
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -41,7 +41,7 @@ interface Param extends Partial<PtyShellUserMethod> {
|
|
|
41
41
|
history_line?: string[];
|
|
42
42
|
history_line_max?: number;
|
|
43
43
|
}
|
|
44
|
-
type CmdHandler = (params: string[], send_prompt?: (data: string) => void) => void
|
|
44
|
+
type CmdHandler = (params: string[], send_prompt?: (data: string) => void) => Promise<void>;
|
|
45
45
|
export declare class PtyShell implements PtyShellUserMethod {
|
|
46
46
|
rows: number;
|
|
47
47
|
cols: number;
|
|
@@ -55,9 +55,14 @@ export declare class PtyShell implements PtyShellUserMethod {
|
|
|
55
55
|
char_num: number;
|
|
56
56
|
};
|
|
57
57
|
on_call: (data: string) => void;
|
|
58
|
+
on_call_child_raw?: (data: string) => void;
|
|
58
59
|
on_control_cmd: (type: exec_cmd_type, data?: string) => void;
|
|
59
60
|
on_child_kill?: (code: number, pid?: number) => void;
|
|
60
|
-
check_exe_cmd:
|
|
61
|
+
check_exe_cmd?: (exe_cmd: string, params: string[]) => Promise<exec_type>;
|
|
62
|
+
cmd_replace?: (exe_cmd: string, params: string[]) => Promise<{
|
|
63
|
+
exe_cmd: string;
|
|
64
|
+
params: string[];
|
|
65
|
+
}>;
|
|
61
66
|
private cmd_set;
|
|
62
67
|
private shell_set;
|
|
63
68
|
private node_require;
|
package/dist/index.js
CHANGED
|
@@ -743,7 +743,7 @@ class PtyShell {
|
|
|
743
743
|
this.clear_line();
|
|
744
744
|
return;
|
|
745
745
|
}
|
|
746
|
-
|
|
746
|
+
let { exe, params } = this.get_exec(this.line);
|
|
747
747
|
this.history_line_index = -1;
|
|
748
748
|
try {
|
|
749
749
|
let use_noe_pty = false;
|
|
@@ -767,14 +767,17 @@ class PtyShell {
|
|
|
767
767
|
return;
|
|
768
768
|
}
|
|
769
769
|
}
|
|
770
|
+
if (this.cmd_replace) {
|
|
771
|
+
const r = yield this.cmd_replace(exe, params);
|
|
772
|
+
exe = r.exe_cmd;
|
|
773
|
+
params = r.params;
|
|
774
|
+
}
|
|
770
775
|
if (this.cmd_set.has(exe)) {
|
|
771
776
|
// 检测某个已经有预处理的命令 包括用户自定义的
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
return;
|
|
777
|
-
}
|
|
777
|
+
yield this.exec_cmd(exe, params);
|
|
778
|
+
// 不用再继续了
|
|
779
|
+
this.clear_line();
|
|
780
|
+
this.exec_end_call(0);
|
|
778
781
|
}
|
|
779
782
|
this.spawn(exe, params, use_noe_pty);
|
|
780
783
|
this.clear_line();
|
|
@@ -833,6 +836,9 @@ class PtyShell {
|
|
|
833
836
|
this.child = this.node_pty.spawn(exe, params, Object.assign({ name: 'xterm-color', cols: this.cols, rows: this.rows, cwd: this.cwd, useConptyDll: false, useConpty: process.env.NODE_ENV !== "production" ? false : undefined, env: Object.assign(Object.assign({}, process.env), this.env) }, spawn_option));
|
|
834
837
|
this.child.onData((data) => {
|
|
835
838
|
this.on_call(data.toString());
|
|
839
|
+
if (this.on_call_child_raw) {
|
|
840
|
+
this.on_call_child_raw(data);
|
|
841
|
+
}
|
|
836
842
|
});
|
|
837
843
|
this.child.onExit(({ exitCode, signal }) => {
|
|
838
844
|
this.close_child(exitCode);
|
|
@@ -856,11 +862,17 @@ class PtyShell {
|
|
|
856
862
|
this.child.stdout.on('data', (data) => {
|
|
857
863
|
// const v = data.toString(); // 子程序没有换行等符号不会立即输出 有缓冲区
|
|
858
864
|
this.send_and_enter(data.toString());
|
|
865
|
+
if (this.on_call_child_raw) {
|
|
866
|
+
this.on_call_child_raw(data);
|
|
867
|
+
}
|
|
859
868
|
});
|
|
860
869
|
this.child.stderr.on('data', (data) => {
|
|
861
870
|
// const v = data.toString();
|
|
862
871
|
this.send_and_enter(data.toString());
|
|
863
872
|
this.next_not_enter = false; // 下一次的换行输出 上一次没有换行
|
|
873
|
+
if (this.on_call_child_raw) {
|
|
874
|
+
this.on_call_child_raw(data);
|
|
875
|
+
}
|
|
864
876
|
});
|
|
865
877
|
this.child.on('exit', (code) => {
|
|
866
878
|
this.close_child(code);
|
|
@@ -879,58 +891,60 @@ class PtyShell {
|
|
|
879
891
|
}
|
|
880
892
|
}
|
|
881
893
|
exec_cmd(exe, params) {
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
}
|
|
891
|
-
switch (exe) {
|
|
892
|
-
case 'pwd':
|
|
893
|
-
{
|
|
894
|
-
this.send_and_enter(`${this.cwd}`);
|
|
895
|
-
}
|
|
894
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
895
|
+
try {
|
|
896
|
+
const handle = this.cmd_exec_map.get(exe);
|
|
897
|
+
if (exe !== 'cd' && handle) {
|
|
898
|
+
// 如果用户有了就用用户的 不用系统自己的 但是 cd 命令排除在外
|
|
899
|
+
yield handle(params, (data) => {
|
|
900
|
+
this.send_and_enter(data, true);
|
|
901
|
+
});
|
|
896
902
|
return true;
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
p = this.node_require.path.isAbsolute(params[0]) ? params[0] : this.node_require.path.join(this.cwd, params[0]);
|
|
903
|
-
if (!this.node_require.fs.existsSync(p)) {
|
|
904
|
-
this.send_and_enter(`not directory ${p}`);
|
|
905
|
-
}
|
|
903
|
+
}
|
|
904
|
+
switch (exe) {
|
|
905
|
+
case 'pwd':
|
|
906
|
+
{
|
|
907
|
+
this.send_and_enter(`${this.cwd}`);
|
|
906
908
|
}
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
909
|
+
return true;
|
|
910
|
+
case 'cd':
|
|
911
|
+
{
|
|
912
|
+
let p;
|
|
913
|
+
if (!this.not_use_node_pre_cmd_exec) {
|
|
914
|
+
// 有node环境可以检测一下
|
|
915
|
+
p = this.node_require.path.isAbsolute(params[0]) ? params[0] : this.node_require.path.join(this.cwd, params[0]);
|
|
916
|
+
if (!this.node_require.fs.existsSync(p)) {
|
|
917
|
+
this.send_and_enter(`not directory ${p}`);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
else {
|
|
921
|
+
// 没有node环境只能这样了 todo 对于 .. 路径有问题
|
|
922
|
+
p = (0, path_util_1.path_join)(this.cwd, params[0]);
|
|
923
|
+
}
|
|
924
|
+
this.cwd = p;
|
|
925
|
+
this.send_and_enter(``);
|
|
926
|
+
this.word_detection = undefined; // 清空检测器
|
|
910
927
|
}
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
928
|
+
return true;
|
|
929
|
+
case 'ls':
|
|
930
|
+
{
|
|
931
|
+
if (this.not_use_node_pre_cmd_exec) {
|
|
932
|
+
return false; // 让其它方式处理
|
|
933
|
+
}
|
|
934
|
+
const items = this.node_require.fs.readdirSync(this.cwd); // 读取目录内容
|
|
935
|
+
const v = this.cols_handle(" " + items.join(" "));
|
|
936
|
+
this.send_and_enter(v);
|
|
920
937
|
}
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
return true;
|
|
926
|
-
default:
|
|
927
|
-
return false; // 让其它方式处理
|
|
938
|
+
return true;
|
|
939
|
+
default:
|
|
940
|
+
return false; // 让其它方式处理
|
|
941
|
+
}
|
|
928
942
|
}
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
943
|
+
catch (e) {
|
|
944
|
+
this.send_and_enter(JSON.stringify(e));
|
|
945
|
+
}
|
|
946
|
+
return false; // 让其它方式处理
|
|
947
|
+
});
|
|
934
948
|
}
|
|
935
949
|
// 解析命令与参数
|
|
936
950
|
get_exec(str) {
|