dsherret / dax

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

Ability to build a `$` with additional properties #77

Closed dsherret closed 1 year ago

dsherret commented 1 year ago

For example:

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

const commandBuilder = new CommandBuilder()
  .cwd("./subDir")
  .env("HTTPS_PROXY", "some_value");
const requestBuilder = new RequestBuilder()
  .header("SOME_NAME", "some value");

const $ = build$({
  commandBuilder,
  requestBuilder,
  // this part is new
  extras: {
    sayHi() {
      console.log("Hi!");
    },
  },
});

$.sayHi(); // outputs "Hi!"

const new$ = $.build$({
  // this part is new
  extras: {
    sayBye() {
      console.log("Bye!");
    },
  },
});

new$.sayHi(); // outputs "Hi!"
new$.sayBye(); // outputs "Bye!"
andrewbrey commented 1 year ago

If this API is added, many of the use-cases I would have for functions to add via the extras (helpers? plugins? worth bikeshedding over the name? :smile: ) object would want to have access to the constructed instance of $ in the function, i.e.:

const $ = build$({
  extras: {
    ghReleaseInfo(org, repo) {
      return $.request(`https://api.github.com/repos/${org}/${repo}/releases/latest`).json()
    },
  },
});

That should work with this "add extras during $ construction" API right? I'm pretty sure it will, but just wanted to put it on the radar for this enhancement request.

dsherret commented 1 year ago

@andrewbrey yeah, you can just do what you showed there. I added a test for it in the PR too.