zx icon indicating copy to clipboard operation
zx copied to clipboard

Please export `utils/formatCmd` to make patching the log function less hacky.

Open jessekrubin opened this issue 3 years ago • 3 comments

Why does the log function default to writing to stderr?

Using a custom log function isn't hard, but my ezpz version just involved me copy-pasta-ing your formatCmd function.

IMO the formatCmd function (as well as most of the things in utils) should be exported publicly.

Is there a recommended way of piping a process' stderr to stderr, BUT piping/writing everything else to stdout?

Thanks!

PS: I really do want to know the rational behind pumping all the process related stuff to stderr. (not trying to be a nuni)

jessekrubin avatar Jul 15 '22 04:07 jessekrubin

I don't what to export all utils, as this is internal things and I want them to change without introducing breaking changes.

But for formatCmd probably we can export it somehow. Please, show an example of your log function.

PS: I really do want to know the rational behind pumping all the process related stuff to stderr.

Stderr usually used for debugging purposes. And stdout for data passing. Consider zx script.js | some-cmd.

antonmedv avatar Jul 15 '22 07:07 antonmedv

@antonmedv My log function (which is p similar to your/zx's log function is below.

It seems weird to me that stderr is the default destination for most outputs. Personally I would almost want just errors from my processes to go to stderr.

Any which way zx is still very good.

export function log(entry) {
  switch (entry.kind) {
    case 'cmd':
      if (!entry.verbose) return;
      process.stdout.write(`\n>$ ${entry.cmd}\n`);
      break;
    case 'stdout':
      if (!entry.verbose) return;
      process.stdout.write(entry.data);
      break;
    case 'stderr':
      if (!entry.verbose) return;
      process.stderr.write(entry.data);
      break;
    case 'cd':
      if (!$.verbose) return;
      process.stdout.write('$ ' + chalk.greenBright('cd') + ` ${entry.dir}\n`);
      break;
    case 'fetch':
      if (!$.verbose) return;
      const init = entry.init ? ' ' + inspect(entry.init) : '';
      process.stdout.write(
        '$ ' + chalk.greenBright('fetch') + ` ${entry.url}${init}\n`,
      );
      break;
    case 'retry':
      if (!$.verbose) return;
      process.stdout.write(entry.error + '\n');
  }
}

$.log = log;

jessekrubin avatar Jul 15 '22 20:07 jessekrubin

One of the reasons is what output of zx script will change if —quiet flag is passed.

So to make sure some cmd are used to pront data should use:

await $’cmd’.pipe(process.stdout)

antonmedv avatar Jul 16 '22 09:07 antonmedv