2
2
'use strict'
3
3
4
4
const fs = require ( 'fs' )
5
- const inquirer = require ( 'inquirer' )
6
- const packageJson = require ( './package.json' )
5
+ const path = require ( 'path' )
6
+ const AWS = require ( 'aws-sdk' )
7
+ const rootDir = path . resolve ( __dirname , '..' )
8
+ const packageJson = require ( `${ rootDir } /package.json` )
9
+ AWS . config . update ( { region :'us-east-1' } ) //packageJson.primaryAwsRegion
10
+
11
+ const cloudformation = new AWS . CloudFormation ( ) ;
12
+ // const inquirer = require('inquirer')
7
13
const primaryAwsRegion = packageJson . config . primaryAwsRegion
8
14
9
- const questions = [ {
15
+ /* const questions = [{
10
16
name: 'apiGatewayApiId',
11
17
message: 'API ID:',
12
18
type: 'input'
@@ -16,12 +22,12 @@ const questions = [{
16
22
type: 'list',
17
23
choices: ['us-east-1', 'us-west-2'],
18
24
default: 'us-east-1'
19
- }*/ , {
25
+ }* /, {
20
26
name: 'cognitoUserPoolId',
21
27
message: 'Cognito User Pool ID:',
22
28
type: 'input'
23
29
}, {
24
- name : 'cognitoClientId ' ,
30
+ name: 'cognitoUserPoolClientId ',
25
31
message: 'Cognito Client ID:',
26
32
type: 'input'
27
33
}, {
@@ -30,40 +36,56 @@ const questions = [{
30
36
type: 'input'
31
37
}]
32
38
33
- inquirer . prompt ( questions ) . then ( ( answers ) => {
34
- modifyPackageFile ( answers . apiGatewayApiId , primaryAwsRegion , answers . cognitoUserPoolId , answers . cognitoClientId , answers . cognitoIdentityPoolId )
35
- modifyDevPortalJs ( answers . cognitoIdentityPoolId , primaryAwsRegion , primaryAwsRegion , answers . cognitoUserPoolId , answers . cognitoClientId )
36
- modifyApigClient ( answers . apiGatewayApiId , primaryAwsRegion )
39
+ inquirer.prompt(questions).then(answers => {
40
+ */
41
+
42
+ cloudformation . describeStacks ( {
43
+ StackName : 'DevPortalStack' //packageJson.config.cloudFormationStackName
44
+ } )
45
+ . promise ( )
46
+ . then ( data => {
47
+ const stack = data . Stacks [ 0 ]
48
+ const outputs = stack . Outputs
49
+ const apiGatewayApiId = getOutputValue ( outputs , 'ApiId' )
50
+ const cognitoIdentityPoolId = getOutputValue ( outputs , 'CognitoIdentityPoolId' )
51
+ const cognitoUserPoolClientId = getOutputValue ( outputs , 'CognitoUserPoolClientId' )
52
+ const cognitoUserPoolId = getOutputValue ( outputs , 'CognitoUserPoolId' )
53
+ modifyPackageFile ( apiGatewayApiId , primaryAwsRegion , cognitoUserPoolId , cognitoUserPoolClientId , cognitoIdentityPoolId )
54
+ modifyDevPortalJs ( cognitoIdentityPoolId , primaryAwsRegion , primaryAwsRegion , cognitoUserPoolId , cognitoUserPoolClientId )
55
+ modifyApigClient ( apiGatewayApiId , primaryAwsRegion )
37
56
} ) . catch ( e => { console . log ( e ) } )
38
57
39
- function modifyPackageFile ( apiGatewayApiId , cognitoRegion , cognitoUserPoolId , cognitoClientId , cognitoIdentityPoolId ) {
40
- const packageJsonPath = './package.json'
58
+ function getOutputValue ( outputs , key ) {
59
+ return outputs . find ( o => o . OutputKey === key ) . OutputValue
60
+ }
61
+ function modifyPackageFile ( apiGatewayApiId , cognitoRegion , cognitoUserPoolId , cognitoUserPoolClientId , cognitoIdentityPoolId ) {
62
+ const packageJsonPath = `${ rootDir } /package.json`
41
63
const packageJson = fs . readFileSync ( packageJsonPath , 'utf8' )
42
64
const packageJsonModified = packageJson
43
65
. replace ( / Y O U R _ A P I _ G A T E W A Y _ A P I _ I D / g, apiGatewayApiId )
44
66
. replace ( / Y O U R _ C O G N I T O _ R E G I O N / g, cognitoRegion )
45
67
. replace ( / Y O U R _ C O G N I T O _ U S E R _ P O O L _ I D / g, cognitoUserPoolId )
46
- . replace ( / Y O U R _ C O G N I T O _ C L I E N T _ I D / g, cognitoClientId )
68
+ . replace ( / Y O U R _ C O G N I T O _ C L I E N T _ I D / g, cognitoUserPoolClientId )
47
69
. replace ( / Y O U R _ C O G N I T O _ I D E N T I T Y _ P O O L _ I D / g, cognitoIdentityPoolId )
48
70
49
71
fs . writeFileSync ( packageJsonPath , packageJsonModified , 'utf8' )
50
72
}
51
73
52
- function modifyDevPortalJs ( cognitoIdentityPoolId , primaryAwsRegion , cognitoRegion , cognitoUserPoolId , cognitoClientId ) {
53
- const htmlPath = '. /dev-portal/src/services/aws.js'
74
+ function modifyDevPortalJs ( cognitoIdentityPoolId , primaryAwsRegion , cognitoRegion , cognitoUserPoolId , cognitoUserPoolClientId ) {
75
+ const htmlPath = ` ${ rootDir } /dev-portal/src/services/aws.js`
54
76
const html = fs . readFileSync ( htmlPath , 'utf8' )
55
77
const htmlModified = html
56
78
. replace ( / Y O U R _ C O G N I T O _ I D E N T I T Y _ P O O L _ I D / g, cognitoIdentityPoolId )
57
79
. replace ( / Y O U R _ C O G N I T O _ R E G I O N / g, cognitoRegion )
58
80
. replace ( / Y O U R _ P R I M A R Y _ A W S _ R E G I O N / g, primaryAwsRegion )
59
81
. replace ( / Y O U R _ C O G N I T O _ U S E R _ P O O L _ I D / g, cognitoUserPoolId )
60
- . replace ( / Y O U R _ C O G N I T O _ C L I E N T _ I D / g, cognitoClientId )
82
+ . replace ( / Y O U R _ C O G N I T O _ C L I E N T _ I D / g, cognitoUserPoolClientId )
61
83
62
84
fs . writeFileSync ( htmlPath , htmlModified , 'utf8' )
63
85
}
64
86
65
87
function modifyApigClient ( apiGatewayApiId , primaryAwsRegion ) {
66
- const apigClientPath = '. /dev-portal/public/apigateway-js-sdk/apigClient.js'
88
+ const apigClientPath = ` ${ rootDir } /dev-portal/public/apigateway-js-sdk/apigClient.js`
67
89
const apigClient = fs . readFileSync ( apigClientPath , 'utf8' )
68
90
const apigClientModified = apigClient
69
91
. replace ( / Y O U R _ A P I _ G A T E W A Y _ A P I _ I D / g, apiGatewayApiId )
0 commit comments