Next.js-like framework for CLIs made with Ink
Next.js-like framework for CLIs made with Ink.
commands folder to add commands.commands to add subcommands.npm install pastel ink react zodUse create-pastel-app to quickly scaffold a Pastel app with TypeScript, linter and tests set up.
npm create pastel-app hello-world
hello-worldmkdir hello-world
cd hello-world
npm init --yesnpm install pastel
npm install --save-dev typescript @sindresorhus/tsconfigtsconfig.json file to set up TypeScript.{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"outDir": "build",
"sourceMap": true
},
"include": [
"source"
]
}source folder for the source code.mkdir sourcesource/cli.ts file with the following code, which will be CLI's entrypoint:#!/usr/bin/env node
import Pastel from 'pastel';
const app = new Pastel({
importMeta: import.meta,
});
await app.run();source/commands folder for defining CLI's commands.mkdir source/commandssource/commands/index.tsx file for a default command, with the following code:import React from 'react';
import {Text} from 'ink';
import zod from 'zod';
export const options = zod.object({
name: zod.string().describe('Your name'),
});
type Props = {
options: zod.infer<typeof options>;
};
export default function Index({options}: Props) {
return <Text>Hello, {options.name}!</Text>;
}npx tsc9.1. Add bin field to package.json, which points to the compiled version of source/cli.ts file.
"bin": "build/cli.js"9.2. Make your CLI available globally.
npm link --globalhello-world --name=JaneHello, Jane!hello-world --helpUsage: hello-world [options]
Options:
--name Your name
-v, --version Show version number
-h, --help Show help
Pastel treats every file in the commands folder as a command, where filename is a command's name (excluding the extension). Files are expected to export a React component, which will be rendered when command is executed.
You can also nest files in folders to create subcommands.
Here's an example, which defines login and logout commands:
commands/
login.tsx
logout.tsxlogin.tsx
import React from 'react';
import {Text} from 'ink';
export default function Login() {
return <Text>Logging in</Text>;
}logout.tsx
import React from 'react';
import {Text} from 'ink';
export default function Logout() {
return <Text>Logging out</Text>;
}Given that your executable is named my-cli, you can execute these commands like so:
$ my-cli login
$ my-cli logoutFiles named index.tsx are index commands. They will be executed by default, when no other command isn't specified.
commands/
index.tsx
login.tsx
logout.tsxRunning my-cli without a command name will execute index.tsx command.
$ my-cliIndex command is useful when you're building a single-purpose CLI, which has only one command. For example, np or fast-cli.
Default commands are similar to index commands, because they too will be executed when an explicit command isn't specified. The difference is default commands still have a name, just like any other command, and they'll show up in the help message.
Default commands are useful for creating shortcuts to commands that are used most often.
Let's say there are 3 commands available: deploy, login and logout.
commands/
deploy.tsx
login.tsx
logout.tsxEach of them can be executed by typing their name.
$ my-cli deploy
$ my-cli login
$ my-cli logoutChances are, deploy command is going to be used a lot more frequently than login and logout, so it makes sense to make deploy a default command in this CLI.
Export a variable named isDefault from the command file and set it to true to mark that command as a default one.
import React from 'react';
import {Text} from 'ink';
+ export const isDefault = true;
export default function Deploy() {
return <Text>Deploying...</Text>;
}Now, running my-cli or my-cli deploy will execute a deploy command.
$ my-cliVercel's CLI is a real-world example of this approach, where both vercel and vercel deploy trigger a new deploy of your project.
As your CLI grows and more commands are added, it makes sense to group the related commands together.
To do that, create nested folders in commands folder and put the relevant commands inside to create subcommands. Here's an example for a CLI that triggers deploys and manages domains for your project:
commands/
deploy.tsx
login.tsx
logout.tsx
domains/
list.tsx
add.tsx
remove.tsxCommands for managing domains would be executed like so:
$ my-cli domains list
$ my-cli domains add
$ my-cli domains removeSubcommands can even be deeply nested within many folders.
Commands can have an alias, which is usually a shorter alternative name for the same command. Power users prefer aliases instead of full names for commands they use often. For example, most users type npm i instead of npm install.
Any command in Pastel can assign an alias by exporting a variable named alias:
import React from 'react';
import {Text} from 'ink';
+ export const alias = 'i';
export default function Install() {
return <Text>Installing something...</Text>;
}Now the same install command can be executed by only typing i:
$ my-cli iCommands can define options to customize their default behavior or ask for some additional data to run properly. For example, a command that creates a new server might specify options for choosing a server's name, an operating system, memory size or a region where that server should be spin up.
Pastel uses Zod to define, parse and validate command options. Export a variable named options and set a Zod object schema. Pastel will parse that schema and automatically set these options up. When command is executed, option values are passed via options prop to your component.
…With options set up, here's an example deploy command:
$ my-cli deploy --name=Test --os=Ubuntu --memory=1024 --region=waw
Deploying a server named "Test" running Ubuntu with memory size of 1024 MB in waw region.Help message is auto-generated for you as well.
$ my-cli deploy --help
Usage: my-cli deploy [options]
Options:
--name Server name
--os Operating system (choices: "Ubuntu", "Debian")
--memory Memory size
--region Region
-v, --version Show version number
-h, --help Show helpPastel only supports string, number, boolean, enum, array and set types for defining options.
Example that defines a --name string option:
import React from 'react';
import {Text} from 'ink';
import zod from 'zod';
export const options = zod.object({
name: zod.string().describe('Your name'),
});
type Props = {
options: zod.infer<typeof options>;
};
export default function Example({options}: Props) {
return <Text>Name = {options.name}</Text>;
}$ my-cli --name=Jane
Name = JaneExample that defines a --age number option:
import React from 'react';
import {Text} from 'ink';
import zod from 'zod';
export const options = zod.object({
age: zod.number().describe('Your age'),
});
type Props = {
options: zod.infer<typeof options>;
};
export default function Example({options}: Props) {
return <Text>Age = {options.age}</Text>;
}$ my-cli --age=28
Age = 28Example that defines a --compress number option:
import React from 'react';
import {Text} from 'ink';
import zod from 'zod';
export const options = zod.object({
compress: zod.boolean().describe('Compress output'),
});
type Props = {
options: zod.infer<typeof options>;
};
export default function Example({options}: Props) {
return <Text>Compress = {String(options.compress)}</Text>;
}$ my-cli --compress
Compress = trueBoolean options are special, because they can't be required and default to false, even if Zod schema doesn't use a default(false) function.
When boolean option defaults to true, it's treated as a negated option, which adds a no- prefix to its name.
import React from 'react';
import {Text} from 'ink';
import zod from 'zod';
export const options = zod.object({
compress: zod.boolean().default(true).describe("Don't compress output"),
});
type Props = {
options: zod.infer<typeof options>;
};
export default function Example({options}: Props) {
return <Text>Compress = {String(options.compress)}</Text>;
}$ my-cli --no-compress
Compress = falseExample that defines an --os enum option with a set of allowed values.
import React from 'react';
import {Text} from 'ink';
import zod from 'zod';
export const options = zod.object({
os: zod.enum(['Ubuntu', 'Debian']).describe('Operating system'),
});
type Props = {
options: zod.infer<typeof options>;
};
export default function Example({options}: Props) {
return <Text>Operating system = {options.os}</Text>;
}$ my-cli --os=Ubuntu
Operating system = Ubuntu
$ my-cli --os=Debian
Operating system = Debian
$ my-cli --os=Windows
error: option '--os <os>' argument 'Windows' is invalid. Allowed choices are Ubuntu, Debian.Example that defines a --tag array option, which can be specified multiple times.
import React from 'react';
import {Text} from 'ink';
import zod from 'zod';
export const options = zod.object({
tag: zod.array(zod.string()).describe('Tags'),
});
type Props = {
options: zod.infer<typeof options>;
};
export default function Example({options}: Props) {
return <Text>Tags = {options.tags.join(', ')}</Text>;
}$ my-cli --tag=App --tNo open issues yet, or sync has not completed.