|
| 1 | +#! /usr/bin/env node |
| 2 | + |
| 3 | +const github = require('octonode'); |
| 4 | +const normalizeUrl = require('normalize-url'); |
| 5 | +const spawn = require('cross-spawn'); |
| 6 | +const travisAfterAll = require('travis-after-all'); |
| 7 | +const urlRegex = require('url-regex'); |
| 8 | +const argv = require('yargs').argv; |
| 9 | + |
| 10 | +const nowCli = require.resolve('now/download/dist/now'); |
| 11 | + |
| 12 | +if (!process.env.CI || !process.env.TRAVIS) { |
| 13 | + throw new Error('Could not detect Travis CI environment'); |
| 14 | +} |
| 15 | + |
| 16 | +const githubToken = process.env.GH_TOKEN; |
| 17 | +const nowToken = process.env.NOW_TOKEN; |
| 18 | + |
| 19 | +if (!githubToken) { |
| 20 | + throw new Error('Missing required environment variable GH_TOKEN'); |
| 21 | +} |
| 22 | + |
| 23 | +if (!nowToken) { |
| 24 | + throw new Error('Missing required environment variable NOW_TOKEN'); |
| 25 | +} |
| 26 | + |
| 27 | +const client = github.client(githubToken); |
| 28 | +const ghRepo = client.repo(process.env.TRAVIS_REPO_SLUG); |
| 29 | + |
| 30 | +function noop() {} |
| 31 | + |
| 32 | +function getUrl(content) { |
| 33 | + const urls = content.match(urlRegex()) || []; |
| 34 | + |
| 35 | + return urls.map(url => normalizeUrl(url.trim().replace(/\.+$/, '')))[0]; |
| 36 | +} |
| 37 | + |
| 38 | +function deploy(context, sha) { |
| 39 | + ghRepo.status( |
| 40 | + sha, |
| 41 | + { |
| 42 | + context, |
| 43 | + state: 'pending', |
| 44 | + description: `Δ Now ${context} deployment pending`, |
| 45 | + }, |
| 46 | + noop, |
| 47 | + ); |
| 48 | + |
| 49 | + const args = ['--token', nowToken, '--no-clipboard']; |
| 50 | + const alias = context === 'production' && process.env.NOW_ALIAS; |
| 51 | + let stdout = ''; |
| 52 | + |
| 53 | + if (alias) { |
| 54 | + args.push(...['--alias', alias]); |
| 55 | + } |
| 56 | + |
| 57 | + if (argv.p || argv.public) { |
| 58 | + args.push(...['-p']); |
| 59 | + } |
| 60 | + |
| 61 | + if (argv.folder) { |
| 62 | + args.push(argv.folder); |
| 63 | + } |
| 64 | + |
| 65 | + const child = spawn(nowCli, args); |
| 66 | + |
| 67 | + if (!alias) { |
| 68 | + child.stdout.on('data', data => { |
| 69 | + stdout += data; |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + child.on('error', err => { |
| 74 | + console.error(err); |
| 75 | + ghRepo.status( |
| 76 | + sha, |
| 77 | + { |
| 78 | + context, |
| 79 | + state: 'error', |
| 80 | + description: `Δ Now ${context} deployment failed. See Travis logs for details.`, |
| 81 | + }, |
| 82 | + noop, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + child.on('close', () => { |
| 87 | + const targetUrl = alias || getUrl(stdout); |
| 88 | + |
| 89 | + ghRepo.status( |
| 90 | + sha, |
| 91 | + { |
| 92 | + context, |
| 93 | + targetUrl, |
| 94 | + state: 'success', |
| 95 | + description: `Δ Now ${context} deployment complete`, |
| 96 | + }, |
| 97 | + noop, |
| 98 | + ); |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +travisAfterAll((code, err) => { |
| 103 | + // Don't do anything if there was an error of if the build returned a failing code |
| 104 | + if (err || code) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + switch (process.env.TRAVIS_EVENT_TYPE) { |
| 109 | + case 'pull_request': |
| 110 | + return deploy('staging', process.env.TRAVIS_PULL_REQUEST_SHA); |
| 111 | + case 'push': |
| 112 | + return deploy('production', process.env.TRAVIS_COMMIT); |
| 113 | + default: |
| 114 | + return ''; |
| 115 | + } |
| 116 | +}); |
0 commit comments