Skip to content
Permalink
Browse files

fix: search for the top-level node_modules folder when using yarn wor…

…kspaces (#902)
  • Loading branch information
malept committed Jun 12, 2019
1 parent 92d7ab5 commit a91d8b317c2649ef3f1ad157305056053c7f0584
@@ -53,6 +53,7 @@
"electron-rebuild": "^1.8.5",
"express": "^4.17.1",
"express-ws": "^4.0.0",
"find-up": "^4.0.0",
"form-data": "2.3.3",
"fs-extra": "^7.0.0",
"glob": "^7.1.4",
@@ -44,6 +44,7 @@
"electron-download": "^4.1.1",
"electron-packager": "^13.0.0",
"electron-rebuild": "^1.8.5",
"find-up": "^4.0.0",
"fs-extra": "^7.0.0",
"glob": "^7.1.4",
"lodash.merge": "^4.6.0",
@@ -62,4 +63,4 @@
"engines": {
"node": ">= 8.0"
}
}
}
@@ -1,8 +1,9 @@
import debug from 'debug';
import findUp from 'find-up';
import fs from 'fs-extra';
import path from 'path';
import semver from 'semver';
import yarnOrNpm from './yarn-or-npm';
import yarnOrNpm, { hasYarn } from './yarn-or-npm';

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

@@ -17,6 +18,34 @@ function findElectronDep(dep: string): boolean {
return electronPackageNames.includes(dep);
}

async function findAncestorNodeModulesPath(dir: string): Promise<string | undefined> {
if (hasYarn()) {
const yarnLockPath = await findUp('yarn.lock', { cwd: dir, type: 'file' });
if (yarnLockPath) {
const nodeModulesPath = path.join(path.dirname(yarnLockPath), 'node_modules');
if (await fs.pathExists(nodeModulesPath)) {
return nodeModulesPath;
}
}
}

return Promise.resolve(undefined);
}

async function determineNodeModulesPath(dir: string): Promise<string | undefined> {
const nodeModulesPath: string | undefined = path.join(dir, 'node_modules');
if (await fs.pathExists(nodeModulesPath)) {
return nodeModulesPath;
}
return findAncestorNodeModulesPath(dir);
}

export class PackageNotFoundError extends Error {
constructor(packageName: string, dir: string) {
super(`Cannot find the package "${packageName}". Perhaps you need to run "${yarnOrNpm()} install" in "${dir}"?`);
}
}

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);
@@ -27,15 +56,18 @@ export async function getElectronVersion(dir: string, packageJSON: any): Promise
}

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 (!semver.valid(version)) { // It's not an exact version, find it in the actual module
const nodeModulesPath = await determineNodeModulesPath(dir);
if (!nodeModulesPath) {
throw new PackageNotFoundError(packageName, dir);
}
const electronPackageJSONPath = path.join(nodeModulesPath, packageName, 'package.json');
if (await fs.pathExists(electronPackageJSONPath)) {
const electronPackageJSON = await fs.readJson(electronPackageJSONPath);
// eslint-disable-next-line prefer-destructuring
version = electronPackageJSON.version;
} else {
throw new Error(`Cannot find the package "${packageName}". Perhaps you need to run "${yarnOrNpm()} install" in "${dir}"?`);
throw new PackageNotFoundError(packageName, dir);
}
}

@@ -2,6 +2,7 @@ import { expect } from 'chai';
import path from 'path';
import { getElectronVersion, updateElectronDependency } from '../../src/util/electron-version';
import { devDeps, exactDevDeps } from '../../src/api/init-scripts/init-npm';
import { hasYarn } from '../../src/util/yarn-or-npm';

describe('updateElectronDependency', () => {
it('adds an Electron dep if one does not already exist', () => {
@@ -72,4 +73,16 @@ describe('getElectronVersion', () => {
};
return expect(await getElectronVersion('', packageJSON)).to.be.equal('1.0.0');
});

it('works with a non-exact version and yarn workspaces', async () => {
const fixtureDir = path.resolve(__dirname, '..', 'fixture', 'yarn-workspace', 'packages', 'subpackage');
const packageJSON = {
devDependencies: { electron: '^4.0.4' },
};
if (hasYarn()) {
expect(await getElectronVersion(fixtureDir, packageJSON)).to.be.equal('4.0.9');
} else {
expect(getElectronVersion(fixtureDir, packageJSON)).to.eventually.be.rejectedWith('Cannot find the package');
}
});
});

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

No changes.
@@ -3867,6 +3867,13 @@ find-up@^2.0.0, find-up@^2.1.0:
dependencies:
locate-path "^2.0.0"

find-up@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.0.0.tgz#c367f8024de92efb75f2d4906536d24682065c3a"
integrity sha512-zoH7ZWPkRdgwYCDVoQTzqjG8JSPANhtvLhh4KVUHyKnaUJJrNeFmWIkTcNuJmR3GLMEmGYEf2S2bjgx26JTF+Q==
dependencies:
locate-path "^5.0.0"

findit2@~2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/findit2/-/findit2-2.2.3.tgz#58a466697df8a6205cdfdbf395536b8bd777a5f6"
@@ -5352,6 +5359,13 @@ locate-path@^3.0.0:
p-locate "^3.0.0"
path-exists "^3.0.0"

locate-path@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0"
integrity sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==
dependencies:
p-locate "^4.1.0"

lodash._reinterpolate@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
@@ -6437,7 +6451,7 @@ p-limit@^1.1.0:
dependencies:
p-try "^1.0.0"

p-limit@^2.0.0:
p-limit@^2.0.0, p-limit@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.2.0.tgz#417c9941e6027a9abcba5092dd2904e255b5fbc2"
integrity sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==
@@ -6458,6 +6472,13 @@ p-locate@^3.0.0:
dependencies:
p-limit "^2.0.0"

p-locate@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07"
integrity sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==
dependencies:
p-limit "^2.2.0"

p-map@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-map/-/p-map-2.0.0.tgz#be18c5a5adeb8e156460651421aceca56c213a50"

0 comments on commit a91d8b3

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