cmd-executor
Tiny proxy object to execute arbitrary CLI commands. All calls return a promise.
Installation
npm i cmd-executor -S
Usage
const cmd = // all calls return a promise, so you should wait for them to complete// before executing another command. You can use `.then()` or `await`.;async { // any attributes/functions/parameters you call on `cmd` will be parsed // into a command string and executed. For example: // will call `touch a.txt` await cmd // will call `echo "hello, world!" > a.txt` await cmd // since the attributes/functions are arbitrary, these are // functionally equivalent to the above line: // await cmd.echo["hello, world!"]('> a.txt') // await cmd.echo['"hello, world!"']['>']('a.txt') // only the last attribute can be called as a function // so this will not work: // await cmd.echo('"hello, world!"')['>']('a.txt') // any text the cli would print out is resolved by the promise const output = await cmd console // "hello, world!\n" // any errors will be rejected by the promise which you can catch // (or `.catch()` if not using `async`/`await`) try await cmd catch e console // error contains "mkdir: a.txt: File exists" // clean up the file await cmd}
Advanced Usage
// lets say you wanted a promise wrapper around `git`, you// could easily do this with cmd-executorconst git = git // and now you have complete access to the git CLI;async { await git await git await gitremote await git} // destructuring also works for whatever commands you wantconst mkdir touch echo cat rm = ;async { await await await console await }