From 4c1f2e5edc9a19d02935bb01a15448a34178c252 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Tue, 2 Nov 2021 11:40:26 -0700 Subject: [PATCH] Make the directories watched relative. --- packages/aws-cdk/lib/cdk-toolkit.ts | 19 ++++++++-------- packages/aws-cdk/test/cdk-toolkit.test.ts | 27 +++++++++-------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index 9201467432282..f471895f88078 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -275,10 +275,10 @@ export class CdkToolkit { // 4. Any node_modules and its content (even if it's not a JS/TS project, you might be using a local aws-cli package) const outputDir = this.props.configuration.settings.get(['output']); const watchExcludes = this.patternsArrayForWatch(watchSettings.exclude, { rootDir, returnRootDirIfEmpty: false }).concat( - `${rootDir}/${outputDir}/**`, - `${rootDir}/**/.*`, - `${rootDir}/**/.*/**`, - `${rootDir}/**/node_modules/**`, + `${outputDir}/**`, + '**/.*', + '**/.*/**', + '**/node_modules/**', ); debug("'exclude' patterns for 'watch': %s", watchExcludes); @@ -288,10 +288,10 @@ export class CdkToolkit { // we will batch them, and trigger another 'cdk deploy' after the current one finishes, // making sure 'cdk deploy's always execute one at a time. // Here's a diagram showing the state transitions: - // -------------- -------- file changed -------------- file changed -------------- - // | | ready event | | ------------------> | | ------------------> | | - // | pre-ready | -------------> | open | | deploying | | queued | - // | | | | <------------------ | | <------------------ | | + // -------------- -------- file changed -------------- file changed -------------- file changed + // | | ready event | | ------------------> | | ------------------> | | --------------| + // | pre-ready | -------------> | open | | deploying | | queued | | + // | | | | <------------------ | | <------------------ | | <-------------| // -------------- -------- 'cdk deploy' done -------------- 'cdk deploy' done -------------- let latch: 'pre-ready' | 'open' | 'deploying' | 'queued' = 'pre-ready'; chokidar.watch(watchIncludes, { @@ -573,8 +573,7 @@ export class CdkToolkit { ? (Array.isArray(patterns) ? patterns : [patterns]) : []; return patternsArray.length > 0 - // we need to prefix each pattern from the file with the root directory, as they are supposed to be relative to it - ? patternsArray.map(pattern => `${options.rootDir}/${pattern}`) + ? patternsArray : (options.returnRootDirIfEmpty ? [options.rootDir] : []); } diff --git a/packages/aws-cdk/test/cdk-toolkit.test.ts b/packages/aws-cdk/test/cdk-toolkit.test.ts index ec20296ff7059..e8445da68a6b6 100644 --- a/packages/aws-cdk/test/cdk-toolkit.test.ts +++ b/packages/aws-cdk/test/cdk-toolkit.test.ts @@ -274,9 +274,7 @@ describe('watch', () => { await toolkit.watch({ selector: { patterns: [] } }); - const includeArgs = fakeChokidarWatch.includeArgs; - expect(includeArgs.length).toBe(1); - expect(includeArgs[0]).toMatch(/\/my-dir$/); + expect(fakeChokidarWatch.includeArgs).toStrictEqual(['my-dir']); }); test("allows providing an array of strings in 'watch.include'", async () => { @@ -287,10 +285,7 @@ describe('watch', () => { await toolkit.watch({ selector: { patterns: [] } }); - const includeArgs = fakeChokidarWatch.includeArgs; - expect(includeArgs.length).toBe(2); - expect(includeArgs[0]).toMatch(/\/my-dir1$/); - expect(includeArgs[1]).toMatch(/\/\*\*\/my-dir2\/\*$/); + expect(fakeChokidarWatch.includeArgs).toStrictEqual(['my-dir1', '**/my-dir2/*']); }); test('ignores the output dir, dot files, dot directories, and node_modules by default', async () => { @@ -300,12 +295,12 @@ describe('watch', () => { await toolkit.watch({ selector: { patterns: [] } }); - const excludeArgs = fakeChokidarWatch.excludeArgs; - expect(excludeArgs.length).toBe(4); - expect(excludeArgs[0]).toMatch(/\/cdk.out\/\*\*$/); - expect(excludeArgs[1]).toMatch(/\/\*\*\/\.\*$/); - expect(excludeArgs[2]).toMatch(/\/\*\*\/\.\*\/\*\*$/); - expect(excludeArgs[3]).toMatch(/\*\*\/node_modules\/\*\*$/); + expect(fakeChokidarWatch.excludeArgs).toStrictEqual([ + 'cdk.out/**', + '**/.*', + '**/.*/**', + '**/node_modules/**', + ]); }); test("allows providing a single string in 'watch.exclude'", async () => { @@ -318,7 +313,7 @@ describe('watch', () => { const excludeArgs = fakeChokidarWatch.excludeArgs; expect(excludeArgs.length).toBe(5); - expect(excludeArgs[0]).toMatch(/\/my-dir$/); + expect(excludeArgs[0]).toBe('my-dir'); }); test("allows providing an array of strings in 'watch.exclude'", async () => { @@ -331,8 +326,8 @@ describe('watch', () => { const excludeArgs = fakeChokidarWatch.excludeArgs; expect(excludeArgs.length).toBe(6); - expect(excludeArgs[0]).toMatch(/\/my-dir1$/); - expect(excludeArgs[1]).toMatch(/\/\*\*\/my-dir2$/); + expect(excludeArgs[0]).toBe('my-dir1'); + expect(excludeArgs[1]).toBe('**/my-dir2'); }); describe('with file change events', () => {