pty-shell 1.1.1 → 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 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,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
- 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);
778
781
  }
779
782
  this.spawn(exe, params, use_noe_pty);
780
783
  this.clear_line();
@@ -888,58 +891,60 @@ class PtyShell {
888
891
  }
889
892
  }
890
893
  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
- }
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
+ });
905
902
  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
- }
903
+ }
904
+ switch (exe) {
905
+ case 'pwd':
906
+ {
907
+ this.send_and_enter(`${this.cwd}`);
915
908
  }
916
- else {
917
- // 没有node环境只能这样了 todo 对于 .. 路径有问题
918
- p = (0, path_util_1.path_join)(this.cwd, params[0]);
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; // 清空检测器
919
927
  }
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; // 让其它方式处理
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);
929
937
  }
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; // 让其它方式处理
938
+ return true;
939
+ default:
940
+ return false; // 让其它方式处理
941
+ }
937
942
  }
938
- }
939
- catch (e) {
940
- this.send_and_enter(JSON.stringify(e));
941
- }
942
- return false; // 让其它方式处理
943
+ catch (e) {
944
+ this.send_and_enter(JSON.stringify(e));
945
+ }
946
+ return false; // 让其它方式处理
947
+ });
943
948
  }
944
949
  // 解析命令与参数
945
950
  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.1",
4
4
  "description": "a virtual PTY shell for javaScript",
5
5
  "author": "xiaobaidadada",
6
6
  "main": "dist/index.js",