@deot/dev-test 1.0.2 → 1.1.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.
@@ -0,0 +1,124 @@
1
+ 'use strict';
2
+
3
+ var devShared = require('@deot/dev-shared');
4
+ var childProcess = require('child_process');
5
+
6
+ function _interopNamespaceDefault(e) {
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n.default = e;
20
+ return Object.freeze(n);
21
+ }
22
+
23
+ var childProcess__namespace = /*#__PURE__*/_interopNamespaceDefault(childProcess);
24
+
25
+ const { LOCAL_COMMAND_MAP } = devShared.Shell;
26
+ const KEY_MAP = {
27
+ DOWN: '\x1b\x5b\x42',
28
+ UP: '\x1b\x5b\x41',
29
+ ENTER: '\x0d',
30
+ SPACE: '\x7f'
31
+ };
32
+ class Command {
33
+ target;
34
+ code;
35
+ error;
36
+ resolve;
37
+ reject;
38
+ stdout;
39
+ stderr;
40
+ emitter;
41
+ isClose;
42
+ schedule;
43
+ constructor(command, args) {
44
+ this.target = new Promise((resolve, reject) => {
45
+ this.resolve = this._handleEnd(resolve);
46
+ this.reject = this._handleEnd(reject);
47
+ });
48
+ this.code = null;
49
+ this.error = null;
50
+ this.stdout = '';
51
+ this.stderr = '';
52
+ this.emitter = this.start(command, args);
53
+ this.isClose = false;
54
+ this.schedule = {
55
+ target: Promise.resolve(),
56
+ complete: () => { }
57
+ };
58
+ }
59
+ _handleEnd(fn) {
60
+ return (e) => {
61
+ this.isClose = true;
62
+ const { code, error } = e;
63
+ this.code = code;
64
+ this.error = error;
65
+ fn({
66
+ ...e,
67
+ stdout: this.stdout,
68
+ stderr: this.stderr
69
+ });
70
+ };
71
+ }
72
+ start(command, args) {
73
+ const SPACE = ' ';
74
+ const [command$, ...args$] = (command + SPACE + args.join(SPACE))
75
+ .replace(/\s+/g, SPACE)
76
+ .split(SPACE)
77
+ .filter(i => !!i)
78
+ .map(i => LOCAL_COMMAND_MAP[i] || i);
79
+ const emitter = childProcess__namespace.spawn(command$, args$, {
80
+ stdio: ['pipe', 'pipe', 'pipe']
81
+ });
82
+ emitter.on('close', (code) => {
83
+ if (code !== 0) {
84
+ this.reject({ code });
85
+ }
86
+ else {
87
+ this.resolve({ code });
88
+ }
89
+ });
90
+ emitter.on('error', (error) => {
91
+ !process.exitCode && (process.exitCode = 1);
92
+ this.reject({ code: process.exitCode, error });
93
+ });
94
+ emitter.stdout.on('data', e => {
95
+ this.stdout += e.toString();
96
+ this.schedule.complete();
97
+ });
98
+ emitter.stderr.on('data', e => this.stderr += e.toString());
99
+ return emitter;
100
+ }
101
+ async stop() {
102
+ await this.schedule.target;
103
+ if (!this.isClose) {
104
+ this.emitter.stdin.end();
105
+ this.isClose = true;
106
+ }
107
+ const response = await this.target;
108
+ return response;
109
+ }
110
+ async press(key, timeout = 200) {
111
+ if (!key || this.isClose)
112
+ return;
113
+ await this.schedule.target;
114
+ this.schedule.target = new Promise(resolve => {
115
+ this.schedule.complete = resolve;
116
+ });
117
+ await new Promise(resolve => {
118
+ this.emitter.stdin.write(KEY_MAP[key.toUpperCase()] || key, 'utf8', resolve);
119
+ });
120
+ await new Promise(_ => setTimeout(_, timeout));
121
+ }
122
+ }
123
+
124
+ exports.Command = Command;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@deot/dev-test",
3
- "version": "1.0.2",
4
- "main": "dist/index.js",
3
+ "version": "1.1.1",
4
+ "main": "dist/index.es.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
7
  "files": [
@@ -12,6 +12,6 @@
12
12
  "access": "public"
13
13
  },
14
14
  "dependencies": {
15
- "@deot/dev-shared": "^1.0.2"
15
+ "@deot/dev-shared": "^1.1.1"
16
16
  }
17
17
  }
File without changes