@j-o-r/sh 1.1.26 → 1.1.28
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 +25 -3
- package/package.json +1 -1
- package/types/SH.d.ts +9 -2
package/lib/SH.js
CHANGED
|
@@ -103,6 +103,27 @@ const parseDuration = (d) => {
|
|
|
103
103
|
}
|
|
104
104
|
throw new Error(`Unknown duration: "${d}".`);
|
|
105
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* bash escape a string suitable as an argument on the commandline
|
|
108
|
+
* for javascript code
|
|
109
|
+
* @param {string} x
|
|
110
|
+
* @returns {string}
|
|
111
|
+
*/
|
|
112
|
+
const bashEscape = (x) => {
|
|
113
|
+
let str = String(x).trim();
|
|
114
|
+
// the trick is to double escape escape vars first
|
|
115
|
+
str = str.replace(/\\/g, '\\\\');
|
|
116
|
+
// then add escaping for oddities
|
|
117
|
+
// Replace literal escape sequences like \n, \t, \r in quoted strings
|
|
118
|
+
str = str.replace(/(['"])\\(.)\1/g, '$1\\\\$2$1');
|
|
119
|
+
// escape $ (is a BASH var)
|
|
120
|
+
str = str.replace(/\$/g, '\\$');
|
|
121
|
+
// Escape backticks
|
|
122
|
+
str = str.replace(/`/g, '\\`');
|
|
123
|
+
// Escape quotes
|
|
124
|
+
str = str.replace(/"/g, '\\"');
|
|
125
|
+
return str;
|
|
126
|
+
}
|
|
106
127
|
|
|
107
128
|
|
|
108
129
|
/** @type {import('./SHDispatch.js').SHOptions} */
|
|
@@ -194,10 +215,10 @@ const within = async (callback) => {
|
|
|
194
215
|
*/
|
|
195
216
|
/**
|
|
196
217
|
* Read entire stdin as UTF-8. If stdin is a TTY, resolves to an empty string.
|
|
197
|
-
* @returns {Promise<string>}
|
|
218
|
+
* @returns {Promise<string|undefined>}
|
|
198
219
|
*/
|
|
199
220
|
const readIn = async () => {
|
|
200
|
-
if (process.stdin.isTTY) return
|
|
221
|
+
if (process.stdin.isTTY) return; // nothing was piped
|
|
201
222
|
let buf = '';
|
|
202
223
|
process.stdin.setEncoding('utf8');
|
|
203
224
|
for await (const chunk of process.stdin) {
|
|
@@ -436,5 +457,6 @@ export {
|
|
|
436
457
|
hasProp,
|
|
437
458
|
Test,
|
|
438
459
|
assert,
|
|
439
|
-
AsyncTracker
|
|
460
|
+
AsyncTracker,
|
|
461
|
+
bashEscape
|
|
440
462
|
}
|
package/package.json
CHANGED
package/types/SH.d.ts
CHANGED
|
@@ -69,9 +69,9 @@ export function retry(count: number, a: string | number | typeof expBackoff | Fu
|
|
|
69
69
|
*/
|
|
70
70
|
/**
|
|
71
71
|
* Read entire stdin as UTF-8. If stdin is a TTY, resolves to an empty string.
|
|
72
|
-
* @returns {Promise<string>}
|
|
72
|
+
* @returns {Promise<string|undefined>}
|
|
73
73
|
*/
|
|
74
|
-
export function readIn(): Promise<string>;
|
|
74
|
+
export function readIn(): Promise<string | undefined>;
|
|
75
75
|
/**
|
|
76
76
|
* Get user input from the command line (stdin)
|
|
77
77
|
* void when input has been aborted
|
|
@@ -187,5 +187,12 @@ export function hasProp(o: any, p: string): boolean;
|
|
|
187
187
|
import Test from './Test.js';
|
|
188
188
|
import assert from 'node:assert';
|
|
189
189
|
import AsyncTracker from './AsyncTracker.js';
|
|
190
|
+
/**
|
|
191
|
+
* bash escape a string suitable as an argument on the commandline
|
|
192
|
+
* for javascript code
|
|
193
|
+
* @param {string} x
|
|
194
|
+
* @returns {string}
|
|
195
|
+
*/
|
|
196
|
+
export function bashEscape(x: string): string;
|
|
190
197
|
import SHDispatch from './SHDispatch.js';
|
|
191
198
|
export { Test, assert, AsyncTracker };
|