From 5282566eab8af7284a7ffb58e15c5c25f715bf32 Mon Sep 17 00:00:00 2001 From: Billy Vong Date: Fri, 6 Nov 2020 17:39:25 -0800 Subject: [PATCH] feat: Add support for multiple patterns when using file status This adds support for using multiple patterns when checking for file status (added, modified, deleted) and as a result also allows you to use YAML anchors. --- README.md | 5 +++++ __tests__/filter.test.ts | 14 ++++++++++++++ src/filter.ts | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b0f747..45bb93d 100644 --- a/README.md +++ b/README.md @@ -350,10 +350,15 @@ jobs: # dictionary, where type(s) of change composes the key. # Multiple change types can be specified using `|` as delimiter. filters: | + shared: &shared + - common/** + - config/** addedOrModified: - added|modified: '**' allChanges: - added|deleted|modified: '**' + addedOrModifiedAnchors: + - added|modified: *shared ``` diff --git a/__tests__/filter.test.ts b/__tests__/filter.test.ts index 4e67464..be42ed4 100644 --- a/__tests__/filter.test.ts +++ b/__tests__/filter.test.ts @@ -165,6 +165,20 @@ describe('matching specific change status', () => { const match = filter.match(files) expect(match.addOrModify).toEqual(files) }) + + test.only('matches when using an anchor', () => { + const yaml = ` + shared: &shared + - common/**/* + - config/**/* + src: + - modified: *shared + ` + let filter = new Filter(yaml) + const files = modified(['config/file.js', 'common/anotherFile.js']) + const match = filter.match(files) + expect(match.src).toEqual(files) + }) }) function modified(paths: string[]): File[] { diff --git a/src/filter.ts b/src/filter.ts index b905670..dd39f63 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -78,7 +78,7 @@ export class Filter { if (typeof item === 'object') { return Object.entries(item).map(([key, pattern]) => { - if (typeof key !== 'string' || typeof pattern !== 'string') { + if (typeof key !== 'string' || (typeof pattern !== 'string' && !Array.isArray(pattern))) { this.throwInvalidFormatError( `Expected [key:string]= pattern:string, but [${key}:${typeof key}]= ${pattern}:${typeof pattern} found` )