|
| 1 | +#!/usr/bin/env node --harmony_arrow_functions |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +var fs = require('fs'), |
| 6 | + path = require('path'), |
| 7 | + co = require('co'), |
| 8 | + electronPackager = require('electron-packager'), |
| 9 | + glob = require('glob'), |
| 10 | + objectAssign = require('object-assign'), |
| 11 | + rimraf = require('rimraf'), |
| 12 | + thunkify = require('thunkify'), |
| 13 | + packageData = require('./package'); |
| 14 | + |
| 15 | +/** |
| 16 | + * wrpping with Electron |
| 17 | + * |
| 18 | + * @param {Object} baseOptions |
| 19 | + * @param {Object} overwriteOptions |
| 20 | + * @return {Promise} |
| 21 | + */ |
| 22 | +function packaging(baseOptions, overwriteOptions) { |
| 23 | + var options = objectAssign({}, baseOptions, overwriteOptions); |
| 24 | + |
| 25 | + return co(function*() { |
| 26 | + return yield thunkify(electronPackager).call(electronPackager, options); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * download for imagemin moudule |
| 32 | + * |
| 33 | + * @param {String} vendorDirPath |
| 34 | + * @return {Promise} |
| 35 | + */ |
| 36 | +function download(vendorDirPath) { |
| 37 | + var pluginModule = require(`${vendorDirPath}/../lib`), |
| 38 | + fetch = thunkify(pluginModule.download.bind(pluginModule)); |
| 39 | + |
| 40 | + return co(function*() { |
| 41 | + return yield fetch(); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * parallel download for imagemin modules |
| 47 | + * |
| 48 | + * @param {String[]} vendorDirPaths |
| 49 | + * @return {Function[]} |
| 50 | + */ |
| 51 | +function parallelDownload(vendorDirPaths) { |
| 52 | + return vendorDirPaths.map( |
| 53 | + (vendorDirPath) => download(vendorDirPath) |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * remove downloaded binaries and download binaries for target platform |
| 59 | + * |
| 60 | + * @param {String} platform |
| 61 | + * @param {String} releasesDir |
| 62 | + * @param {Promise} |
| 63 | + */ |
| 64 | +function setupBinaries(platform, releasesDir) { |
| 65 | + return co(function*() { |
| 66 | + var vendorDirGlob = `${releasesDir}/${platform}/**/vendor`, |
| 67 | + vendorDirs = yield thunkify(glob).call(glob, vendorDirGlob); |
| 68 | + |
| 69 | + // remove installed module's binaries |
| 70 | + yield vendorDirs.map( |
| 71 | + (vendorDir) => thunkify(rimraf).call(rimraf, vendorDir) |
| 72 | + ); |
| 73 | + |
| 74 | + // download to binaries for target platform |
| 75 | + return yield parallelDownload(vendorDirs); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * process faker |
| 81 | + * |
| 82 | + * @class |
| 83 | + */ |
| 84 | +function ProcessFaker() { |
| 85 | + this._originalProcess = process; |
| 86 | + this._originalDescripters = {}; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * fake property |
| 91 | + * |
| 92 | + * @param {String} propertyName |
| 93 | + * @param {*} fakeValue |
| 94 | + */ |
| 95 | +ProcessFaker.prototype.fake = function fake(propertyName, fakeValue) { |
| 96 | + var originalDescripter = |
| 97 | + Object.getOwnPropertyDescriptor(this._originalProcess, propertyName); |
| 98 | + |
| 99 | + this._originalDescripters[propertyName] = originalDescripter; |
| 100 | + Object.defineProperty(process, propertyName, fakeValue); |
| 101 | +}; |
| 102 | + |
| 103 | +/** |
| 104 | + * revert faked property. |
| 105 | + * |
| 106 | + * @param {String} propertyName |
| 107 | + */ |
| 108 | +ProcessFaker.prototype.unfake = function unfake(propertyName) { |
| 109 | + var originalDescripter = |
| 110 | + this._originalDescripters[propertyName]; |
| 111 | + |
| 112 | + Object.defineProperty(process, propertyName, originalDescripter); |
| 113 | + this._originalDescripters[propertyName] = null; |
| 114 | +}; |
| 115 | + |
| 116 | +co(function*() { |
| 117 | + const RELEASES_DIR = path.join(__dirname, '/releases'); |
| 118 | + |
| 119 | + var options = { |
| 120 | + dir: __dirname, |
| 121 | + name: 'minify-image', |
| 122 | + arch: 'x64', |
| 123 | + version: '0.28.1', |
| 124 | + prune: true, |
| 125 | + ignore: [ |
| 126 | + // common |
| 127 | + '\.(?:coffee|jade|jsx|scss)$', |
| 128 | + |
| 129 | + // development files |
| 130 | + 'webpack.config.js$', |
| 131 | + |
| 132 | + // MEMO: fail when prune option is true |
| 133 | + // 'package.json$', |
| 134 | + |
| 135 | + // empty dirs |
| 136 | + 'renderer/actions', |
| 137 | + 'renderer/components', |
| 138 | + 'renderer/constants', |
| 139 | + 'renderer/dispatcher', |
| 140 | + 'renderer/stores', |
| 141 | + |
| 142 | + // this file |
| 143 | + path.basename(__filename), |
| 144 | + |
| 145 | + // releases dir |
| 146 | + RELEASES_DIR, |
| 147 | + ], |
| 148 | + 'app-version': packageData.version, |
| 149 | + }; |
| 150 | + |
| 151 | + // packaging |
| 152 | + // TODO: parallel execution |
| 153 | + console.log('packaged to', yield packaging(options, { |
| 154 | + platform: 'win32', |
| 155 | + out: RELEASES_DIR, |
| 156 | + })); |
| 157 | + console.log('packaged to', yield packaging(options, { |
| 158 | + platform: 'linux', |
| 159 | + out: path.normalize(`${RELEASES_DIR}/minify-image-linux-x86_64`), |
| 160 | + })); |
| 161 | + console.log('packaged to', yield packaging(options, { |
| 162 | + platform: 'darwin', |
| 163 | + out: path.normalize(`${RELEASES_DIR}/minify-image-osx-x86_64`), |
| 164 | + })); |
| 165 | + |
| 166 | + // rename directory |
| 167 | + yield thunkify(fs.rename).call( |
| 168 | + fs, |
| 169 | + path.normalize(`${RELEASES_DIR}/minify-image-win32`), |
| 170 | + path.normalize(`${RELEASES_DIR}/minify-image-win-x86_64`) |
| 171 | + ); |
| 172 | + |
| 173 | + //---------------------------------------------------------------------------- |
| 174 | + |
| 175 | + var processFaker = new ProcessFaker(); |
| 176 | + |
| 177 | + try { |
| 178 | + let platforms = ['win32', 'linux', 'darwin']; |
| 179 | + |
| 180 | + // fake process.arch |
| 181 | + processFaker.fake('arch', { value: options.arch }); |
| 182 | + |
| 183 | + for (let i = 0, len = platforms.length; i < len; ++i) { |
| 184 | + // fake process.platform |
| 185 | + processFaker.fake('platform', { value: platforms[i] }); |
| 186 | + |
| 187 | + console.log(`downloading ${platforms[i]} imagemin binaries`); |
| 188 | + |
| 189 | + // remove downloaded binaries, |
| 190 | + // and download to binaries for target platform |
| 191 | + yield setupBinaries(platforms[i], RELEASES_DIR); |
| 192 | + |
| 193 | + console.log(`downloaded ${platforms[i]} imagemin binaries`); |
| 194 | + } |
| 195 | + } finally { |
| 196 | + // revert faked properties |
| 197 | + processFaker.unfake('arch'); |
| 198 | + processFaker.unfake('platform'); |
| 199 | + } |
| 200 | +}).then(function() { |
| 201 | + console.log('done'); |
| 202 | +}).catch(function(err) { |
| 203 | + console.error('error:'); |
| 204 | + console.error(err.stack); |
| 205 | +}); |
0 commit comments