CLI Companion

  • Hugging Face CLI
    • login
    • whoami
    • repo create
    • upload
    • download
    • lfs-enable-largefiles
    • scan-cache
    • delete-cache
  • Hapi CLI
    • new
    • start
    • build
    • test
    • plugin create
    • route add
  • Cloudflared
    • tunnel
    • tunnel run
    • tunnel list
    • tunnel delete
    • access
    • access tcp
    • update

    The `hapi plugin create` command is part of the Hapi CLI and is used to scaffold a new Hapi plugin project. It generates a basic directory structure and essential files, making it easier to start developing a new Hapi plugin.

    Syntax

    bash
    hapi plugin create <plugin-name> [options]

    - `<plugin-name>`: This is a required argument that specifies the name of your new Hapi plugin. This name will be used for the directory, `package.json` name, and the plugin's registration name.

    Options

    - `--path <path>`: Specifies the directory where the plugin project should be created. If not provided, the plugin will be created in the current working directory.

    - `--namespace <namespace>`: Allows you to specify an npm organization or scope for the plugin name (e.g., `@my-org`). If used, the plugin's `package.json` name will be formatted as `@namespace/plugin-name`.

    - `--git`: Initializes a Git repository within the newly created plugin directory.

    - `--npm`: Initializes a `package.json` file. This is typically the default behavior.

    - `--eslint`: Adds an ESLint configuration (`.eslintrc.js`) to the project for linting and code style enforcement.

    - `--lab`: Sets up Lab for testing, including a basic test file structure and a `test/index.js`.

    - `--joi`: Includes Joi for schema validation, commonly used for plugin options or route validation.

    - `--yes` or `-y`: Skips all interactive prompts and uses default values for options. This is useful for automated scripting.

    Usage Examples

    1. **Create a basic plugin in the current directory:**

    This will create a new directory named `my-awesome-plugin` with the core plugin files.

    bash
    hapi plugin create my-awesome-plugin

    2. **Create a plugin in a specific subdirectory:**

    This creates the plugin inside a directory named `plugins`.

    bash
    hapi plugin create my-feature --path ./plugins

    3. **Create a namespaced plugin with ESLint and Lab testing:**

    This example creates a plugin under the `@my-company` npm scope and includes common development tools.

    bash
    hapi plugin create my-api-plugin --namespace @my-company --eslint --lab

    4. **Create a plugin quickly without prompts (using defaults):**

    bash
    hapi plugin create another-plugin -y

    Explanation

    The `hapi plugin create` command streamlines the initial setup for Hapi plugins. When executed, it performs the following actions:

    1. **Creates a directory:** A new directory matching `plugin-name` (or `@namespace/plugin-name`) is created at the specified path.

    2. **Generates `package.json`:** A `package.json` file is initialized with basic metadata, `main` entry point (`index.js`), and default scripts (e.g., `npm test`). It also includes Hapi as a dependency.

    3. **Creates `index.js`:** This is the main entry point for your Hapi plugin. It typically contains the `name`, `version`, and `register` method required for a Hapi plugin.

    javascript
    'use strict';
    
        const Pkg = require('./package.json');
    
        exports.plugin = {
            name: Pkg.name,
            version: Pkg.version,
            register: async function (server, options) {
    
                // Your plugin logic here
    
                server.route({
                    method: 'GET',
                    path: '/example',
                    handler: (request, h) => {
    
                        return 'Hello, Hapi Plugin!';
                    }
                });
            }
        };

    4. **Optional files and configurations:** Based on the provided options (`--eslint`, `--lab`, `--git`), it will add respective configuration files (`.eslintrc.js`, `test/index.js`, `.git/`) to the project.

    After running `hapi plugin create`, you will have a ready-to-use plugin structure. You can then navigate into the new directory, install dependencies (`npm install`), and start developing your plugin's functionality within `index.js` and other related files. To use this plugin in a Hapi server, you would require it and register it with `server.register()`.