dax icon indicating copy to clipboard operation
dax copied to clipboard

How about adding pipe function to command

Open impactaky opened this issue 1 year ago • 4 comments

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/[email protected]/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);

impactaky avatar Apr 09 '23 10:04 impactaky

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

import { $, CommandBuilder } from "https://deno.land/x/[email protected]/mod.ts";

declare module "https://deno.land/x/[email protected]/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 avatar Jun 23 '23 07:06 sigmaSd

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

sigmaSd avatar Jun 23 '23 07:06 sigmaSd

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.

impactaky avatar Jun 25 '23 05:06 impactaky

cool thanks as well

sigmaSd avatar Jun 25 '23 06:06 sigmaSd