The `plugin create` command in Hapi CLI is used to quickly scaffold a new Hapi plugin project. It generates the necessary directory structure and basic files, allowing you to start developing your plugin immediately without manual setup.
hapi plugin create <name> [options]- `<name>`: The name of the plugin. This will also be used as the directory name for the new plugin project.
The Hapi CLI `plugin create` command generally offers options to customize the generated plugin. While the exact options can vary slightly with different versions of the Hapi CLI, common ones include:
- `--path <path>`: Specify the directory where the plugin should be created. If not provided, the plugin will be created in a subdirectory named `<name>` within the current working directory.
- `--template <template-name>`: Choose a specific template for the plugin. Default templates usually provide a basic Hapi plugin structure. (Note: In older versions or specific setups, this might not be directly exposed or might rely on internal templates).
- `--test`: Include a basic test setup (e.g., using Lab).
#### 1. Create a basic plugin in the current directory
This command creates a new plugin named `my-awesome-plugin` in a subdirectory of the same name within your current working directory.
hapi plugin create my-awesome-plugin**Explanation:**
The CLI will output something similar to:
info Initializing 'my-awesome-plugin'
create my-awesome-plugin/.env
create my-awesome-plugin/.gitignore
create my-awesome-plugin/lib/index.js
create my-awesome-plugin/package.json
create my-awesome-plugin/README.md
create my-awesome-plugin/test/index.js
Plugin 'my-awesome-plugin' successfully created!This creates a directory structure like:
my-awesome-plugin/
├── .env
├── .gitignore
├── lib/
│ └── index.js
├── package.json
├── README.md
└── test/
└── index.js#### 2. Create a plugin in a specific path
This command creates the `user-auth` plugin inside the `plugins/` directory relative to your current path.
hapi plugin create user-auth --path ./plugins**Explanation:**
If the `./plugins` directory does not exist, the CLI will create it. The `user-auth` plugin project will then be scaffolded inside `./plugins/user-auth`.
#### 3. Inspecting the generated `package.json`
The `package.json` for a newly created plugin typically includes standard metadata and dependencies:
{
"name": "my-awesome-plugin",
"version": "1.0.0",
"description": "My awesome Hapi plugin",
"main": "lib/index.js",
"scripts": {
"test": "lab -a code -t 100 -L",
"start": "node -r dotenv/config server.js"
},
"keywords": [
"hapi",
"plugin"
],
"author": "",
"license": "ISC",
"dependencies": {
"@hapi/hapi": "^20.0.0"
},
"devDependencies": {
"@hapi/code": "^8.0.0",
"@hapi/lab": "^24.0.0",
"dotenv": "^8.2.0"
}
}#### 4. Inspecting the generated plugin file (`lib/index.js`)
The main plugin file (`lib/index.js`) will contain the basic Hapi plugin structure:
'use strict';
const Package = require('../package.json');
module.exports = {
plugin: {
name: Package.name,
version: Package.version,
register: async function (server, options) {
// Plugin registration logic goes here
server.route({
method: 'GET',
path: '/',
handler: function (request, h) {
return 'Hello from ' + Package.name;
}
});
}
}
};When you run `hapi plugin create`, the Hapi CLI performs the following actions:
1. **Directory Creation**: It creates a new directory named after your plugin (or the specified `--path` followed by the plugin name).
2. **File Scaffolding**: It populates this directory with essential files for a Hapi plugin, including:
* `package.json`: Configures the project, defines dependencies (like `@hapi/hapi`), and scripts (like `test`).
* `lib/index.js`: The main entry point for your Hapi plugin, containing the `register` function where you define routes, extensions, decorations, etc.
* `README.md`: A basic markdown file for documentation.
* `test/index.js`: A boilerplate for unit tests using `@hapi/lab` and `@hapi/code` if the `--test` option is implicitly or explicitly used.
* `.gitignore`: To ignore common development artifacts.
* `.env`: An environment variable file (often used with `dotenv`).
3. **Dependency Installation (Optional)**: While the CLI itself does not typically run `npm install` or `yarn install` automatically after creating the plugin, it's the next logical step. You would navigate into the created plugin directory and run your package manager's install command (`npm install` or `yarn install`) to fetch the dependencies listed in `package.json`.
This command significantly speeds up the initial setup of new Hapi plugins, ensuring consistency and adherence to best practices.