Skip to content

Commit 5ce0983

Browse files
authored
fix(cli): templates don't include .gitignore (#19482)
Fixes #19460
1 parent 33e3677 commit 5ce0983

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

tools/@aws-cdk/node-bundle/src/api/bundle.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,12 @@ export class Bundle {
224224

225225
const target = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-write-'));
226226

227+
// we definitely don't need these directories in the package
228+
// so no need to copy them over.
229+
const ignoreDirectories = ['node_modules', '.git'];
230+
227231
// copy the entire project since we are retaining the original files.
228-
// except for `node_modules` and `.git` which definitely don't belong in the package.
229-
fs.copySync(this.packageDir, target, { filter: n => !n.includes('node_modules') && !n.includes('.git') });
232+
fs.copySync(this.packageDir, target, { filter: n => !n.split(path.sep).some((p => ignoreDirectories.includes(p))) });
230233

231234
// clone the original manifest since we are going to
232235
// to mutate it.

tools/@aws-cdk/node-bundle/test/api/bundle.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,31 @@ test('validate and fix', () => {
127127
expect(fs.existsSync(tarball)).toBeTruthy();
128128

129129
});
130+
131+
test('write ignores only .git and node_modules directories', () => {
132+
133+
const pkg = Package.create({ name: 'consumer', licenses: ['Apache-2.0'] });
134+
pkg.addDependency({ name: 'dep1', licenses: ['MIT'] });
135+
pkg.addDependency({ name: 'dep2', licenses: ['Apache-2.0'] });
136+
137+
pkg.write();
138+
pkg.install();
139+
140+
const bundle = new Bundle({
141+
packageDir: pkg.dir,
142+
entryPoints: [pkg.entrypoint],
143+
allowedLicenses: ['Apache-2.0', 'MIT'],
144+
});
145+
146+
// add a gitignore file to the package - it should be included
147+
fs.writeFileSync(path.join(pkg.dir, '.gitignore'), 'something');
148+
149+
// add a silly node_modules_file to the package - it should be included
150+
fs.writeFileSync(path.join(pkg.dir, 'node_modules_file'), 'something');
151+
152+
const bundleDir = bundle.write();
153+
154+
expect(fs.existsSync(path.join(bundleDir, '.gitignore'))).toBeTruthy();
155+
expect(fs.existsSync(path.join(bundleDir, 'node_modules_file'))).toBeTruthy();
156+
157+
});

0 commit comments

Comments
 (0)