@j-o-r/sh 1.1.19 → 1.1.20

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/lib/SH.js CHANGED
@@ -107,6 +107,7 @@ const SH = new Proxy(function(pieces, ...args) {
107
107
  let s;
108
108
 
109
109
  if (Array.isArray(args[i])) {
110
+ // @ts-ignore
110
111
  s = args[i].map(/** @param {string} x */(x) => {
111
112
  // Trim every element
112
113
  let str = String(x).trim();
@@ -163,32 +164,42 @@ const readIn = async () => {
163
164
 
164
165
  /**
165
166
  * Get user input from the command line (stdin)
167
+ * void when input has been aborted
166
168
  * @param {string} prompt - prompt or question
167
- * @returns {Promise<string>}
169
+ * @returns {{input: Promise<string|void>, abort: function(): void}}
168
170
  * @example
169
- * const content = await userIn();
171
+ * const user = userIn('Question: ');
172
+ * const content = await user.input
173
+ * // user.abort()
170
174
  */
171
-
172
- const userIn = (prompt) => new Promise((resolve) => {
175
+ const userIn = (prompt) => {
176
+ let resolvePromise;
177
+ const input = new Promise((resolve) => { resolvePromise = resolve; });
173
178
  const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
174
179
  let buffer = '';
175
180
  let timer;
176
- /**
177
- * @param {string} chunk
178
- */
181
+ /** @param {string} chunk */
179
182
  const onData = (chunk) => {
180
183
  if (chunk.length > 4 && chunk.includes('\n')) {
181
184
  clearTimeout(timer);
182
185
  timer = setTimeout(resolveFunc, 50);
183
186
  }
184
187
  };
188
+
185
189
  const resolveFunc = () => {
186
190
  let fullText = buffer;
187
191
  if (rl.line) fullText += rl.line;
192
+ cleanup();
193
+ resolvePromise(fullText);
194
+ };
195
+
196
+ const cleanup = () => {
188
197
  process.stdin.removeListener('data', onData);
189
- resolve(fullText);
198
+ rl.removeAllListeners('line');
199
+ clearTimeout(timer);
190
200
  rl.close();
191
201
  };
202
+
192
203
  rl.on('line', (line) => {
193
204
  buffer += line + '\n';
194
205
  clearTimeout(timer);
@@ -198,8 +209,15 @@ const userIn = (prompt) => new Promise((resolve) => {
198
209
 
199
210
  rl.setPrompt(prompt);
200
211
  rl.prompt();
201
- });
202
212
 
213
+ return {
214
+ input,
215
+ abort: () => {
216
+ cleanup();
217
+ resolvePromise();
218
+ }
219
+ };
220
+ };
203
221
  /**
204
222
  * Retries a given asynchronous function a specified number of times with optional delays between attempts.
205
223
  *
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@j-o-r/sh",
3
3
  "author": "Jorrit Duin <j-o-r@duin.work>",
4
4
  "type": "module",
5
- "version": "1.1.19",
5
+ "version": "1.1.20",
6
6
  "description": "Execute shell commands on Linux-based systems from javascript",
7
7
  "main": "lib/SH.js",
8
8
  "types": "types/SH.d.ts",
package/types/SH.d.ts CHANGED
@@ -62,12 +62,18 @@ export function retry(count: number, a: string | number | typeof expBackoff | Fu
62
62
  export function readIn(): Promise<string>;
63
63
  /**
64
64
  * Get user input from the command line (stdin)
65
+ * void when input has been aborted
65
66
  * @param {string} prompt - prompt or question
66
- * @returns {Promise<string>}
67
+ * @returns {{input: Promise<string|void>, abort: function(): void}}
67
68
  * @example
68
- * const content = await userIn();
69
+ * const user = userIn('Question: ');
70
+ * const content = await user.input
71
+ * // user.abort()
69
72
  */
70
- export function userIn(prompt: string): Promise<string>;
73
+ export function userIn(prompt: string): {
74
+ input: Promise<string | void>;
75
+ abort: () => void;
76
+ };
71
77
  /**
72
78
  * Create a async/sync context in new execution callstack
73
79
  * @param {function} callback - async/sync function