Commit 5671166

Anton Medvedev <anton@medv.io>
2021-07-03 23:02:24
Use bash $'...' for quote
1 parent 7cd3299
index.mjs
@@ -26,7 +26,6 @@ import {createInterface} from 'readline'
 import {default as nodeFetch} from 'node-fetch'
 import which from 'which'
 import chalk from 'chalk'
-import shq from 'shq'
 
 export function $(pieces, ...args) {
   let __from = (new Error().stack.split('at ')[2]).trim()
@@ -91,7 +90,7 @@ try {
   // Bash not found, no prefix.
   $.prefix = ''
 }
-$.quote = shq
+$.quote = quote
 $.cwd = undefined
 
 export function cd(path) {
@@ -236,6 +235,24 @@ function substitute(arg) {
   return arg.toString()
 }
 
+function quote(arg) {
+  if (/^[a-z0-9_.-]+$/i.test(arg)) {
+    return arg
+  }
+  return `$'`
+    + arg
+      .replaceAll('\\', '\\\\')
+      .replaceAll('\'', '\\\'')
+      .replaceAll('\b', '\\b')
+      .replaceAll('\f', '\\f')
+      .replaceAll('\n', '\\n')
+      .replaceAll('\r', '\\r')
+      .replaceAll('\t', '\\t')
+      .replaceAll('\v', '\\v')
+      .replaceAll('\u0000', '\\0')
+    + `'`
+}
+
 Object.assign(global, {
   $,
   cd,
package.json
@@ -14,11 +14,10 @@
     "test": "node zx.mjs test.mjs"
   },
   "dependencies": {
-    "@types/node": "^15.3",
+    "@types/node": "^16.0",
     "@types/node-fetch": "^2.5.10",
     "chalk": "^4.1.1",
     "node-fetch": "^2.6.1",
-    "shq": "^1.0.2",
     "which": "^2.0.2"
   },
   "publishConfig": {
README.md
@@ -261,8 +261,6 @@ Default is `set -euo pipefail;`.
 Specifies a function for escaping special characters during 
 command substitution.
 
-Default is the [shq](https://www.npmjs.com/package/shq) package.
-
 #### `$.verbose`
 
 Specifies verbosity. Default is `true`.