A tool and task runner for writing better scripts with Deno
dx is a tool and task runner for writing better scripts with Deno, and origin idea is from https://github.com/google/zx/.
dx is based on Deno and with following pros:
import {red, green} from "https://deno.land/[email protected]/fmt/colors.ts"
, no idea about zx to import third party npm(package.json???)$a
for async iterable line output, file globs, .env support etcdeno install -q -A --unstable --no-check -r -f -n dx https://deno.land/x/deno_dx/cli.ts
Create a demo.ts
file with following code:
#!/usr/bin/env dx
import {$, nothrow, cd, pwd, question, os, fs, env, printf, glob, $a, echo} from "https://deno.land/x/deno_dx/mod.ts";
import {red, yellow, blue, green} from "https://deno.land/[email protected]/fmt/colors.ts";
// aliases
$.alias("ll", "ls -al");
// prompt to input your name
let name = await question(blue("what's your name: "));
echo("Hello ", blue(name ?? "guest"));
// pwd(), env variables and params
echo("Current working directory:", pwd());
echo("Your home:", HOME);
echo("Your name:", USER);
echo("Script name:", $0);
// current file count
const output = await $`ls -1 | wc -l`;
echo("Files count: ", parseInt(output));
// output as lines
for await (const fileName of $a`ls -1 *.ts`) {
echo("TS file: ", fileName);
}
// alias and output as lines
for await (const fileName of $a`ll *.ts`) {
echo("TS file: ", fileName);
}
// print your internet outbound ip
let json = await fetch('https://httpbin.org/ip').then(resp => resp.json());
echo("Your ip: ", json.origin)
//printf
printf("hello %s\n", "world");
//glob *.ts
for await (const fileName of glob("*.ts")) {
echo(`${pwd()}/${fileName}`);
}
Then run dx demo.ts
or chmod u+x demo.ts ; ./demo.ts
'
Taskfile.ts
or Taskfile.js
is file to manage tasks, and you can use dx to run the task.
The task is normal TypeScript's function with export directive, example as following:
/// <reference lib="esnext" />
import {$, cd, pwd, question, os, fs, env, printf, glob, $a, echo} from "https://deno.land/x/deno_dx/mod.ts";
import {red, yellow, blue, green} from "https://deno.land/[email protected]/fmt/colors.ts";
export default hello;
export async function hello() {
echo(green("Hello"));
}
hello.desc = "Hello task";
export async function first() {
console.log(blue("first task"));
}
Then execute dx hello
to run task.
dx --tasks
to list tasks in Taskfile.ts
or Taskfile.js
~/.zshrc
.mkdir -p ~/.oh-my-zsh/custom/plugins/dx/
dx -c zsh > ~/.oh-my-zsh/custom/plugins/dx/_dx
import {$, nothrow, cd, pwd, question, os, fs, env} from "https://deno.land/x/deno_dx/mod.ts";
if( (await nothrow($`lll -al`)).exitCode > 0 ) {
console.log("Failed")
}
if(test('-e mod.ts')) { }
$.alias("ll", "ls -al")
$.expoort('NO_COLOR','true');
await sleep(5);
ls -1 *.ts
for await (const fileName of glob("*.ts")) {
console.log(fileName);
}
env.get("HOME")
// builtin env variables for hint, such as USER, HOME, PATH
const output = await $`ls -al ${HOME}`;
console.log(HOME);
// custom env variables for hint
declare global {
const JAVA_HOME: string;
}
dx supplies three styles to run command, and they are $
, $o
and $a
.
Use $
tag to execute command and return value captured stdout.
let count = parseInt(await $`ls -1 | wc -l`)
console.log(`Files count: ${count}`)
Use $o
tag to execute command with stdout and stderr output.
await $o`ls -al`
Use $a
tag to execute command and capture stdout and convert output into async iterable lines, then use for await...of
to iterate the lines.
for await (const fileName of $a`ls -1 *.ts`) {
console.log("file: ", fileName);
}
If exit code is not 0, and exception will be thrown.
try {
await $`exit 1`
} catch (p) {
console.log(`Exit code: ${p.exitCode}`)
console.log(`Error: ${p.stderr}`)
}
Deno std has fmt/colors.ts
already, and you don't need chalk for simple cases.
import {red, yellow, blue, green} from "https://deno.land/[email protected]/fmt/colors.ts";
console.log(green("Hello"));
which bash
set -euo pipefail;
for strict mode.import {grep} from "https://deno.land/x/deno_dx/mod.ts";
for await (const line of grep`\\d+`("demo.txt")) {
console.log("line: ", line);
}
import {awk} from "https://deno.land/x/deno_dx/mod.ts";
for await (const line of awk`{ print }`("demo.txt")) {
console.log("line: ", line);
}
deno compile --unstable -A --lite demo.ts