From b126237994f1cc03c4c9290670d2bc6725f48abc Mon Sep 17 00:00:00 2001 From: jden Date: Wed, 6 May 2015 12:11:03 -0700 Subject: [PATCH 1/2] add windows support #3 --- cli.js | 4 +- index.js | 1 + package.json | 2 + windows.js | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 windows.js diff --git a/cli.js b/cli.js index 6d9010f..f24e7f5 100755 --- a/cli.js +++ b/cli.js @@ -18,10 +18,10 @@ if (!args.dir || !args.name) { console.error('Usage: electron-packager ') process.exit(1) } - +console.log('args', args) packager(args, function done (err, appPath) { if (err) { - console.error(err) + console.error(err.stack) process.exit(1) } diff --git a/index.js b/index.js index 3c4a3c9..d54bc3c 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,7 @@ module.exports = function packager (opts, cb) { switch (os.platform()) { case 'darwin': platform = require('./mac'); break case 'linux': platform = require('./linux'); break + case 'win32': platform = require('./windows'); break default: cb(new Error('Unsupported platform')) } diff --git a/package.json b/package.json index 07fc4df..958ae8d 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,10 @@ "asar": "^0.6.1", "minimist": "^1.1.1", "mkdirp": "^0.5.0", + "mv": "^2.0.3", "ncp": "^2.0.0", "plist": "^1.1.0", + "rcedit": "^0.3.0", "rimraf": "^2.3.2" }, "devDependencies": { diff --git a/windows.js b/windows.js new file mode 100644 index 0000000..06fa516 --- /dev/null +++ b/windows.js @@ -0,0 +1,126 @@ +var os = require('os') +var path = require('path') +var fs = require('fs') +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') + +module.exports = { + createApp: function createApp (opts, cb, electronPath) { + var electronApp = path.join(electronPath, 'dist') + var tmpDir = path.join(os.tmpdir(), 'electron-packager-windows') + + var newApp = path.join(tmpDir, opts.name + '.app') + console.log('newApp', newApp) + // reset build folders + copy template app + rimraf(tmpDir, function rmrfd () { + // ignore errors + mkdirp(newApp, function mkdirpd () { + // ignore errors + // copy .app folder and use as template (this is exactly what Atom editor does) + ncp(electronApp, newApp, function copied (err) { + if (err) return cb(err) + // rename electron.exe + mv(path.join(newApp,'electron.exe'), path.join(newApp, opts.name+'.exe'), function (err) { + if (err) return cb(err) + + buildWinApp(opts, cb, newApp) + }) + }) + }) + }) + } +} + +function copy (from, to, cb) { + rimraf(to, function () { + mkdirp(to, function () { + ncp(from, to, function (err) { + if (err) { return cb(err) } + cb() + }) + }) + }) +} + +function buildWinApp (opts, cb, newApp) { + var paths = { + app: path.join(newApp, 'resources','app') + } + + 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}, 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 + '.app') + console.log('finalPath', finalPath) + copy(newApp, finalPath, function moved (err) { + if (err) return cb(err) + if (opts.asar) { + asarApp(function (err) { + if (err) return cb(err) + updateIcon() + }) + } else { + updateIcon() + } + }) + } + + function updateIcon () { + var finalPath = path.join(opts.out || process.cwd(), opts.name + '.app') + + if (!opts.icon) { + return cb(null, finalPath) + } + + var exePath = path.join(opts.out || process.cwd(), opts.name + '.app', opts.name+'.exe') + + rcedit(exePath, {icon: opts.icon}, function (err) { + cb(err, finalPath) + }) + + } + + function asarApp (cb) { + var finalPath = path.join(opts.out || process.cwd(), opts.name + '.app', '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 00440bc6858cc7c8d10299839134ed1ec99d8ba1 Mon Sep 17 00:00:00 2001 From: jden Date: Wed, 6 May 2015 12:19:03 -0700 Subject: [PATCH 2/2] fix javascript standard style issues --- windows.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/windows.js b/windows.js index 06fa516..d5a8f91 100644 --- a/windows.js +++ b/windows.js @@ -1,6 +1,5 @@ var os = require('os') var path = require('path') -var fs = require('fs') var child = require('child_process') var mkdirp = require('mkdirp') @@ -25,12 +24,12 @@ module.exports = { // copy .app folder and use as template (this is exactly what Atom editor does) ncp(electronApp, newApp, function copied (err) { if (err) return cb(err) - // rename electron.exe - mv(path.join(newApp,'electron.exe'), path.join(newApp, opts.name+'.exe'), function (err) { - if (err) return cb(err) - - buildWinApp(opts, cb, newApp) - }) + // rename electron.exe + mv(path.join(newApp, 'electron.exe'), path.join(newApp, opts.name + '.exe'), function (err) { + if (err) return cb(err) + + buildWinApp(opts, cb, newApp) + }) }) }) }) @@ -50,7 +49,7 @@ function copy (from, to, cb) { function buildWinApp (opts, cb, newApp) { var paths = { - app: path.join(newApp, 'resources','app') + app: path.join(newApp, 'resources', 'app') } function filter (file) { @@ -105,7 +104,7 @@ function buildWinApp (opts, cb, newApp) { return cb(null, finalPath) } - var exePath = path.join(opts.out || process.cwd(), opts.name + '.app', opts.name+'.exe') + var exePath = path.join(opts.out || process.cwd(), opts.name + '.app', opts.name + '.exe') rcedit(exePath, {icon: opts.icon}, function (err) { cb(err, finalPath)