Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix(assets): support exceptions to exclude patterns (#4473)" #5022

Merged
merged 1 commit into from
Nov 14, 2019
Merged
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
52 changes: 35 additions & 17 deletions packages/@aws-cdk/assets/lib/fs/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,56 @@ import fs = require('fs');
import path = require('path');
import { CopyOptions } from './copy-options';
import { FollowMode } from './follow-mode';
import { mkdirpSync } from './mkdirpSync';
import { listFilesRecursively, shouldFollow } from './utils';
import { shouldExclude, shouldFollow } from './utils';

export function copyDirectory(srcDir: string, destDir: string, options: CopyOptions = { }, rootDir?: string) {
const follow = options.follow !== undefined ? options.follow : FollowMode.EXTERNAL;
const exclude = options.exclude || [];

rootDir = rootDir || srcDir;

if (!fs.statSync(srcDir).isDirectory()) {
throw new Error(`${srcDir} is not a directory`);
}

for (const assetFile of listFilesRecursively(srcDir, {...options, follow}, rootDir)) {
const filePath = assetFile.relativePath;
const destFilePath = path.join(destDir, filePath);
const files = fs.readdirSync(srcDir);
for (const file of files) {
const sourceFilePath = path.join(srcDir, file);

if (follow !== FollowMode.ALWAYS) {
if (assetFile.isSymbolicLink) {
const targetPath = path.normalize(path.resolve(srcDir, assetFile.symlinkTarget));
if (!shouldFollow(follow, rootDir, targetPath)) {
fs.symlinkSync(assetFile.symlinkTarget, destFilePath);
if (shouldExclude(exclude, path.relative(rootDir, sourceFilePath))) {
continue;
}

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

let stat: fs.Stats | undefined = follow === FollowMode.ALWAYS
? fs.statSync(sourceFilePath)
: fs.lstatSync(sourceFilePath);

if (stat && stat.isSymbolicLink()) {
const target = fs.readlinkSync(sourceFilePath);

continue;
}
// determine if this is an external link (i.e. the target's absolute path
// is outside of the root directory).
const targetPath = path.normalize(path.resolve(srcDir, target));

if (shouldFollow(follow, rootDir, targetPath)) {
stat = fs.statSync(sourceFilePath);
} else {
fs.symlinkSync(target, destFilePath);
stat = undefined;
}
}

if (!assetFile.isDirectory) {
mkdirpSync(path.dirname(destFilePath));
fs.copyFileSync(assetFile.absolutePath, destFilePath);
} else {
mkdirpSync(destFilePath);
if (stat && stat.isDirectory()) {
fs.mkdirSync(destFilePath);
copyDirectory(sourceFilePath, destFilePath, options, rootDir);
stat = undefined;
}

if (stat && stat.isFile()) {
fs.copyFileSync(sourceFilePath, destFilePath);
stat = undefined;
}
}
}
44 changes: 28 additions & 16 deletions packages/@aws-cdk/assets/lib/fs/fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs = require('fs');
import path = require('path');
import { CopyOptions } from './copy-options';
import { FollowMode } from './follow-mode';
import { listFilesRecursively, shouldFollow } from './utils';
import { shouldExclude, shouldFollow } from './utils';

const BUFFER_SIZE = 8 * 1024;
const CTRL_SOH = '\x01';
Expand Down Expand Up @@ -38,28 +38,40 @@ export function fingerprint(fileOrDirectory: string, options: FingerprintOptions
const rootDirectory = fs.statSync(fileOrDirectory).isDirectory()
? fileOrDirectory
: path.dirname(fileOrDirectory);
const exclude = options.exclude || [];
_processFileOrDirectory(fileOrDirectory);

for (const assetFile of listFilesRecursively(fileOrDirectory, {...options, follow}, rootDirectory)) {
const relativePath = path.relative(fileOrDirectory, assetFile.absolutePath);
return hash.digest('hex');

function _processFileOrDirectory(symbolicPath: string, realPath = symbolicPath) {
if (shouldExclude(exclude, symbolicPath)) {
return;
}

const stat = fs.lstatSync(realPath);
const relativePath = path.relative(fileOrDirectory, symbolicPath);

if (assetFile.isSymbolicLink) {
const resolvedLinkTarget = path.resolve(path.dirname(assetFile.absolutePath), assetFile.symlinkTarget);
if (!shouldFollow(follow, rootDirectory, resolvedLinkTarget)) {
_hashField(hash, `link:${relativePath}`, assetFile.symlinkTarget);
if (stat.isSymbolicLink()) {
const linkTarget = fs.readlinkSync(realPath);
const resolvedLinkTarget = path.resolve(path.dirname(realPath), linkTarget);
if (shouldFollow(follow, rootDirectory, resolvedLinkTarget)) {
_processFileOrDirectory(symbolicPath, resolvedLinkTarget);
} else {
_hashField(hash, `file:${relativePath}`, _contentFingerprint(assetFile.absolutePath, assetFile.size));
_hashField(hash, `link:${relativePath}`, linkTarget);
}
} else if (assetFile.isFile) {
_hashField(hash, `file:${relativePath}`, _contentFingerprint(assetFile.absolutePath, assetFile.size));
} else if (!assetFile.isDirectory) {
throw new Error(`Unable to hash ${assetFile.absolutePath}: it is neither a file nor a directory`);
} else if (stat.isFile()) {
_hashField(hash, `file:${relativePath}`, _contentFingerprint(realPath, stat));
} else if (stat.isDirectory()) {
for (const item of fs.readdirSync(realPath).sort()) {
_processFileOrDirectory(path.join(symbolicPath, item), path.join(realPath, item));
}
} else {
throw new Error(`Unable to hash ${symbolicPath}: it is neither a file nor a directory`);
}
}

return hash.digest('hex');
}

function _contentFingerprint(file: string, size: number): string {
function _contentFingerprint(file: string, stat: fs.Stats): string {
const hash = crypto.createHash('sha256');
const buffer = Buffer.alloc(BUFFER_SIZE);
// tslint:disable-next-line: no-bitwise
Expand All @@ -73,7 +85,7 @@ function _contentFingerprint(file: string, size: number): string {
} finally {
fs.closeSync(fd);
}
return `${size}:${hash.digest('hex')}`;
return `${stat.size}:${hash.digest('hex')}`;
}

function _hashField(hash: crypto.Hash, header: string, value: string | Buffer | DataView) {
Expand Down
67 changes: 0 additions & 67 deletions packages/@aws-cdk/assets/lib/fs/mkdirpSync.ts

This file was deleted.

Loading