Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 374b847

Browse files
author
Max Stoiber
committed
Add deployment script 🎉
Usage: $ yarn run deploy api # Deploys api.alpha $ yarn run deploy hyperion # Deploys hyperion.alpha $ yarn run deploy api --prod # Deploys api.spectrum.chat $ yarn run deploy athena --prod # Deploys athena.workers.spectrum.chat $ yarn run deploy all --prod # Deploy all servers to prod
1 parent 099076a commit 374b847

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@
278278
"webpack-defaults": "webpack-defaults",
279279
"process:emails:test": "(cd email-template-scripts && python3 inline-html-emails.py && node sendgrid-sync.js)",
280280
"process:emails:prod": "(cd email-template-scripts && python3 inline-html-emails.py && node sendgrid-sync.js prod)",
281-
"process:emails:send": "(cd email-template-scripts && node send-test-emails.js [email protected])"
281+
"process:emails:send": "(cd email-template-scripts && node send-test-emails.js [email protected])",
282+
"deploy": "node scripts/deploy"
282283
},
283284
"lint-staged": {
284285
"*.js": [

scripts/deploy.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
const { execSync } = require('child_process');
2+
const path = require('path');
3+
const parse = require('./utils/parse-argv');
4+
const error = require('./utils/error');
5+
6+
// Debug util
7+
const exec = process.env.DEBUG
8+
? cmd => {
9+
console.log(`[DEBUG] ${cmd}`);
10+
}
11+
: execSync;
12+
// Append --team space-program to all now commands
13+
const now = (cmd = '') => `now ${cmd} --team space-program`;
14+
15+
const VALID_SERVERS = [
16+
'all',
17+
'analytics',
18+
'api',
19+
'athena',
20+
'chronos',
21+
'hermes',
22+
'hyperion',
23+
'mercury',
24+
'vulcan',
25+
];
26+
const VALID_ALPHA_SERVERS = ['api', 'hyperion'];
27+
const { args, flags } = parse(process.argv);
28+
29+
let servers = args;
30+
31+
if (servers.length === 0)
32+
error(
33+
'Server name missing',
34+
`Please provide one of the following server names: ${VALID_SERVERS.map(
35+
w => `"${w}"`
36+
).join(', ')}`
37+
);
38+
39+
servers.forEach(server => {
40+
if (VALID_SERVERS.indexOf(server) === -1)
41+
error(
42+
`Cannot deploy unknown server "${args[0]}"`,
43+
`Please provide one of the following server names: ${VALID_SERVERS.map(
44+
w => `"${w}"`
45+
).join(', ')}`
46+
);
47+
});
48+
49+
if (flags.prod && servers.indexOf('all') > -1) {
50+
servers = VALID_SERVERS.filter(w => w !== 'all');
51+
} else if (servers.indexOf('all') > -1) {
52+
servers = VALID_ALPHA_SERVERS;
53+
}
54+
55+
if (!flags.prod) {
56+
servers.forEach(server => {
57+
if (VALID_ALPHA_SERVERS.indexOf(server) === -1) {
58+
error(
59+
`Cannot deploy ${server} to alpha`,
60+
'Did you mean to use the "--prod" flag?'
61+
);
62+
}
63+
});
64+
servers = servers.filter(server => VALID_ALPHA_SERVERS.indexOf(server) > -1);
65+
}
66+
67+
console.log(`\nDeploying to ${flags.prod ? 'production' : 'alpha'}!\n`);
68+
69+
// Hyperion needs to be deployed especially
70+
if (servers.indexOf('hyperion') > -1) {
71+
servers = servers.filter(w => w !== 'hyperion');
72+
console.log(`\n---hyperion---`);
73+
console.log(`Deploying hyperion`);
74+
exec(now(), {
75+
stdio: 'inherit',
76+
});
77+
console.log('Aliasing to hyperion.workers.spectrum.chat');
78+
exec(now('alias hyperion.workers.spectrum.chat'), {
79+
stdio: 'inherit',
80+
});
81+
console.log('Clearing cache');
82+
exec(now('alias -r rules.json spectrum.chat'), {
83+
stdio: 'inherit',
84+
});
85+
console.log('hyperion is live!\n');
86+
}
87+
88+
if (servers.length > 0) {
89+
console.log('Installing fresh dependencies...');
90+
exec('yarn');
91+
servers.forEach(server => {
92+
const buildDir = path.join(__dirname, `../build-${server}`);
93+
console.log(`\n---${server}---`);
94+
console.log(`Installing ${server} dependencies...`);
95+
exec('yarn', {
96+
cwd: path.join(__dirname, `../${server}`),
97+
});
98+
console.log(`Building ${server}...`);
99+
exec(`yarn run build:${server}`);
100+
101+
console.log(`Deploying ${server}...`);
102+
const stdout = exec(now(`build-${server}`), {
103+
stdio: 'pipe',
104+
});
105+
106+
const alias =
107+
server === 'api'
108+
? `api.${!flags.prod ? 'alpha.' : ''}spectrum.chat`
109+
: `${server}.${
110+
flags.prod === true ? 'workers' : 'alpha'
111+
}.spectrum.chat`;
112+
console.log(`Aliasing ${stdout.toString()} to ${alias}...`);
113+
exec(now(`alias ${stdout.toString()} ${alias}`), {
114+
cwd: buildDir,
115+
stdio: 'inherit',
116+
});
117+
118+
console.log(`${server} is live!\n`);
119+
});
120+
}

scripts/utils/error.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = (...args) => {
2+
console.error('\n🚨 Error:', args[0], '🚨\n\n', ...args.slice(1), '\n');
3+
process.exit(1);
4+
return;
5+
};

scripts/utils/parse-argv.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Cheap process.argv parser
2+
module.exports = argv => {
3+
const processArgs = argv.slice(2);
4+
const args = processArgs.filter(arg => arg.indexOf('--') !== 0);
5+
const flags = processArgs
6+
.filter(arg => arg.indexOf('--') === 0)
7+
.reduce((flags, flag) => {
8+
flags[flag.replace(/^--/, '')] = true;
9+
return flags;
10+
}, {});
11+
12+
return { args, flags };
13+
};

0 commit comments

Comments
 (0)