Home

Awesome

<h1 align="center">dzx</h1> <p align="center" class="badges-container"> <a href="https://github.com/c4spar/deno-dzx/releases"> <img alt="Latest" src="https://img.shields.io/github/v/tag/c4spar/deno-dzx?style=flat&label=latest" /> </a> <a href="https://github.com/c4spar/deno-dzx/actions/workflows/test.yaml"> <img alt="Build" src="https://img.shields.io/github/workflow/status/c4spar/deno-dzx/Test?style=flat"/> </a> <a href="https://codecov.io/gh/c4spar/deno-dzx"> <img alt="Coverage" src="https://img.shields.io/codecov/c/github/c4spar/deno-dzx?style=flat"> </a> <a href="https://github.com/c4spar/deno-dzx/issues"> <img alt="Issues" src="https://img.shields.io/github/issues/c4spar/deno-dzx?style=flat" /> </a> <a href="https://github.com/c4spar/deno-dzx/blob/main/LICENSE"> <img alt="License" src="https://img.shields.io/github/license/c4spar/deno-dzx?style=flat" /> </a> <a href="https://deno.land/x/dzx"> <img alt="deno.land/x" src="https://img.shields.io/badge/deno.land/x/dzx-blue?logo=deno&logoColor=959DA6&color=272727" /> </a> </p> <p align="center"> <b>Deno shell tools inspired by <a href="https://github.com/google/zx">zx</a></b> </p>

Example

Warning This project is in a very experimental state. Many things are subject to change.

import {
  $,
  async,
  cd,
  fs,
  io,
  path,
} from "https://deno.land/x/dzx@0.4.0/mod.ts";

// Output stdout & stderr. Can be: true, false, 0, 1 or 2. Default is: 1
$.verbose = 2;
$.shell = "/usr/local/bin/zsh";

console.log(`Hello from ${$.blue.bold("dzx")}!`);

const branch = await $`git branch --show-current`;
await $`dep deploy --branch=${branch}`;

await Promise.all([
  $`deno lint`,
  $`deno fmt --check`,
  $`deno test --allow-all`,
]);

const name = "foo bar";
await $`mkdir ./tmp/${name}`; // Params will be quoted if required: /tmp/'foo bar'.

cd("tmp/foo bar");
console.log(Deno.cwd()); // ./tmp/foo bar

cd("tmp");
console.log(Deno.cwd()); // ./tmp

await async.delay(1000);
const basename = path.basename(import.meta.url);
const stdin = await io.readAll(Deno.stdin);
await fs.ensureDir("./tmp");

Content

Usage

dzx has different entry points.

Shell

The $ symbol (shell) is the main context of dzx.

Variables

Methods

Process

Methods and properties of the Process class which implements Promise<ProcessOutput> and is returned by the $ method.

Std modules

Note The ./mod.ts module exports several deno/std modules. If you don't need these modules you can just import the $ symbol from the ./shell.ts module.

Globals

Note Globals are mostly used by the cli. In most cases you don't need globals and you can just import all members from ./mod.ts or ./shell.ts.

When impoting ./globals.ts, all members exported by ./mod.ts are globally available.

import "https://deno.land/x/dzx@0.4.0/globals.ts";

cd("foo/bar");

await $`ls | wc -l`;

CLI

The CLI is a tool-chain and playground for dzx. It can execute scripts and provides some sub-commands like repl and eval where dzx is globally available.

Install

deno install --allow-all -f https://deno.land/x/dzx@0.4.0/dzx.ts

Commands

Execute scripts via cli

You can run a script with the cli by simply calling dzx followed by the filename dzx script.ts.

When a dzx script is executed via CLI, you don't need to import anything. All exports are automatically globally available. This applies also to commands like dzx eval "console.log($)".

To create an executable script, add next shebang at the beginning of your script:

#!/usr/bin/env dzx

After making your script executable,

chmod +x ./script.js

you can simply run it with:

./script.js

To enable typescript support in your IDE, you can optionally add a tripple slash reference to the top of the file.

#!/usr/bin/env dzx
/// <reference path="https://deno.land/x/dzx@0.4.0/globals.ts" />

Permissions

You can use dzx without installation by using next shebang. This also allows you to explicitly set the permissions for your script.

#!/usr/bin/env deno run --allow-run --allow-read --allow-env https://deno.land/x/dzx@0.4.0/dzx.ts
/// <reference path="https://deno.land/x/dzx@0.4.0/globals.ts" />

console.log(`Hello ${$.blue.bold("world")}!`);

Markdown

With dzx you can run the js/ts code blocks from a Markdown file as if they were a regular script. This is very convenient when you want to blend some nicely formatted documentation in with the actual steps of execution.

Give it a try by running:

dzx ./examples/markdown.md

Note See the markdown example for further documentation and notes.

Experimental

worker

Warning This is an exerminental feature. Permission flags doesn't support values currently. Read permissions are required by default.

If dzx is called with -w or --worker, the script is executed inside an isolated web worker. If enabled, you can pass explicit permissions directly to the dzx cli.

#!/usr/bin/env dzx --worker --allow-read
/// <reference path="https://deno.land/x/dzx@0.4.0/globals.ts" />

console.log(`Hello from ${$.blue.bold("worker")}!`);

Contributing

Any kind of contribution is very welcome!

License

MIT