├── README.md ├── package.json ├── serverless-hooks-plugin.js └── serverless.yml /README.md: -------------------------------------------------------------------------------- 1 | # Serverless Hooks Plugin 2 | 3 | ## How to use 4 | 5 | ### Install from npm 6 | 7 | 1. Ensure your project has a valid `package.json`. 8 | 1. Run `npm install --save serverless-hooks-plugin`. 9 | 1. Add `serverless-hooks-plugin` to the list of plugins in `serverless.yml`. The plugins list is an array at the root level (an example `serverless.yml` is included in this project). 10 | 1. Add an array of hooks to be used as per examples, at `custom: hooks`. 11 | 12 | ### Local installation (not recommended) 13 | 1. Copy `serverless-hooks-plugin.js` into `.serverless-plugins` in the serverless project. You may need to create this folder. 14 | 1. Add `serverless-hooks-plugin` to the list of plugins in `serverless.yml`. The plugins list is an array at the root level (an example `serverless.yml` is included in this project). 15 | 1. Add an array of hooks to be used as per examples, at `custom: hooks`. 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-hooks-plugin", 3 | "version": "1.1.0", 4 | "main": "serverless-hooks-plugin.js", 5 | "keywords": [ 6 | "hooks", 7 | "plugin", 8 | "serverless" 9 | ], 10 | "author": "Fell Sunderland", 11 | "repository" : { 12 | "type": "git", 13 | "url" : "https://github.com/uswitch/serverless-hooks-plugin.git" 14 | }, 15 | "license": "ISC", 16 | "description": "A plugin to give access from config to hooks for serverless" 17 | } 18 | -------------------------------------------------------------------------------- /serverless-hooks-plugin.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const execSync = require('child_process').execSync 4 | 5 | class ServerlessHooksPlugin { 6 | 7 | constructor(serverless, options) { 8 | this.serverless = serverless; 9 | this.options = options; 10 | 11 | const buildHookFunction = hook => ( 12 | () => { 13 | const commands = serverless.service.custom.hooks[hook] || [] 14 | commands.forEach( 15 | command => { 16 | serverless.cli.log(`Running ${hook} command: "${command}"`) 17 | var output = execSync(command).toString() 18 | if (output) { 19 | serverless.cli.log(output) 20 | } 21 | } 22 | ) 23 | } 24 | ) 25 | 26 | const hooksObj = {} 27 | 28 | const custom = serverless.service.custom || {} 29 | const hooks = custom.hooks || {} 30 | Object.keys(serverless.service.custom.hooks).forEach( 31 | hook => { 32 | hooksObj[hook] = buildHookFunction(hook) 33 | } 34 | ) 35 | 36 | this.hooks = hooksObj 37 | } 38 | } 39 | 40 | module.exports = ServerlessHooksPlugin; 41 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: serverless-hooks-plugin # NOTE: update this with your service name 2 | 3 | plugins: 4 | - serverless-hooks-plugin 5 | 6 | custom: 7 | hooks: 8 | before:command:lifecycleEvent: 9 | - echo "Before a command called 'command': an example!" 10 | after:command:lifecycleEvent: 11 | - echo "After a command called 'command': another example!" 12 | 13 | # Both provider and functions are required 14 | 15 | # provider: 16 | # name: aws 17 | # runtime: nodejs6.10 18 | # 19 | # functions: 20 | # hello: 21 | # handler: handler.hello 22 | --------------------------------------------------------------------------------