nx/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.ts
Jason Jean c2c6a13514
fix(core): do not create projects twice from package.json (#26700)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

NPM scripts show up twice in the project details view.


![image](https://github.com/nrwl/nx/assets/8104246/98c55a03-7e71-430f-9fc3-de91ce180e26)


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

NPM scripts only show up once in the project details view.


![image](https://github.com/nrwl/nx/assets/8104246/27787794-a8ef-40be-b00a-c9b1d26182f2)


## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-06-26 17:31:49 -04:00

76 lines
2.3 KiB
TypeScript

import { dirname, join } from 'path';
import { existsSync } from 'fs';
import { NxPluginV2 } from '../../../project-graph/plugins';
import { readJsonFile } from '../../../utils/fileutils';
import { ProjectConfiguration } from '../../../config/workspace-json-project-json';
import {
PackageJson,
getMetadataFromPackageJson,
getTagsFromPackageJson,
readTargetsFromPackageJson,
} from '../../../utils/package-json';
import { buildPackageJsonWorkspacesMatcher } from '../../package-json-workspaces';
// TODO: Remove this one day, this should not need to be done.
export const PackageJsonProjectsNextToProjectJsonPlugin: NxPluginV2 = {
// Its not a problem if plugins happen to have same name, and this
// will look least confusing in the source map.
name: 'nx/core/package-json',
createNodes: [
'{project.json,**/project.json}',
(file, _, { workspaceRoot }) => {
const project = createProjectFromPackageJsonNextToProjectJson(
file,
workspaceRoot
);
return project
? {
projects: {
[project.name]: project,
},
}
: {};
},
],
};
export default PackageJsonProjectsNextToProjectJsonPlugin;
function createProjectFromPackageJsonNextToProjectJson(
projectJsonPath: string,
workspaceRoot: string
): ProjectConfiguration | null {
const root = dirname(projectJsonPath);
const relativePackageJsonPath = join(root, 'package.json');
const packageJsonPath = join(workspaceRoot, relativePackageJsonPath);
const readJson = (f) => readJsonFile(join(workspaceRoot, f));
// Do not create projects for package.json files
// that are part of the package manager workspaces
// Those package.json files will be processed later on
const matcher = buildPackageJsonWorkspacesMatcher(workspaceRoot, readJson);
if (!existsSync(packageJsonPath) || matcher(relativePackageJsonPath)) {
return null;
}
try {
const packageJson: PackageJson = readJsonFile(packageJsonPath);
let { nx, name } = packageJson;
return {
...nx,
name,
root,
targets: readTargetsFromPackageJson(packageJson),
metadata: getMetadataFromPackageJson(packageJson),
tags: getTagsFromPackageJson(packageJson),
} as ProjectConfiguration;
} catch (e) {
console.log(e);
return null;
}
}