This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·216 lines (189 loc) · 4.63 KB
/
main.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env node
'use strict';
const axios = require('axios');
const getCiEnv = require('get-ci-env');
const path = require('path');
const signale = require('signale');
const yargs = require('yargs');
const exec = require('./exec.js');
const fatalError = require('./fatal-error.js');
// Service specific dependencies
const importLazy = require('import-lazy')(require);
const fly = importLazy('./fly.js');
const now = importLazy('./now.js');
const hasSubdomain = domain => domain.split('.').length > 2;
const backendAxios = data => axios({
url: process.env.BACKEND_URL || 'https://plek-server.now.sh/',
method: 'post',
transformRequest: requestData =>
JSON.stringify({
name: path.parse(process.cwd()).name,
...requestData,
}),
data,
})
.catch(() => signale.warn(
'Could not reach server to update GitHub commit status'
));
const cleanupFlow = command => ciEnv => {
signale.start('Cleaning up...');
backendAxios({
ciEnv,
flow: 'cleanup',
state: 'pending',
});
return command().then(() => {
signale.success('Cleaned up old domains & deployments');
});
};
const deployFlow = command => ciEnv => {
signale.start('Deploying...');
backendAxios({
ciEnv,
flow: 'deploy',
state: 'pending',
});
return command().then(url => {
backendAxios({
ciEnv,
flow: 'deploy',
state: 'success',
targetUrl: url,
});
signale.success(`Deployed to ${url}`);
return url;
});
};
const aliasFlow = (command, domain) => ciEnv => {
const domainDivider = hasSubdomain(domain) ? '-' : '.';
if (ciEnv.pr) {
process.env.DOMAIN = `pr-${ciEnv.pr.number}${domainDivider}${domain}`;
} else if (ciEnv.branch === 'master') {
process.env.DOMAIN = domain;
} else {
return Promise.resolve();
}
signale.start('Aliasing domain...');
backendAxios({
ciEnv,
flow: 'alias',
state: 'pending',
});
return command().then(output => {
backendAxios({
ciEnv,
flow: 'alias',
state: 'success',
targetUrl: `https://${process.env.DOMAIN}`,
});
signale.success(output);
return output;
});
};
const [cleanup, deploy, alias] = [cleanupFlow, deployFlow, aliasFlow].map(
flow => (...args) => getCiEnv().then(flow(...args))
);
yargs
.usage('$0 <cmd> [args]')
.demandCommand(1)
.help()
.alias('h', 'help')
.version()
.alias('v', 'version')
.example('$0 now myproject.now.sh --app myproject -- --public');
yargs.command(
'cleanup <command>',
'Run cleanup step with custom command',
{},
({ command }) => {
cleanup(() => exec(command));
}
);
yargs.command(
'deploy <command>',
'Run deploy step with custom command',
{},
({ command }) => {
deploy(
() => exec(command).then(({ stdout, stderr }) => {
signale.note(stderr);
return stdout;
})
);
}
);
yargs.command(
'alias <command> <domain>',
'Run alias step with custom command',
{},
({ command, domain }) => {
alias(() => exec(command), domain);
}
);
yargs.command(
'now <domain>',
`Use ZEIT Now, pass through flags to the Now CLI after the command seperated with "--",`,
{
app: {
alias: 'a',
describe: 'Zeit Now app name',
type: 'string',
nargs: 1,
demandOption: true,
},
team: {
alias: 't',
describe: 'Zeit team name slug',
type: 'string',
nargs: 1,
default: '',
coerce: team => ({
flag: team ? `--scope ${team}` : '',
slug: team,
}),
},
nowFlags: {
skipValidation: true,
hidden: true,
},
},
({ domain, app, team, _: nowFlags }) => {
if (!process.env.NOW_TOKEN)
throw Error('Missing NOW_TOKEN environment variable.');
nowFlags = nowFlags.slice(1).join(' ');
cleanup(now.cleanup({ app, teamSlug: team.slug, domain })).then(
deploy(now.deploy({ nowFlags, app, teamFlag: team.flag })).then(url =>
alias(now.alias({ url, teamFlag: team.flag }), domain)
)
);
}
);
yargs.command(
'fly <appName>',
'Use Fly',
{
stage: {
alias: 's',
describe: 'Environment stage to use',
type: 'string',
nargs: 1,
default: 'production',
},
},
({ appName, stage }) => {
if (!process.env.FLY_TOKEN)
throw Error('Missing FLY_TOKEN environment variable.');
cleanup(fly.cleanup()).then(
alias(
() =>
fly
.alias({ appName: process.env.DOMAIN.split('.')[0] })
.then(appName => deploy(fly.deploy({ appName, stage }))),
`${appName}.edgeapp.net`
)
);
}
);
yargs.parse(process.argv.slice(2));
process.title = 'plek';
process.on('unhandledRejection', fatalError);