2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 02:28:55 +00:00

Merge pull request #61 from malept/deduplicate-code

Deduplicate code + cleanup
This commit is contained in:
Mark Lee 2015-06-11 08:29:10 -07:00
commit 81340093ee
5 changed files with 71 additions and 125 deletions

50
common.js Normal file
View File

@ -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
}
}
}

View File

@ -11,7 +11,7 @@ var linux = require('./linux.js')
var win32 = require('./win32.js') var win32 = require('./win32.js')
module.exports = function packager (opts, cb) { module.exports = function packager (opts, cb) {
var packager var platformPackager
var platform = opts.platform var platform = opts.platform
var arch = opts.arch var arch = opts.arch
var version = opts.version var version = opts.version
@ -25,9 +25,9 @@ module.exports = function packager (opts, cb) {
} }
switch (platform) { switch (platform) {
case 'darwin': packager = mac; break case 'darwin': platformPackager = mac; break
case 'linux': packager = linux; break case 'linux': platformPackager = linux; break
case 'win32': packager = win32; break case 'win32': platformPackager = win32; break
default: return cb(new Error('Unsupported platform. Must be either darwin, linux, or win32')) 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) if (err) return cb(err)
extract(zipPath, {dir: tmpDir}, function (err) { extract(zipPath, {dir: tmpDir}, function (err) {
if (err) return cb(err) if (err) return cb(err)
packager.createApp(opts, tmpDir, cb) platformPackager.createApp(opts, tmpDir, cb)
}) })
}) })
}) })

View File

@ -1,10 +1,8 @@
var path = require('path') var path = require('path')
var fs = require('fs') var fs = require('fs')
var child = require('child_process')
var mkdirp = require('mkdirp') var mkdirp = require('mkdirp')
var ncp = require('ncp').ncp var ncp = require('ncp').ncp
var rimraf = require('rimraf') var common = require('./common')
var asar = require('asar')
module.exports = { module.exports = {
createApp: function createApp (opts, templateApp, cb) { createApp: function createApp (opts, templateApp, cb) {
@ -28,16 +26,9 @@ module.exports = {
} }
function copyUserApp () { 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 (err) return cb(err)
if (opts.prune) { common.prune(opts, userAppDir, cb, renameElectronBinary)
prune(function pruned (err) {
if (err) return cb(err)
renameElectronBinary()
})
} else {
renameElectronBinary()
}
}) })
} }
@ -45,45 +36,17 @@ module.exports = {
fs.rename(originalBinary, finalBinary, function electronRenamed (err) { fs.rename(originalBinary, finalBinary, function electronRenamed (err) {
if (err) return cb(err) if (err) return cb(err)
if (opts.asar) { if (opts.asar) {
asarApp(cb) common.asarApp(finalDir, cb)
} else { } else {
cb(null, finalBinary) cb(null, finalBinary)
} }
}) })
} }
function prune (cb) {
child.exec('npm prune --production', { cwd: userAppDir }, cb)
}
function appFilter (file) { function appFilter (file) {
return file.match(/default_app/) === null 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() copyApp()
} }
} }

43
mac.js
View File

@ -7,7 +7,7 @@ var plist = require('plist')
var mkdirp = require('mkdirp') var mkdirp = require('mkdirp')
var rimraf = require('rimraf') var rimraf = require('rimraf')
var ncp = require('ncp').ncp var ncp = require('ncp').ncp
var asar = require('asar') var common = require('./common')
module.exports = { module.exports = {
createApp: function createApp (opts, electronPath, cb) { 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.info1, plist.build(pl1))
fs.writeFileSync(paths.info2, plist.build(pl2)) 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 // 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 (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 () { function moveApp () {
// finally, move app into cwd // finally, move app into cwd
var outdir = opts.out || process.cwd() var outdir = opts.out || process.cwd()
@ -106,7 +82,8 @@ function buildMacApp (opts, cb, newApp) {
fs.rename(newApp, finalPath, function moved (err) { fs.rename(newApp, finalPath, function moved (err) {
if (err) return cb(err) if (err) return cb(err)
if (opts.asar) { 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) if (err) return cb(err)
updateMacIcon(function (err) { updateMacIcon(function (err) {
if (err) return cb(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 () { function codesign () {
var appPath = path.join(opts.out || process.cwd(), opts.name + '.app') var appPath = path.join(opts.out || process.cwd(), opts.name + '.app')
@ -154,5 +121,7 @@ function buildMacApp (opts, cb, newApp) {
cb(err, appPath) cb(err, appPath)
}) })
} }
common.prune(opts, paths.app, cb, moveApp)
}) })
} }

View File

@ -1,13 +1,12 @@
var os = require('os') var os = require('os')
var path = require('path') var path = require('path')
var child = require('child_process')
var mkdirp = require('mkdirp') var mkdirp = require('mkdirp')
var rimraf = require('rimraf') var rimraf = require('rimraf')
var ncp = require('ncp').ncp var ncp = require('ncp').ncp
var asar = require('asar')
var mv = require('mv') var mv = require('mv')
var rcedit = require('rcedit') var rcedit = require('rcedit')
var common = require('./common')
module.exports = { module.exports = {
createApp: function createApp (opts, electronApp, cb) { createApp: function createApp (opts, electronApp, cb) {
@ -50,44 +49,18 @@ function buildWinApp (opts, cb, newApp) {
app: path.join(newApp, 'resources', 'app') app: path.join(newApp, 'resources', 'app')
} }
function filter (file) { // copy users app into destination path
// convert slashes so unix-format ignores work ncp(opts.dir, paths.app, {filter: common.userIgnoreFilter(opts, true), dereference: true}, function copied (err) {
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) {
if (err) return cb(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 () { function moveApp () {
// finally, move app into cwd // finally, move app into cwd
var finalPath = path.join(opts.out || process.cwd(), opts.name + '-win32') var finalPath = path.join(opts.out || process.cwd(), opts.name + '-win32')
copy(newApp, finalPath, function moved (err) { copy(newApp, finalPath, function moved (err) {
if (err) return cb(err) if (err) return cb(err)
if (opts.asar) { 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) if (err) return cb(err)
updateIcon() updateIcon()
}) })
@ -109,17 +82,8 @@ function buildWinApp (opts, cb, newApp) {
rcedit(exePath, {icon: opts.icon}, function (err) { rcedit(exePath, {icon: opts.icon}, function (err) {
cb(err, finalPath) cb(err, finalPath)
}) })
} }
function asarApp (cb) { common.prune(opts, paths.app, cb, moveApp)
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)
})
}
}) })
} }