diff --git a/tools/@aws-cdk/node-bundle/src/api/bundle.ts b/tools/@aws-cdk/node-bundle/src/api/bundle.ts index c438e059a8cf8..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` which definitely don't belong in the package. - fs.copySync(this.packageDir, target, { filter: n => !n.includes('node_modules') && !n.includes('.git') }); + 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. 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