@whop-cli/runtime v0.0.9
@whop-cli/runtime
The provided CLI runner allows you to create a command-line interface with various commands and flags. Each command can be associated with specific flags, and you can execute these commands through the terminal.
Getting Started
To use the CLI runner, you need to follow these steps:
Install the required dependencies:
npm install @whop-cli/runtimeCreate your command files in the
cmddirectory following the structure mentioned below.Use the
clifunction to run the CLI and execute commands.
Command File Structure
The CLI runner expects the following structure for your command files:
- Each command is represented by a separate folder inside the
cmddirectory. - For each command, there should be an
index.jsfile that exports the function that will be executed when the command is run. - Optionally, you can include a
config.jsfile that provides additional information about the command, such as a description, flags, and aliases.
Adding a New Command
To add a new command, follow these steps:
Create a new folder inside the
cmddirectory with the desired command name.Inside the new folder, create an
index.jsfile that exports the function to be executed when the command is run. The function should accept two arguments:input(containing flag values) andcontext(containing additional context data).Optionally, create a
config.jsfile in the same folder to provide a description, flags, and aliases for the command. Theconfig.jsfile should export an object with the following properties:description(optional): A description of the command.flags(required): An object defining the flags for the command. Flags can be of different types, such as string, number, boolean, etc.alias(optional): An array of strings representing the aliases for the command.
Run the
clifunction in your main file (e.g.,index.js) to initialize the CLI and execute the commands.
Command Configuration
The config.js file for a command can include the following properties:
description: A description of the command that will be displayed in the CLI help.flags: An object defining the flags for the command. Each flag should be defined using one of the following interfaces:StringFlag: A single string flag.StringsFlag: Multiple string flags.NumberFlag: A single number flag.NumbersFlag: Multiple number flags.BooleanFlag: A boolean flag.
alias(optional): An array of strings representing the aliases for the command. Aliases can be used as shortcuts for the main command.
Running the CLI
To run the CLI, call the cli function in your main file (e.g., index.js). The CLI will parse the command-line arguments and execute the corresponding command based on the provided input.
Example
Here's an example of how you can add a new command:
Create a new folder named
greetinside thecmddirectory.Inside the
greetfolder, create anindex.jsfile with the following content:import type { default as init } from '@/commands/init'; import type { Command } from '@whop-cli/runtime'; import type { flags } from './config'; const command: Command<typeof flags, typeof init> = async ({ name }) => { console.log(`Hello, ${name}!`); }; export default command;Optionally, create a
config.jsfile in thegreetfolder with the following content:import { FlagsConfig } from '@whop-cli/runtime'; export const alias = ['i']; export const description = 'Describe the command here'; export const flags = { name: { type: 'string', required: true, message: 'Enter your name:', }, } as const satisfies FlagsConfig;In your main file (e.g.,
index.js), call theclifunction to run the CLI:#!/usr/bin/env node import { cli } from '@whop-cli/runtime'; cli(import.meta.url);Add the binary binding to your package.json
{ "type": "module", "bin": { "mycommand": "index.js" } }
Now, you can execute the new command from the terminal:
node index.js greet --name JohnThis will output: Hello, John!
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago