diff --git a/common.js b/common.js new file mode 100644 index 0000000..24fd323 --- /dev/null +++ b/common.js @@ -0,0 +1,50 @@ +var asar = require('asar') +var child = require('child_process') +var path = require('path') +var rimraf = require('rimraf') + +module.exports = { + asarApp: function asarApp (finalDir, cb) { + var src = path.join(finalDir, 'resources', 'app') + var dest = path.join(finalDir, 'resources', 'app.asar') + asar.createPackage(src, dest, function (err) { + if (err) return cb(err) + rimraf(src, function (err) { + if (err) return cb(err) + cb(null, dest) + }) + }) + }, + + prune: function prune (opts, cwd, cb, nextStep) { + if (opts.prune) { + child.exec('npm prune --production', { cwd: cwd }, function pruned (err) { + if (err) return cb(err) + nextStep() + }) + } else { + nextStep() + } + }, + + userIgnoreFilter: function userIgnoreFilter (opts, is_win32, finalDir) { + return function filter (file) { + if (is_win32) { + // convert slashes so unix-format ignores work + file = file.replace(/\\/g, '/') + } + + var ignore = opts.ignore || [] + if (!Array.isArray(ignore)) ignore = [ignore] + if (typeof finalDir !== 'undefined') { + ignore = ignore.concat([finalDir]) + } + for (var i = 0; i < ignore.length; i++) { + if (file.match(ignore[i])) { + return false + } + } + return true + } + } +} diff --git a/index.js b/index.js index 31f242c..6d5ed16 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,7 @@ var linux = require('./linux.js') var win32 = require('./win32.js') module.exports = function packager (opts, cb) { - var packager + var platformPackager var platform = opts.platform var arch = opts.arch var version = opts.version @@ -25,9 +25,9 @@ module.exports = function packager (opts, cb) { } switch (platform) { - case 'darwin': packager = mac; break - case 'linux': packager = linux; break - case 'win32': packager = win32; break + case 'darwin': platformPackager = mac; break + case 'linux': platformPackager = linux; break + case 'win32': platformPackager = win32; break default: return cb(new Error('Unsupported platform. Must be either darwin, linux, or win32')) } @@ -51,7 +51,7 @@ module.exports = function packager (opts, cb) { if (err) return cb(err) extract(zipPath, {dir: tmpDir}, function (err) { if (err) return cb(err) - packager.createApp(opts, tmpDir, cb) + platformPackager.createApp(opts, tmpDir, cb) }) }) }) diff --git a/linux.js b/linux.js index 4fe048d..83426c6 100644 --- a/linux.js +++ b/linux.js @@ -1,10 +1,8 @@ var path = require('path') var fs = require('fs') -var child = require('child_process') var mkdirp = require('mkdirp') var ncp = require('ncp').ncp -var rimraf = require('rimraf') -var asar = require('asar') +var common = require('./common') module.exports = { createApp: function createApp (opts, templateApp, cb) { @@ -28,16 +26,9 @@ module.exports = { } function copyUserApp () { - ncp(opts.dir, userAppDir, {filter: userFilter, dereference: true}, function copied (err) { + ncp(opts.dir, userAppDir, {filter: common.userIgnoreFilter(opts, false, finalDir), dereference: true}, function copied (err) { if (err) return cb(err) - if (opts.prune) { - prune(function pruned (err) { - if (err) return cb(err) - renameElectronBinary() - }) - } else { - renameElectronBinary() - } + common.prune(opts, userAppDir, cb, renameElectronBinary) }) } @@ -45,45 +36,17 @@ module.exports = { fs.rename(originalBinary, finalBinary, function electronRenamed (err) { if (err) return cb(err) if (opts.asar) { - asarApp(cb) + common.asarApp(finalDir, cb) } else { cb(null, finalBinary) } }) } - function prune (cb) { - child.exec('npm prune --production', { cwd: userAppDir }, cb) - } - function appFilter (file) { return file.match(/default_app/) === null } - function userFilter (file) { - var ignore = opts.ignore || [] - if (!Array.isArray(ignore)) ignore = [ignore] - ignore = ignore.concat([finalDir]) - for (var i = 0; i < ignore.length; i++) { - if (file.match(ignore[i])) { - return false - } - } - return true - } - - function asarApp (cb) { - var src = path.join(finalDir, 'resources', 'app') - var dest = path.join(finalDir, 'resources', 'app.asar') - asar.createPackage(src, dest, function (err) { - if (err) return cb(err) - rimraf(src, function (err) { - if (err) return cb(err) - cb(null, dest) - }) - }) - } - copyApp() } } diff --git a/mac.js b/mac.js index 26ab295..b125c74 100644 --- a/mac.js +++ b/mac.js @@ -7,7 +7,7 @@ var plist = require('plist') var mkdirp = require('mkdirp') var rimraf = require('rimraf') var ncp = require('ncp').ncp -var asar = require('asar') +var common = require('./common') module.exports = { createApp: function createApp (opts, electronPath, cb) { @@ -68,34 +68,10 @@ function buildMacApp (opts, cb, newApp) { fs.writeFileSync(paths.info1, plist.build(pl1)) fs.writeFileSync(paths.info2, plist.build(pl2)) - function filter (file) { - var ignore = opts.ignore || [] - if (!Array.isArray(ignore)) ignore = [ignore] - for (var i = 0; i < ignore.length; i++) { - if (file.match(ignore[i])) { - return false - } - } - return true - } - // copy users app into .app - ncp(opts.dir, paths.app, {filter: filter, dereference: true}, function copied (err) { + ncp(opts.dir, paths.app, {filter: common.userIgnoreFilter(opts), dereference: true}, function copied (err) { if (err) return cb(err) - if (opts.prune) { - prune(function pruned (err) { - if (err) return cb(err) - moveApp() - }) - } else { - moveApp() - } - - function prune (cb) { - child.exec('npm prune --production', { cwd: paths.app }, cb) - } - function moveApp () { // finally, move app into cwd var outdir = opts.out || process.cwd() @@ -106,7 +82,8 @@ function buildMacApp (opts, cb, newApp) { fs.rename(newApp, finalPath, function moved (err) { if (err) return cb(err) if (opts.asar) { - asarApp(function (err) { + var finalPath = path.join(opts.out || process.cwd(), opts.name + '.app', 'Contents', 'Resources') + common.asarApp(finalPath, function (err) { if (err) return cb(err) updateMacIcon(function (err) { if (err) return cb(err) @@ -135,16 +112,6 @@ function buildMacApp (opts, cb, newApp) { }) } - function asarApp (cb) { - var finalPath = path.join(opts.out || process.cwd(), opts.name + '.app', 'Contents', 'Resources') - var src = path.join(finalPath, 'app') - var dest = path.join(finalPath, 'app.asar') - asar.createPackage(src, dest, function (err) { - if (err) return cb(err) - rimraf(src, cb) - }) - } - function codesign () { var appPath = path.join(opts.out || process.cwd(), opts.name + '.app') @@ -154,5 +121,7 @@ function buildMacApp (opts, cb, newApp) { cb(err, appPath) }) } + + common.prune(opts, paths.app, cb, moveApp) }) } diff --git a/win32.js b/win32.js index be3a5eb..bc3eb81 100644 --- a/win32.js +++ b/win32.js @@ -1,13 +1,12 @@ var os = require('os') var path = require('path') -var child = require('child_process') var mkdirp = require('mkdirp') var rimraf = require('rimraf') var ncp = require('ncp').ncp -var asar = require('asar') var mv = require('mv') var rcedit = require('rcedit') +var common = require('./common') module.exports = { createApp: function createApp (opts, electronApp, cb) { @@ -50,44 +49,18 @@ function buildWinApp (opts, cb, newApp) { app: path.join(newApp, 'resources', 'app') } - function filter (file) { - // convert slashes so unix-format ignores work - file = file.replace(/\\/g, '/') - - var ignore = opts.ignore || [] - if (!Array.isArray(ignore)) ignore = [ignore] - for (var i = 0; i < ignore.length; i++) { - if (file.match(ignore[i])) { - return false - } - } - return true - } - - // copy users app into .app - ncp(opts.dir, paths.app, {filter: filter, dereference: true}, function copied (err) { + // copy users app into destination path + ncp(opts.dir, paths.app, {filter: common.userIgnoreFilter(opts, true), dereference: true}, function copied (err) { if (err) return cb(err) - if (opts.prune) { - prune(function pruned (err) { - if (err) return cb(err) - moveApp() - }) - } else { - moveApp() - } - - function prune (cb) { - child.exec('npm prune --production', { cwd: paths.app }, cb) - } - function moveApp () { // finally, move app into cwd var finalPath = path.join(opts.out || process.cwd(), opts.name + '-win32') copy(newApp, finalPath, function moved (err) { if (err) return cb(err) if (opts.asar) { - asarApp(function (err) { + var finalPath = path.join(opts.out || process.cwd(), opts.name + '-win32', 'resources') + common.asarApp(finalPath, function (err) { if (err) return cb(err) updateIcon() }) @@ -109,17 +82,8 @@ function buildWinApp (opts, cb, newApp) { rcedit(exePath, {icon: opts.icon}, function (err) { cb(err, finalPath) }) - } - function asarApp (cb) { - var finalPath = path.join(opts.out || process.cwd(), opts.name + '-win32', 'resources') - var src = path.join(finalPath, 'app') - var dest = path.join(finalPath, 'app.asar') - asar.createPackage(src, dest, function (err) { - if (err) return cb(err) - rimraf(src, cb) - }) - } + common.prune(opts, paths.app, cb, moveApp) }) }