1. #!/usr/bin/env node
    
  2. 
    
  3. 'use strict';
    
  4. 
    
  5. const {exec, execSync} = require('child_process');
    
  6. const {readFileSync, writeFileSync} = require('fs');
    
  7. const {join} = require('path');
    
  8. 
    
  9. const main = async buildId => {
    
  10.   const root = join(__dirname, buildId);
    
  11.   const buildPath = join(root, 'build');
    
  12. 
    
  13.   execSync(`node ${join(root, './build')}`, {
    
  14.     cwd: __dirname,
    
  15.     env: {
    
  16.       ...process.env,
    
  17.       NODE_ENV: 'production',
    
  18.     },
    
  19.     stdio: 'inherit',
    
  20.   });
    
  21. 
    
  22.   await exec(`cp ${join(root, 'now.json')} ${join(buildPath, 'now.json')}`, {
    
  23.     cwd: root,
    
  24.   });
    
  25. 
    
  26.   const file = readFileSync(join(root, 'now.json'));
    
  27.   const json = JSON.parse(file);
    
  28.   const alias = json.alias[0];
    
  29. 
    
  30.   const commit = execSync('git rev-parse HEAD').toString().trim().slice(0, 7);
    
  31. 
    
  32.   let date = new Date();
    
  33.   date = `${date.toLocaleDateString()}${date.toLocaleTimeString()}`;
    
  34. 
    
  35.   const installationInstructions =
    
  36.     buildId === 'chrome'
    
  37.       ? readFileSync(join(__dirname, 'deploy.chrome.html'))
    
  38.       : readFileSync(join(__dirname, 'deploy.firefox.html'));
    
  39. 
    
  40.   let html = readFileSync(join(__dirname, 'deploy.html')).toString();
    
  41.   html = html.replace(/%commit%/g, commit);
    
  42.   html = html.replace(/%date%/g, date);
    
  43.   html = html.replace(/%installation%/, installationInstructions);
    
  44. 
    
  45.   writeFileSync(join(buildPath, 'index.html'), html);
    
  46. 
    
  47.   await exec(`now deploy && now alias ${alias}`, {
    
  48.     cwd: buildPath,
    
  49.     stdio: 'inherit',
    
  50.   });
    
  51. 
    
  52.   console.log(`Deployed to https://${alias}.now.sh`);
    
  53. };
    
  54. 
    
  55. module.exports = main;