2017-08-03 04:13:30 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/* eslint global-require: 0 */
|
|
|
|
/* eslint quote-props: 0 */
|
|
|
|
const path = require('path');
|
2017-08-11 13:47:06 +08:00
|
|
|
const fs = require('fs');
|
|
|
|
const rimraf = require('rimraf');
|
2017-08-03 04:13:30 +08:00
|
|
|
const safeExec = require('./utils/child-process-wrapper.js').safeExec;
|
|
|
|
|
2017-08-11 13:47:06 +08:00
|
|
|
const npmElectronTarget = require('../app/package.json').dependencies.electron;
|
2017-08-03 04:13:30 +08:00
|
|
|
const npmEnvs = {
|
|
|
|
system: process.env,
|
|
|
|
electron: Object.assign({}, process.env, {
|
2017-08-11 13:47:06 +08:00
|
|
|
'npm_config_target': npmElectronTarget,
|
2017-08-03 04:13:30 +08:00
|
|
|
'npm_config_arch': process.arch,
|
|
|
|
'npm_config_target_arch': process.arch,
|
|
|
|
'npm_config_disturl': 'https://atom.io/download/atom-shell',
|
|
|
|
'npm_config_runtime': 'electron',
|
|
|
|
'npm_config_build_from_source': true,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
function npm(cmd, options) {
|
|
|
|
const {cwd, env} = Object.assign({cwd: '.', env: 'system'}, options);
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-11 12:45:05 +08:00
|
|
|
console.log(`\n-- Running npm ${cmd} in ${cwd} with ${env} config --`)
|
2017-08-03 04:13:30 +08:00
|
|
|
|
|
|
|
safeExec(`npm ${cmd}`, {
|
|
|
|
cwd: path.resolve(__dirname, '..', cwd),
|
|
|
|
env: npmEnvs[env],
|
|
|
|
}, (err) => {
|
|
|
|
return err ? reject(err) : resolve(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-11 14:20:34 +08:00
|
|
|
// For speed, we cache app/node_modules. However, we need to
|
|
|
|
// be sure to do a full rebuild of native node modules when the
|
|
|
|
// Electron version changes. To do this we check a marker file.
|
2017-08-11 14:31:37 +08:00
|
|
|
const appModulesPath = path.resolve(__dirname, '..', 'app', 'node_modules');
|
|
|
|
const cacheVersionPath = path.join(appModulesPath, '.postinstall-target-version');
|
2017-08-11 14:20:34 +08:00
|
|
|
const cacheElectronTarget = fs.existsSync(cacheVersionPath) && fs.readFileSync(cacheVersionPath).toString();
|
|
|
|
|
|
|
|
if (cacheElectronTarget !== npmElectronTarget) {
|
|
|
|
console.log(`\n-- Clearing app/node_modules --`)
|
2017-08-11 14:31:37 +08:00
|
|
|
rimraf.sync(appModulesPath);
|
2017-08-11 13:47:06 +08:00
|
|
|
}
|
|
|
|
|
2017-08-11 14:20:34 +08:00
|
|
|
// run `npm install` in ./app with Electron NPM config
|
|
|
|
npm('install', {cwd: './app', env: 'electron'}).then(() =>
|
|
|
|
// run `npm dedupe` in ./app with Electron NPM config
|
|
|
|
npm('dedupe', {cwd: './app', env: 'electron'}).then(() =>
|
|
|
|
// write the marker with the electron version
|
|
|
|
fs.writeFileSync(cacheVersionPath, npmElectronTarget)
|
|
|
|
)
|
|
|
|
)
|