-
Notifications
You must be signed in to change notification settings - Fork 818
/
Copy pathindex.ts
196 lines (174 loc) · 6.26 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { nspawn as spawn, getCLIPath, singleSelect } from '..';
import { EOL } from 'os';
type AmplifyConfiguration = {
accessKeyId: string;
secretAccessKey: string;
profileName?: string;
region?: string;
};
type CommandFlags = 'usage-data-on' | 'usage-data-off' | 'share-project-config-on' | 'share-project-config-off';
const commandFlagsReturnMessage: { [key in CommandFlags]: string } = {
'usage-data-on': 'Usage Data has been turned on',
'usage-data-off': 'Usage Data has been turned off',
'share-project-config-on': 'Share Project Config has been turned on',
'share-project-config-off': 'Share Project Config has been turned off',
};
const defaultSettings = {
profileName: 'amplify-integ-test-user',
region: 'us-east-2',
userName: EOL,
};
export const amplifyRegions = [
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2',
'eu-north-1',
'eu-south-1',
'eu-west-1',
'eu-west-2',
'eu-west-3',
'eu-central-1',
'ap-east-1',
'ap-northeast-1',
'ap-northeast-2',
'ap-northeast-3',
'ap-southeast-1',
'ap-southeast-2',
'ap-south-1',
'ca-central-1',
'me-south-1',
'sa-east-1',
];
const configurationOptions = ['Project information', 'AWS Profile setting', 'Advanced: Container-based deployments'];
const profileOptions = ['No', 'Update AWS Profile', 'Remove AWS Profile'];
const authenticationOptions = ['AWS profile', 'AWS access keys'];
const MANDATORY_PARAMS = ['accessKeyId', 'secretAccessKey', 'region'];
export function amplifyConfigure(cwd: string | null, settings: AmplifyConfiguration | CommandFlags | null): Promise<void> {
if (typeof settings === 'string') {
return spawn(getCLIPath(), ['configure', `--${settings}`], { cwd, stripColors: true })
.wait(commandFlagsReturnMessage[settings])
.runAsync();
} else {
const allSettings = { ...defaultSettings, ...settings };
const missingParam = MANDATORY_PARAMS.filter((p) => !Object.keys(allSettings).includes(p));
if (missingParam.length) {
throw new Error(`mandatory params ${missingParam.join(' ')} are missing`);
}
return new Promise((resolve, reject) => {
const chain = spawn(getCLIPath(), ['configure'], { cwd, stripColors: true })
.wait('Sign in to your AWS administrator account:')
.wait('Press Enter to continue')
.sendCarriageReturn()
.wait('Specify the AWS Region');
singleSelect(chain, allSettings.region, amplifyRegions);
chain
.wait('Press Enter to continue')
.sendCarriageReturn()
.wait('accessKeyId')
.pauseRecording()
.sendLine(allSettings.accessKeyId)
.wait('secretAccessKey')
.sendLine(allSettings.secretAccessKey)
.resumeRecording()
.wait('Profile Name:')
.sendLine(allSettings.profileName)
.wait('Successfully set up the new user.')
.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
}
}
export const amplifyConfigureBeforeOrAtV10_7 = (settings: AmplifyConfiguration): Promise<void> => {
const s = { ...defaultSettings, ...settings };
const missingParam = MANDATORY_PARAMS.filter((p) => !Object.keys(s).includes(p));
if (missingParam.length) {
throw new Error(`mandatory params ${missingParam.join(' ')} are missing`);
}
return new Promise((resolve, reject) => {
const chain = spawn(getCLIPath(), ['configure'], { stripColors: true })
.wait('Sign in to your AWS administrator account:')
.wait('Press Enter to continue')
.sendCarriageReturn()
.wait('Specify the AWS Region');
singleSelect(chain, s.region, amplifyRegions);
chain
.wait('user name:')
.sendCarriageReturn()
.wait('Press Enter to continue')
.sendCarriageReturn()
.wait('accessKeyId')
.pauseRecording()
.sendLine(s.accessKeyId)
.wait('secretAccessKey')
.sendLine(s.secretAccessKey)
.resumeRecording()
.wait('Profile Name:')
.sendLine(s.profileName)
.wait('Successfully set up the new user.')
.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
};
// TODO amplify admin enabled case
export function amplifyConfigureProject(settings: {
cwd: string;
enableContainers?: boolean;
configLevel?: string;
profileOption?: string;
authenticationOption?: string;
region?: string;
}): Promise<void> {
const {
cwd,
enableContainers = false,
profileOption = profileOptions[0],
authenticationOption,
configLevel = 'project',
region = defaultSettings.region,
} = settings;
return new Promise((resolve, reject) => {
const chain = spawn(getCLIPath(), ['configure', 'project'], { cwd, stripColors: true }).wait('Which setting do you want to configure?');
if (enableContainers) {
singleSelect(chain, configurationOptions[2], configurationOptions);
chain.wait('Do you want to enable container-based deployments?').sendConfirmYes();
} else {
singleSelect(chain, configurationOptions[1], configurationOptions);
if (configLevel === 'project') {
chain.wait('Do you want to update or remove the project level AWS profile?');
singleSelect(chain, profileOption, profileOptions);
} else {
chain.wait('Do you want to set the project level configuration').sendConfirmYes();
}
if (profileOption === profileOptions[1] || configLevel === 'general') {
chain.wait('Select the authentication method you want to use:');
singleSelect(chain, authenticationOption, authenticationOptions);
if (authenticationOption === authenticationOptions[0]) {
chain.wait('Please choose the profile you want to use').sendCarriageReturn(); // Default profile
} else if (authenticationOption === authenticationOptions[1]) {
chain.wait('accessKeyId:').sendLine(process.env.AWS_ACCESS_KEY_ID);
chain.wait('secretAccessKey:').sendLine(process.env.AWS_SECRET_ACCESS_KEY);
chain.wait('region:');
singleSelect(chain, region, amplifyRegions);
}
}
}
chain.wait('Successfully made configuration changes to your project.').run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
}