From 05025bcd11531fd124709c7b7d68127b35b89bad Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Thu, 18 Jun 2020 09:26:41 +0200 Subject: [PATCH] fs-extra --- package.json | 2 ++ packages/@aws-cdk/core/lib/asset-staging.ts | 8 +++----- packages/@aws-cdk/core/package.json | 2 ++ packages/@aws-cdk/core/test/test.staging.ts | 11 ++++++++++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index e1387a4881b48..a1718715be9cf 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,8 @@ "@aws-cdk/cloud-assembly-schema/semver/**", "@aws-cdk/cloudformation-include/yaml", "@aws-cdk/cloudformation-include/yaml/**", + "@aws-cdk/core/fs-extra", + "@aws-cdk/core/fs-extra/**", "@aws-cdk/core/minimatch", "@aws-cdk/core/minimatch/**", "@aws-cdk/cx-api/semver", diff --git a/packages/@aws-cdk/core/lib/asset-staging.ts b/packages/@aws-cdk/core/lib/asset-staging.ts index c169f070841e3..89e129d56d3b6 100644 --- a/packages/@aws-cdk/core/lib/asset-staging.ts +++ b/packages/@aws-cdk/core/lib/asset-staging.ts @@ -1,6 +1,6 @@ import * as cxapi from '@aws-cdk/cx-api'; import * as crypto from 'crypto'; -import * as fs from 'fs'; +import * as fs from 'fs-extra'; import * as os from 'os'; import * as path from 'path'; import { AssetHashType, AssetOptions } from './assets'; @@ -121,7 +121,7 @@ export class AssetStaging extends Construct { // Asset has been bundled if (this.bundleDir) { // Rename bundling directory to staging directory - fs.renameSync(this.bundleDir, targetPath); + fs.moveSync(this.bundleDir, targetPath); return; } @@ -140,9 +140,7 @@ export class AssetStaging extends Construct { private bundle(options: BundlingOptions): string { // Temp staging directory in the working directory const stagingTmp = path.join('.', STAGING_TMP); - if (!fs.existsSync(stagingTmp)) { - fs.mkdirSync(stagingTmp); - } + fs.ensureDirSync(stagingTmp); // Create temp directory for bundling inside the temp staging directory const bundleDir = fs.mkdtempSync(path.join(stagingTmp, 'asset-bundle-')); diff --git a/packages/@aws-cdk/core/package.json b/packages/@aws-cdk/core/package.json index a81128bfc2702..0c99c177e2027 100644 --- a/packages/@aws-cdk/core/package.json +++ b/packages/@aws-cdk/core/package.json @@ -166,6 +166,7 @@ "ts-mock-imports": "^1.3.0" }, "dependencies": { + "fs-extra": "^9.0.1", "minimatch": "^3.0.4", "@aws-cdk/cx-api": "0.0.0", "@aws-cdk/cdk-assets-schema": "0.0.0", @@ -173,6 +174,7 @@ "constructs": "^3.0.2" }, "bundledDependencies": [ + "fs-extra", "minimatch" ], "homepage": "https://github.com/aws/aws-cdk", diff --git a/packages/@aws-cdk/core/test/test.staging.ts b/packages/@aws-cdk/core/test/test.staging.ts index 08e20fa0fbad0..81766f8a34879 100644 --- a/packages/@aws-cdk/core/test/test.staging.ts +++ b/packages/@aws-cdk/core/test/test.staging.ts @@ -1,8 +1,9 @@ import * as cxapi from '@aws-cdk/cx-api'; -import * as fs from 'fs'; +import * as fs from 'fs-extra'; import { Test } from 'nodeunit'; import * as os from 'os'; import * as path from 'path'; +import * as sinon from 'sinon'; import { App, AssetHashType, AssetStaging, BundlingDockerImage, Stack } from '../lib'; const STUB_INPUT_FILE = '/tmp/docker-stub.input'; @@ -26,6 +27,7 @@ export = { fs.unlinkSync(STUB_INPUT_FILE); } cb(); + sinon.restore(); }, 'base case'(test: Test) { @@ -103,6 +105,8 @@ export = { const app = new App(); const stack = new Stack(app, 'stack'); const directory = path.join(__dirname, 'fs', 'fixtures', 'test1'); + const ensureDirSyncSpy = sinon.spy(fs, 'ensureDirSync'); + const mkdtempSyncSpy = sinon.spy(fs, 'mkdtempSync'); // WHEN new AssetStaging(stack, 'Asset', { @@ -127,6 +131,11 @@ export = { 'tree.json', ]); + // asset is bundled in a directory inside .cdk.staging + const stagingTmp = path.join('.', '.cdk.staging'); + test.ok(ensureDirSyncSpy.calledWith(stagingTmp)); + test.ok(mkdtempSyncSpy.calledWith(sinon.match(path.join(stagingTmp, 'asset-bundle-')))); + test.done(); },