|
| 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 | +} |
0 commit comments