Skip to content

Commit 2fde6d4

Browse files
author
Andrews
committed
add scripts dir
1 parent 95b0387 commit 2fde6d4

File tree

5 files changed

+125
-18
lines changed

5 files changed

+125
-18
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
"package": "aws cloudformation package --template cloudformation/base.yaml --s3-bucket $npm_package_config_artifactsS3Bucket --output-template packaged-sam.yaml --region $npm_package_config_primaryAwsRegion",
2525
"deploy": "aws cloudformation deploy --template-file packaged-sam.yaml --stack-name $npm_package_config_cloudFormationStackName --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM --parameter-overrides ArtifactsS3Bucket=$npm_package_config_artifactsS3Bucket DevPortalSiteS3Bucket=$npm_package_config_siteS3Bucket MarketplaceSubscriptionTopic=$npm_package_config_marketplaceSubscriptionTopic CognitoIdentityPoolName=$npm_package_config_identityPoolName DevPortalCustomersTableName=$npm_package_config_customersTableName --region $npm_package_config_primaryAwsRegion",
2626
"package-deploy": "npm run package && npm run deploy",
27-
"pre-config": "node pre-configure.js",
28-
"post-config": "node post-configure.js",
29-
"deconfig": "node deconfigure.js",
27+
"config": "node scripts/configure.js",
28+
"pre-config": "node scripts/pre-configure.js",
29+
"post-config": "node scripts/post-configure.js",
30+
"deconfig": "node scripts/deconfigure.js",
3031
"create-artifacts-bucket": "aws s3 mb s3://$npm_package_config_artifactsS3Bucket --region $npm_package_config_primaryAwsRegion",
3132
"delete-artifacts-bucket": "aws s3 rm s3://$npm_package_config_artifactsS3Bucket/lambda-function.zip --region $npm_package_config_primaryAwsRegion; aws s3 rm s3://$npm_package_config_artifactsS3Bucket/dev-portal-express-proxy-api.yaml --region $npm_package_config_primaryAwsRegion; aws s3 rb s3://$npm_package_config_artifactsS3Bucket --region $npm_package_config_primaryAwsRegion",
3233
"upload-site": "cd ./dev-portal && npm run build && aws s3 sync ./build s3://$npm_package_config_siteS3Bucket --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers --region $npm_package_config_primaryAwsRegion && cd ..",

