Skip to content

Commit

Permalink
DRY up electron-download related code
Browse files Browse the repository at this point in the history
  • Loading branch information
malept committed Sep 3, 2016
1 parent 1ebe0db commit 97d0847
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
42 changes: 32 additions & 10 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const asar = require('asar')
const child = require('child_process')
const debug = require('debug')('electron-packager')
const download = require('electron-download')
const fs = require('fs-extra')
const ignore = require('./ignore')
const minimist = require('minimist')
Expand Down Expand Up @@ -81,6 +82,10 @@ function asarApp (appPath, asarOptions, cb) {
})
}

function isPlatformMac (platform) {
return platform === 'darwin' || platform === 'mas'
}

function sanitizeAppName (name) {
return sanitize(name, {replacement: '-'})
}
Expand Down Expand Up @@ -116,28 +121,45 @@ function createAsarOpts (opts) {
return asarOptions
}

function createDownloadOpts (opts, platform, arch) {
let downloadOpts = opts.download || {}

subOptionWarning(downloadOpts, 'download', 'platform', platform)
subOptionWarning(downloadOpts, 'download', 'arch', arch)
subOptionWarning(downloadOpts, 'download', 'version', opts.version)

return downloadOpts
}

module.exports = {
archs: archs,
platforms: platforms,

parseCLIArgs: parseCLIArgs,

isPlatformMac: function isPlatformMac (platform) {
return platform === 'darwin' || platform === 'mas'
},
isPlatformMac: isPlatformMac,

subOptionWarning: subOptionWarning,

createAsarOpts: createAsarOpts,
createDownloadOpts: createDownloadOpts,
createDownloadCombos: function createDownloadCombos (opts, selectedPlatforms, selectedArchs, ignoreFunc) {
let combinations = []
for (let arch of selectedArchs) {
for (let platform of selectedPlatforms) {
// Electron does not have 32-bit releases for Mac OS X, so skip that combination
if (isPlatformMac(platform) && arch === 'ia32') continue
if (typeof ignoreFunc === 'function' && ignoreFunc(platform, arch)) continue
combinations.push(createDownloadOpts(opts, platform, arch))
}
}

createDownloadOpts: function createDownloadOpts (opts, platform, arch) {
let downloadOpts = opts.download || {}

subOptionWarning(downloadOpts, 'download', 'platform', platform)
subOptionWarning(downloadOpts, 'download', 'arch', arch)
subOptionWarning(downloadOpts, 'download', 'version', opts.version)
return combinations
},

return downloadOpts
downloadElectronZip: function downloadElectronZip (downloadOpts, cb) {
debug(`Downloading Electron with options ${JSON.stringify(downloadOpts)}`)
download(downloadOpts, cb)
},

generateFinalBasename: generateFinalBasename,
Expand Down
17 changes: 3 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const common = require('./common')
const debug = require('debug')('electron-packager')
const download = require('electron-download')
const extract = require('extract-zip')
const fs = require('fs-extra')
const getPackageInfo = require('get-package-info')
Expand Down Expand Up @@ -92,30 +91,20 @@ function createSeries (opts, archs, platforms) {
})
}

var combinations = []
archs.forEach(function (arch) {
platforms.forEach(function (platform) {
// Electron does not have 32-bit releases for Mac OS X, so skip that combination
if (common.isPlatformMac(platform) && arch === 'ia32') return
combinations.push(common.createDownloadOpts(opts, platform, arch))
})
})

var tasks = []
var useTempDir = opts.tmpdir !== false
if (useTempDir) {
tasks.push(function (cb) {
fs.remove(tempBase, cb)
})
}
return tasks.concat(combinations.map(function (combination) {
return tasks.concat(common.createDownloadCombos(opts, platforms, archs).map(combination => {
var arch = combination.arch
var platform = combination.platform
var version = combination.version

return function (callback) {
debug(`Downloading Electron with options ${JSON.stringify(combination)}`)
download(combination, function (err, zipPath) {
return (callback) => {
common.downloadElectronZip(combination, (err, zipPath) => {
if (err) return callback(err)

function createApp (comboOpts) {
Expand Down
29 changes: 9 additions & 20 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const bufferEqual = require('buffer-equal')
const common = require('../common')
const config = require('./config.json')
const download = require('electron-download')
const fs = require('fs-extra')
const path = require('path')
const series = require('run-series')
Expand All @@ -13,22 +12,6 @@ const test = require('tape')
const ORIGINAL_CWD = process.cwd()
const WORK_CWD = path.join(__dirname, 'work')

var combinations = []
common.archs.forEach(function (arch) {
common.platforms.forEach(function (platform) {
// Electron does not have 32-bit releases for Mac OS X, so skip that combination
// Also skip testing darwin/mas target on Windows since electron-packager itself skips it
// (see https://github.com/electron-userland/electron-packager/issues/71)
if (common.isPlatformMac(platform) && (arch === 'ia32' || require('os').platform() === 'win32')) return

combinations.push({
arch: arch,
platform: platform,
version: config.version
})
})
})

exports.areFilesEqual = function areFilesEqual (file1, file2, callback) {
series([
function (cb) {
Expand All @@ -43,11 +26,17 @@ exports.areFilesEqual = function areFilesEqual (file1, file2, callback) {
}

exports.downloadAll = function downloadAll (version, callback) {
series(combinations.map(function (combination) {
return function (cb) {
let combinations = common.createDownloadCombos({version: config.version}, common.platforms, common.archs, (platform, arch) => {
// Skip testing darwin/mas target on Windows since electron-packager itself skips it
// (see https://github.com/electron-userland/electron-packager/issues/71)
return common.isPlatformMac(platform) && process.platform === 'win32'
})

series(combinations.map(combination => {
return (cb) => {
var downloadOpts = Object.assign({}, combination)
downloadOpts.version = version
download(downloadOpts, cb)
common.downloadElectronZip(downloadOpts, cb)
}
}), callback)
}
Expand Down

0 comments on commit 97d0847

Please sign in to comment.