-
Notifications
You must be signed in to change notification settings - Fork 28
/
stackOutput.js
50 lines (41 loc) · 1.35 KB
/
stackOutput.js
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
const path = require('path');
const fs = require('fs-extra');
const dotenv = require('dotenv');
const os = require('os');
const replaceInEnvFile = async (file, envs) => {
const keys = Object.keys(envs);
if (keys.length <= 0) {
return;
}
const envFile = path.join(__dirname, '../frontend', file);
await fs.ensureFile(envFile);
const content = await fs.readFile(envFile);
const envConfig = await dotenv.parse(content);
keys.forEach(key => {
envConfig[key] = envs[key];
});
await fs.remove(envFile);
await Promise.all(
Object.keys(envConfig).map(key =>
fs.appendFile(envFile, `${key}=${envConfig[key]}${os.EOL}`),
),
);
};
const handler = async (data, serverless, _) => {
//this handler creates the environment for the frontend based on the services deployment output
const { ServiceEndpoint, WebAppCloudFrontDistributionOutput } = data;
if (WebAppCloudFrontDistributionOutput) {
await replaceInEnvFile('.env.production.local', {
REACT_APP_AUTH0_REDIRECT_URI: `https://${WebAppCloudFrontDistributionOutput}/callback`,
});
}
if (ServiceEndpoint) {
const serviceName = serverless.service.serviceObject.name
.replace(/\-/g, '_')
.toUpperCase();
await replaceInEnvFile('.env.local', {
[`REACT_APP_${serviceName}_ENDPOINT`]: ServiceEndpoint,
});
}
};
module.exports = { handler };