Skip to content

Commit

Permalink
fix: require.resolve for packages with exports field in package.json (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rigor789 authored Sep 30, 2021
1 parent a992a26 commit 56ed35b
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 124 deletions.
6 changes: 6 additions & 0 deletions lib/helpers/package-path-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {
resolvePackagePath,
resolvePackageJSONPath,
} from "@rigor789/resolve-package-path";

export { resolvePackagePath, resolvePackageJSONPath };
10 changes: 5 additions & 5 deletions lib/services/karma-execution.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as path from "path";
import { resolvePackagePath } from "../helpers/package-path-helper";

process.on("message", (data: any) => {
if (data.karmaConfig) {
const pathToKarma = path.dirname(
require.resolve("karma/package.json", {
paths: [data.karmaConfig.projectDir],
})
);
const pathToKarma = resolvePackagePath("karma", {
paths: [data.karmaConfig.projectDir],
});

const KarmaServer = require(path.join(pathToKarma, "lib/server"));
const karma = new KarmaServer(data.karmaConfig, (exitCode: number) => {
// Exit with the correct exit code and signal the manager process.
Expand Down
25 changes: 13 additions & 12 deletions lib/services/plugins-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import { IFilesHashService } from "../definitions/files-hash-service";
import * as _ from "lodash";
import { IInjector } from "../common/definitions/yok";
import { injector } from "../common/yok";
import {
resolvePackagePath,
resolvePackageJSONPath,
} from "../helpers/package-path-helper";

export class PluginsService implements IPluginsService {
private static INSTALL_COMMAND_NAME = "install";
Expand Down Expand Up @@ -285,23 +289,20 @@ export class PluginsService implements IPluginsService {

const notInstalledDependencies = allDependencies
.map((dep) => {
try {
this.$logger.trace(`Checking if ${dep} is installed...`);
require.resolve(`${dep}/package.json`, {
paths: [projectData.projectDir],
});
this.$logger.trace(`Checking if ${dep} is installed...`);
const pathToPackage = resolvePackagePath(dep, {
paths: [projectData.projectDir],
});

if (pathToPackage) {
// return false if the dependency is installed - we'll filter out boolean values
// and end up with an array of dep names that are not installed if we end up
// inside the catch block.
return false;
} catch (err) {
this.$logger.trace(
`Error while checking if ${dep} is installed. Error is: `,
err
);
return dep;
}

this.$logger.trace(`${dep} is not installed, or couldn't be found`);
return dep;
})
.filter(Boolean);

Expand Down Expand Up @@ -684,7 +685,7 @@ This framework comes from ${dependencyName} plugin, which is installed multiple
moduleName: string,
projectDir: string
): string {
const pathToJsonFile = require.resolve(`${moduleName}/package.json`, {
const pathToJsonFile = resolvePackageJSONPath(moduleName, {
paths: [projectDir],
});
return pathToJsonFile;
Expand Down
11 changes: 9 additions & 2 deletions lib/services/project-data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import * as _ from "lodash";
import { IInjector } from "../common/definitions/yok";
import { injector } from "../common/yok";
import * as semver from "semver";
import { resolvePackageJSONPath } from "../helpers/package-path-helper";

interface IProjectFileData {
projectData: any;
Expand Down Expand Up @@ -637,12 +638,18 @@ export class ProjectDataService implements IProjectDataService {
// in case we are using a local tgz for the runtime or a range like ~8.0.0, ^8.0.0 etc.
if (runtimePackage.version.includes("tgz") || isRange) {
try {
const runtimePackageJsonPath = require.resolve(
`${runtimePackage.name}/package.json`,
const runtimePackageJsonPath = resolvePackageJSONPath(
runtimePackage.name,
{
paths: [projectDir],
}
);

if (!runtimePackageJsonPath) {
// caught below
throw new Error("Runtime package.json not found.");
}

runtimePackage.version = this.$fs.readJson(
runtimePackageJsonPath
).version;
Expand Down
14 changes: 7 additions & 7 deletions lib/services/test-execution-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import * as _ from "lodash";
import { injector } from "../common/yok";
import { ICommandParameter } from "../common/definitions/commands";
import { resolvePackagePath } from "../helpers/package-path-helper";

interface IKarmaConfigOptions {
debugBrk: boolean;
Expand Down Expand Up @@ -146,13 +147,12 @@ export class TestExecutionService implements ITestExecutionService {
return;
}
});
try {
require.resolve("karma/package.json", {
paths: [projectData.projectDir],
});
} catch (ignore) {
canStartKarmaServer = false;
}

const pathToKarma = resolvePackagePath("karma", {
paths: [projectData.projectDir],
});

canStartKarmaServer = !!pathToKarma;

return canStartKarmaServer;
}
Expand Down
57 changes: 27 additions & 30 deletions lib/services/webpack/webpack-compiler-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import {
} from "../../common/declarations";
import { ICleanupService } from "../../definitions/cleanup-service";
import { injector } from "../../common/yok";
import {
resolvePackagePath,
resolvePackageJSONPath,
} from "../../helpers/package-path-helper";

// todo: move out of here
interface IWebpackMessage<T = any> {
Expand Down Expand Up @@ -71,7 +75,7 @@ export class WebpackCompilerService
): Promise<any> {
return new Promise(async (resolve, reject) => {
if (this.webpackProcesses[platformData.platformNameLowerCase]) {
resolve();
resolve(void 0);
return;
}

Expand Down Expand Up @@ -584,45 +588,38 @@ export class WebpackCompilerService

private getWebpackExecutablePath(projectData: IProjectData): string {
if (this.isWebpack5(projectData)) {
const packagePath = require.resolve(
"@nativescript/webpack/package.json",
{
paths: [projectData.projectDir],
}
);
const packagePath = resolvePackagePath("@nativescript/webpack", {
paths: [projectData.projectDir],
});

return path.resolve(
packagePath.replace("package.json", ""),
"dist",
"bin",
"index.js"
);
if (packagePath) {
return path.resolve(packagePath, "dist", "bin", "index.js");
}
}

return path.join(
projectData.projectDir,
"node_modules",
"webpack",
"bin",
"webpack.js"
);
const packagePath = resolvePackagePath("webpack", {
paths: [projectData.projectDir],
});

if (!packagePath) {
return "";
}

return path.resolve(packagePath, "bin", "webpack.js");
}

private isWebpack5(projectData: IProjectData): boolean {
try {
const packagePath = require.resolve(
"@nativescript/webpack/package.json",
{
paths: [projectData.projectDir],
}
);
const ver = semver.coerce(require(packagePath).version);
const packageJSONPath = resolvePackageJSONPath("@nativescript/webpack", {
paths: [projectData.projectDir],
});

if (packageJSONPath) {
const packageData = this.$fs.readJson(packageJSONPath);
const ver = semver.coerce(packageData.version);

if (semver.satisfies(ver, ">= 5.0.0")) {
return true;
}
} catch (ignore) {
//
}

return false;
Expand Down
17 changes: 15 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"dependencies": {
"@nativescript/doctor": "2.0.4-next-02-05-2021-541397986",
"@nativescript/schematics-executor": "0.0.2",
"@rigor789/resolve-package-path": "^1.0.5",
"axios": "^0.21.1",
"byline": "5.0.0",
"chalk": "4.1.0",
Expand Down
Loading

0 comments on commit 56ed35b

Please sign in to comment.