Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
fix: fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
arantespp committed Feb 22, 2021
1 parent 64b6b93 commit b49e39e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
60 changes: 59 additions & 1 deletion packages/cli/src/deploy/staticApp/staticApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import { CloudFormation } from 'aws-sdk';
import * as faker from 'faker';

import { AWS_DEFAULT_REGION } from '../../config';

const region = AWS_DEFAULT_REGION;

const workingStackName = faker.random.word();

const workingStackBucketName = faker.random.word();
Expand Down Expand Up @@ -42,7 +46,61 @@ jest.mock('aws-sdk', () => ({
})),
}));

import { getStaticAppBucket } from './staticApp';
jest.mock('../cloudFormation', () => ({
...(jest.requireActual('../cloudFormation') as any),
deploy: jest.fn().mockResolvedValue({ Outputs: [] }),
}));

jest.mock('../s3', () => ({
getAllFilesInsideADirectory: jest.fn().mockResolvedValue([]),
uploadDirectoryToS3: jest.fn(),
}));

import * as s3Module from '../s3';

import * as staticAppModule from './staticApp';

const { getStaticAppBucket } = jest.requireActual('./staticApp');

describe('Fixes #23 https://github.com/ttoss/carlin/issues/23', () => {
(staticAppModule.getStaticAppBucket as jest.Mock) = jest
.fn()
.mockResolvedValue(workingStackBucketName);

const buildFolder = faker.random.word();

test('uploadDirectoryToS3 bucket key must be undefined if cloudfront is false', async () => {
const cloudfront = false;

await staticAppModule.deployStaticApp({
buildFolder,
cloudfront,
region,
});

expect(s3Module.uploadDirectoryToS3).toHaveBeenCalledWith(
expect.objectContaining({
bucketKey: undefined,
}),
);
});

test('uploadDirectoryToS3 bucket key must not be undefined if cloudfront is true', async () => {
const cloudfront = true;

await staticAppModule.deployStaticApp({
buildFolder,
cloudfront,
region,
});

expect(s3Module.uploadDirectoryToS3).toHaveBeenCalledWith(
expect.objectContaining({
bucketKey: expect.any(String),
}),
);
});
});

describe('Fixes #22 https://github.com/ttoss/carlin/issues/22', () => {
test('should return the bucket name', async () => {
Expand Down
18 changes: 10 additions & 8 deletions packages/cli/src/deploy/staticApp/staticApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ const findDefaultBuildFolder = async () => {
export const uploadBuiltAppToS3 = async ({
buildFolder: directory,
bucket,
cloudfront,
}: {
buildFolder?: string;
bucket: string;
cloudfront?: boolean;
}) => {
const version = getPackageVersion();
const version = cloudfront ? getPackageVersion() : undefined;

/**
* Only empty directory if the number of the files inside $directory.
Expand Down Expand Up @@ -187,10 +189,10 @@ export const deployStaticApp = async ({
acm?: string;
aliases?: string[];
buildFolder?: string;
cloudfront: boolean;
cloudfront?: boolean;
gtmId?: string;
csp?: CSP;
spa: boolean;
spa?: boolean;
hostedZoneName?: string;
region: string;
skipUpload?: boolean;
Expand Down Expand Up @@ -222,7 +224,7 @@ export const deployStaticApp = async ({
*/
if (bucket) {
if (!skipUpload) {
await uploadBuiltAppToS3({ buildFolder, bucket });
await uploadBuiltAppToS3({ buildFolder, bucket, cloudfront });
}

const { Outputs } = await deploy({ params, template });
Expand All @@ -238,14 +240,14 @@ export const deployStaticApp = async ({
const newBucket = await getStaticAppBucket({ stackName });

if (!newBucket) {
throw new Error(`Cannot find bucket at ${stackName}`);
throw new Error(`Cannot find bucket at ${stackName}.`);
}

await uploadBuiltAppToS3({ buildFolder, bucket: newBucket });
await uploadBuiltAppToS3({ buildFolder, bucket: newBucket, cloudfront });
}
} catch (err) {
log.error(logPrefix, 'An error occurred. Cannot deploy static app');
log.error(logPrefix, 'An error occurred. Cannot deploy static app.');
log.error(logPrefix, 'Error message: %j', err.message);
process.exit();
process.exit(1);
}
};

0 comments on commit b49e39e

Please sign in to comment.