File size: 3,260 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { execSync } = require('child_process');
const path = require('path');
const parse = require('./utils/parse-argv');
const error = require('./utils/error');

// Debug util
const exec = process.env.DEBUG
  ? cmd => {
      console.log(`[DEBUG] ${cmd}`);
      return 'fake-url-asdf123.now.sh';
    }
  : execSync;
// Append --scope space-program to all now commands
const now = (cmd = '') => `now ${cmd} --scope space-program`;

const VALID_SERVERS = ['all', 'api', 'hyperion'];
const VALID_ALPHA_SERVERS = ['api', 'hyperion'];
const { args, flags } = parse(process.argv);

let servers = args;

if (servers.length === 0)
  error(
    'Server name missing',
    `Please provide one of the following server names: ${VALID_SERVERS.map(
      w => `"${w}"`
    ).join(', ')}`
  );

servers.forEach(server => {
  if (VALID_SERVERS.indexOf(server) === -1)
    error(
      `Cannot deploy unknown server "${args[0]}"`,
      `Please provide one of the following server names: ${VALID_SERVERS.map(
        w => `"${w}"`
      ).join(', ')}`
    );
});

if (flags.prod && servers.indexOf('all') > -1) {
  servers = VALID_SERVERS.filter(w => w !== 'all');
} else if (servers.indexOf('all') > -1) {
  servers = VALID_ALPHA_SERVERS;
}

if (!flags.prod) {
  servers.forEach(server => {
    if (VALID_ALPHA_SERVERS.indexOf(server) === -1) {
      error(
        `Cannot deploy ${server} to alpha`,
        'Did you mean to use the "--prod" flag?'
      );
    }
  });
  servers = servers.filter(server => VALID_ALPHA_SERVERS.indexOf(server) > -1);
}

console.log(`\nDeploying to ${flags.prod ? 'production' : 'alpha'}!\n`);

if (servers.length > 0) {
  console.log('Installing fresh dependencies...');
  exec('yarn');
  servers.forEach(server => {
    const buildDir = path.join(__dirname, `../build-${server}`);
    console.log(`\n---${server}---`);
    console.log(`Installing ${server} dependencies...`);
    exec('yarn', {
      cwd: path.join(__dirname, `../${server}`),
    });
    console.log(`Building ${server}...`);
    exec(`yarn run build:${server}`);

    console.log(`Deploying ${server}...`);
    const stdout = exec(now(`build-${server} --debug`), {
      stdio: 'pipe',
    });

    const alias =
      server === 'api'
        ? `api.${!flags.prod ? 'alpha.' : ''}spectrum.chat`
        : `${server}.${
            flags.prod === true ? 'workers' : 'alpha'
          }.spectrum.chat`;
    console.log(`Aliasing ${stdout.toString()} to ${alias}...`);
    exec(now(`alias ${stdout.toString()} ${alias}`), {
      cwd: buildDir,
      stdio: 'inherit',
    });

    console.log(`${server} is live!`);

    if (server === 'hyperion') {
      console.log('Clearing hyperion cache...');
      exec(
        now(
          `alias -r rules${!flags.prod ? '-alpha' : ''}.json ${
            !flags.prod ? 'alpha.' : ''
          }spectrum.chat`
        ),
        {
          stdio: 'inherit',
        }
      );
      console.log('Cache cleared!');
    }
    console.log('Deleting old deploy(s)...');
    if (server === 'hyperion') {
      exec(now(`rm --safe --yes Spectrum`), {
        stdio: 'inherit',
      });
    } else {
      exec(now(`rm --safe --yes build-${server}`), {
        stdio: 'inherit',
      });
    }
    console.log('Done!\n');
  });
}