<!-- 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.  ## 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.  ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
76 lines
2.3 KiB
TypeScript
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;
|
|
}
|
|
}
|