zx icon indicating copy to clipboard operation
zx copied to clipboard

Allow running a command from a string instead of string template

Open vjpr opened this issue 3 years ago • 1 comments

Expected Behavior

const foo = 'foo'
const cmd = `${foo} bar`
console.log(cmd)
$(cmd)

Works.

Actual Behavior

I doesn't work properly.


I want this so I can log commands before I run them.

Related

https://github.com/google/zx/issues/394

vjpr avatar Sep 12 '22 15:09 vjpr

  1. How it actually works:
const args = [
    'npm',
    'install',
    '--no-save',
    '--no-audit',
    '--no-fund',
  ]
  const pieces = new Array(args.length + 1).fill(' ')

  await $(pieces as any as TemplateStringsArray, ...args)
  1. quotes stands for security reasons.

  2. zx logs cmd if $.verbose is set to true. There's no need to add a custom hook for this.

export function log(entry: LogEntry) {
  switch (entry.kind) {
    case 'cmd':
      if (!entry.verbose) return
      process.stderr.write(formatCmd(entry.cmd))
      break
  1. You can substitute $ with your own custom version with any hooks. https://github.com/qiwi/zx-extra/blob/master/src/main/js/index.mjs#L11

antongolub avatar Sep 12 '22 17:09 antongolub