Skip to content

Commit

Permalink
fix(core): search for electron in a node_modules folder with electr…
Browse files Browse the repository at this point in the history
…on in it (#2326)
  • Loading branch information
vhashimotoo committed Jul 11, 2021
1 parent cd0f052 commit 43cbb0a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/api/core/src/util/electron-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ function findElectronDep(dep: string): boolean {
return electronPackageNames.includes(dep);
}

async function findAncestorNodeModulesPath(dir: string): Promise<string | undefined> {
async function findAncestorNodeModulesPath(
dir: string,
packageName: 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');
const nodeModulesPath = path.join(path.dirname(yarnLockPath), 'node_modules', packageName);
if (await fs.pathExists(nodeModulesPath)) {
return nodeModulesPath;
}
Expand All @@ -32,12 +35,15 @@ async function findAncestorNodeModulesPath(dir: string): Promise<string | undefi
return Promise.resolve(undefined);
}

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

export class PackageNotFoundError extends Error {
Expand All @@ -63,11 +69,12 @@ async function getElectronPackageJSONPath(
dir: string,
packageName: string,
): Promise<string | undefined> {
const nodeModulesPath = await determineNodeModulesPath(dir);
const nodeModulesPath = await determineNodeModulesPath(dir, packageName);
if (!nodeModulesPath) {
throw new PackageNotFoundError(packageName, dir);
}
const electronPackageJSONPath = path.join(nodeModulesPath, packageName, 'package.json');

const electronPackageJSONPath = path.join(nodeModulesPath, 'package.json');
if (await fs.pathExists(electronPackageJSONPath)) {
return electronPackageJSONPath;
}
Expand All @@ -79,7 +86,7 @@ export async function getElectronModulePath(
dir: string,
packageJSON: any,
): Promise<string | undefined> {
const moduleName = await getElectronModuleName(packageJSON);
const moduleName = getElectronModuleName(packageJSON);
const packageJSONPath = await getElectronPackageJSONPath(dir, moduleName);
if (packageJSONPath) {
return path.dirname(packageJSONPath);
Expand Down
30 changes: 30 additions & 0 deletions packages/api/core/test/fast/electron-version_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,43 @@ describe('getElectronModulePath', () => {
const packageJSON = {
devDependencies: { electron: '^4.0.4' },
};

if (hasYarn()) {
expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
} else {
expect(getElectronModulePath(fixtureDir, packageJSON)).to.eventually.be.rejectedWith('Cannot find the package');
}
});

it('works when yarn workspaces create additional node_modules folder inside package', async () => {
const workspaceDir = path.resolve(__dirname, '..', 'fixture', 'yarn-workspace');
const fixtureDir = path.join(workspaceDir, 'packages', 'with-node-modules');
const packageJSON = {
devDependencies: { electron: '^4.0.4' },
};

if (hasYarn()) {
expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
} else {
expect(getElectronModulePath(fixtureDir, packageJSON)).to.eventually.be.rejectedWith('Cannot find the package');
}
});

it('works with yarn workspaces in ala nohoist mode', async () => {
const workspaceDir = path.resolve(__dirname, '..', 'fixture', 'yarn-workspace');
const fixtureDir = path.join(workspaceDir, 'packages', 'electron-folder-in-node-modules');
const packageJSON = {
devDependencies: { electron: '^13.0.0' },
};

if (hasYarn()) {
expect(await getElectronModulePath(fixtureDir, packageJSON)).to.be.equal(path.join(fixtureDir, 'node_modules', 'electron'));
expect(await getElectronModulePath(fixtureDir, packageJSON)).not.to.be.equal(path.join(workspaceDir, 'node_modules', 'electron'));
} else {
expect(getElectronModulePath(fixtureDir, packageJSON)).to.eventually.be.rejectedWith('Cannot find the package');
}
});

after(async () => {
await fs.remove(tempDir);
});
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 43cbb0a

Please sign in to comment.