0.0.9 • Published 5 months ago

@whop-cli/runtime v0.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

@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:

  1. Install the required dependencies:

    npm install @whop-cli/runtime
  2. Create your command files in the cmd directory following the structure mentioned below.

  3. Use the cli function 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 cmd directory.
  • For each command, there should be an index.js file that exports the function that will be executed when the command is run.
  • Optionally, you can include a config.js file 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:

  1. Create a new folder inside the cmd directory with the desired command name.

  2. Inside the new folder, create an index.js file that exports the function to be executed when the command is run. The function should accept two arguments: input (containing flag values) and context (containing additional context data).

  3. Optionally, create a config.js file in the same folder to provide a description, flags, and aliases for the command. The config.js file 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.
  4. Run the cli function 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:

  1. Create a new folder named greet inside the cmd directory.

  2. Inside the greet folder, create an index.js file 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;
  3. Optionally, create a config.js file in the greet folder 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;
  4. In your main file (e.g., index.js), call the cli function to run the CLI:

     #!/usr/bin/env node
    
     import { cli } from '@whop-cli/runtime';
    
     cli(import.meta.url);
  5. 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 John

This will output: Hello, John!

0.0.9-canary.0

5 months ago

0.0.9

5 months ago

0.0.8

9 months ago

0.0.7

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago

0.0.1-canary.10

9 months ago

0.0.1-canary.9

9 months ago

0.0.1-canary.8

9 months ago

0.0.1-canary.7

9 months ago

0.0.1-canary.6

9 months ago

0.0.1-canary.5

9 months ago

0.0.1-canary.4

9 months ago

0.0.1-canary.3

9 months ago