Merge pull request #2163 from strapi/update-hook-doc

Update hook documentation
This commit is contained in:
Jim LAURIE 2018-10-19 09:28:18 +02:00 committed by GitHub
commit 9b4714e070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
The hooks are modules that add functionality to the core. They are loaded during the server boot. For example, if your project needs to work with a SQL database, your will have to add the hook `strapi-hook-bookshelf` to be able to connect your app with your database.
**Path —** `./hooks/documentation/lib/index.js`.
**File structure**
```js
const fs = require('fs');
const path = require('path');
@ -15,9 +15,7 @@ module.exports = strapi => {
*/
defaults: {
documentation: {
path: '/public/documentation'
}
// config object
},
/**
@ -25,28 +23,11 @@ module.exports = strapi => {
*/
initialize: cb => {
try {
// Check if documentation folder exist.
fs.accessSync(path.resolve(process.cwd(), this.defaults.documentation.path));
} catch (e) {
// Otherwise, create the folder.
fs.mkdirSync(path.resolve(process.cwd(), this.defaults.documentation.path));
}
// Write your code here.
// This function doesn't really exist,
// it's just an example to tell you that you
// run your business logic and when it's done
// you just need to call the callback `cb`
generateDocumentation(path.resolve(process.cwd(), this.defaults.documentation.path), function(err) {
if (err) {
// Error: it will display the error to the user
// and the hook won't be loaded.
return cb(err);
}
// this.defaults['your_config'] to access to your configs.
// Success.
cb();
});
cb();
}
};
@ -57,14 +38,18 @@ module.exports = strapi => {
- `defaults` (object): Contains the defaults configurations. This object is merged to `strapi.config.hook.settings.**`.
- `initialize` (function): Called during the server boot. The callback `cb` needs to be called. Otherwise, the hook won't be loaded.
Every folder that follows this name pattern `strapi-*` in your `./node_modules` folder will be loaded as a hook. The hooks are accessible through the `strapi.hook` variable.
The hooks are accessible through the `strapi.hook` variable.
## Structure
### Node modules
Every folder that follows this name pattern `strapi-hook-*` in your `./node_modules` folder will be loaded as a hook.
A hook needs to follow the structure below:
```
/hook
/strapi-hook-[...]
└─── lib
- index.js
- LICENSE.md
@ -74,6 +59,40 @@ A hook needs to follow the structure below:
The `index.js` is the entry point to your hook. It should look like the example above.
### Custom hooks
The framework allows to load hooks from the project directly without having to install them from npm. It's great way to take advantage of the features of the hooks system for code that doesn't need to be shared between apps. To achieve this, you have to create a `./hooks` folder at the root of your project and put the hooks into it.
```
/project
└─── admin
└─── api
└─── config
└─── hooks
│ └─── strapi-documentation
│ - index.js
│ └─── strapi-server-side-rendering
│ - index.js
└─── plugins
└─── public
- favicon.ico
- package.json
- server.js
```
## Configuration and activation
To activate and configure your hook with custom options, you need to edit your `./config/hooks.json` file in your Strapi app.
```javascript
{
...
"hook-name": {
"enabled": true,
...
}
}
```
## Dependencies
It happens that a hook has a dependency to another one. For example, the `strapi-hook-bookshelf` has a dependency to `strapi-hook-knex`. Without it, the `strapi-hook-bookshelf` can't work correctly. It also means that the `strapi-hook-knex` hook has to be loaded before.
@ -95,24 +114,3 @@ To handle this case, you need to update the `package.json` at the root of your h
}
}
```
## Custom hooks
The framework allows to load hooks from the project directly without having to install them from npm. It's great way to take advantage of the features of the hooks system for code that doesn't need to be shared between apps. To achieve this, you have to create a `./hooks` folder at the root of your project and put the hooks into it.
```
/project
└─── admin
└─── api
└─── config
└─── hooks
│ └─── strapi-documentation
│ - index.js
│ └─── strapi-server-side-rendering
│ - index.js
└─── plugins
└─── public
- favicon.ico
- package.json
- server.js
```