Skip to content

Commit

Permalink
Decouple main dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtkowiak committed Oct 3, 2018
1 parent b0277e4 commit 41e1907
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 2,326 deletions.
6 changes: 6 additions & 0 deletions lib/defaultDependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
electron: '2.0.10',
'electron-builder': '20.28.4',
'electron-packager': '12.1.2'
};

7 changes: 4 additions & 3 deletions lib/desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ export default class Desktop {
* @property {boolean} devTools
* @property {boolean} devtron
* @property {boolean} desktopHCP
* @property {string} autoUpdateFeedUrl
* @property {Object} autoUpdateFeedHeaders
* @property {Object} autoUpdateManualCheck
* @property {Object} squirell
* @property {string} squirell.autoUpdateFeedUrl
* @property {Object} squirell.autoUpdateFeedHeaders
* @property {Object} squirell.autoUpdateManualCheck
* @property {Object} desktopHCPSettings
* @property {boolean} desktopHCPSettings.ignoreCompatibilityVersion
* @property {boolean} desktopHCPSettings.blockAppUpdateOnDesktopIncompatibility
Expand Down
9 changes: 7 additions & 2 deletions lib/electron.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import regeneratorRuntime from 'regenerator-runtime/runtime';
import spawn from 'cross-spawn';
import pathToElectron from 'electron';

import Log from './log';
import defaultDependencies from './defaultDependencies';

/**
* Simple Electron runner. Runs the project with the bin provided by the 'electron' package.
Expand All @@ -13,6 +14,10 @@ export default class Electron {
this.$ = $;
}

async init() {
this.electron = await this.$.getDependency('electron', defaultDependencies.electron);
}

run() {
// Until: https://github.com/electron-userland/electron-prebuilt/pull/118
const { env } = process;
Expand All @@ -26,7 +31,7 @@ export default class Electron {

cmd.push('.');

const child = spawn(pathToElectron, cmd, {
const child = spawn(this.electron.dependency, cmd, {
cwd: this.$.env.paths.electronApp.root,
env
});
Expand Down
64 changes: 22 additions & 42 deletions lib/electronApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ import presetEnv from '@babel/preset-env';
import fs from 'fs';
import path from 'path';
import shell from 'shelljs';
import spawn from 'cross-spawn';
import semver from 'semver';
import uglify from 'terser';

import { getGypEnv } from 'app-builder-lib/out/util/yarn';

import Log from './log';
import ElectronAppScaffold from './electronAppScaffold';
import DependenciesManager from './dependenciesManager';
Expand Down Expand Up @@ -268,6 +265,16 @@ export default class ElectronApp {
this.compatibilityVersion = md5.digest('hex');
}

async init() {
try {
await this.$.electron.init();
await this.$.electronBuilder.init();
} catch (e) {
this.log.warn('error occurred while initialising electron and electron-builder integration', e);
process.exit(1);
}
}

/**
* Runs all necessary tasks to build the desktopified app.
*/
Expand All @@ -287,6 +294,9 @@ export default class ElectronApp {
}
}

await this.init();


try {
this.$.meteorApp.updateGitIgnore();
} catch (e) {
Expand Down Expand Up @@ -582,43 +592,6 @@ export default class ElectronApp {
}
}

/**
* NOT IN USE RIGHT NOW // DEPRECATED
*
* Wrapper for spawning npm.
* @param {Array} commands - commands for spawn
* @param {string} stdio
* @return {Promise}
*/
runNpm(commands, stdio = 'ignore') {
return new Promise((resolve, reject) => {
// TODO: find a way to run npm without depending on it cause it's a huge dependency.
let npm = path.join(
this.$.env.paths.meteorApp.root, 'node_modules', '.bin', 'npm'
);

if (!this.$.utils.exists(npm)) {
npm = path.join(
this.$.env.paths.meteorApp.root, 'node_modules', 'meteor-desktop', 'node_modules', '.bin', 'npm'
);
}

if (!this.$.utils.exists(npm)) {
npm = shell.which('npm');
}

this.log.verbose(`executing npm ${commands.join(' ')}`);

spawn(npm, commands, {
cwd: this.$.env.paths.electronApp.root,
stdio
}).on('exit', code => (
(code === 0) ? resolve() : reject(new Error(`npm exit code was ${code}`))
));
});
}


