2015-05-05 04:11:53 +00:00
|
|
|
var path = require('path')
|
|
|
|
var fs = require('fs')
|
|
|
|
var child = require('child_process')
|
|
|
|
var mkdirp = require('mkdirp')
|
|
|
|
var ncp = require('ncp').ncp
|
2015-05-06 04:32:17 +00:00
|
|
|
var rimraf = require('rimraf')
|
|
|
|
var asar = require('asar')
|
2015-05-05 04:11:53 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createApp: function createApp (opts, cb, electronPath) {
|
|
|
|
var templateApp = path.join(electronPath, 'dist')
|
|
|
|
var finalDir = opts.out || path.join(process.cwd(), 'dist')
|
|
|
|
var userAppDir = path.join(finalDir, 'resources', 'default_app')
|
|
|
|
var originalBinary = path.join(finalDir, 'electron')
|
|
|
|
var finalBinary = path.join(finalDir, opts.name)
|
|
|
|
|
|
|
|
function copyApp () {
|
|
|
|
mkdirp(finalDir, function AppFolderCreated (err) {
|
|
|
|
if (err) return cb(err)
|
|
|
|
copyAppTemplate()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyAppTemplate () {
|
|
|
|
ncp(templateApp, finalDir, {filter: appFilter}, function AppCreated (err) {
|
|
|
|
if (err) return cb(err)
|
|
|
|
copyUserApp()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyUserApp () {
|
|
|
|
ncp(opts.dir, userAppDir, {filter: userFilter}, function copied (err) {
|
|
|
|
if (err) return cb(err)
|
|
|
|
if (opts.prune) {
|
|
|
|
prune(function pruned (err) {
|
|
|
|
if (err) return cb(err)
|
|
|
|
renameElectronBinary()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
renameElectronBinary()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function renameElectronBinary () {
|
|
|
|
fs.rename(originalBinary, finalBinary, function electronRenamed (err) {
|
|
|
|
if (err) return cb(err)
|
2015-05-06 04:32:17 +00:00
|
|
|
if (opts.asar) {
|
|
|
|
asarApp(cb)
|
|
|
|
} else {
|
|
|
|
cb()
|
|
|
|
}
|
2015-05-05 04:11:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:32:17 +00:00
|
|
|
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, cb)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-05 04:11:53 +00:00
|
|
|
copyApp()
|
|
|
|
}
|
|
|
|
}
|