From 1e76dd64dd478f5fd1446b2f5f53ca8fdada2bb9 Mon Sep 17 00:00:00 2001 From: Mitchell Valine Date: Tue, 28 Jan 2020 17:34:33 -0800 Subject: [PATCH] Use tar Module and Cleanup 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. --- buildspec.yaml | 1 - packages/@jsii/integ-test/package.json | 1 + .../@jsii/integ-test/test/build-cdk.test.ts | 36 +++++++------------ packages/@jsii/integ-test/utils/index.ts | 10 +++--- 4 files changed, 18 insertions(+), 30 deletions(-) diff --git a/buildspec.yaml b/buildspec.yaml index dbeddf51dd..6eb3988768 100644 --- a/buildspec.yaml +++ b/buildspec.yaml @@ -12,7 +12,6 @@ phases: build: commands: - yarn build && yarn test - - yarn test:integ post_build: commands: - '[ ${CODEBUILD_BUILD_SUCCEEDING} = 1 ] && yarn package' diff --git a/packages/@jsii/integ-test/package.json b/packages/@jsii/integ-test/package.json index 3e0d37c309..462fb64215 100644 --- a/packages/@jsii/integ-test/package.json +++ b/packages/@jsii/integ-test/package.json @@ -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": { diff --git a/packages/@jsii/integ-test/test/build-cdk.test.ts b/packages/@jsii/integ-test/test/build-cdk.test.ts index 141f8ac00f..ca354e92ba 100644 --- a/packages/@jsii/integ-test/test/build-cdk.test.ts +++ b/packages/@jsii/integ-test/test/build-cdk.test.ts @@ -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)); diff --git a/packages/@jsii/integ-test/utils/index.ts b/packages/@jsii/integ-test/utils/index.ts index 69a6e4062f..4e054354a7 100644 --- a/packages/@jsii/integ-test/utils/index.ts +++ b/packages/@jsii/integ-test/utils/index.ts @@ -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);