diff --git a/package.json b/package.json index bb03023..93250f1 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "version": "0.0.0", "main": "src/plugin.js", "scripts": { + "fix": "standard --fix", "test": "jest test", "test:cover": "jest test --coverage", "coveralls": "cat ./coverage/lcov.info | coveralls", diff --git a/src/plugin.js b/src/plugin.js index 4f5c7cc..f2b58a2 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -18,8 +18,15 @@ class Plugin { * * @param {object} serverless */ - constructor (serverless) { + constructor (serverless, options) { this.serverless = serverless + + if (options && options.stage) { + this.stage = options.stage + } else { + this.stage = serverless.service.provider.stage + } + this.hooks = { 'deploy:compileEvents': this.beforeDeployResources.bind(this) } @@ -71,7 +78,7 @@ class Plugin { */ resources (table, index, config) { const resources = [] - const stage = this.serverless.service.provider.stage + const stage = this.stage const data = this.defaults(config) // Start processing configuration diff --git a/test/plugin.spec.js b/test/plugin.spec.js index 98f95b3..6a97b93 100644 --- a/test/plugin.spec.js +++ b/test/plugin.spec.js @@ -4,7 +4,7 @@ const Plugin = require('../') describe('Normalize', () => { it('converts everything to an array', () => { - const test = new Plugin() + const test = new Plugin({ service: { provider: { stage: 'foo' } } }) expect(test.normalize('test')).toEqual(['test']) expect(test.normalize(['test'])).toEqual(['test']) @@ -21,7 +21,7 @@ describe('Defaults', () => { write: { minimum: 20 } } - const test = new Plugin() + const test = new Plugin({ service: { provider: { stage: 'foo' } } }) const data = test.defaults(config) expect(data.read.minimum).toBe(5) @@ -33,3 +33,17 @@ describe('Defaults', () => { expect(data.write.usage).toBe(0.75) }) }) + +describe('Stage', () => { + it('uses stage from configuration', () => { + const p = new Plugin({ service: { provider: { stage: 'foo' } } }) + + expect(p.stage).toBe('foo') + }) + + it('overwrites stage from options', () => { + const p = new Plugin({ service: { provider: { stage: 'foo' } } }, { stage: 'bar' }) + + expect(p.stage).toBe('bar') + }) +})