@j-o-r/sh 1.1.18 → 1.1.19

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
@@ -27,6 +27,7 @@
27
27
  // Modified by: jorrit.duin+sh[AT]gmail.com
28
28
  /** @type {assert} */
29
29
  import assert from 'node:assert';
30
+ import readline from 'node:readline/promises';
30
31
  import SHDispatch from './SHDispatch.js';
31
32
  import Test from './Test.js'
32
33
  import AsyncTracker from './AsyncTracker.js'
@@ -147,7 +148,7 @@ const within = async (callback) => {
147
148
  * This function reads the standard input (stdin) from the current process.
148
149
  * @returns {Promise<string>}
149
150
  * @example
150
- * const content = await stdin();
151
+ * const content = await readIn();
151
152
  */
152
153
  const readIn = async () => {
153
154
  if (process.stdin.isTTY) return ''; // nothing was piped
@@ -159,6 +160,46 @@ const readIn = async () => {
159
160
  return buf;
160
161
  }
161
162
 
163
+
164
+ /**
165
+ * Get user input from the command line (stdin)
166
+ * @param {string} prompt - prompt or question
167
+ * @returns {Promise<string>}
168
+ * @example
169
+ * const content = await userIn();
170
+ */
171
+
172
+ const userIn = (prompt) => new Promise((resolve) => {
173
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
174
+ let buffer = '';
175
+ let timer;
176
+ /**
177
+ * @param {string} chunk
178
+ */
179
+ const onData = (chunk) => {
180
+ if (chunk.length > 4 && chunk.includes('\n')) {
181
+ clearTimeout(timer);
182
+ timer = setTimeout(resolveFunc, 50);
183
+ }
184
+ };
185
+ const resolveFunc = () => {
186
+ let fullText = buffer;
187
+ if (rl.line) fullText += rl.line;
188
+ process.stdin.removeListener('data', onData);
189
+ resolve(fullText);
190
+ rl.close();
191
+ };
192
+ rl.on('line', (line) => {
193
+ buffer += line + '\n';
194
+ clearTimeout(timer);
195
+ timer = setTimeout(resolveFunc, 50);
196
+ });
197
+ process.stdin.on('data', onData);
198
+
199
+ rl.setPrompt(prompt);
200
+ rl.prompt();
201
+ });
202
+
162
203
  /**
163
204
  * Retries a given asynchronous function a specified number of times with optional delays between attempts.
164
205
  *
@@ -308,6 +349,7 @@ export {
308
349
  sleep,
309
350
  retry,
310
351
  readIn,
352
+ userIn,
311
353
  within,
312
354
  expBackoff,
313
355
  parseArgs,
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.18",
5
+ "version": "1.1.19",
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
@@ -57,10 +57,18 @@ export function retry(count: number, a: string | number | typeof expBackoff | Fu
57
57
  * This function reads the standard input (stdin) from the current process.
58
58
  * @returns {Promise<string>}
59
59
  * @example
60
- * const content = await stdin();
60
+ * const content = await readIn();
61
61
  */
62
62
  export function readIn(): Promise<string>;
63
63
  /**
64
+ * Get user input from the command line (stdin)
65
+ * @param {string} prompt - prompt or question
66
+ * @returns {Promise<string>}
67
+ * @example
68
+ * const content = await userIn();
69
+ */
70
+ export function userIn(prompt: string): Promise<string>;
71
+ /**
64
72
  * Create a async/sync context in new execution callstack
65
73
  * @param {function} callback - async/sync function
66
74
  * @example