@j-o-r/sh 1.1.26 → 1.1.27

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
@@ -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} */
@@ -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
@@ -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.26",
5
+ "version": "1.1.27",
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
@@ -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 };