Commit 9aaf788

Anton Medvedev <anton@medv.io>
2021-05-16 18:21:44
Add array argument support
1 parent 07a2b7b
index.mjs
@@ -39,7 +39,15 @@ 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 += $.quote(substitute(args[i])) + pieces[++i]
+  while (i < args.length) {
+    let s
+    if (Array.isArray(args[i])) {
+      s = args[i].map(x => $.quote(substitute(x))).join(' ')
+    } else {
+      s = $.quote(substitute(args[i]))
+    }
+    cmd += s + pieces[++i]
+  }
 
   if ($.verbose) console.log('$', colorize(cmd))
 
README.md
@@ -241,6 +241,17 @@ process.env.FOO = 'bar'
 await $`echo $FOO`
 ```
 
+### Passing array of values
+
+If array of values passed as argument to `$`, items of the array will be escaped
+individually and concatenated via space.
+
+Example:
+```js
+let files = [...]
+await $`tar cz ${files}`
+```
+
 ### Executing remote scripts
 
 If the argument to the `zx` executable starts with `https://`, the file will be 
test.mjs
@@ -74,8 +74,17 @@ import {strict as assert} from 'assert'
 
 {
   let foo = 0
-  let p = await $`echo -n ${foo}`
-  assert(p.stdout === '0')
+  let p = await $`echo ${foo}`
+  assert(p.stdout === '0\n')
+}
+
+{
+  try {
+    let files = ['./index.mjs', './zx.mjs', './package.json']
+    await $`tar czf archive ${files}`
+  } finally {
+    await $`rm archive`
+  }
 }
 
 {