❯ DZX
Deno shell tools inspired by zx
#!/usr/bin/env dzx
/// <reference path="https://deno.land/x/dzx/types.d.ts" />
console.log(`Hello from ${$.blue.bold("dzx")}!`);
const branch = await $`git branch --show-current`;
await $`dep deploy --branch=${branch}`;
await Promise.all([
$`deno lint --unstable`,
$`deno fmt --check`,
$`deno test --allow-all`,
]);
const name = "foo bar";
await $`mkdir /tmp/${name}`; // <-- string will be safly quoted to: /tmp/'foo bar'
Content
Install
deno install --allow-all -r -f https://deno.land/x/dzx/dzx.ts
Documentation
You can write your scripts in a file with .js, .mjs or .ts extension.
Add next shebang at the beginning of your script:
#!/usr/bin/env zx
Now you will be able to run your script as:
chmod +x ./script.js
./script.js
If you want to use typescript you need to add a tripple slash reference for the typings at the top of the file, but not before the shebang line.
#!/usr/bin/env zx
/// <reference path="https://deno.land/x/dzx/types.d.ts" />
Now you will be able to run your typescript script the same way as your js script:
chmod +x ./script.ts
./script.ts
You can also import all symbol directly from dzx/mod.ts
instead of using
globals.
#!/usr/bin/env zx
import { $ } from "https://deno.land/x/dzx/mod.ts";
$.verbose
Enable debugging output.
$.shell
Set the current shel.
$.cwd
Set the current working directory.
$.quote
Parser method that is used to safely quote strings. Used by: $`command`
$`command`
const count = parseInt(await $`ls -1 | wc -l`);
console.log(`Files count: ${count}`);
ProcessOutput
If the executed program was successful, an instance of ProcessOutput
will be
return.
class ProcessOutput {
readonly stdout: string;
readonly stderr: string;
readonly combined: string;
readonly status: Deno.ProcessStatus;
toString(): string;
}
ProcessError
The ProcessError
class extends from the Error
class and implements all
properties and methods from ProcessOutput
.
class ProcessError extends Error implements ProcessOutput {}
If the executed program returns a non-zero exit code, a ProcessError
will be
thrown.
try {
await $`exit 1`;
} catch (process) {
console.log(`Exit code: ${process.status.code}`);
console.log(`Error: ${process.stderr}`);
}
cd()
Set the current working directory. If path does not exist, an error is thrown.
$.[style]()
dzx has chainable color methods that are available on the global $
symbol.
console.log($.blue.bold("Hello world!"));
quote`string`
The quote methods quotes safly a string. by default the shq
package is used.
Can be overidden with $.quote
.
Remote usage
#!/usr/bin/env deno run --allow-run --allow-read --allow-env https://deno.land/x/dzx/dzx.ts
/// <reference path="https://deno.land/x/dzx/types.d.ts" />
console.log(`Hello ${$.blue.bold("world")}!`);
Contributing
Any kind of contribution is welcome!