Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/@aws-cdk/core/lib/fs/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ export function copyDirectory(srcDir: string, destDir: string, options: CopyOpti
throw new Error(`${srcDir} is not a directory`);
}

const files = fs.readdirSync(srcDir);
const files = fs.readdirSync(srcDir, { withFileTypes: true });
for (const file of files) {
const sourceFilePath = path.join(srcDir, file);
const sourceFilePath = path.join(srcDir, file.name);

if (ignoreStrategy.ignores(sourceFilePath)) {
const ignorePath = sourceFilePath + (file.name && file.isDirectory() ? path.sep : '');
if (ignoreStrategy.ignores(ignorePath)) {
continue;
}

const destFilePath = path.join(destDir, file);
const destFilePath = path.join(destDir, file.name);

let stat: fs.Stats | undefined = follow === SymlinkFollowMode.ALWAYS
? fs.statSync(sourceFilePath)
Expand Down
7 changes: 4 additions & 3 deletions packages/@aws-cdk/core/lib/fs/fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ export function fingerprint(fileOrDirectory: string, options: FingerprintOptions
return hash.digest('hex');

function _processFileOrDirectory(symbolicPath: string, isRootDir: boolean = false, realPath = symbolicPath) {
if (!isRootDir && ignoreStrategy.ignores(symbolicPath)) {
const stat = fs.lstatSync(realPath);

const ignorePath = symbolicPath + (stat.isDirectory() ? path.sep : '');
if (!isRootDir && ignoreStrategy.ignores(ignorePath)) {
return;
}

const stat = fs.lstatSync(realPath);

// Use relative path as hash component. Normalize it with forward slashes to ensure
// same hash on Windows and Linux.
const hashComponent = path.relative(fileOrDirectory, symbolicPath).replace(/\\/g, '/');
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/core/lib/fs/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ export class GitIgnoreStrategy extends IgnoreStrategy {
}

let relativePath = path.relative(this.absoluteRootPath, absoluteFilePath);
if (relativePath) {
relativePath += absoluteFilePath.endsWith(path.sep) ? path.sep : '';
}

return this.ignore.ignores(relativePath);
}
Expand Down
17 changes: 16 additions & 1 deletion packages/@aws-cdk/core/test/fs/fs-fingerprint.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { FileSystem, SymlinkFollowMode } from '../../lib/fs';
import { FileSystem, IgnoreMode, SymlinkFollowMode } from '../../lib/fs';
import { contentFingerprint } from '../../lib/fs/fingerprint';

describe('fs fingerprint', () => {
Expand Down Expand Up @@ -79,6 +79,21 @@ describe('fs fingerprint', () => {

});

test('does not ignore requested files with negate modifier', () => {
// GIVEN
const srcdir = path.join(__dirname, 'fixtures', 'test1');
const outdir = fs.mkdtempSync(path.join(os.tmpdir(), 'copy-tests'));
FileSystem.copyDirectory(srcdir, outdir);

// WHEN
const hashSrc = FileSystem.fingerprint(srcdir, { exclude: ['*', '!*/', '!*.*'], ignoreMode: IgnoreMode.GIT });
const hashCopy = FileSystem.fingerprint(outdir, { ignoreMode: IgnoreMode.GIT });

// THEN
expect(hashSrc).toEqual(hashCopy);

});

test('changes with file names', () => {
// GIVEN
const srcdir = path.join(__dirname, 'fixtures', 'symlinks');
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/core/test/fs/fs-ignore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ describe('GitIgnoreStrategy', () => {

expect(strategyPermits(strategy, permits)).toEqual(permits);
});

test('does not exclude allow listed files in subdirectories after excluding everything', () => {
const strategy = IgnoreStrategy.git('/tmp', ['*', '!*/', '!*.html']);
const ignores = [
'/tmp/file.ignored',
'/tmp/some/file.ignored',
];
const permits = [
'/tmp/*.html',
'/tmp/some/*.html',
];

expect(strategyIgnores(strategy, ignores)).toEqual(ignores);
expect(strategyPermits(strategy, permits)).toEqual(permits);
});
});

describe('DockerIgnoreStrategy', () => {
Expand Down