{"id":13902829,"url":"https://github.com/linux-china/dx","last_synced_at":"2025-04-10T01:33:11.215Z","repository":{"id":45708480,"uuid":"365675308","full_name":"linux-china/dx","owner":"linux-china","description":"A tool and task runner for writing better scripts with Deno","archived":false,"fork":false,"pushed_at":"2021-10-24T20:52:33.000Z","size":805,"stargazers_count":95,"open_issues_count":2,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-02T02:51:08.914Z","etag":null,"topics":["cli","deno","runner"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/linux-china.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-09T05:33:07.000Z","updated_at":"2025-03-30T00:48:03.000Z","dependencies_parsed_at":"2022-09-22T20:00:49.709Z","dependency_job_id":null,"html_url":"https://github.com/linux-china/dx","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fdx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fdx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fdx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fdx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/linux-china","download_url":"https://codeload.github.com/linux-china/dx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248140798,"owners_count":21054353,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","deno","runner"],"created_at":"2024-08-06T22:01:27.041Z","updated_at":"2025-04-10T01:33:06.204Z","avatar_url":"https://github.com/linux-china.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"dx: A tool/runner for writing better scripts\n==========================================\n\ndx is a tool and task runner for writing better scripts with Deno, and origin idea is from [https://github.com/google/zx/](https://github.com/google/zx/).\n\n# why a dx instead of Google zx\n\ndx is based on Deno and with following pros:\n\n* TypeScript friendly\n* Task runner support: Taskfile.ts/Taskfile.js to manage tasks\n* Easy to import third party modules, just `import {red, green} from \"https://deno.land/std@0.97.0/fmt/colors.ts\"`, no idea about zx to import third party npm(package.json???)\n* More features: alias, export, `$a` for async iterable line output, file globs, .env support etc\n* I ❤️ 🦕\n\n# Install\n\n```bash\ndeno install -q -A --unstable --no-check -r -f -n dx https://deno.land/x/deno_dx/cli.ts\n```\n\n# Get started\n\nCreate a `demo.ts` file with following code:\n\n```typescript\n#!/usr/bin/env dx\nimport {$, nothrow, cd, pwd, question, os, fs, env, printf, glob, $a, echo} from \"https://deno.land/x/deno_dx/mod.ts\";\nimport {red, yellow, blue, green} from \"https://deno.land/std@0.108.0/fmt/colors.ts\";\n\n// aliases\n$.alias(\"ll\", \"ls -al\");\n\n// prompt to input your name\nlet name = await question(blue(\"what's your name: \"));\necho(\"Hello \", blue(name ?? \"guest\"));\n\n// pwd(), env variables and params\necho(\"Current working directory:\", pwd());\necho(\"Your home:\", HOME);\necho(\"Your name:\", USER);\necho(\"Script name:\", $0);\n\n// current file count\nconst output = await $`ls -1 | wc -l`;\necho(\"Files count: \", parseInt(output));\n\n// output as lines\nfor await (const fileName of $a`ls -1 *.ts`) {\n    echo(\"TS file: \", fileName);\n}\n\n// alias and output as lines\nfor await (const fileName of $a`ll *.ts`) {\n    echo(\"TS file: \", fileName);\n}\n\n// print your internet outbound ip\nlet json = await fetch('https://httpbin.org/ip').then(resp =\u003e resp.json());\necho(\"Your ip: \", json.origin)\n\n//printf\nprintf(\"hello %s\\n\", \"world\");\n\n//glob *.ts\nfor await (const fileName of glob(\"*.ts\")) {\n    echo(`${pwd()}/${fileName}`);\n}\n```\n\nThen run `dx demo.ts` or `chmod u+x demo.ts ; ./demo.ts`'\n\n# Task runner support: Taskfile.ts or Taskfile.js\n\n`Taskfile.ts` or `Taskfile.js` is file to manage tasks, and you can use dx to run the task.\n\nThe task is normal TypeScript's function with export directive, example as following:\n\n```typescript\n/// \u003creference lib=\"esnext\" /\u003e\nimport {$, cd, pwd, question, os, fs, env, printf, glob, $a, echo} from \"https://deno.land/x/deno_dx/mod.ts\";\nimport {red, yellow, blue, green} from \"https://deno.land/std@0.99.0/fmt/colors.ts\";\n\nexport default hello;\n\nexport async function hello() {\n    echo(green(\"Hello\"));\n}\n\nhello.desc = \"Hello task\";\n\nexport async function first() {\n    console.log(blue(\"first task\"));\n}\n```\n\nThen execute `dx hello` to run task.\n\n* `dx --tasks` to list tasks in `Taskfile.ts` or `Taskfile.js`\n* Task names completion with o-my-zsh. Please add dx to plugins in `~/.zshrc`.\n\n```bash\nmkdir -p ~/.oh-my-zsh/custom/plugins/dx/\ndx -c zsh \u003e ~/.oh-my-zsh/custom/plugins/dx/_dx\n```\n\n# functions and variables\n\n```typescript\nimport {$, nothrow, cd, pwd, question, os, fs, env} from \"https://deno.land/x/deno_dx/mod.ts\";\n```\n\n* $: execute command and return the stdout\n* nothrow: changes behavior of $ to not throw an exception on non-zero exit codes\n\n```typescript \nif( (await nothrow($`lll -al`)).exitCode \u003e 0 ) { \n   console.log(\"Failed\")\n}\n```\n\n* built-in functions: cd, pw, echo, printf, cp, mv, rm, mkdir, getops\n* test: file conditions and single file test only, such as `if(test('-e mod.ts')) { }`\n* $.alias: introduce alias for command. `$.alias(\"ll\", \"ls -al\")`\n* $.export: export env variable for command.  `$.expoort('NO_COLOR','true');`\n* cat:  read text file as string\n* read/question: read value from stdin with prompt\n* sleep: `await sleep(5);`\n* os: OS related functions\n* fs: file system related functions\n* glob:  glob files, like commands `ls -1 *.ts`\n\n```typescript\nfor await (const fileName of glob(\"*.ts\")) {\n    console.log(fileName);\n}\n```\n\n* env: env global object `env.get(\"HOME\")`\n* Shell params support: $0(script name), $1 to $9, $['@'] for all arguments, $['#'] for number of arguments, $['*'] for params as a string\n* Shell env variables as global variables in TypeScript automatically.\n\n```typescript\n// builtin env variables for hint, such as USER, HOME, PATH\nconst output = await $`ls -al  ${HOME}`;\nconsole.log(HOME);\n\n// custom env variables for hint\ndeclare global {\n    const JAVA_HOME: string;\n}\n```\n\n# Execute command\n\ndx supplies three styles to run command, and they are `$`, `$o` and `$a`.\n\n### $: execute command with stdout capture\n\nUse `$` tag to execute command and return value captured stdout.\n\n```typescript\nlet count = parseInt(await $`ls -1 | wc -l`)\nconsole.log(`Files count: ${count}`)\n```\n\n### $o: execute command with stdout and stderr\n\nUse `$o` tag to execute command with stdout and stderr output.\n\n```typescript\nawait $o`ls -al`\n```\n\n### $a: execute command and convert output into async iterable lines\n\nUse `$a` tag to execute command and capture stdout and convert output into async iterable lines, then use `for await...of` to iterate the lines.\n\n```typescript\nfor await (const fileName of $a`ls -1 *.ts`) {\n    console.log(\"file: \", fileName);\n}\n```\n\n### command error handler\n\nIf exit code is not 0, and exception will be thrown.\n\n```typescript\ntry {\n    await $`exit 1`\n} catch (p) {\n    console.log(`Exit code: ${p.exitCode}`)\n    console.log(`Error: ${p.stderr}`)\n}\n```\n\n# color output\n\nDeno std has `fmt/colors.ts` already, and you don't need chalk for simple cases.\n\n```typescript\nimport {red, yellow, blue, green} from \"https://deno.land/std@0.99.0/fmt/colors.ts\";\n\nconsole.log(green(\"Hello\"));\n```\n\n# $ configuration\n\n* $.shell: set shell for the command and default is `which bash`\n* $.prefix: prefix for every command line, default is `set -euo pipefail;` for strict mode.\n\n# packages\n\n* fs is for file system from https://deno.land/std@0.96.0/node/fs.ts\n* os is for operating system from https://deno.land/std@0.96.0/node/os.ts\n\n# Examples\n\n### grep\n\n```typescript\nimport {grep} from \"https://deno.land/x/deno_dx/mod.ts\";\n\nfor await (const line of grep`\\\\d+`(\"demo.txt\")) {\n    console.log(\"line: \", line);\n}\n```\n\n### awk\n\n```typescript\nimport {awk} from \"https://deno.land/x/deno_dx/mod.ts\";\n\nfor await (const line of awk`{ print }`(\"demo.txt\")) {\n    console.log(\"line: \", line);\n}\n```\n\n# Misc\n\n* .env auto load\n* Compile script into executable binary: `deno compile --unstable -A --lite demo.ts`\n* dx JetBrains IDE plugin: https://plugins.jetbrains.com/plugin/16805-dx\n\n![dx JetBrains IDEs plugin](docs/dx-jetbrains-plugin.png)\n\n# References\n\n* Google zx: https://github.com/google/zx\n* dx JetBrains IDEs plugin: https://plugins.jetbrains.com/plugin/16805-dx\n* Bash Cheatsheet: https://devhints.io/bash https://shellmagic.xyz/\n* Advanced Bash-Scripting Guide: https://tldp.org/LDP/abs/html/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinux-china%2Fdx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinux-china%2Fdx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinux-china%2Fdx/lists"}