Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 7 #292

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 1 addition & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:
You can use NPM instead of YARN (Up to you)

### `yarn start` OR `npm run start`

Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br />
You will also see any lint errors in the console.

### `yarn test` OR `npm run test`

Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `yarn build` OR `npm run build`

Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `yarn eject` OR `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
### Front-end AWS
16,015 changes: 16,015 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

39 changes: 26 additions & 13 deletions package.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,46 @@
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"client:deploy": "sls client deploy --no-config-change --no-policy-change --no-cors-change",
"client:deploy:nc": "npm run client:deploy -- --no-confirm",
"client:build:deploy": "npm run build && npm run client:deploy",
"client:build:deploy:nc": "npm run build && npm run client:deploy:nc",
"cloudfront:setup": "sls deploy",
"cloudfront:domainInfo": "sls domainInfo",
"cloudfront:invalidateCache": "sls invalidateCloudFrontCache",
"cloudfront:build:deploy": "npm run client:build:deploy && npm run cloudfront:invalidateCache",
"cloudfront:build:deploy:nc": "npm run client:build:deploy:nc && npm run cloudfront:invalidateCache",
"cloudfront:update:build:deploy": "npm run cloudfront:setup && npm run cloudfront:build:deploy",
"cloudfront:update:build:deploy:nc": "npm run cloudfront:setup && npm run cloudfront:build:deploy:nc"
},
"dependencies": {
"axios": "^0.19.2",
"formik": "^2.1.5",
"formik-material-ui": "^2.0.1",
"yup": "^0.29.1",
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
"@reduxjs/toolkit": "^1.2.5",
"@types/lodash": "^4.14.158",
"@types/node": "^12.0.0",
"@types/react": "^16.9.43",
"@types/react-dom": "^16.9.8",
"@types/react-redux": "^7.1.7",
"@types/react-router-dom": "^5.1.5",
"@types/yup": "^0.29.3",
"axios": "^0.19.2",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"enzyme-to-json": "^3.4.4",
"formik": "^2.1.5",
"formik-material-ui": "^2.0.1",
"lodash": "^4.17.19",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-redux": "^7.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.1",
"serverless-finch": "^2.6.0",
"typescript": "~3.7.2",
"@reduxjs/toolkit": "^1.2.5",
"react-redux": "^7.2.0",
"@types/react-redux": "^7.1.7",
"@types/lodash": "^4.14.158",
"lodash": "^4.17.19",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"enzyme-to-json": "^3.4.4"
"yup": "^0.29.1",
"serverless-single-page-app-plugin": "file:./serverless-single-page-app-plugin"
},
"devDependencies": {
"@testing-library/jest-dom": "^4.2.4",
Expand Down
150 changes: 150 additions & 0 deletions serverless-single-page-app-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
'use strict';

const spawnSync = require('child_process').spawnSync;

class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.commands = {
syncToS3: {
usage: 'Deploys the `app` directory to your bucket',
lifecycleEvents: [
'sync',
],
},
domainInfo: {
usage: 'Fetches and prints out the deployed CloudFront domain names',
lifecycleEvents: [
'domainInfo',
],
},
invalidateCloudFrontCache: {
usage: 'Invalidates CloudFront cache',
lifecycleEvents: [
'invalidateCache',
],
},
};

this.hooks = {
'syncToS3:sync': this.syncDirectory.bind(this),
'domainInfo:domainInfo': this.domainInfo.bind(this),
'invalidateCloudFrontCache:invalidateCache': this.invalidateCache.bind(
this,
),
};
}

runAwsCommand(args) {
let command = 'aws';
if (this.serverless.variables.service.provider.region) {
command = `${command} --region ${this.serverless.variables.service.provider.region}`;
}
if (this.serverless.variables.service.provider.profile) {
command = `${command} --profile ${this.serverless.variables.service.provider.profile}`;
}
const result = spawnSync(command, args);
const stdout = typeof result.stdout === 'string' ? result.stdout : result.stdout && result.stdout.toString();
const sterr = typeof result.stderr === 'string' ? result.stderr : result.stderr && result.stderr.toString();
if (stdout) {
this.serverless.cli.log(stdout);
}
if (sterr) {
this.serverless.cli.log(sterr);
}

return { stdout, sterr };
}

// syncs the `app` directory to the provided bucket
syncDirectory() {
const s3Bucket = this.serverless.variables.service.custom.s3Bucket;
const args = [
's3',
'sync',
'app/',
`s3://${s3Bucket}/`,
'--delete',
];
const { sterr } = this.runAwsCommand(args);
if (!sterr) {
this.serverless.cli.log('Successfully synced to the S3 bucket');
} else {
throw new Error('Failed syncing to the S3 bucket');
}
}

// fetches the domain name from the CloudFront outputs and prints it out
async domainInfo() {
const provider = this.serverless.getProvider('aws');
const stackName = provider.naming.getStackName(this.options.stage);
const result = await provider.request(
'CloudFormation',
'describeStacks',
{ StackName: stackName },
this.options.stage,
this.options.region,
);

const outputs = result.Stacks[0].Outputs;
const output = outputs.find(
entry => entry.OutputKey === 'WebAppCloudFrontDistributionOutput',
);

if (output && output.OutputValue) {
this.serverless.cli.log(`Web App Domain: ${output.OutputValue}`);
return output.OutputValue;
}

this.serverless.cli.log('Web App Domain: Not Found');
const error = new Error('Could not extract Web App Domain');
throw error;
}

async invalidateCache() {
const provider = this.serverless.getProvider('aws');

const domain = await this.domainInfo();

const result = await provider.request(
'CloudFront',
'listDistributions',
{},
this.options.stage,
this.options.region,
);

const distributions = result.DistributionList.Items;
const distribution = distributions.find(
entry => entry.DomainName === domain,
);

if (distribution) {
this.serverless.cli.log(
`Invalidating CloudFront distribution with id: ${distribution.Id}`,
);
const args = [
'cloudfront',
'create-invalidation',
'--distribution-id',
distribution.Id,
'--paths',
'/*',
];
const { sterr } = this.runAwsCommand(args);
if (!sterr) {
this.serverless.cli.log('Successfully invalidated CloudFront cache');
} else {
throw new Error('Failed invalidating CloudFront cache');
}
} else {
const message = `Could not find distribution with domain ${domain}`;
const error = new Error(message);
this.serverless.cli.log(message);
throw error;
}
}
}

module.exports = ServerlessPlugin;
7 changes: 7 additions & 0 deletions serverless-single-page-app-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "serverless-single-page-app-plugin",
"version": "1.0.0",
"description": "A plugin to simplify deploying Single Page Application using S3 and CloudFront",
"author": "",
"license": "MIT"
}
Loading