pty-shell 1.1.1 → 1.2.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/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;
@@ -58,7 +58,11 @@ export declare class PtyShell implements PtyShellUserMethod {
58
58
  on_call_child_raw?: (data: string) => void;
59
59
  on_control_cmd: (type: exec_cmd_type, data?: string) => void;
60
60
  on_child_kill?: (code: number, pid?: number) => void;
61
- check_exe_cmd: any;
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
+ }>;
62
66
  private cmd_set;
63
67
  private shell_set;
64
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
- const { exe, params } = this.get_exec(this.line);
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,18 @@ 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
- if (this.exec_cmd(exe, params)) {
773
- // 成功执行了不用再继续了
774
- this.clear_line();
775
- this.exec_end_call(0);
776
- return;
777
- }
777
+ yield this.exec_cmd(exe, params);
778
+ // 不用再继续了
779
+ this.clear_line();
780
+ this.exec_end_call(0);
781
+ return;
778
782
  }
779
783
  this.spawn(exe, params, use_noe_pty);
780
784
  this.clear_line();
@@ -888,58 +892,60 @@ class PtyShell {
888
892
  }
889
893
  }
890
894
  exec_cmd(exe, params) {
891
- try {
892
- const handle = this.cmd_exec_map.get(exe);
893
- if (exe !== 'cd' && handle) {
894
- // 如果用户有了就用用户的 不用系统自己的 但是 cd 命令排除在外
895
- handle(params, (data) => {
896
- this.send_and_enter(data, true);
897
- });
898
- return true;
899
- }
900
- switch (exe) {
901
- case 'pwd':
902
- {
903
- this.send_and_enter(`${this.cwd}`);
904
- }
895
+ return __awaiter(this, void 0, void 0, function* () {
896
+ try {
897
+ const handle = this.cmd_exec_map.get(exe);
898
+ if (exe !== 'cd' && handle) {
899
+ // 如果用户有了就用用户的 不用系统自己的 但是 cd 命令排除在外
900
+ yield handle(params, (data) => {
901
+ this.send_and_enter(data, true);
902
+ });
905
903
  return true;
906
- case 'cd':
907
- {
908
- let p;
909
- if (!this.not_use_node_pre_cmd_exec) {
910
- // 有node环境可以检测一下
911
- p = this.node_require.path.isAbsolute(params[0]) ? params[0] : this.node_require.path.join(this.cwd, params[0]);
912
- if (!this.node_require.fs.existsSync(p)) {
913
- this.send_and_enter(`not directory ${p}`);
914
- }
904
+ }
905
+ switch (exe) {
906
+ case 'pwd':
907
+ {
908
+ this.send_and_enter(`${this.cwd}`);
915
909
  }
916
- else {
917
- // 没有node环境只能这样了 todo 对于 .. 路径有问题
918
- p = (0, path_util_1.path_join)(this.cwd, params[0]);
910
+ return true;
911
+ case 'cd':
912
+ {
913
+ let p;
914
+ if (!this.not_use_node_pre_cmd_exec) {
915
+ // 有node环境可以检测一下
916
+ p = this.node_require.path.isAbsolute(params[0]) ? params[0] : this.node_require.path.join(this.cwd, params[0]);
917
+ if (!this.node_require.fs.existsSync(p)) {
918
+ this.send_and_enter(`not directory ${p}`);
919
+ }
920
+ }
921
+ else {
922
+ // 没有node环境只能这样了 todo 对于 .. 路径有问题
923
+ p = (0, path_util_1.path_join)(this.cwd, params[0]);
924
+ }
925
+ this.cwd = p;
926
+ this.send_and_enter(``);
927
+ this.word_detection = undefined; // 清空检测器
919
928
  }
920
- this.cwd = p;
921
- this.send_and_enter(``);
922
- this.word_detection = undefined; // 清空检测器
923
- }
924
- return true;
925
- case 'ls':
926
- {
927
- if (this.not_use_node_pre_cmd_exec) {
928
- return false; // 让其它方式处理
929
+ return true;
930
+ case 'ls':
931
+ {
932
+ if (this.not_use_node_pre_cmd_exec) {
933
+ return false; // 让其它方式处理
934
+ }
935
+ const items = this.node_require.fs.readdirSync(this.cwd); // 读取目录内容
936
+ const v = this.cols_handle(" " + items.join(" "));
937
+ this.send_and_enter(v);
929
938
  }
930
- const items = this.node_require.fs.readdirSync(this.cwd); // 读取目录内容
931
- const v = this.cols_handle(" " + items.join(" "));
932
- this.send_and_enter(v);
933
- }
934
- return true;
935
- default:
936
- return false; // 让其它方式处理
939
+ return true;
940
+ default:
941
+ return false; // 让其它方式处理
942
+ }
937
943
  }
938
- }
939
- catch (e) {
940
- this.send_and_enter(JSON.stringify(e));
941
- }
942
- return false; // 让其它方式处理
944
+ catch (e) {
945
+ this.send_and_enter(JSON.stringify(e));
946
+ }
947
+ return false; // 让其它方式处理
948
+ });
943
949
  }
944
950
  // 解析命令与参数
945
951
  get_exec(str) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pty-shell",
3
- "version": "1.1.1",
3
+ "version": "1.2.2",
4
4
  "description": "a virtual PTY shell for javaScript",
5
5
  "author": "xiaobaidadada",
6
6
  "main": "dist/index.js",