scripts/configure.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env node
2+
'use strict'
3+
4+
const fs = require('fs')
5+
const inquirer = require('inquirer')
6+
const deconfigure = require('./deconfigure')
7+
console.log(deconfigure)
8+
deconfigure()
9+
const questions = [{
10+
name: 'accountId',
11+
message: 'AWS account id:',
12+
type: 'input',
13+
validate: value => value.length === 12
14+
}, {
15+
name: 'primaryAwsRegion',
16+
message: 'Region:',
17+
type: 'list',
18+
choices: ['us-east-1', 'us-west-2', 'eu-west-1', 'eu-central-1', 'ap-northeast-1', 'ap-northeast-2'],
19+
default: 'us-east-1'
20+
}, {
21+
name: 'artifactsS3BucketName',
22+
message: 'S3 bucket for artifacts such as Lambda package and Swagger file (will be created if it doesn\'t exist):',
23+
type: 'input',
24+
validate: value => /^[a-zA-Z0-9.\-_]{1,255}/.test(value)
25+
}, {
26+
name: 'clientS3BucketName',
27+
message: 'S3 bucket for website (will be created; do not provide existing bucket):',
28+
type: 'input',
29+
validate: value => /^[a-zA-Z0-9.\-_]{1,255}/.test(value)
30+
}, {
31+
name: 'cloudFormationStackName',
32+
message: 'CloudFormation stack name:',
33+
type: 'input',
34+
default: 'DevPortalStack',
35+
validate: value => /^[a-zA-Z][a-zA-Z0-9\-]*/.test(value)
36+
}, {
37+
name: 'apiGatewayApiName',
38+
message: 'API name:',
39+
type: 'input',
40+
default: 'Developer Portal'
41+
}/*, {
42+
name: 'expressLambdaFunctionName',
43+
message: 'Lambda function name:',
44+
type: 'input',
45+
default: 'DevPortalFunction'
46+
}*/]
47+
48+
inquirer.prompt(questions).then((answers) => {
49+
deconfigure()
50+
modifyPackageFile(answers.artifactsS3BucketName, answers.clientS3BucketName, answers.primaryAwsRegion, answers.apiGatewayApiName, answers.cloudFormationStackName/*, answers.expressLambdaFunctionName*/, answers.accountId)
51+
modifyUiPackageFile(answers.clientS3BucketName, answers.primaryAwsRegion)
52+
modifyExpressServer(answers.clientS3BucketName, answers.primaryAwsRegion)
53+
modifySwaggerFile(answers.accountId, answers.primaryAwsRegion, answers.apiGatewayApiName/*, answers.expressLambdaFunctionName*/)
54+
}).catch(e => {console.log(e)})
55+
56+
function modifyPackageFile(artifactsS3BucketName, clientS3BucketName, primaryAwsRegion, apiGatewayApiName, cloudFormationStackName/*, expressLambdaFunctionName*/, accountId) {
57+
const packageJsonPath = './package.json'
58+
const packageJson = fs.readFileSync(packageJsonPath, 'utf8')
59+
const packageJsonModified = packageJson
60+
.replace(/YOUR_ARTIFACTS_BUCKET_NAME/g, artifactsS3BucketName)
61+
.replace(/YOUR_CLIENT_BUCKET_NAME/g, clientS3BucketName)
62+
.replace(/YOUR_API_GATEWAY_API_NAME/g, apiGatewayApiName)
63+
// .replace(/YOUR_LAMBDA_FUNCTION_NAME/g, expressLambdaFunctionName)
64+
.replace(/YOUR_ACCOUNT_ID/g, accountId)
65+
.replace(/YOUR_CLOUDFORMATION_STACK_NAME/g, cloudFormationStackName)
66+
.replace(/YOUR_PRIMARY_AWS_REGION/g, primaryAwsRegion)
67+
68+
fs.writeFileSync(packageJsonPath, packageJsonModified, 'utf8')
69+
}
70+
71+
function modifyUiPackageFile(clientS3BucketName, primaryAwsRegion) {
72+
const packageJsonPath = './dev-portal/package.json'
73+
const packageJson = fs.readFileSync(packageJsonPath, 'utf8')
74+
const packageJsonModified = packageJson
75+
.replace(/YOUR_CLIENT_BUCKET_NAME/g, clientS3BucketName)
76+
.replace(/YOUR_PRIMARY_AWS_REGION/g, primaryAwsRegion)
77+
78+
fs.writeFileSync(packageJsonPath, packageJsonModified, 'utf8')
79+
}
80+
81+
function modifyExpressServer(clientS3BucketName, primaryAwsRegion) {
82+
const expressServerPath = './lambdas/backend/express-server.js'
83+
const expressServer = fs.readFileSync(expressServerPath, 'utf8')
84+
const expressServerModified = expressServer
85+
.replace(/YOUR_CLIENT_BUCKET_NAME/g, clientS3BucketName)
86+
.replace(/YOUR_PRIMARY_AWS_REGION/g, primaryAwsRegion)
87+
88+
fs.writeFileSync(expressServerPath, expressServerModified, 'utf8')
89+
}
90+
91+
function modifySwaggerFile(accountId, primaryAwsRegion, apiGatewayApiName/*, expressLambdaFunctionName*/) {
92+
const swaggerDefinitionPath = './lambdas/backend/dev-portal-express-proxy-api.yaml'
93+
const swaggerDefinition = fs.readFileSync(swaggerDefinitionPath, 'utf8')
94+
const simpleProxyApiModified = swaggerDefinition
95+
.replace(/YOUR_ACCOUNT_ID/g, accountId)
96+
// .replace(/YOUR_LAMBDA_FUNCTION_NAME/g, expressLambdaFunctionName)
97+
.replace(/YOUR_API_GATEWAY_API_NAME/g, apiGatewayApiName)
98+
.replace(/YOUR_PRIMARY_AWS_REGION/g, primaryAwsRegion)
99+
100+
fs.writeFileSync(swaggerDefinitionPath, simpleProxyApiModified, 'utf8')
101+
}

deconfigure.js renamed to scripts/deconfigure.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66
'use strict'
77

88
const fs = require('fs')
9-
const packageJson = require('./package.json')
9+
const path = require('path')
10+
11+
const rootDir = path.resolve(__dirname, '..')
12+
const packageJson = require(`${rootDir}/package.json`)
1013
const config = packageJson.config
1114

12-
modifyApigClient(config.apiGatewayApiId, config.primaryAwsRegion)
13-
modifyDevPortalJs(config.cognitoIdentityPoolId, config.primaryAwsRegion, config.cognitoRegion, config.cognitoUserPoolId, config.cognitoClientId)
14-
modifySwaggerFile(config.accountId, config.primaryAwsRegion, config.apiGatewayApiName)
15-
modifyExpressServer(config.siteS3Bucket, config.primaryAwsRegion)
16-
modifyCloudFormation(config.cognitoIdentityPoolId)
17-
modifyPackageFile(config)
18-
modifyUiPackageFile(config.siteS3Bucket, config.primaryAwsRegion)
15+
module.exports = function() {
16+
modifyApigClient(config.apiGatewayApiId, config.primaryAwsRegion)
17+
modifyDevPortalJs(config.cognitoIdentityPoolId, config.primaryAwsRegion, config.cognitoRegion, config.cognitoUserPoolId, config.cognitoClientId)
18+
modifySwaggerFile(config.accountId, config.primaryAwsRegion, config.apiGatewayApiName)
19+
modifyExpressServer(config.siteS3Bucket, config.primaryAwsRegion)
20+
modifyCloudFormation(config.cognitoIdentityPoolId)
21+
modifyPackageFile(config)
22+
modifyUiPackageFile(config.siteS3Bucket, config.primaryAwsRegion)
23+
}
1924

