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

Commit f6268e5

Browse files
authored
Merge pull request #4671 from withspectrum/deployment-script
Deployment script ✨
2 parents 8c58466 + 42f336b commit f6268e5

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-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: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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(
83+
now(
84+
`alias -r rules${!flags.prod ? '-alpha' : ''}.json ${
85+
!flags.prod ? 'alpha.' : ''
86+
}spectrum.chat`
87+
),
88+
{
89+
stdio: 'inherit',
90+
}
91+
);
92+
console.log('hyperion is live!\n');
93+
}
94+
95+
if (servers.length > 0) {
96+
console.log('Installing fresh dependencies...');
97+
exec('yarn');
98+
servers.forEach(server => {
99+
const buildDir = path.join(__dirname, `../build-${server}`);
100+
console.log(`\n---${server}---`);
101+
console.log(`Installing ${server} dependencies...`);
102+
exec('yarn', {
103+
cwd: path.join(__dirname, `../${server}`),
104+
});
105+
console.log(`Building ${server}...`);
106+
exec(`yarn run build:${server}`);
107+
108+
console.log(`Deploying ${server}...`);
109+
const stdout = exec(now(`build-${server}`), {
110+
stdio: 'pipe',
111+
});
112+
113+
const alias =
114+
server === 'api'
115+
? `api.${!flags.prod ? 'alpha.' : ''}spectrum.chat`
116+
: `${server}.${
117+
flags.prod === true ? 'workers' : 'alpha'
118+
}.spectrum.chat`;
119+
console.log(`Aliasing ${stdout.toString()} to ${alias}...`);
120+
exec(now(`alias ${stdout.toString()} ${alias}`), {
121+
cwd: buildDir,
122+
stdio: 'inherit',
123+
});
124+
125+
console.log(`${server} is live!\n`);
126+
});
127+
}
File renamed without changes.

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)