Commit 31b7956

Anton Medvedev <anton@medv.io>
2021-05-08 16:55:01
Add $.prefix and $.quote
1 parent 4675b60
Changed files (2)
index.mjs
@@ -18,7 +18,7 @@ import {promisify} from 'util'
 import {createInterface} from 'readline'
 import {default as nodeFetch} from 'node-fetch'
 import chalk from 'chalk'
-import {default as escape} from 'shq'
+import shq from 'shq'
 
 export {chalk}
 
@@ -38,7 +38,7 @@ function substitute(arg) {
 export function $(pieces, ...args) {
   let __from = (new Error().stack.split('at ')[2]).trim()
   let cmd = pieces[0], i = 0
-  while (i < args.length) cmd += escape(substitute(args[i])) + pieces[++i]
+  while (i < args.length) cmd += $.quote(substitute(args[i])) + pieces[++i]
 
   if ($.verbose) console.log('$', colorize(cmd))
 
@@ -49,7 +49,7 @@ export function $(pieces, ...args) {
     if (typeof $.shell !== 'undefined') options.shell = $.shell
     if (typeof $.cwd !== 'undefined') options.cwd = $.cwd
 
-    let child = exec('set -euo pipefail;' + cmd, options)
+    let child = exec($.prefix + cmd, options)
     let stdout = '', stderr = '', combined = ''
     child.stdout.on('data', data => {
       if ($.verbose) process.stdout.write(data)
@@ -74,6 +74,8 @@ $.verbose = true
 // Try `which`, should cover most other cases.
 // Try `type` command, if the rest fails.
 $.shell = `${execSync('command -v bash || which bash || type -p bash')}`.trim()
+$.prefix = 'set -euo pipefail;'
+$.quote = shq
 $.cwd = undefined
 
 export function cd(path) {
README.md
@@ -179,6 +179,17 @@ Specifies what shell is used. Default is `which bash`.
 $.shell = '/usr/bin/bash'
 ```
 
+### `$.prefix`
+
+Specifies command what will be added to all command. Default is 
+`set -euo pipefail;`.
+
+### `$.quote`
+
+Specifies a function what will be used for escaping special characters in 
+command substitution. Default is [shq](https://www.npmjs.com/package/shq) 
+package.
+
 ### `$.verbose`
 
 Specifies verbosity. Default: `true`.