2025
function modifyApigClient(apiGatewayApiId, primaryAwsRegion) {
21-
const apigClientPath = './dev-portal/public/apigateway-js-sdk/apigClient.js'
26+
const apigClientPath = `${rootDir}/dev-portal/public/apigateway-js-sdk/apigClient.js`
2227
const apigClient = fs.readFileSync(apigClientPath, 'utf8')
2328
const apiGatewayApiIdRegex = new RegExp(apiGatewayApiId, 'g')
2429
const primaryAwsRegionRegex = new RegExp(primaryAwsRegion, 'g')
@@ -30,7 +35,7 @@ function modifyApigClient(apiGatewayApiId, primaryAwsRegion) {
3035
}
3136

3237
function modifyDevPortalJs(cognitoIdentityPoolId, primaryAwsRegion, cognitoRegion, cognitoUserPoolId, cognitoClientId) {
33-
const htmlPath = './dev-portal/src/services/aws.js'
38+
const htmlPath = `${rootDir}/dev-portal/src/services/aws.js`
3439
const html = fs.readFileSync(htmlPath, 'utf8')
3540
const cognitoIdentityPoolIdRegex = new RegExp(cognitoIdentityPoolId, 'g')
3641
const cognitoRegionRegex = new RegExp(`const cognitoRegion = '${cognitoRegion}'`, 'g')
@@ -48,7 +53,7 @@ function modifyDevPortalJs(cognitoIdentityPoolId, primaryAwsRegion, cognitoRegio
4853
}
4954

5055
function modifySwaggerFile(accountId, primaryAwsRegion, apiGatewayApiName) {
51-
const swaggerDefinitionPath = './lambdas/backend/dev-portal-express-proxy-api.yaml'
56+
const swaggerDefinitionPath = `${rootDir}/lambdas/backend/dev-portal-express-proxy-api.yaml`
5257
const swaggerDefinition = fs.readFileSync(swaggerDefinitionPath, 'utf8')
5358
const accountIdRegex = new RegExp(accountId, 'g')
5459
const apiGatewayApiNameRegex = new RegExp(apiGatewayApiName, 'g')
@@ -62,7 +67,7 @@ function modifySwaggerFile(accountId, primaryAwsRegion, apiGatewayApiName) {
6267
}
6368

6469
function modifyExpressServer(siteS3Bucket, primaryAwsRegion) {
65-
const expressServerPath = './lambdas/backend/express-server.js'
70+
const expressServerPath = `${rootDir}/lambdas/backend/express-server.js`
6671
const expressServer = fs.readFileSync(expressServerPath, 'utf8')
6772
const siteS3BucketRegex = new RegExp(siteS3Bucket, 'g')
6873
const primaryAwsRegionRegex = new RegExp(primaryAwsRegion, 'g')
@@ -74,7 +79,7 @@ function modifyExpressServer(siteS3Bucket, primaryAwsRegion) {
7479
}
7580

7681
function modifyCloudFormation(cognitoIdentityPoolId) {
77-
const cloudFormationPath = './cloudformation/base.yaml'
82+
const cloudFormationPath = `${rootDir}/cloudformation/base.yaml`
7883
const cloudFormation = fs.readFileSync(cloudFormationPath, 'utf8')
7984
const cognitoIdentityPoolIdRegex = new RegExp(cognitoIdentityPoolId, 'g')
8085
const cloudFormationModified = cloudFormation
@@ -84,7 +89,7 @@ function modifyCloudFormation(cognitoIdentityPoolId) {
8489
}
8590

8691
function modifyPackageFile(config) {
87-
const packageJsonPath = './package.json'
92+
const packageJsonPath = `${rootDir}/package.json`
8893
const packageJson = fs.readFileSync(packageJsonPath, 'utf8')
8994
const artifactsS3BucketRegex = new RegExp(`"artifactsS3Bucket": "${config.artifactsS3Bucket}"`, 'g')
9095
const siteS3BucketRegex = new RegExp(config.siteS3Bucket, 'g')
@@ -114,7 +119,7 @@ function modifyPackageFile(config) {
114119
}
115120

116121
function modifyUiPackageFile(siteS3Bucket, primaryAwsRegion) {
117-
const packageJsonPath = './dev-portal/package.json'
122+
const packageJsonPath = `${rootDir}/dev-portal/package.json`
118123
const packageJson = fs.readFileSync(packageJsonPath, 'utf8')
119124
const primaryAwsRegionRegex = new RegExp(primaryAwsRegion, 'g')
120125
const siteS3BucketRegex = new RegExp(siteS3Bucket, 'g')
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)