Skip to content

Commit 861e9d6

Browse files
author
Niko Lehtovirta
authored
fix: Match properly when using / in include/exclude (#5)
1 parent 68ba692 commit 861e9d6

4 files changed

Lines changed: 44 additions & 24 deletions

File tree

package-lock.json

Lines changed: 15 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
"handlebars": "^4.7.6",
3131
"inquirer": "^7.1.0",
3232
"js-yaml": "^3.13.1",
33-
"micromatch": "^4.0.2",
33+
"picomatch": "^2.2.2",
3434
"yargs": "^15.1.0"
3535
},
3636
"devDependencies": {
3737
"@types/diff": "^4.0.2",
3838
"@types/inquirer": "^6.5.0",
3939
"@types/js-yaml": "^3.12.2",
40-
"@types/micromatch": "^4.0.1",
4140
"@types/node": "^13.7.0",
41+
"@types/picomatch": "^2.2.1",
4242
"@types/yargs": "^15.0.3",
4343
"@typescript-eslint/eslint-plugin": "^2.20.0",
4444
"@typescript-eslint/parser": "^2.20.0",

src/lib/utils/match.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ test('Validate match log', t => {
1515
const excl = ['*dev*'];
1616
t.deepEqual(match(str, inc, excl), false);
1717
});
18+
19+
test('Validate log group match with /', t => {
20+
const str = '/aws/lambda/lambda-dev';
21+
const inc: string[] = [];
22+
const excl = ['/aws/*/lambda-dev'];
23+
t.deepEqual(match(str, inc, excl), false);
24+
});

src/lib/utils/match.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
import { isMatch } from 'micromatch';
1+
import { isMatch } from 'picomatch';
2+
3+
// This fix is needed for picomatch library
4+
// Otherwise there will be error when matching log groups for example
5+
function fixMatchString(str: string): string {
6+
return str.replace(/\//g, '-');
7+
}
8+
9+
function matchIncluded(str: string, included: string[]): boolean {
10+
return included.length === 0 || isMatch(str, included);
11+
}
12+
13+
function matchExcluded(str: string, excluded: string[]): boolean {
14+
return excluded.length === 0 || !isMatch(str, excluded);
15+
}
216

317
export default function match(str: string, include: string[], exclude: string[]): boolean {
4-
const matchStr = str.replace(/\//g, '-');
5-
return (include.length === 0 || isMatch(matchStr, include)) && (exclude.length === 0 || !isMatch(matchStr, exclude));
18+
const matchStr = fixMatchString(str);
19+
const included = include.map(str => fixMatchString(str));
20+
const excluded = exclude.map(str => fixMatchString(str));
21+
22+
return matchIncluded(matchStr, included) && matchExcluded(matchStr, excluded);
623
}

0 commit comments

Comments
 (0)