Skip to content

Commit

Permalink
feat(aws-cdk): directory assets follow symlinks (#1318)
Browse files Browse the repository at this point in the history
When building a nodejs lambda it is really common for dependencies under the `node_modules` folder to actually be symlink'd to other folders on your system. The symlinks will now be followed and the indicated files packaged (instead of the symlinks themselves).

Fixes #731
  • Loading branch information
justinmchase authored and rix0rrr committed Dec 11, 2018
1 parent 3fdbc95 commit 2dfd593
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/aws-cdk/lib/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ export function zipDirectory(directory: string, outputFile: string): Promise<voi
return new Promise((ok, fail) => {
const output = fs.createWriteStream(outputFile);
const archive = archiver('zip');
archive.directory(directory, false);
// The below options are needed to support following symlinks when building zip files:
// - nodir: This will prevent symlinks themselves from being copied into the zip.
// - follow: This will follow symlinks and copy the files within.
const globOptions = {
dot: true,
nodir: true,
follow: true,
cwd: directory
};
archive.glob('**', globOptions);
archive.pipe(output);
archive.finalize();

Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/test/test-archive-follow/data/linked
1 change: 1 addition & 0 deletions packages/aws-cdk/test/test-archive-follow/data/one.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions packages/aws-cdk/test/test-archive-follow/linked/two.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
19 changes: 19 additions & 0 deletions packages/aws-cdk/test/test.archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,24 @@ export = {

test.deepEqual(hash1, hash2, 'md5 hash of two zips of the same content are not the same');
test.done();
},

async 'zipDirectory follows symlinks'(test: Test) {
const originalDir = path.join(__dirname, 'test-archive-follow', 'data');
const stagingDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test.archive'));
const zipFile = path.join(stagingDir, 'output.zip');
const extractDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test.archive.follow'));

try {
await zipDirectory(originalDir, zipFile);
await exec(`unzip ${zipFile}`, { cwd: extractDir });
await exec(`diff -bur ${originalDir} ${extractDir}`);
} catch (e) {
test.ok(false, `extracted directory ${extractDir} differs from original ${originalDir}, symlinks not followed.`);
}

await fs.remove(stagingDir);
await fs.remove(extractDir);
test.done();
}
};

0 comments on commit 2dfd593

Please sign in to comment.