Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept regex as stages #282

Open
wants to merge 3 commits into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function matchStage(currentStage, stage) {
const matches = currentStage.match(new RegExp(stage));
return !!(matches && matches.length > 0);
}

module.exports = matchStage;
10 changes: 10 additions & 0 deletions test/testUtil.js
Original file line number Diff line number Diff line change
@@ -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);
});
})