Skip to content

0.23.0

Compare
Choose a tag to compare
@dsherret dsherret released this 10 Jan 01:45
· 133 commits to main since this release
574a19c

What's Changed

  • feat: ability to add extra functions to $ #78
  • feat: add streaming api for command inputs and outputs #82
  • feat: explicit capturing of the combined stdout and stderr (BREAKING CHANGE) #80
  • fix: exit command should work in boolean list if not first item #81

Adding extra functions to $

In addition to setting different defaults for commands and requests, you can now also add your own functions to $ when you build a custom $:

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

const $ = build$({
  commandBuilder: new CommandBuilder()
    .cwd("./subDir")
    .env("HTTPS_PROXY", "some_value"),
  requestBuilder: new RequestBuilder()
    .header("SOME_NAME", "some value"),
  extras: {
    add(a: number, b: number) {
      return a + b;
    },
  },
});

await $`deno run my_script.ts`;
console.log(await $.request("https://plugins.dprint.dev/info.json").json());

// use your custom function
console.log($.add(1, 2));

Streaming API / CommandChild

The .spawn() method on a CommandBuilder will now return a CommandChild:

import $ from "https://deno.land/x/dax@0.23.0/mod.ts";

const child = $`echo 1 && sleep 2 && echo 2`.stdout("pipe").spawn();

This provides child.abort() to kill the child shell and two new methods for streaming the output—child.stdout() and child.stderr(). These return web streams that can be provided to the body of $.request or stdin of another command.

For example, the following will output 1, wait 2 seconds, then output 2 to the current process' stderr:

const child = $`echo 1 && sleep 2 && echo 2`.stdout("piped").spawn();
await $`deno eval 'await Deno.stdin.readable.pipeTo(Deno.stderr.writable);'`
  .stdin(child.stdout());

Explicit capturing of the combined stdout and stderr - BREAKING CHANGE

Previously, Dax would implicitly do a combined stdout and stderr capture when you piped both stdout and stderr. This would use more memory if necessary in some cases and led to some complexity with the new streaming API. You now have to explicitly opt in to get this output by toggling the captureCombined() builder method:

const result = await $`deno eval 'console.log(1); console.error(2); console.log(3);'`
  .captureCombined();

console.log(result.combined); // 1\n2\n3\n

Note: this will set stdout and stderr to "piped" if not already either "piped" or "inheritPiped".

Full Changelog: 0.22.0...0.23.0