dsherret / dax

Cross-platform shell tools for Deno and Node.js inspired by zx.
MIT License
968 stars 33 forks source link

How about adding pipe function to command #137

Closed impactaky closed 5 months ago

impactaky commented 1 year ago

Hello,

Thanks for a great tool. I wanted to write neatly by chaining pipe() to command call.

I wrote the following code. This met my requirements. What do you think?

import { CommandBuilder } from "https://deno.land/x/dax@0.31.0/mod.ts";
CommandBuilder.prototype.pipe = function (next: CommandBuilder) {
  const p = this.stdout("piped").spawn();
  return next.stdin(p.stdout());
};

const ret = await $`echo "foo\nbar"`
  .pipe($`grep foo`)
  .text();
console.log(ret);
sigmaSd commented 1 year ago

looks useful, I just happened to use this (+chatgpt added type declarations)

import { $, CommandBuilder } from "https://deno.land/x/dax@0.31.0/mod.ts";

declare module "https://deno.land/x/dax@0.31.0/mod.ts" {
  interface CommandBuilder {
    pipe(next: CommandBuilder): CommandBuilder;
  }
}

CommandBuilder.prototype.pipe = function (
  next: CommandBuilder,
): CommandBuilder {
  const p = this.stdout("piped").spawn();
  return next.stdin(p.stdout());
};

await $`curl https://nim-lang.org/choosenim/init.sh -sSf`.pipe($`sh -s -- -y`);

maybe this is a good addition until pipes in strings are supported (like "curl | sh")

sigmaSd commented 1 year ago

and ideally something like .xargs("sh ${i} -y") , though no idea for now how to make such thing work

impactaky commented 1 year ago

Thank you! I enhanced the Wrapper I wrote for myself and made it public. https://github.com/impactaky/dax_extras I also thought about using the (string) => CommandBuilder function for xargs and implemented it.

sigmaSd commented 1 year ago

cool thanks as well