|
| 1 | +#!/usr/bin/env babel-node |
| 2 | + |
| 3 | +import Promise from 'bluebird'; |
| 4 | +import { spawn, execSync } from 'child_process'; |
| 5 | +import Github from 'github'; |
| 6 | +import R from 'ramda'; |
| 7 | + |
| 8 | +const fs = Promise.promisifyAll(require('fs-extra')); |
| 9 | +const request = Promise.promisify(require('request')); |
| 10 | + |
| 11 | + |
| 12 | +function spawnAsync(cmd) { |
| 13 | + const options = { |
| 14 | + env: process.env, |
| 15 | + stdio: 'inherit' |
| 16 | + }; |
| 17 | + |
| 18 | + const parameters = R.filter(R.identity, cmd.replace(/ \\n/g, '').replace('\t', '').split(' ')); |
| 19 | + const executable = parameters[0]; |
| 20 | + parameters.shift(); |
| 21 | + |
| 22 | + console.log(executable, parameters); |
| 23 | + |
| 24 | + return new Promise(resolve => { |
| 25 | + const proc = spawn(executable, parameters, options); |
| 26 | + proc.on('close', code => { |
| 27 | + if (code !== 0) process.exit(code); |
| 28 | + resolve(); |
| 29 | + }); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +if (!process.env.GITHUB_TOKEN) { |
| 34 | + console.log('Github token is missing from env. Exiting...'); |
| 35 | + process.exit(1); |
| 36 | +} |
| 37 | + |
| 38 | +if (!process.env.PHANTOM_VERSION) { |
| 39 | + console.log('Phantom version is missing from env. Exiting...'); |
| 40 | + process.exit(1); |
| 41 | +} |
| 42 | + |
| 43 | +const github = new Github({ |
| 44 | + version: '3.0.0', |
| 45 | + protocol: 'https', |
| 46 | + timeout: 5000, |
| 47 | + headers: { |
| 48 | + 'user-agent': 'Phantomized-Gulp-Release' |
| 49 | + } |
| 50 | +}); |
| 51 | +github.authenticate({ |
| 52 | + type: 'oauth', |
| 53 | + token: process.env.GITHUB_TOKEN |
| 54 | +}); |
| 55 | +const releases = Promise.promisifyAll(github.releases); |
| 56 | + |
| 57 | +console.log(`Downloading PhantomJS ${process.env.PHANTOM_VERSION}`); |
| 58 | +const download_options = { |
| 59 | + url: `https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-${process.env.PHANTOM_VERSION}-linux-x86_64.tar.bz2`, |
| 60 | + encoding: null |
| 61 | +}; |
| 62 | + |
| 63 | +request(download_options) |
| 64 | + .then(res => fs.writeFileAsync('./phantomjs.tar.bz2', res.body, null)) |
| 65 | + .then(() => console.log('Extracting')) |
| 66 | + .then(() => spawnAsync('tar -jxvf phantomjs.tar.bz2')) |
| 67 | + .then(() => fs.copyAsync(`./phantomjs-${process.env.PHANTOM_VERSION}-linux-x86_64/bin/phantomjs`, '/usr/local/bin/phantomjs', {})) |
| 68 | + .then(() => { |
| 69 | + console.log('Running dockerize'); |
| 70 | + const cmd = `dockerize -n -o dockerized-phantomjs \ |
| 71 | + -e /usr/local/bin/phantomjs \ |
| 72 | + -a /bin/dash /bin/sh \ |
| 73 | + -a /etc/fonts /etc \ |
| 74 | + -a /etc/ssl /etc \ |
| 75 | + -a /usr/share/fonts /usr/share \ |
| 76 | + --verbose \ |
| 77 | + /usr/local/bin/phantomjs \ |
| 78 | + /usr/bin/curl`; |
| 79 | + return spawnAsync(cmd); |
| 80 | + }) |
| 81 | + .then(() => fs.removeAsync('./dockerized-phantomjs/Dockerfile')) |
| 82 | + .then(() => fs.removeAsync('./dockerized-phantomjs/usr/local/bin/phantomjs')) |
| 83 | + .then(() => { |
| 84 | + console.log('Taring archive'); |
| 85 | + process.chdir('./dockerized-phantomjs'); |
| 86 | + return execSync('tar -zcf ../dockerized-phantomjs.tar.gz ./*'); |
| 87 | + }) |
| 88 | + .then(() => { |
| 89 | + console.log('Uploading release to Github'); |
| 90 | + process.chdir('../'); |
| 91 | + return releases.createReleaseAsync({ |
| 92 | + owner: 'Gravebot', |
| 93 | + repo: 'phantomized', |
| 94 | + tag_name: process.env.PHANTOM_VERSION, |
| 95 | + draft: true, |
| 96 | + name: `Phantomize ${process.env.PHANTOM_VERSION}` |
| 97 | + }); |
| 98 | + }) |
| 99 | + .then(release => releases.uploadAssetAsync({ |
| 100 | + owner: 'Gravebot', |
| 101 | + repo: 'phantomized', |
| 102 | + id: release.id, |
| 103 | + name: 'dockerized-phantomjs.tar.gz', |
| 104 | + filePath: './dockerized-phantomjs.tar.gz' |
| 105 | + })) |
| 106 | + .then(() => console.log('Done')) |
| 107 | + .catch(err => { |
| 108 | + console.log(err.stack || err); |
| 109 | + process.exit(1); |
| 110 | + }); |
0 commit comments