Skip to content

Commit

Permalink
chore(repo): update usage of new readModulePackageJson function
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Jun 1, 2022
1 parent 39ed34e commit 02b2c29
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,9 @@ export default async function* fileServerExecutor(
const outputPath = getBuildTargetOutputPath(options, context);
const args = getHttpServerArgs(options);

const pathToHttpServerPkgJson = readModulePackageJson('http-server').path;
const pathToHttpServerBin = readJsonFile(pathToHttpServerPkgJson).bin[
'http-server'
];
const { path: pathToHttpServerPkgJson, packageJson } =
readModulePackageJson('http-server');
const pathToHttpServerBin = packageJson.bin['http-server'];
const pathToHttpServer = resolve(
pathToHttpServerPkgJson.replace('package.json', ''),
pathToHttpServerBin
Expand Down
8 changes: 5 additions & 3 deletions packages/angular/src/utils/mfe/mfe-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
readTsConfig,
} from '@nrwl/workspace/src/utilities/typescript';
import { existsSync, lstatSync, readdirSync } from 'fs';
import { readModulePackageJson } from 'nx/src/utils/package-json';
import { PackageJson, readModulePackageJson } from 'nx/src/utils/package-json';
import { dirname, join, normalize, relative } from 'path';
import { ParsedCommandLine } from 'typescript';
import { NormalModuleReplacementPlugin } from 'webpack';
Expand Down Expand Up @@ -154,8 +154,9 @@ function collectPackageSecondaryEntryPoints(
): void {
let pathToPackage: string;
let packageJsonPath: string;
let packageJson: PackageJson;
try {
const packageJsonPath = readModulePackageJson(pkgName).path;
({ path: packageJsonPath, packageJson } = readModulePackageJson(pkgName));
pathToPackage = dirname(packageJsonPath);
} catch {
// the package.json might not resolve if the package has the "exports"
Expand All @@ -167,9 +168,10 @@ function collectPackageSecondaryEntryPoints(
// might not exist if it's nested in another package, just return here
return;
}
packageJson = readJsonFile(packageJsonPath);
}

const { exports } = readJsonFile(packageJsonPath);
const { exports } = packageJson;
const subDirs = getNonNodeModulesSubDirs(pathToPackage);
recursivelyCollectSecondaryEntryPointsFromDirectory(
pkgName,
Expand Down
16 changes: 4 additions & 12 deletions packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
ProjectsConfigurations,
} from '../config/workspace-json-project-json';
import { readNxJson } from '../generators/utils/project-configuration';
import { readModulePackageJson } from '../utils/package-json';
import { PackageJson, readModulePackageJson } from '../utils/package-json';

export async function scheduleTarget(
root: string,
Expand Down Expand Up @@ -897,18 +897,10 @@ function resolveMigrationsCollection(name: string): string {
if (extname(name)) {
collectionPath = require.resolve(name);
} else {
let packageJsonPath;
try {
packageJsonPath = readModulePackageJson(name, [process.cwd()]).path;
} catch (e) {
// workaround for a bug in node 12
packageJsonPath = require.resolve(
join(process.cwd(), name, 'package.json')
);
}
const { path: packageJsonPath, packageJson } = readModulePackageJson(name, [
process.cwd(),
]);

// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJson = require(packageJsonPath);
let pkgJsonSchematics =
packageJson['nx-migrations'] ?? packageJson['ng-update'];
if (!pkgJsonSchematics) {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/utils/package-json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'path';
import { workspaceRoot } from './app-root';
import { workspaceRoot } from './workspace-root';
import { readJsonFile } from './fileutils';
import {
buildTargetFromScript,
Expand Down
5 changes: 3 additions & 2 deletions packages/nx/src/utils/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { existsSync } from 'fs';
import { dirname, join } from 'path';
import { TargetConfiguration } from '../config/workspace-json-project-json';
import { workspaceRoot } from './app-root';
import { readJsonFile } from './fileutils';
import { workspaceRoot } from './workspace-root';

export type PackageJsonTargetConfiguration = Omit<
TargetConfiguration,
Expand Down Expand Up @@ -40,6 +40,7 @@ export interface PackageJson {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
bin?: Record<string, string>;
workspaces?:
| string[]
| {
Expand Down Expand Up @@ -101,7 +102,7 @@ export function readModulePackageJson(

const packageJson = readJsonFile(packageJsonPath);

if (!(packageJson.name === moduleSpecifier)) {
if (packageJson.name !== moduleSpecifier) {
throw new Error(
`Found module ${packageJson.name} while trying to locate ${moduleSpecifier}/package.json`
);
Expand Down
7 changes: 3 additions & 4 deletions packages/web/src/executors/file-server/file-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,9 @@ export default async function* fileServerExecutor(
const outputPath = getBuildTargetOutputPath(options, context);
const args = getHttpServerArgs(options);

const pathToHttpServerPkgJson = readModulePackageJson('http-server').path;
const pathToHttpServerBin = readJsonFile(pathToHttpServerPkgJson).bin[
'http-server'
];
const { path: pathToHttpServerPkgJson, packageJson } =
readModulePackageJson('http-server');
const pathToHttpServerBin = packageJson.bin['http-server'];
const pathToHttpServer = resolve(
pathToHttpServerPkgJson.replace('package.json', ''),
pathToHttpServerBin
Expand Down
3 changes: 2 additions & 1 deletion scripts/patched-jest-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ module.exports = function (path, options) {
}
// Try to use the defaultResolver
try {
if (path.startsWith('@nrwl/')) throw new Error('custom resolution');
if (path.startsWith('@nrwl/') && !path.startsWith('@nrwl/nx-cloud'))
throw new Error('custom resolution');
if (path.startsWith('nx/')) throw new Error('custom resolution');

if (path.indexOf('@nrwl/workspace') > -1) {
Expand Down

0 comments on commit 02b2c29

Please sign in to comment.