Making CLI with Deno 1
Making CLI with Deno version 1
Basic build
This tutorial will bs using yarn
. You may change to npm
as you wish.
The rollup
pack will be using this time.
Structure
Folder Structure
π .
βββ deno.json
βββ README.md
βββ¬ π src
βββ index.ts
1. Setup deno.json
deno.json
{
"compilerOptions": {
"allowJs": true,
"lib": ["deno.window"],
"strict": true
},
"tasks": {
"start": "deno run --allow-all --unstable src/index.ts",
"dev": "deno run --allow-all --unstable src/index.ts -n",
"install": "deno install --allow-all --unstable -n dtree -f src/index.ts",
"pwd": "cd $INIT_CWD && pwd"
},
"fmt": {
"useTabs": true,
"lineWidth": 80,
"indentWidth": 4,
"semiColons": true,
"singleQuote": true,
"proseWrap": "preserve",
"include": ["src/"]
}
}
2. Setup index.ts
index.ts
import { parseFlags } from 'https://deno.land/x/cliffy@v0.25.7/flags/mod.ts';
async function main() {
const { flags } = parseFlags(Deno.args, {
flags: [
{
name: 'maxDepth',
aliases: ['m', 'maxDepth'],
standalone: false,
default: Infinity,
type: 'integer',
},
{
name: 'icon',
aliases: ['icon'],
standalone: false,
default: "π",
type: 'string',
},
],
});
const icon: string = flags.icon
const maxDepth: number = flags.maxDepth;
console.log(`Hello ${icon} - ${maxDepth}`)
}
if (import.meta.main) {
main();
}
3. Install
Either one is ok for register a bin command
Using task
deno task install
Using deno command
deno install --allow-all --unstable -n dtree -f src/index.ts
# -n: name