From 83dd1dcb34b756db990d9fad587b56399533e72f Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Tue, 9 Jun 2015 19:17:21 -0700 Subject: [PATCH 1/5] Move asar code to a common location --- common.js | 17 +++++++++++++++++ linux.js | 17 ++--------------- mac.js | 15 +++------------ win32.js | 16 +++------------- 4 files changed, 25 insertions(+), 40 deletions(-) create mode 100644 common.js diff --git a/common.js b/common.js new file mode 100644 index 0000000..2c9d522 --- /dev/null +++ b/common.js @@ -0,0 +1,17 @@ +var asar = require('asar') +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) + }) + }) + } +} diff --git a/linux.js b/linux.js index 4fe048d..a00bbeb 100644 --- a/linux.js +++ b/linux.js @@ -3,8 +3,7 @@ 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) { @@ -45,7 +44,7 @@ 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) } @@ -72,18 +71,6 @@ module.exports = { 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..6027d11 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) { @@ -106,7 +106,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 +136,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') diff --git a/win32.js b/win32.js index be3a5eb..ca5b31a 100644 --- a/win32.js +++ b/win32.js @@ -5,9 +5,9 @@ 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) { @@ -87,7 +87,8 @@ function buildWinApp (opts, cb, newApp) { 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 +110,6 @@ 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) - }) } }) } From f3d17bceb2f66df964a71cfd1bd28a92cd549cdc Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Tue, 9 Jun 2015 19:17:57 -0700 Subject: [PATCH 2/5] Fix comment --- win32.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/win32.js b/win32.js index ca5b31a..f28176d 100644 --- a/win32.js +++ b/win32.js @@ -64,7 +64,7 @@ function buildWinApp (opts, cb, newApp) { return true } - // copy users app into .app + // copy users app into destination path ncp(opts.dir, paths.app, {filter: filter, dereference: true}, function copied (err) { if (err) return cb(err) From 16b2feb39f848667c512a2ca9cca3b283a1869e4 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Tue, 9 Jun 2015 19:26:55 -0700 Subject: [PATCH 3/5] Move user ignore filter to common module --- common.js | 21 +++++++++++++++++++++ linux.js | 14 +------------- mac.js | 13 +------------ win32.js | 16 +--------------- 4 files changed, 24 insertions(+), 40 deletions(-) diff --git a/common.js b/common.js index 2c9d522..19172fc 100644 --- a/common.js +++ b/common.js @@ -13,5 +13,26 @@ module.exports = { cb(null, dest) }) }) + }, + + 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/linux.js b/linux.js index a00bbeb..fa37391 100644 --- a/linux.js +++ b/linux.js @@ -27,7 +27,7 @@ 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) { @@ -59,18 +59,6 @@ module.exports = { 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 - } - copyApp() } } diff --git a/mac.js b/mac.js index 6027d11..b22b110 100644 --- a/mac.js +++ b/mac.js @@ -68,19 +68,8 @@ 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) { diff --git a/win32.js b/win32.js index f28176d..e5d7ab7 100644 --- a/win32.js +++ b/win32.js @@ -50,22 +50,8 @@ 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 destination path - ncp(opts.dir, paths.app, {filter: filter, dereference: true}, function copied (err) { + ncp(opts.dir, paths.app, {filter: common.userIgnoreFilter(opts, true), dereference: true}, function copied (err) { if (err) return cb(err) if (opts.prune) { From bdc8c46362a3fa0639dac5b32ec47d2b0d9faa47 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Tue, 9 Jun 2015 19:30:43 -0700 Subject: [PATCH 4/5] Move prune to common module --- common.js | 12 ++++++++++++ linux.js | 14 +------------- mac.js | 15 ++------------- win32.js | 16 ++-------------- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/common.js b/common.js index 19172fc..24fd323 100644 --- a/common.js +++ b/common.js @@ -1,4 +1,5 @@ var asar = require('asar') +var child = require('child_process') var path = require('path') var rimraf = require('rimraf') @@ -15,6 +16,17 @@ module.exports = { }) }, + 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) { diff --git a/linux.js b/linux.js index fa37391..83426c6 100644 --- a/linux.js +++ b/linux.js @@ -1,6 +1,5 @@ var path = require('path') var fs = require('fs') -var child = require('child_process') var mkdirp = require('mkdirp') var ncp = require('ncp').ncp var common = require('./common') @@ -29,14 +28,7 @@ module.exports = { function copyUserApp () { 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) }) } @@ -51,10 +43,6 @@ module.exports = { }) } - function prune (cb) { - child.exec('npm prune --production', { cwd: userAppDir }, cb) - } - function appFilter (file) { return file.match(/default_app/) === null } diff --git a/mac.js b/mac.js index b22b110..b125c74 100644 --- a/mac.js +++ b/mac.js @@ -72,19 +72,6 @@ function buildMacApp (opts, cb, newApp) { 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() @@ -134,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 e5d7ab7..bc3eb81 100644 --- a/win32.js +++ b/win32.js @@ -1,6 +1,5 @@ var os = require('os') var path = require('path') -var child = require('child_process') var mkdirp = require('mkdirp') var rimraf = require('rimraf') @@ -54,19 +53,6 @@ function buildWinApp (opts, cb, newApp) { 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') @@ -97,5 +83,7 @@ function buildWinApp (opts, cb, newApp) { cb(err, finalPath) }) } + + common.prune(opts, paths.app, cb, moveApp) }) } From 9bcb1a81d913ade6d3671e90b3e825c1c9f92106 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Tue, 9 Jun 2015 19:37:46 -0700 Subject: [PATCH 5/5] Rename variable which mirrors the function name --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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) }) }) })