Created
February 20, 2016 10:01
-
-
Save geowarin/93e7aaef640c6f90d9e4 to your computer and use it in GitHub Desktop.
Packagin an electron app with node-git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
runtime = electron | |
target = 0.36.7 | |
target_arch = x64 | |
disturl = https://atom.io/download/atom-shell |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const os = require('os'); | |
const packager = require('electron-packager'); | |
const del = require('del'); | |
const exec = require('child_process').execSync; | |
const pexec = require('promised-exec'); | |
const pkg = require('./package.json'); | |
const devDeps = Object.keys(pkg.devDependencies); | |
const semver = require('semver'); | |
const series = require('es6-promise-series'); | |
const argv = require('minimist')(process.argv.slice(2)); | |
const buildAllTargets = argv.all || false; | |
const platform = argv.platform || os.platform(); | |
const arch = argv.arch || os.arch(); | |
const iconPath = 'app/assets/icon/app'; // without extension | |
const DEFAULT_OPTS = { | |
dir: './', | |
name: pkg.productName, | |
// https://github.com/atom/electron/blob/master/docs/tutorial/application-packaging.md | |
asar: false, | |
ignore: [ | |
'/test($|/)', | |
'/tools($|/)', | |
'/release($|/)' | |
].concat(devDeps.map(name => `/node_modules/${name}($|/)`)), | |
version: getElectronVersion() | |
}; | |
const targets = buildAllTargets ? getAllTargets() : [{platform: platform, arch: arch}]; | |
checkNodeVersion(); | |
const builds = targets.map(target => () => build(target)); | |
series(builds) | |
.then(() => { | |
console.log("All done"); | |
}) | |
.catch(err => { | |
console.error('Error during the build:'); | |
console.error(err); | |
}); | |
function build(target) { | |
return clean(target) | |
.then(() => installNodeGit(target)) | |
.then(() => packageApp(target)); | |
} | |
// utils | |
function clean(target) { | |
console.log(`Removing release/${target.platform}-${target.arch}`); | |
return del(`release/${target.platform}-${target.arch}`); | |
} | |
function packageApp(target) { | |
console.log(`building: ${target.platform}-${target.arch}`); | |
const opts = Object.assign({}, DEFAULT_OPTS, target, { | |
icon: getIcon(target.platform), | |
prune: true, | |
out: `release/${target.platform}-${target.arch}` | |
}); | |
return doPackage(opts); | |
} | |
function checkNodeVersion() { | |
const npmVersion = exec('npm -v').toString(); | |
if (!semver.satisfies(npmVersion, '< 3.0.0')) { | |
throw new Error('Please use npm 2 to build'); | |
} | |
} | |
function installNodeGit(target) { | |
console.log(`Installing nodegit for ${target.platform}-${target.arch}`); | |
return pexec(`npm i nodegit --target_arch=${target.arch} --target_platform=${target.platform}`); | |
} | |
function doPackage(opts) { | |
return new Promise((resolve, reject) => { | |
packager(opts, function (err, appPath) { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(appPath); | |
} | |
}); | |
}) | |
} | |
function getElectronVersion() { | |
try { | |
const out = exec('npm list electron-prebuilt').toString(); | |
return out.split('electron-prebuilt@')[1].replace(/\s/g, ''); | |
} catch (err) { | |
return '0.36.2'; | |
} | |
} | |
function getAllTargets() { | |
const allVersions = []; | |
const archs = ['ia32', 'x64']; | |
const platforms = ['linux', 'win32', 'darwin']; | |
platforms.forEach(platform => { | |
archs.forEach(arch => { | |
if (!(platform === 'darwin' && arch === 'ia32')) { // darwin 32 does not exist | |
allVersions.push({platform, arch}) | |
} | |
}); | |
}); | |
return allVersions; | |
} | |
function getIcon(platform) { | |
const iconExt = platform === 'darwin' ? '.icns' : platform === 'win32' ? '.ico' : '.png'; | |
return iconPath + iconExt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment