Skip to content
Permalink
Browse files

feat(core): add basic support for non-exact Electron versions

ISSUES CLOSED: #679
  • Loading branch information
malept committed Jan 29, 2019
1 parent 45d454a commit 177012e9602bd424b03523204e189ffcdb692efe
@@ -152,7 +152,7 @@ export default async ({

await runHook(forgeConfig, 'preMake');

for (const targetArch of parseArchs(platform, arch, getElectronVersion(packageJSON))) {
for (const targetArch of parseArchs(platform, arch, await getElectronVersion(dir, packageJSON))) {
const packageDir = path.resolve(actualOutDir, `${appName}-${actualTargetPlatform}-${targetArch}`);
if (!(await fs.pathExists(packageDir))) {
throw new Error(`Couldn't find packaged app at: ${packageDir}`);
@@ -171,7 +171,7 @@ export default async ({
afterExtract: sequentialHooks(afterExtractHooks),
afterPrune: sequentialHooks(afterPruneHooks),
out: calculatedOutDir,
electronVersion: getElectronVersion(packageJSON),
electronVersion: await getElectronVersion(dir, packageJSON),
});
packageOpts.quiet = true;

@@ -41,7 +41,7 @@ export default async ({

await rebuild(
dir,
getElectronVersion(packageJSON),
await getElectronVersion(dir, packageJSON),
process.platform as ForgePlatform,
process.arch as ForgeArch,
forgeConfig.electronRebuildConfig,
@@ -1,4 +1,8 @@
import debug from 'debug';
import fs from 'fs-extra';
import path from 'path';
import semver from 'semver';
import yarnOrNpm from './yarn-or-npm';

const d = debug('electron-forge:electron-version');

@@ -13,15 +17,28 @@ function findElectronDep(dep: string): boolean {
return electronPackageNames.includes(dep);
}

export function getElectronVersion(packageJSON: any) {
export async function getElectronVersion(dir: string, packageJSON: any): Promise<string> {
if (!packageJSON.devDependencies) {
throw new Error('package.json for app does not have any devDependencies'.red);
}
const packageName = electronPackageNames.find(pkg => packageJSON.devDependencies[pkg]);
if (packageName === undefined) {
throw new Error('Could not find any Electron packages in devDependencies');
}
return packageJSON.devDependencies[packageName];

let version = packageJSON.devDependencies[packageName];
if (!semver.valid(version)) {
// It's not an exact version, find it in the actual module
const electronPackageJSONPath = path.join(dir, 'node_modules', packageName, 'package.json');
if (await fs.pathExists(electronPackageJSONPath)) {
const electronPackageJSON = await fs.readJson(electronPackageJSONPath);
version = electronPackageJSON.version;
} else {
throw new Error(`Cannot find the package "${packageName}". Perhaps you need to run "${yarnOrNpm()} install" in "${dir}"?`);
}
}

return version;
}

export function updateElectronDependency(packageJSON: any, dev: string[], exact: string[]): [string[], string[]] {
@@ -24,13 +24,11 @@ export default async (dir: string) => {

// TODO: Move this check to inside the forge config resolver and use
// mutatedPackageJson reader
const electronVersion = getElectronVersion(packageJSON);
if (electronVersion) {
if (!/[0-9]/.test(electronVersion[0])) {
lastError = `You must depend on an EXACT version of electron not a range (${electronVersion})`;
}
} else {
lastError = 'You must depend on "electron" in your devDependencies';
let electronVersion;
try {
electronVersion = await getElectronVersion(mDir, packageJSON);
} catch (err) {
lastError = err.message;
}

if (packageJSON.config && packageJSON.config.forge) {
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import path from 'path';
import { getElectronVersion, updateElectronDependency } from '../../src/util/electron-version';
import { deps, devDeps, exactDevDeps } from '../../src/api/init-scripts/init-npm';

@@ -31,38 +32,48 @@ describe('updateElectronDependency', () => {

describe('getElectronVersion', () => {
it('fails without devDependencies', () => {
expect(() => getElectronVersion({})).to.throw('does not have any devDependencies');
return expect(getElectronVersion('', {})).to.eventually.be.rejectedWith('does not have any devDependencies');
});

it('fails without electron devDependencies', () => {
expect(() => getElectronVersion({ devDependencies: {} })).to.throw('Electron packages in devDependencies');
return expect(getElectronVersion('', { devDependencies: {} })).to.eventually.be.rejectedWith('Electron packages in devDependencies');
});

it('fails with a non-exact version and no electron installed', () => {
const fixtureDir = path.resolve(__dirname, '..', 'fixture', 'dummy_app');
return expect(getElectronVersion(fixtureDir, { devDependencies: { electron: '^4.0.2' } })).to.eventually.be.rejectedWith('Cannot find the package');
});

it('works with a non-exact version with electron installed', () => {
const fixtureDir = path.resolve(__dirname, '..', 'fixture', 'non-exact');
return expect(getElectronVersion(fixtureDir, { devDependencies: { electron: '^4.0.2' } })).to.eventually.equal('4.0.9');
});

it('works with electron-prebuilt-compile', () => {
const packageJSON = {
devDependencies: { 'electron-prebuilt-compile': '1.0.0' },
};
expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0');
return expect(getElectronVersion('', packageJSON)).to.eventually.equal('1.0.0');
});

it('works with electron-prebuilt', () => {
it('works with electron-prebuilt', async () => {
const packageJSON = {
devDependencies: { 'electron-prebuilt': '1.0.0' },
};
expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0');
return expect(await getElectronVersion('', packageJSON)).to.be.equal('1.0.0');
});

it('works with electron-nightly', () => {
it('works with electron-nightly', async () => {
const packageJSON = {
devDependencies: { 'electron-nightly': '5.0.0-nightly.20190107' },
};
expect(getElectronVersion(packageJSON)).to.be.equal('5.0.0-nightly.20190107');
return expect(await getElectronVersion('', packageJSON)).to.be.equal('5.0.0-nightly.20190107');
});

it('works with electron', () => {
it('works with electron', async () => {
const packageJSON = {
devDependencies: { electron: '1.0.0' },
};
expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0');
return expect(await getElectronVersion('', packageJSON)).to.be.equal('1.0.0');
});
});

Some generated files are not rendered by default. Learn more.

0 comments on commit 177012e

Please sign in to comment.
You can’t perform that action at this time.