diff --git a/README.md b/README.md index ba5f56cb..91af1d06 100644 --- a/README.md +++ b/README.md @@ -91,9 +91,10 @@ Docker setup: ```yml custom: dynamodb: - # If you only want to use DynamoDB Local in some stages, declare them here + # If you only want to use DynamoDB Local in some stages, declare them here as string or stringified regex stages: - dev + - 'dev-\\d+' start: docker: true port: 8000 diff --git a/index.js b/index.js index d29a9e3e..5132b53e 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ const AWS = require("aws-sdk"); const dynamodbLocal = require("dynamodb-localhost"); const seeder = require("./src/seeder"); const path = require('path'); +const matchStage = require('./src/util'); +const { match } = require("assert"); class ServerlessDynamodbLocal { constructor(serverless, options) { @@ -168,8 +170,8 @@ class ServerlessDynamodbLocal { * @return {Boolean} if the handler can run for the provided stage */ shouldExecute() { - if (this.config.stages && this.config.stages.includes(this.stage)) { - return true; + if (this.config.stages) { + return this.config.stages.some((stage) => matchStage(this.stage, stage)); } return false; } diff --git a/package-lock.json b/package-lock.json index 82d80324..f054171b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "serverless-dynamodb-local", - "version": "0.2.37", + "version": "0.2.40", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/util.js b/src/util.js new file mode 100644 index 00000000..76836af2 --- /dev/null +++ b/src/util.js @@ -0,0 +1,6 @@ +function matchStage(currentStage, stage) { + const matches = currentStage.match(new RegExp(stage)); + return !!(matches && matches.length > 0); +} + +module.exports = matchStage; \ No newline at end of file diff --git a/test/testUtil.js b/test/testUtil.js new file mode 100644 index 00000000..c222ddae --- /dev/null +++ b/test/testUtil.js @@ -0,0 +1,10 @@ +const { expect } = require('chai'); +const matchStage = require('../src/util'); + +describe('Test matching stages', () => { + it('should match regex', () => { + expect(matchStage('dev-777', 'dev-\\d+')).to.eq(true); + expect(matchStage('dev', 'dev-\\d+')).to.eq(false); + expect(matchStage('dev', 'dev')).to.eq(true); + }); +})