Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
MrArnoldPalmer committed Jan 29, 2020
1 parent 21d6d20 commit 1e76dd6
Showing 4 changed files with 18 additions and 30 deletions.
1 change: 0 additions & 1 deletion buildspec.yaml
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@ phases:
build:
commands:
- yarn build && yarn test
- yarn test:integ
post_build:
commands:
- '[ ${CODEBUILD_BUILD_SUCCEEDING} = 1 ] && yarn package'
1 change: 1 addition & 0 deletions packages/@jsii/integ-test/package.json
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
"jest": "^25.1.0",
"jsii": "^0.21.2",
"jsii-pacmak": "^0.21.2",
"tar": "^6.0.1",
"typescript": "~3.7.5"
},
"jest": {
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();
@@ -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 () => {
@@ -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));
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';
@@ -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);
@@ -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);

0 comments on commit 1e76dd6

Please sign in to comment.