-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner-helpers.js
203 lines (173 loc) · 6.46 KB
/
runner-helpers.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
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
197
198
199
200
201
202
const helpers = require('./config-helpers');
const getWebEnvVars = async (argv) => {
const local = (argv.l || argv.local) ? true : false;
const domain = await helpers.getFQDN(null, getResolveVariablesShim(argv));
const gitBranch = await helpers.getGitBranch(process.env.GITHUB_REF);
const appStage = (argv || { }).stage || 'nonprod';
const defaultApiPrefix = require('./package.json')['api-prefix'] || 'api';
const apiPrefix = (argv || { }).apiPrefix || defaultApiPrefix;
const defaultPath = `/${gitBranch}`;
let publicUrl, apiRoot;
if (local) {
apiRoot = `${defaultPath}/${apiPrefix}`;
publicUrl = `${argv.publicUrl}${defaultPath}`;
} else {
apiRoot = apiPrefix;
publicUrl = `https://${domain}${(argv || {}).publicUrl || defaultPath}`
}
return Promise.resolve([
`REACT_APP_LOCAL=${local}`,
`REACT_APP_API_PREFIX=${apiPrefix}`,
`REACT_APP_API_ROOT=${apiRoot}`,
`REACT_APP_STAGE=${appStage}`,
`REACT_APP_PUBLIC_URL=${publicUrl}`,
`PUBLIC_URL=${publicUrl}`,
`DISABLE_ESLINT_PLUGIN=true`
].join(' '));
}
const getCypressEnvVars = async (argv) => {
const gitBranch = await helpers.getGitBranch(process.env.GITHUB_REF);
const publicUrl = `${argv.publicUrl}/${gitBranch}`;
const apiPrefix = `api`;
const appStage = 'nonprod';
return Promise.resolve([
`CYPRESS_API_PREFIX=${apiPrefix}`,
`CYPRESS_STAGE=${appStage}`,
`CYPRESS_PUBLIC_URL=${publicUrl}`,
`PUBLIC_URL=${publicUrl}`,
].join(' '));
}
const getResolveVariablesShim = (argv) => {
const stage = argv.stage || 'nonprod';
const package = require('./package.json');
const service = package.name;
const cname = package['prod-cname'] || 'www';
const domain = (package.domain || { name: '{yourdomain.com}' }).name || '{yourdomain.com}';
return (varName) => {
return Promise.resolve({
'self:provider.stage': stage,
'self:custom.prod-CNAME': cname,
'self:custom.domain-name': domain,
'self:service': service
}[varName]);
}
}
const runActions = {
web: async (argv) => {
const envVars = await getWebEnvVars(argv);
const startResultCode = await helpers.runProcess(`${envVars} npm run localProxy`);
if (startResultCode) {
process.exit(startResultCode);
}
},
api: async (argv) => {
if (require('fs').existsSync(argv.dotenv || '.env')){
require('dotenv').config();
}
process.env.BROWSER = 'none';
process.env.FAST_REFRESH = 'false';
process.env.SLS_DEBUG='*';
const port = argv.port || '8080';
const gitBranch = await helpers.getGitBranch(process.env.GITHUB_REF);
const runOfflineCode = await helpers.runProcess(`sls offline start --printOutput --httpPort ${port} --stage ${gitBranch}`);
if (runOfflineCode) {
process.exit(runOfflineCode);
}
},
unit: async (argv) => {
const envVars = await getWebEnvVars(argv);
const startResultCode = await helpers.runProcess(`${envVars} ${argv.command}`);
if (startResultCode) {
process.exit(startResultCode);
}
},
cypress: async (argv) => {
const runOrOpen = (argv.l || argv.local) ? 'open': 'run --headless';
const envVars = await getCypressEnvVars(argv);
const runCypressCode = await helpers.runProcess(`${envVars} $(npm bin)/cypress ${runOrOpen}`);
if (runCypressCode) {
process.exit(runCypressCode);
}
},
_: async () => {
console.error(`You must specify a target for argv[2] (supported options: 'web', 'api')`)
return Promise.resolve(process.exit(1));
}
}
/*
* returns void
* argv: { publicUrl: string, apiPrefix: string, stage: string }
*/
module.exports.build = async (argv) => {
const envVars = await getWebEnvVars(argv);
const buildResultCode = await helpers.runProcess(`${envVars} expo-cli build:web`);
if (buildResultCode) {
process.exit(buildResultCode);
}
}
/*
* returns void
* argv: { stage: string, r|remove?: boolean }
*/
module.exports.deploy = async (argv) => {
const deployOrRemove = argv.r || argv.remove ? 'remove': 'deploy';
const gitBranch = await helpers.getGitBranch(process.env.GITHUB_REF);
const stage = argv.stage || gitBranch;
const deployResultCode = await helpers.runProcess(`SLS_DEBUG=* sls ${deployOrRemove} --stage ${stage}`);
if (deployResultCode) {
process.exit(deployResultCode);
}
}
/*
* returns Promise<void>
* Prints the subdomain and domain generated by automation
*/
module.exports.domain = async (argv) => {
if (require('fs').existsSync('.env')){
require('dotenv').config();
}
const writeOutput = (output) => {
console.log('\x1b[33m%s\x1b[0m', `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`);
console.log('\x1b[33m%s\x1b[0m', `-`);
console.log('\x1b[33m%s\x1b[0m', `-`);
console.log('\x1b[33m%s\x1b[0m', `- Your app will be deployed to https://${output}`);
console.log('\x1b[33m%s\x1b[0m', `-`);
console.log('\x1b[33m%s\x1b[0m', `-`);
console.log('\x1b[33m%s\x1b[0m', `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`);
console.log('');
console.log('');
console.log('');
console.log('');
}
console.log('');
console.log('');
console.log('');
console.log('');
process.env.GITHUB_ACTOR = process.env.GITHUB_ACTOR || helpers.getUsername();
const branchName = await helpers.getGitBranch(process.env.GITHUB_REF);
const stage = ~branchName.indexOf('master') ? 'dev' : 'nonprod';
return helpers.getFQDN(undefined, getResolveVariablesShim({ ...argv, stage })).then((fqdn) => {
writeOutput(fqdn);
});
}
module.exports.migrate = async (argv) => {
if (require('fs').existsSync('.env')){
require('dotenv').config();
}
const stackName = await helpers.getStackName(undefined, getResolveVariablesShim(argv));
const envKey = await helpers.getEnvironmentKey(undefined, getResolveVariablesShim(argv));
process.env.AWS_PROFILE = `${require('./package.json').name}-${envKey}`;
process.env.DDB_TABLE_PREFIX = stackName;
process.env.MIGRATIONS_TABLE = `${stackName}.migrations`;
const migrationsResultCode = await helpers.runProcess(`mograte ${argv.cmd}`, true);
if (migrationsResultCode) {
process.exit(migrationsResultCode);
}
}
/*
* returns Promise<any>
* argv: { target: string }
*/
module.exports.run = async (argv) => {
return (runActions[argv.target] || runActions._)(argv);
}