/**
* Runs npm link for every package specified in settings.json->linkPackages.
*/
Expand All @@ -631,7 +604,14 @@ export default class ElectronApp {
if ('linkPackages' in this.$.desktop.getSettings()) {
if (Array.isArray(settings.linkPackages)) {
settings.linkPackages.forEach(packageName =>
promises.push(this.runNpm(['link', packageName])));
promises.push(
this.$.meteorApp.runNpm(
['link', packageName],
undefined,
this.$.env.paths.electronApp.root
)
)
);
}
}
await Promise.all(promises);
Expand Down Expand Up @@ -728,7 +708,7 @@ export default class ElectronApp {
}
this.log.info('installing local node modules');
const lastRebuild = this.$.electronBuilder.prepareLastRebuildObject(arch);
const env = getGypEnv(lastRebuild.frameworkInfo, lastRebuild.platform, lastRebuild.arch);
const env = this.$.electronBuilder.getGypEnv(lastRebuild.frameworkInfo, lastRebuild.platform, lastRebuild.arch);
const installer = new LocalInstaller(
{ [this.$.env.paths.electronApp.root]: localDependencies },
{ npmEnv: env }
Expand Down
33 changes: 20 additions & 13 deletions lib/electronBuilder.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// eslint-disable-next-line no-unused-vars
import regeneratorRuntime from 'regenerator-runtime/runtime';
import { build, Platform, createTargets } from 'electron-builder';
import { installOrRebuild } from 'app-builder-lib/out/util/yarn';
import { createLazyProductionDeps } from 'app-builder-lib/out/util/packageDependencies';
import shell from 'shelljs';
import path from 'path';
import fs from 'fs';
import rimraf from 'rimraf';
import spawn from 'cross-spawn';
import Log from './log';
import defaultDependencies from './defaultDependencies';

/**
* Promisfied rimraf.
Expand Down Expand Up @@ -51,6 +49,15 @@ export default class InstallerBuilder {
this.installerDir = path.join(this.$.env.options.output, this.$.env.paths.installerDir);
}

async init() {
this.builder = await this.$.getDependency('electron-builder', defaultDependencies['electron-builder']);
const appBuilder = await this.$.getDependency('app-builder-lib', defaultDependencies['electron-builder']);

this.yarn = require(path.join(appBuilder.path, 'out', 'util', 'yarn'));
this.getGypEnv = this.yarn.getGypEnv;
this.packageDependencies = require(path.join(appBuilder.path, 'out', 'util', 'packageDependencies'));
}

/**
* Prepares the last rebuild object for electron-builder.
*
Expand All @@ -59,7 +66,7 @@ export default class InstallerBuilder {
* @returns {Object}
*/
prepareLastRebuildObject(arch, platform = process.platform) {
const productionDeps = createLazyProductionDeps(this.$.env.paths.electronApp.root);
const productionDeps = this.packageDependencies.createLazyProductionDeps(this.$.env.paths.electronApp.root);
this.lastRebuild = {
frameworkInfo: { version: this.$.getElectronVersion(), useCustomDist: true },
platform,
Expand All @@ -79,7 +86,7 @@ export default class InstallerBuilder {
async installOrRebuild(arch, platform = process.platform, install = false) {
this.log.debug(`calling installOrRebuild from electron-builder for arch ${arch}`);
this.prepareLastRebuildObject(arch, platform);
await installOrRebuild(this.$.desktop.getSettings().builderOptions || {},
await this.yarn.installOrRebuild(this.$.desktop.getSettings().builderOptions || {},
this.$.env.paths.electronApp.root, this.lastRebuild, install);
}

Expand Down Expand Up @@ -278,25 +285,25 @@ export default class InstallerBuilder {
const targets = [];

if (this.$.env.options.win) {
targets.push(Platform.WINDOWS);
targets.push(this.builder.dependency.Platform.WINDOWS);
}
if (this.$.env.options.linux) {
targets.push(Platform.LINUX);
targets.push(this.builder.dependency.Platform.LINUX);
}
if (this.$.env.options.mac) {
targets.push(Platform.MAC);
targets.push(this.builder.dependency.Platform.MAC);
}

if (targets.length === 0) {
if (this.$.env.os.isWindows) {
targets.push(Platform.WINDOWS);
targets.push(this.builder.dependency.Platform.WINDOWS);
} else if (this.$.env.os.isLinux) {
targets.push(Platform.LINUX);
targets.push(this.builder.dependency.Platform.LINUX);
} else {
targets.push(Platform.MAC);
targets.push(this.builder.dependency.Platform.MAC);
}
}
return createTargets(targets, null, arch);
return this.builder.dependency.createTargets(targets, null, arch);
}

async build() {
Expand Down Expand Up @@ -324,7 +331,7 @@ export default class InstallerBuilder {

try {
this.log.debug('calling build from electron-builder');
await build(Object.assign({
await this.builder.dependency.build(Object.assign({
targets: this.prepareTargets(),
config: builderOptions
}, settings.builderCliOptions));
Expand Down
38 changes: 35 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class MeteorDesktop {
this.electronApp = new ElectronApp(this);
this.desktop = new Desktop(this);
this.meteorApp = new MeteorApp(this);
this.packager = new Packager(this);
this.utils = utils;
}

Expand Down Expand Up @@ -91,10 +90,10 @@ class MeteorDesktop {
return version;
}


init() {
async init() {
this.desktop.scaffold();
this.meteorApp.updateGitIgnore();
await this.electronApp.init();
}

async buildInstaller(throwError = false) {
Expand Down Expand Up @@ -123,12 +122,45 @@ class MeteorDesktop {
}

async runPackager() {
this.packager = new Packager(this);
await this.packager.init();
await this.electronApp.build();

this.packager.packageApp().catch((e) => {
this.log.error(`while trying to build a package an error occurred: ${e}`);
});
}

async getDependency(name, version) {
const dependencyPath = path.join(this.env.paths.meteorApp.root, 'node_modules', name);
let dependency;
try {
dependency = require(dependencyPath);
} catch (e) {
this.log.warn(`could not find ${name}, installing the default version for you: ${name}@${version}`);
try {
await this.meteorApp.runNpm(['i', '-D', '-E', '--only=dev', `${name}@${version}`], 'inherit');
} catch (e) {
this.log.error(e);
process.exit(1);
}
} finally {
dependency = require(dependencyPath);
}
const dependencyVersion = require(path.join(dependencyPath, 'package.json')).version;

if (dependencyVersion !== version) {
if (dependencyVersion.split('.')[0] !== version.split('.')[0]) {
this.log.warn(`you are using a ${name}@${dependencyVersion} while the recommended version is ` +
`${version}, the compatibility version is different, use at your own risk, be sure to report ` +
'that when submitting issues');
} else {
this.log.warn(`you are using a ${name}@${dependencyVersion} while the recommended version is ` +
`${version}, be sure to report that when submitting issues`);
}
}
return { dependency, path: dependencyPath };
}
}

export default function exports(input, output, options, { log = Logger } = { log: Logger }) {
Expand Down
27 changes: 27 additions & 0 deletions lib/meteorApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,4 +909,31 @@ export default class MeteorApp {
}
));
}

/**
* Wrapper for spawning npm.
* @param {Array} commands - commands for spawn
* @param {string} stdio
* @param {string} cwd
* @return {Promise}
*/
runNpm(commands, stdio = 'ignore', cwd = this.$.env.paths.meteorApp.root) {
return new Promise((resolve, reject) => {
const npm = shell.which('npm');
if (!npm) {
reject('npm not found');
return;
}

this.log.verbose(`executing npm ${commands.join(' ')}`);

spawn(npm, commands, {
cwd,
stdio
}).on('exit', code => (
(code === 0) ? resolve() : reject(new Error(`npm exit code was ${code}`))
));
});
}

}
8 changes: 6 additions & 2 deletions lib/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import assignIn from 'lodash/assignIn';
import path from 'path';
import fs from 'fs';
import shell from 'shelljs';
import packager from 'electron-packager';

import Log from './log';
import defaultDependencies from './defaultDependencies';

const { join } = path;

Expand All @@ -20,6 +20,10 @@ export default class ElectronPackager {
this.$ = $;
}

async init() {
this.packager = await this.$.getDependency('electron-packager', defaultDependencies['electron-packager']).dependency;
}

/**
* Runs the packager with provided arguments.
*
Expand All @@ -28,7 +32,7 @@ export default class ElectronPackager {
*/
runPackager(args) {
return new Promise((resolve, reject) => {
packager(args, (err) => {
this.packager(args, (err) => {
if (err) {
reject(err);
} else {
Expand Down
Loading

0 comments on commit 41e1907

Please sign in to comment.