dax
dax copied to clipboard
How about adding pipe function to command
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);
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")
and ideally something like .xargs("sh ${i} -y")
, though no idea for now how to make such thing work
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.
cool thanks as well