The `hapi plugin create` command is used to generate a new Hapi plugin boilerplate. This command helps streamline the creation of new plugins by setting up the basic directory structure and essential files, allowing developers to focus directly on the plugin's logic.
hapi plugin create <name> [options]* `<name>`: This is a required argument specifying the name of the new plugin. This name will be used for the plugin's directory and as the base name for the plugin's main file (e.g., `my-plugin` will create a directory `my-plugin` and a file `my-plugin.js` inside it).
Currently, the `hapi plugin create` command in the Hapi CLI is quite minimalistic. In most versions, it does not include many specific flags or options beyond the plugin name itself. The CLI aims to provide a sensible default structure.
#### 1. Creating a Basic Plugin
This is the most common use case. It generates a new plugin named `my-auth-plugin` in a directory of the same name within your current working directory.
hapi plugin create my-auth-plugin**Output (example directory structure):
my-auth-plugin/
├── lib/
│ └── my-auth-plugin.js # Main plugin file
├── test/
│ └── index.test.js # Placeholder for tests
└── package.json # Plugin's package manifestThe `my-auth-plugin.js` file would typically contain a basic Hapi plugin structure, such as:
'use strict';
const Package = require('../package.json');
exports.plugin = {
pkg: Package,
once: true,
register: async (server, options) => {
// Your plugin logic here
// For example, adding a route:
// server.route({
// method: 'GET',
// path: '/hello',
// handler: (request, h) => {
// return 'Hello from my-auth-plugin!';
// }
// });
}
};#### 2. Creating Another Plugin for Database Interactions
hapi plugin create db-connectorThis will create a `db-connector` directory with the standard Hapi plugin boilerplate, ready for you to implement database connection logic, models, and queries.
* **Boilerplate Generation**: The primary function of `hapi plugin create` is to quickly set up the necessary files and directory structure for a Hapi plugin. This includes a `package.json`, a main plugin file (e.g., `lib/my-plugin.js`), and a test file (`test/index.test.js`).
* **`package.json`**: This file is generated with basic metadata, including the plugin name, version, and typically a `main` entry pointing to your plugin's main file. It also sets up standard `scripts` for testing.
* **`lib/my-plugin.js`**: This is where the core logic of your plugin resides. It exports an object with a `plugin` key containing the `pkg` (package info), `once` (ensuring the plugin registers only once), and the `register` asynchronous function. The `register` function receives the Hapi `server` instance and `options`, allowing you to extend the server's functionality (e.g., add routes, decorators, methods, authentication strategies).
* **`test/index.test.js`**: A basic test file is provided to encourage test-driven development. You should expand upon this with actual tests for your plugin's features.
* **Integration**: Once created, you can integrate this plugin into your main Hapi application by installing it (if it's a separate package) or requiring it directly and registering it with your Hapi server using `server.register()`.
This command significantly speeds up the initial setup process for new Hapi plugins, promoting consistency and best practices.