-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.mjs
executable file
·196 lines (171 loc) · 4.17 KB
/
deploy.mjs
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
#!/usr/bin/env zx
/**
* deploy.js
* Handles transpiling and deploying source code.
*
* Available options:
* --help: prints script usage instructions.
* --location: (required) determines env variables and where compiled
* assets should be rsync'd to.
* --transfer: (optional) disables rsync when set to false.
* --build: (optional) disables sapper export when set to false
*
* Examples (assumes script is being run from root of repository):
*
* npm run deploy -- --help
* npm run deploy -- --location=dev
* npm run deploy -- --location=beta --transfer=false
* npm run deploy -- --location=prod --build=false
*/
import { $ } from "zx";
const SAPPER_EXPORT_DIR = "__sapper__/export/";
const USER = "gitdev";
const BASE_URI = "cal-adapt.org";
const DEPLOY_URI = `api.${BASE_URI}`;
const DIR = "/var/www/";
let allowTransfer = true;
let allowBuild = true;
main();
async function main() {
// zx shorthand for parsing script arguments in the form of `--foo=bar`
// eslint-disable-next-line no-undef
const { location, help, transfer, build } = argv;
if (help) {
return await usage();
}
if (!location) {
return await usage("Missing argument for --location");
}
if (transfer === "false") {
allowTransfer = false;
}
if (build === "false") {
allowBuild = false;
}
try {
await handleLocation(location);
} catch (error) {
handleError(error);
}
}
async function usage(message) {
if (message) {
console.log(message);
}
console.log(
"Usage: zx deploy.mjs --location=<string> --transfer=<boolean> --build=<boolean>"
);
console.log(
"--location is required; --transfer & --build are optional, both default to true"
);
await $`exit 0`;
}
async function handleError(error) {
console.log(`Error: ${error.message}`);
process.exit(1);
}
async function sapperExport() {
if (allowBuild) {
await $`npm run export`;
} else {
console.log("not running sapper export via --build option");
return Promise.resolve();
}
}
async function transfer(subdomain) {
if (allowTransfer) {
const domain = subdomain ? `${subdomain}.${BASE_URI}` : BASE_URI;
await $`rsync \
-avz ${SAPPER_EXPORT_DIR} \
--delete \
${USER}@${DEPLOY_URI}:${DIR}${domain}/`;
} else {
console.log("transfer disabled via --transfer option, exiting");
return Promise.resolve();
}
}
async function deployBeta() {
try {
await sapperExport();
await transfer("beta");
await $`exit 0`;
} catch (error) {
handleError(error);
}
}
async function deployDev() {
try {
await sapperExport();
await transfer("dev");
await $`exit 0`;
} catch (error) {
handleError(error);
}
}
async function deployNetlify() {
if (!process.env.NETLIFY_AUTH_TOKEN || !process.env.NETLIFY_ALIAS) {
throw new Error(
"Deploying to Netlify requires setting environment variables: $NETLIFY_AUTH_TOKEN and $NETLIFY_ALIAS"
);
}
try {
await sapperExport();
await $`netlify deploy \
--dir ${SAPPER_EXPORT_DIR}\
--auth $NETLIFY_AUTH_TOKEN\
--alias $NETLIFY_ALIAS\
--open`;
await $`exit 0`;
} catch (error) {
handleError(error);
}
}
async function deployProd() {
try {
await sapperExport();
await transfer();
await $`exit 0`;
} catch (error) {
handleError(error);
}
}
function setEnvBeta() {
process.env.NODE_ENV = "production";
process.env.DEPLOY = "beta";
}
function setEnvDev() {
process.env.NODE_ENV = "production";
process.env.DEPLOY = "dev";
}
function setEnvProd() {
process.env.NODE_ENV = "production";
process.env.DEPLOY = "prod";
}
function setEnvNetlify() {
process.env.NODE_ENV = "production";
process.env.DEPLOY = "netlify";
}
async function handleLocation(location) {
switch (location) {
case "beta":
setEnvBeta();
await deployBeta();
break;
case "dev":
setEnvDev();
await deployDev();
break;
case "netlify":
setEnvNetlify();
await deployNetlify();
break;
case "prod":
setEnvProd();
await deployProd();
break;
default:
usage(
`Unrecognized --location param: ${location}.\nValid params are: beta, prod, dev, netlify`
);
}
}