From 11e5efed240c1f253792ac7ee6f99843103fffad Mon Sep 17 00:00:00 2001 From: epolon Date: Sun, 20 Mar 2022 17:52:49 +0200 Subject: [PATCH 1/2] only ignore .git directory files --- tools/@aws-cdk/node-bundle/src/api/bundle.ts | 4 +-- .../node-bundle/test/api/bundle.test.ts | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/tools/@aws-cdk/node-bundle/src/api/bundle.ts b/tools/@aws-cdk/node-bundle/src/api/bundle.ts index c438e059a8cf8..8b27644c83ff5 100644 --- a/tools/@aws-cdk/node-bundle/src/api/bundle.ts +++ b/tools/@aws-cdk/node-bundle/src/api/bundle.ts @@ -225,8 +225,8 @@ export class Bundle { const target = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-write-')); // copy the entire project since we are retaining the original files. - // except for `node_modules` and `.git` which definitely don't belong in the package. - fs.copySync(this.packageDir, target, { filter: n => !n.includes('node_modules') && !n.includes('.git') }); + // except for `node_modules` and `.git` directories which definitely don't belong in the package. + fs.copySync(this.packageDir, target, { filter: n => !n.includes(`node_modules${path.sep}`) && !n.includes(`.git${path.sep}`) }); // clone the original manifest since we are going to // to mutate it. diff --git a/tools/@aws-cdk/node-bundle/test/api/bundle.test.ts b/tools/@aws-cdk/node-bundle/test/api/bundle.test.ts index de7f741bc880e..3a01c349d5669 100644 --- a/tools/@aws-cdk/node-bundle/test/api/bundle.test.ts +++ b/tools/@aws-cdk/node-bundle/test/api/bundle.test.ts @@ -127,3 +127,31 @@ test('validate and fix', () => { expect(fs.existsSync(tarball)).toBeTruthy(); }); + +test('write ignores only .git and node_modules directories', () => { + + const pkg = Package.create({ name: 'consumer', licenses: ['Apache-2.0'] }); + pkg.addDependency({ name: 'dep1', licenses: ['MIT'] }); + pkg.addDependency({ name: 'dep2', licenses: ['Apache-2.0'] }); + + pkg.write(); + pkg.install(); + + const bundle = new Bundle({ + packageDir: pkg.dir, + entryPoints: [pkg.entrypoint], + allowedLicenses: ['Apache-2.0', 'MIT'], + }); + + // add a gitignore file to the package - it should be included + fs.writeFileSync(path.join(pkg.dir, '.gitignore'), 'something'); + + // add a silly node_modules_file to the package - it should be included + fs.writeFileSync(path.join(pkg.dir, 'node_modules_file'), 'something'); + + const bundleDir = bundle.write(); + + expect(fs.existsSync(path.join(bundleDir, '.gitignore'))).toBeTruthy(); + expect(fs.existsSync(path.join(bundleDir, 'node_modules_file'))).toBeTruthy(); + +}); \ No newline at end of file From df92e643b61ff8368ff36fef67b4818a598b4ac5 Mon Sep 17 00:00:00 2001 From: epolon Date: Sun, 20 Mar 2022 19:29:01 +0200 Subject: [PATCH 2/2] ignore empty dirs as well --- tools/@aws-cdk/node-bundle/src/api/bundle.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/@aws-cdk/node-bundle/src/api/bundle.ts b/tools/@aws-cdk/node-bundle/src/api/bundle.ts index 8b27644c83ff5..b1356ed6a6505 100644 --- a/tools/@aws-cdk/node-bundle/src/api/bundle.ts +++ b/tools/@aws-cdk/node-bundle/src/api/bundle.ts @@ -224,9 +224,12 @@ export class Bundle { const target = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-write-')); + // we definitely don't need these directories in the package + // so no need to copy them over. + const ignoreDirectories = ['node_modules', '.git']; + // copy the entire project since we are retaining the original files. - // except for `node_modules` and `.git` directories which definitely don't belong in the package. - fs.copySync(this.packageDir, target, { filter: n => !n.includes(`node_modules${path.sep}`) && !n.includes(`.git${path.sep}`) }); + fs.copySync(this.packageDir, target, { filter: n => !n.split(path.sep).some((p => ignoreDirectories.includes(p))) }); // clone the original manifest since we are going to // to mutate it.