Creating an Installable Hook
Installable hooks are custom Sails hooks that reside in an application’s node_modules folder. They are useful when you want to share functionality between Sails apps, or publish your hook to NPM to share it with the Sails community. If you wish to create a hook for use in just one Sails app, see creating a project hook instead.
To create a new installable hook:
- Choose a name for your new hook. It must not conflict with any of the core hook names.
- Create a new folder on your system with the name
sails-hook-<your hook name>. Thesails-hook-prefix is optional but recommended for consistency; it is stripped off by Sails when the hook is loaded. - Create a
package.jsonfile in the folder. If you havenpminstalled on your system, you can do this easily by runningnpm initand following the prompts. Otherwise, you can create the file manually, and ensure that it contains at a minimum the following:
If you use{ "name": "sails-hook-your-hook-name", "version": "0.0.0", "description": "a brief description of your hook", "main": "index.js", "sails": { "isHook": true } }npm initto create yourpackage.json, be sure to open the file afterwards and manually insert thesailskey containingisHook: true. - Write your hook code in
index.jsin accordance with the hook specification.
Your new folder may contain other files as well, which can be loaded in your hook via require; only index.js will be read automatically by Sails. Use the dependencies key of your package.json to refer to any dependencies that need to be installed in order for your hook to work (you may also use npm install <dependency> --save to easily save dependency information to package.json).
Testing your new hook
Before you distribute your installable hook to others, you’ll want to write some tests for it. This will help ensure compatibility with future Sails versions and significantly reduce hair-pulling and destruction of nearby objects in fits of rage. While a full guide to writing tests is outside the scope of this doc, the following steps should help get you started:
- Add Sails as a
devDependencyin your hook’spackage.jsonfile:"devDependencies": { "sails": "~0.11.0" } - Install Sails as a dependency of your hook with
npm install sailsornpm link sails(if you have Sails installed globally on your system). - Install Mocha on your system with
npm install -g mocha, if you haven’t already. - Add a
testfolder inside your hook’s main folder. Add a
basic.jsfile with the following basic test:var Sails = require('sails').Sails; describe('Basic tests ::', function() { // Var to hold a running sails app instance var sails; // Before running any tests, attempt to lift Sails before(function (done) { // Hook will timeout in 10 seconds this.timeout(11000); // Attempt to lift sails Sails().lift({ hooks: { // Load the hook "your-hook-name": require('../'), // Skip grunt (unless your hook uses it) "grunt": false }, log: {level: "error"} },function (err, _sails) { if (err) return done(err); sails = _sails; return done(); }); }); // After tests are complete, lower Sails after(function (done) { // Lower Sails (if it successfully lifted) if (sails) { return sails.lower(done); } // Otherwise just return return done(); }); // Test that Sails can lift with the hook in place it ('sails does not crash', function() { return true; }); });- Run the test with
mocha -R specto see full results. - See the Mocha docs for a full reference.
Publishing your hook
Assuming your hook is tested and looks good, and assuming that the hook name isn’t already in use by another NPM module, you can share it with world by running npm publish. Go you!