Skip to content

Commit

Permalink
Use tar Module and Cleanup
Browse files Browse the repository at this point in the history
Use the tar node module instead of shelling out to tar. This allows us
to stream the download contents directly to unpacking and removes the
need to manually parse the beginning of the commit id hash to traverse
into the build directory.
  • Loading branch information
MrArnoldPalmer committed Jan 29, 2020
1 parent 6b7f7f0 commit 342020b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/@jsii/integ-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"jest": "^25.1.0",
"jsii": "^0.21.2",
"jsii-pacmak": "^0.21.2",
"tar": "^6.0.0",
"typescript": "~3.7.5"
},
"jest": {
Expand Down
36 changes: 12 additions & 24 deletions packages/@jsii/integ-test/test/build-cdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mkdtemp, remove } from 'fs-extra';
import { mkdtemp } from 'fs-extra';
import * as path from 'path';
import * as Octokit from '@octokit/rest';
import { downloadReleaseAsset, minutes, ProcessManager, writeFileStream } from '../utils';
import { downloadReleaseAsset, minutes, ProcessManager, extractFileStream } from '../utils';
import * as dotenv from 'dotenv';

dotenv.config();
Expand All @@ -23,7 +23,7 @@ describe('Build CDK', () => {

afterAll(async () => {
await processes.killAll();
await remove(buildDir);
// await remove(buildDir);
});

test('can build latest cdk release', async () => {
Expand All @@ -34,38 +34,26 @@ describe('Build CDK', () => {
repo: 'aws-cdk'
});

// save code to tmp dir
const fileName = 'cdk.tar.gz';
const tarFile = path.join(buildDir, fileName);
// download and extract code
const code = await downloadReleaseAsset(`https://api.github.com/repos/aws/aws-cdk/tarball/${release.data.tag_name}`);

await writeFileStream(code, tarFile);

// unzip tar archive
await processes.spawn('tar', ['-xzf', fileName], {
cwd: buildDir
});

// root dir of extracted src
// `${buildDir}/${owner}-${repo}-${first 7 chars of commit hash}
const srcDir = path.join(buildDir, `aws-aws-cdk-${release.data.target_commitish.substring(0, 7)}`);
await extractFileStream(code, buildDir);

// install cdk dependencies
await processes.spawn('yarn', ['install'], {
cwd: srcDir
cwd: buildDir
});

// link local jsii/jsii-pacmak builds
await processes.spawn('rm', ['-rf', './node_modules/jsii'], { cwd: srcDir });
await processes.spawn('rm', ['-rf', './node_modules/jsii-pacmak'], { cwd: srcDir });
await processes.spawn('ln', ['-s', JSII_DIR, './node_modules'], { cwd: srcDir });
await processes.spawn('ln', ['-s', JSII_PACMAK_DIR, './node_modules'], { cwd: srcDir });
await processes.spawn('rm', ['-rf', './node_modules/jsii'], { cwd: buildDir });
await processes.spawn('rm', ['-rf', './node_modules/jsii-pacmak'], { cwd: buildDir });
await processes.spawn('ln', ['-s', JSII_DIR, './node_modules'], { cwd: buildDir });
await processes.spawn('ln', ['-s', JSII_PACMAK_DIR, './node_modules'], { cwd: buildDir });

// build cdk
await processes.spawn('npx', ['lerna', 'run', 'build', '--stream'], { cwd: srcDir });
await processes.spawn('npx', ['lerna', 'run', 'build', '--stream'], { cwd: buildDir });

// package modules
await processes.spawn('yarn', ['run', 'pack'], { cwd: srcDir });
await processes.spawn('yarn', ['run', 'pack'], { cwd: buildDir });

console.timeEnd('cdkbuild');
}, minutes(60));
Expand Down
10 changes: 5 additions & 5 deletions packages/@jsii/integ-test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Readable } from 'stream';
import { createWriteStream } from 'fs';
import { extract } from 'tar';
import * as cp from 'child_process';
import * as https from 'https';
import { IncomingMessage } from 'http';
Expand Down Expand Up @@ -27,7 +27,7 @@ export class ProcessManager {
/**
* kill all still running processes
*
* @param [signal] - signal sent to terminate process
* @param signal sent to terminate process
*/
async killAll(signal?: string) {
const values = Object.values(this.processes);
Expand Down Expand Up @@ -85,11 +85,11 @@ export class ProcessManager {
* write downloaded asset to file
*
* @param source stream
* @param destination of saved file
* @param destination directory for extracted files
*/
export function writeFileStream(source: Readable, destination: string) {
export function extractFileStream(source: Readable, destination: string) {
return new Promise((ok, ko) => {
const destStream = createWriteStream(destination);
const destStream = extract({ cwd: destination });
destStream.once('close', ok);
destStream.once('error', ko);
source.once('error', ko);
Expand Down

0 comments on commit 342020b

Please sign in to comment.