├── .editorconfig ├── .github └── workflows │ └── ci.yaml ├── .gitignore ├── LICENSE.md ├── README.md ├── example ├── basic │ ├── .gitignore │ ├── README.md │ ├── package.json │ ├── serverless.yml │ └── src │ │ └── handlers │ │ └── users.js └── with-serverless-offline-plugin │ ├── .gitignore │ ├── README.md │ ├── package.json │ ├── serverless.yml │ └── src │ └── handlers │ └── users.js ├── index.js ├── lib ├── base-path-plugin.js └── base-path-plugin.test.js ├── package-lock.json └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | insert_final_newline = true 6 | trim_trailing_whitespace = true 7 | 8 | [{*.js,*.json,*.yml}] 9 | indent_size = 2 10 | indent_style = space 11 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ${{ matrix.os }} 9 | 10 | strategy: 11 | matrix: 12 | node-version: [10.x, 12.x] 13 | os: [ubuntu-latest] 14 | 15 | steps: 16 | - uses: actions/checkout@v1 17 | - name: Use Node.js ${{ matrix.node-version }} 18 | uses: actions/setup-node@v1 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - name: npm install and test 22 | run: | 23 | npm ci 24 | npm test 25 | npm run coverage 26 | env: 27 | CI: true 28 | - name: Upload coverage to Codecov 29 | uses: codecov/codecov-action@v1 30 | with: 31 | token: ${{ secrets.CODECOV_TOKEN }} 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .nyc_output 3 | coverage 4 | node_modules 5 | *.lcov 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Kevin Rambaud 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # serverless-functions-base-path 2 | 3 | [![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) 4 | [![actions](https://img.shields.io/github/workflow/status/kevinrambaud/serverless-functions-base-path/Node%20CI?logo=github)](https://github.com/kevinrambaud/serverless-functions-base-path/actions?query=workflow%3A%22Node+CI%22) 5 | [![Codecov](https://img.shields.io/codecov/c/github/kevinrambaud/serverless-functions-base-path.svg)](https://codecov.io/gh/kevinrambaud/serverless-functions-base-path) 6 | [![npm](https://img.shields.io/npm/v/serverless-functions-base-path.svg)](https://www.npmjs.com/package/serverless-functions-base-path) 7 | [![npm](https://img.shields.io/npm/dt/serverless-functions-base-path.svg)](https://www.npmjs.com/package/serverless-functions-base-path) 8 | 9 | When a project scaffolded with Serverless framework starts to be large or so you simply want to structure in a specific way your project, your `handler` paths can be quite long to write. That's where `serverless-functions-base-path` comes in, this plugin will allow you to define a base path that indicates the location of your lambda function while keeping your `handler` paths as minimal as possible. 10 | 11 | ## Installation 12 | 13 | Go to your project directory and install the plugin by running : 14 | 15 | for npm users 16 | 17 | ```bash 18 | npm i -D serverless-functions-base-path 19 | ``` 20 | 21 | for yarn users 22 | 23 | ```bash 24 | yarn add --dev serverless-functions-base-path 25 | ``` 26 | 27 | ## Usage 28 | 29 | Let's say that our project have the following strucutre : 30 | 31 | #### Project structure 32 | 33 | ``` 34 | my-new-serverless-project 35 | ├── src 36 | │   └── handlers 37 | │      └── users.js 38 | │   └── lib 39 | ├── package.json 40 | └── serverless.yml 41 | ``` 42 | 43 | #### Configuration 44 | 45 | Open your `serverless.yml` configuration file and : 46 | 47 | * add a `plugins` section 48 | * add our fresh `serverless-functions-base-path` plugin into it 49 | * add a `custom` section 50 | * add a `functionsBasePath` property with your base path location 51 | 52 | ```yaml 53 | # serverless.yml 54 | 55 | service: hello-world-service 56 | 57 | provider: 58 | name: aws 59 | runtime: nodejs12.x 60 | stage: dev 61 | region: eu-west-1 62 | memorySize: 128 63 | timemout: 10 64 | 65 | custom: 66 | functionsBasePath: src/handlers 67 | 68 | functions: 69 | hello: 70 | handler: users.hello 71 | events: 72 | - http: 73 | path: hello 74 | method: get 75 | 76 | plugins: 77 | - serverless-functions-base-path 78 | ``` 79 | 80 | #### Invoke 81 | 82 | Try to invoke your local function with 83 | 84 | ```bash 85 | serverless invoke locale -f hello 86 | 87 | # output 88 | { 89 | "statusCode": 200, 90 | "body": "{\"message\":\"Hello world!\"}" 91 | } 92 | ``` 93 | 94 | You can find more usage examples in the example folder. 95 | 96 | ## Contributing 97 | 98 | 1. Fork it! 99 | 2. Create your feature branch: `git checkout -b my-new-feature` 100 | 3. Commit your changes: `git commit -am 'Add some feature'` 101 | 4. Push to the branch: `git push origin my-new-feature` 102 | 5. Submit a pull request :D 103 | 104 | ## License 105 | 106 | MIT 107 | -------------------------------------------------------------------------------- /example/basic/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /example/basic/README.md: -------------------------------------------------------------------------------- 1 | # Example 2 | 3 | Demo of usage of `serverless-functions-base-path` plugin. 4 | 5 | ## Installation 6 | 7 | Clone the project 8 | 9 | ```bash 10 | git clone https://github.com/kevinrambaud/serverless-functions-base-path.git 11 | ``` 12 | 13 | Go to the example/basic directory 14 | 15 | ```bash 16 | cd serverless-functions-base-path/example/basic 17 | ``` 18 | 19 | And install dependencies 20 | 21 | ```bash 22 | npm i 23 | ``` 24 | 25 | ## Usage 26 | 27 | You can either invoke manually the hello function by running 28 | 29 | ```bash 30 | ./node_modules/.bin/serverless invoke local -f hello 31 | ``` 32 | 33 | or use the `start` script by running 34 | 35 | ```bash 36 | npm start 37 | ``` 38 | -------------------------------------------------------------------------------- /example/basic/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "basic", 3 | "version": "0.0.1", 4 | "description": "", 5 | "main": "index.js", 6 | "private": true, 7 | "scripts": { 8 | "start": "serverless invoke local -f hello", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "author": 12 | "Kevin Rambaud (https://twitter.com/kevinrambaud)", 13 | "license": "MIT", 14 | "devDependencies": { 15 | "serverless": "^1.26.0", 16 | "serverless-functions-base-path": "^1.0.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /example/basic/serverless.yml: -------------------------------------------------------------------------------- 1 | # serverless.yml 2 | 3 | service: hello-world-service 4 | 5 | provider: 6 | name: aws 7 | runtime: nodejs12.x 8 | stage: dev 9 | region: eu-west-1 10 | memorySize: 128 11 | timemout: 10 12 | 13 | custom: 14 | functionsBasePath: src/handlers 15 | 16 | functions: 17 | hello: 18 | handler: users.hello 19 | events: 20 | - http: 21 | path: hello 22 | method: get 23 | 24 | plugins: 25 | - serverless-functions-base-path 26 | -------------------------------------------------------------------------------- /example/basic/src/handlers/users.js: -------------------------------------------------------------------------------- 1 | // users.js 2 | 3 | module.exports.hello = async (event, context) => { 4 | return { 5 | statusCode: 200, 6 | body: JSON.stringify({ message: 'Hello world!' }) 7 | }; 8 | }; 9 | -------------------------------------------------------------------------------- /example/with-serverless-offline-plugin/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /example/with-serverless-offline-plugin/README.md: -------------------------------------------------------------------------------- 1 | # Example 2 | 3 | Demo of usage of `serverless-functions-base-path` plugin. 4 | 5 | ## Installation 6 | 7 | Clone the project 8 | 9 | ```bash 10 | git clone https://github.com/kevinrambaud/serverless-functions-base-path.git 11 | ``` 12 | 13 | Go to the example/basic directory 14 | 15 | ```bash 16 | cd serverless-functions-base-path/example/with-serverless-offline-plugin 17 | ``` 18 | 19 | And install dependencies 20 | 21 | ```bash 22 | npm i 23 | ``` 24 | 25 | ## Usage 26 | 27 | You can either invoke manually the hello function by running 28 | 29 | ```bash 30 | ./node_modules/.bin/serverless offline 31 | ``` 32 | 33 | or use the `start` script by running 34 | 35 | ```bash 36 | npm start 37 | ``` 38 | 39 | then open a new tab in your terminal and run 40 | 41 | ```bash 42 | curl -i http://localhost:3000/hello 43 | 44 | # output 45 | HTTP/1.1 200 OK 46 | Content-Type: application/json 47 | cache-control: no-cache 48 | content-length: 26 49 | accept-ranges: bytes 50 | Date: Fri, 02 Feb 2018 14:08:57 GMT 51 | Connection: keep-alive 52 | 53 | {"message":"Hello world!"} 54 | ``` 55 | -------------------------------------------------------------------------------- /example/with-serverless-offline-plugin/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "with-serverless-offline-plugin", 3 | "version": "0.0.1", 4 | "description": "", 5 | "main": "index.js", 6 | "private": true, 7 | "scripts": { 8 | "start": "serverless offline", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "author": 12 | "Kevin Rambaud (https://twitter.com/kevinrambaud)", 13 | "license": "MIT", 14 | "devDependencies": { 15 | "serverless": "^1.26.0", 16 | "serverless-functions-base-path": "^1.0.0", 17 | "serverless-offline": "^3.16.0" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /example/with-serverless-offline-plugin/serverless.yml: -------------------------------------------------------------------------------- 1 | # serverless.yml 2 | 3 | service: hello-world-service 4 | 5 | provider: 6 | name: aws 7 | runtime: nodejs12.x 8 | stage: dev 9 | region: eu-west-1 10 | memorySize: 128 11 | timemout: 10 12 | 13 | custom: 14 | functionsBasePath: src/handlers 15 | 16 | functions: 17 | hello: 18 | handler: users.hello 19 | events: 20 | - http: 21 | path: hello 22 | method: get 23 | 24 | plugins: 25 | - serverless-functions-base-path 26 | - serverless-offline 27 | -------------------------------------------------------------------------------- /example/with-serverless-offline-plugin/src/handlers/users.js: -------------------------------------------------------------------------------- 1 | // users.js 2 | 3 | module.exports.hello = async (event, context) => { 4 | return { 5 | statusCode: 200, 6 | body: JSON.stringify({ message: 'Hello world!' }) 7 | }; 8 | }; 9 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/base-path-plugin'); 2 | -------------------------------------------------------------------------------- /lib/base-path-plugin.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | /** 4 | * BasePathPlugin class. 5 | */ 6 | class BasePathPlugin { 7 | /** 8 | * Constructor. 9 | * 10 | * @param {Object} serverless Serverless 11 | * @param {Object} options Options 12 | */ 13 | constructor(serverless, options) { 14 | this.serverless = serverless; 15 | this.options = options; 16 | this.basePath = 17 | typeof this.serverless.service.custom.functionsBasePath === 'string' 18 | ? this.serverless.service.custom.functionsBasePath 19 | : ''; 20 | 21 | this.hooks = { 22 | 'before:run:run': this.rewriteHandlersPath.bind(this), 23 | 'before:offline:start': this.rewriteHandlersPath.bind(this), 24 | 'before:offline:start:init': this.rewriteHandlersPath.bind(this), 25 | 'before:package:initialize': this.rewriteHandlersPath.bind(this), 26 | 'before:deploy:function:packageFunction': this.rewriteHandlersPath.bind( 27 | this 28 | ), 29 | 'before:invoke:local:invoke': this.rewriteHandlersPath.bind(this) 30 | }; 31 | } 32 | 33 | /** 34 | * Rewrite handlers path of the instance's functions 35 | * based on the functionsBasePath custom property passed 36 | * through serverless config file. 37 | * 38 | * @returns {void} 39 | */ 40 | rewriteHandlersPath() { 41 | if (this.basePath) { 42 | const { functions } = this.serverless.service; 43 | 44 | Object.keys(functions).forEach(functionName => { 45 | const handlerPath = functions[functionName].handler; 46 | 47 | if ('image' in functions[functionName]) { 48 | return; 49 | } 50 | 51 | if (!handlerPath) { 52 | throw new Error( 53 | `handler path is not defined for function "${functionName}"` 54 | ); 55 | } 56 | 57 | if (typeof handlerPath !== 'string') { 58 | throw new Error( 59 | `handler path must be a string for function "${functionName}"` 60 | ); 61 | } 62 | 63 | functions[functionName].handler = path.posix.join( 64 | this.basePath, 65 | handlerPath 66 | ); 67 | }); 68 | } 69 | } 70 | } 71 | 72 | module.exports = BasePathPlugin; 73 | -------------------------------------------------------------------------------- /lib/base-path-plugin.test.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const { expect } = require('chai'); 3 | 4 | const BasePathPlugin = require('./base-path-plugin'); 5 | 6 | /** 7 | * Simple object copy to avoid reference copy. 8 | * 9 | * Note: only properties with primitive value will be copied, 10 | * properties with function assigned won't be copied. 11 | * 12 | * @param {Object} obj Object 13 | * @returns {Object} Copied Object 14 | */ 15 | function copyObj(obj) { 16 | return JSON.parse(JSON.stringify(obj)); 17 | } 18 | 19 | /** 20 | * Validate that the handler path rewriting match by comparing 21 | * the previous state of a set of functions and a new one. 22 | * 23 | * @param {string} basePath Bash path 24 | * @param {Object} previousFunctions Functions 25 | * @param {Object} currentFunctions Functions 26 | * @returns {void} 27 | */ 28 | function validatePath(basePath, prevFunctions, currFunctions) { 29 | Object.keys(prevFunctions).forEach(functionName => { 30 | const expectedPath = path.posix.join( 31 | basePath, 32 | prevFunctions[functionName].handler 33 | ); 34 | const newPath = currFunctions[functionName].handler; 35 | 36 | expect(expectedPath).to.be.equal(newPath); 37 | }); 38 | } 39 | 40 | describe('BasePathPlugin', () => { 41 | beforeEach(() => { 42 | this.serverlessMock = { 43 | providers: {}, 44 | service: { 45 | service: 'serverless-demo-functions-base-path', 46 | serviceObject: { name: 'serverless-demo-functions-base-path' }, 47 | provider: { 48 | stage: 'dev', 49 | region: 'eu-west-1', 50 | variableSyntax: '', 51 | name: 'aws', 52 | runtime: 'nodejs6.10', 53 | memorySize: 128, 54 | timemout: 10, 55 | versionFunctions: true 56 | }, 57 | custom: { functionsBasePath: 'src/handlers' }, 58 | plugins: ['serverless-functions-base-path'], 59 | functions: { 60 | hello: { 61 | handler: 'users.hello', 62 | events: [{ http: { path: 'hello', method: 'get' } }], 63 | name: 'serverless-demo-functions-base-path-dev-hello' 64 | }, 65 | bye: { 66 | handler: 'users.bye', 67 | events: [{ http: { path: 'bye', method: 'get' } }], 68 | name: 'serverless-demo-functions-base-path-dev-bye' 69 | } 70 | }, 71 | resources: undefined, 72 | package: {} 73 | } 74 | }; 75 | 76 | this.optionsMock = {}; 77 | }); 78 | 79 | it('should not rewrite the functions handler path if "basePath" is not specified', () => { 80 | this.serverlessMock.service.custom.functionsBasePath = null; 81 | 82 | const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); 83 | const previousFunctions = copyObj(Plugin.serverless.service.functions); 84 | 85 | Plugin.rewriteHandlersPath(); 86 | 87 | const currentFunctions = copyObj(Plugin.serverless.service.functions); 88 | 89 | validatePath(Plugin.basePath, previousFunctions, currentFunctions); 90 | }); 91 | 92 | it('should not rewrite the functions handler path if "basePath" is something else than a string', () => { 93 | this.serverlessMock.service.custom.functionsBasePath = { prop: 'val' }; 94 | 95 | const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); 96 | const previousFunctions = copyObj(Plugin.serverless.service.functions); 97 | 98 | Plugin.rewriteHandlersPath(); 99 | 100 | const currentFunctions = copyObj(Plugin.serverless.service.functions); 101 | 102 | validatePath(Plugin.basePath, previousFunctions, currentFunctions); 103 | }); 104 | 105 | it('should rewrite the functions handler path if a "basePath" variable is passed', () => { 106 | const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); 107 | const previousFunctions = copyObj(Plugin.serverless.service.functions); 108 | 109 | Plugin.rewriteHandlersPath(); 110 | 111 | const currentFunctions = copyObj(Plugin.serverless.service.functions); 112 | 113 | validatePath(Plugin.basePath, previousFunctions, currentFunctions); 114 | }); 115 | 116 | it('should throw an error if handler path is not specified', () => { 117 | this.serverlessMock.service.functions.bye.handler = null; 118 | 119 | const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); 120 | const testFunc = () => { 121 | Plugin.rewriteHandlersPath(); 122 | }; 123 | 124 | expect(testFunc).to.throw( 125 | Error, 126 | /handler path is not defined for function/ 127 | ); 128 | }); 129 | 130 | it('should throw an error if handler path is something else than a string', () => { 131 | this.serverlessMock.service.functions.bye.handler = { props: 'val' }; 132 | 133 | const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); 134 | const testFunc = () => { 135 | Plugin.rewriteHandlersPath(); 136 | }; 137 | 138 | expect(testFunc).to.throw( 139 | Error, 140 | /handler path must be a string for function/ 141 | ); 142 | }); 143 | 144 | it('should skip a function if it has an image specified', () => { 145 | const mock = copyObj(this.serverlessMock); 146 | mock.service.functions.dockerized = { 147 | image: { 148 | name: 'app' 149 | } 150 | }; 151 | 152 | const Plugin = new BasePathPlugin(mock, this.optionsMock); 153 | const previousFunctions = copyObj(Plugin.serverless.service.functions); 154 | 155 | Plugin.rewriteHandlersPath(); 156 | 157 | const currentFunctions = copyObj(Plugin.serverless.service.functions); 158 | 159 | expect(previousFunctions.dockerized).to.be.deep.equal( 160 | currentFunctions.dockerized 161 | ); 162 | }); 163 | }); 164 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-functions-base-path", 3 | "version": "1.0.33", 4 | "description": "Easily define a base path where your serverless functions are located", 5 | "main": "index.js", 6 | "directories": { 7 | "example": "example", 8 | "lib": "lib" 9 | }, 10 | "files": [ 11 | "index.js", 12 | "lib/base-path-plugin.js" 13 | ], 14 | "scripts": { 15 | "coverage": "nyc report --reporter=text-lcov > coverage.lcov", 16 | "pretest": "eslint --fix \"lib/**/*.js\"", 17 | "test": "nyc mocha \"lib/**/*.test.js\"" 18 | }, 19 | "repository": { 20 | "type": "git", 21 | "url": "git+https://github.com/kevinrambaud/serverless-functions-base-path.git" 22 | }, 23 | "keywords": [ 24 | "serverless", 25 | "lambda", 26 | "aws", 27 | "function", 28 | "plugin", 29 | "base", 30 | "path", 31 | "src" 32 | ], 33 | "author": "Kevin Rambaud (https://twitter.com/kevinrambaud)", 34 | "license": "MIT", 35 | "bugs": { 36 | "url": "https://github.com/kevinrambaud/serverless-functions-base-path/issues" 37 | }, 38 | "homepage": "https://github.com/kevinrambaud/serverless-functions-base-path#readme", 39 | "devDependencies": { 40 | "chai": "^4.1.2", 41 | "codecov": "^3.0.0", 42 | "eslint": "^6.8.0", 43 | "eslint-config-kevinrambaud": "^1.0.1", 44 | "husky": "^4.0.0", 45 | "lint-staged": "^10.0.0", 46 | "mocha": "^7.0.0", 47 | "nyc": "^15.0.0" 48 | }, 49 | "eslintConfig": { 50 | "extends": "kevinrambaud" 51 | }, 52 | "husky": { 53 | "hooks": { 54 | "pre-commit": "npm test" 55 | } 56 | }, 57 | "lint-staged": { 58 | "*.js": [ 59 | "eslint --fix", 60 | "git add" 61 | ] 62 | }, 63 | "nyc": { 64 | "include": [ 65 | "lib/**/*.js" 66 | ], 67 | "exclude": [ 68 | "lib/**/*.test.js" 69 | ], 70 | "all": true 71 | } 72 | } 73 | --------------------------------------------------------------------------------