| 7 | const {join} = require('path'); |
| 8 | const shell = require('shelljs'); |
| 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 | shell.cp(join(root, 'now.json'), join(buildPath, 'now.json')); |
| 22 | const file = readFileSync(join(root, 'now.json')); |
| 23 | const json = JSON.parse(file); |
| 24 | const alias = json.alias[0]; |
| 25 | |
| 26 | const commit = execSync('git rev-parse HEAD').toString().trim().slice(0, 7); |
| 27 | |
| 28 | let date = new Date(); |
| 29 | date = `${date.toLocaleDateString()} – ${date.toLocaleTimeString()}`; |
| 30 | |
| 31 | const installationInstructions = |
| 32 | buildId === 'chrome' |
| 33 | ? readFileSync(join(__dirname, 'deploy.chrome.html')) |
| 34 | : readFileSync(join(__dirname, 'deploy.firefox.html')); |
| 35 | |
| 36 | let html = readFileSync(join(__dirname, 'deploy.html')).toString(); |
| 37 | html = html.replace(/%commit%/g, commit); |
| 38 | html = html.replace(/%date%/g, date); |
| 39 | html = html.replace(/%installation%/, installationInstructions); |
| 40 | |
| 41 | writeFileSync(join(buildPath, 'index.html'), html); |
| 42 | |
| 43 | await exec(`now deploy && now alias ${alias}`, { |
| 44 | cwd: buildPath, |
| 45 | stdio: 'inherit', |
| 46 | }); |
| 47 | |
| 48 | console.log(`Deployed to https://${alias}.now.sh`); |
| 49 | }; |
| 50 | |
| 51 | module.exports = main; |