Skip to content

Commit 56ed35b

Browse files
authored
fix: require.resolve for packages with exports field in package.json (#5579)
1 parent a992a26 commit 56ed35b

9 files changed

+124
-124
lines changed

lib/helpers/package-path-helper.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {
2+
resolvePackagePath,
3+
resolvePackageJSONPath,
4+
} from "@rigor789/resolve-package-path";
5+
6+
export { resolvePackagePath, resolvePackageJSONPath };

lib/services/karma-execution.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as path from "path";
2+
import { resolvePackagePath } from "../helpers/package-path-helper";
23

34
process.on("message", (data: any) => {
45
if (data.karmaConfig) {
5-
const pathToKarma = path.dirname(
6-
require.resolve("karma/package.json", {
7-
paths: [data.karmaConfig.projectDir],
8-
})
9-
);
6+
const pathToKarma = resolvePackagePath("karma", {
7+
paths: [data.karmaConfig.projectDir],
8+
});
9+
1010
const KarmaServer = require(path.join(pathToKarma, "lib/server"));
1111
const karma = new KarmaServer(data.karmaConfig, (exitCode: number) => {
1212
// Exit with the correct exit code and signal the manager process.

lib/services/plugins-service.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import { IFilesHashService } from "../definitions/files-hash-service";
3232
import * as _ from "lodash";
3333
import { IInjector } from "../common/definitions/yok";
3434
import { injector } from "../common/yok";
35+
import {
36+
resolvePackagePath,
37+
resolvePackageJSONPath,
38+
} from "../helpers/package-path-helper";
3539

3640
export class PluginsService implements IPluginsService {
3741
private static INSTALL_COMMAND_NAME = "install";
@@ -285,23 +289,20 @@ export class PluginsService implements IPluginsService {
285289

286290
const notInstalledDependencies = allDependencies
287291
.map((dep) => {
288-
try {
289-
this.$logger.trace(`Checking if ${dep} is installed...`);
290-
require.resolve(`${dep}/package.json`, {
291-
paths: [projectData.projectDir],
292-
});
292+
this.$logger.trace(`Checking if ${dep} is installed...`);
293+
const pathToPackage = resolvePackagePath(dep, {
294+
paths: [projectData.projectDir],
295+
});
293296

297+
if (pathToPackage) {
294298
// return false if the dependency is installed - we'll filter out boolean values
295299
// and end up with an array of dep names that are not installed if we end up
296300
// inside the catch block.
297301
return false;
298-
} catch (err) {
299-
this.$logger.trace(
300-
`Error while checking if ${dep} is installed. Error is: `,
301-
err
302-
);
303-
return dep;
304302
}
303+
304+
this.$logger.trace(`${dep} is not installed, or couldn't be found`);
305+
return dep;
305306
})
306307
.filter(Boolean);
307308

@@ -684,7 +685,7 @@ This framework comes from ${dependencyName} plugin, which is installed multiple
684685
moduleName: string,
685686
projectDir: string
686687
): string {
687-
const pathToJsonFile = require.resolve(`${moduleName}/package.json`, {
688+
const pathToJsonFile = resolvePackageJSONPath(moduleName, {
688689
paths: [projectDir],
689690
});
690691
return pathToJsonFile;

lib/services/project-data-service.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import * as _ from "lodash";
3434
import { IInjector } from "../common/definitions/yok";
3535
import { injector } from "../common/yok";
3636
import * as semver from "semver";
37+
import { resolvePackageJSONPath } from "../helpers/package-path-helper";
3738

3839
interface IProjectFileData {
3940
projectData: any;
@@ -637,12 +638,18 @@ export class ProjectDataService implements IProjectDataService {
637638
// in case we are using a local tgz for the runtime or a range like ~8.0.0, ^8.0.0 etc.
638639
if (runtimePackage.version.includes("tgz") || isRange) {
639640
try {
640-
const runtimePackageJsonPath = require.resolve(
641-
`${runtimePackage.name}/package.json`,
641+
const runtimePackageJsonPath = resolvePackageJSONPath(
642+
runtimePackage.name,
642643
{
643644
paths: [projectDir],
644645
}
645646
);
647+
648+
if (!runtimePackageJsonPath) {
649+
// caught below
650+
throw new Error("Runtime package.json not found.");
651+
}
652+
646653
runtimePackage.version = this.$fs.readJson(
647654
runtimePackageJsonPath
648655
).version;

lib/services/test-execution-service.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import * as _ from "lodash";
1919
import { injector } from "../common/yok";
2020
import { ICommandParameter } from "../common/definitions/commands";
21+
import { resolvePackagePath } from "../helpers/package-path-helper";
2122

2223
interface IKarmaConfigOptions {
2324
debugBrk: boolean;
@@ -146,13 +147,12 @@ export class TestExecutionService implements ITestExecutionService {
146147
return;
147148
}
148149
});
149-
try {
150-
require.resolve("karma/package.json", {
151-
paths: [projectData.projectDir],
152-
});
153-
} catch (ignore) {
154-
canStartKarmaServer = false;
155-
}
150+
151+
const pathToKarma = resolvePackagePath("karma", {
152+
paths: [projectData.projectDir],
153+
});
154+
155+
canStartKarmaServer = !!pathToKarma;
156156

157157
return canStartKarmaServer;
158158
}

lib/services/webpack/webpack-compiler-service.ts

+27-30
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import {
2828
} from "../../common/declarations";
2929
import { ICleanupService } from "../../definitions/cleanup-service";
3030
import { injector } from "../../common/yok";
31+
import {
32+
resolvePackagePath,
33+
resolvePackageJSONPath,
34+
} from "../../helpers/package-path-helper";
3135

3236
// todo: move out of here
3337
interface IWebpackMessage<T = any> {
@@ -71,7 +75,7 @@ export class WebpackCompilerService
7175
): Promise<any> {
7276
return new Promise(async (resolve, reject) => {
7377
if (this.webpackProcesses[platformData.platformNameLowerCase]) {
74-
resolve();
78+
resolve(void 0);
7579
return;
7680
}
7781

@@ -584,45 +588,38 @@ export class WebpackCompilerService
584588

585589
private getWebpackExecutablePath(projectData: IProjectData): string {
586590
if (this.isWebpack5(projectData)) {
587-
const packagePath = require.resolve(
588-
"@nativescript/webpack/package.json",
589-
{
590-
paths: [projectData.projectDir],
591-
}
592-
);
591+
const packagePath = resolvePackagePath("@nativescript/webpack", {
592+
paths: [projectData.projectDir],
593+
});
593594

594-
return path.resolve(
595-
packagePath.replace("package.json", ""),
596-
"dist",
597-
"bin",
598-
"index.js"
599-
);
595+
if (packagePath) {
596+
return path.resolve(packagePath, "dist", "bin", "index.js");
597+
}
600598
}
601599

602-
return path.join(
603-
projectData.projectDir,
604-
"node_modules",
605-
"webpack",
606-
"bin",
607-
"webpack.js"
608-
);
600+
const packagePath = resolvePackagePath("webpack", {
601+
paths: [projectData.projectDir],
602+
});
603+
604+
if (!packagePath) {
605+
return "";
606+
}
607+
608+
return path.resolve(packagePath, "bin", "webpack.js");
609609
}
610610

611611
private isWebpack5(projectData: IProjectData): boolean {
612-
try {
613-
const packagePath = require.resolve(
614-
"@nativescript/webpack/package.json",
615-
{
616-
paths: [projectData.projectDir],
617-
}
618-
);
619-
const ver = semver.coerce(require(packagePath).version);
612+
const packageJSONPath = resolvePackageJSONPath("@nativescript/webpack", {
613+
paths: [projectData.projectDir],
614+
});
615+
616+
if (packageJSONPath) {
617+
const packageData = this.$fs.readJson(packageJSONPath);
618+
const ver = semver.coerce(packageData.version);
620619

621620
if (semver.satisfies(ver, ">= 5.0.0")) {
622621
return true;
623622
}
624-
} catch (ignore) {
625-
//
626623
}
627624

628625
return false;

package-lock.json

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"dependencies": {
6060
"@nativescript/doctor": "2.0.4-next-02-05-2021-541397986",
6161
"@nativescript/schematics-executor": "0.0.2",
62+
"@rigor789/resolve-package-path": "^1.0.5",
6263
"axios": "^0.21.1",
6364
"byline": "5.0.0",
6465
"chalk": "4.1.0",

0 commit comments

Comments
 (0)