-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
57 lines (48 loc) · 1.78 KB
/
index.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
51
52
53
54
55
56
57
const spawn = require('react-dev-utils/crossSpawn');
class BuildReactForSyncPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'before:sync:buckets': this.buildReactApp.bind(this),
'after:deploy:deploy': this.buildReactApp.bind(this)
};
}
getStackOutputs() {
const service = this.serverless.service;
const provider = this.serverless.getProvider('aws');
const stage = provider.getStage();
const region = provider.getRegion();
const serviceName = service.getServiceName();
const StackName = serviceName + '-' + stage;
const toUnderscoreCase = s =>
s.replace(/\.?([A-Z]+)/g, (x,y) => ('_' + y)).replace(/^_/, '').toUpperCase();
return provider.request('CloudFormation', 'describeStacks', { StackName }, stage, region)
.then(({ Stacks: [stack] }) => stack.Outputs)
.then(outputs =>
outputs.reduce((env, output) =>
Object.assign(env, { [toUnderscoreCase(output.OutputKey)]: output.OutputValue }),
{})
)
}
buildReactApp() {
return this.getStackOutputs().then(outputs => {
const command = [];
const env = { ...process.env, ...outputs };
const buildopts =
this.serverless.service.custom &&
this.serverless.service.custom['build-create-react-app'];
if (buildopts) {
command.push(require.resolve(buildopts.command + '/scripts/build'));
if (buildopts.params) {
command.push(...buildopts.params);
}
} else {
command.push(require.resolve('react-scripts/scripts/build'));
}
const result = spawn.sync('node', command, {stdio: 'inherit', env});
return Promise.resolve(result.status);
});
}
}
module.exports = BuildReactForSyncPlugin;