Skip to content

Commit

Permalink
fix(core): read project name from package json if not set in project …
Browse files Browse the repository at this point in the history
…json
  • Loading branch information
AgentEnder committed Jun 10, 2024
1 parent b54f5c3 commit 22eb068
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/nx/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
"version": "19.2.2-beta.0",
"description": "Updates the nx wrapper.",
"implementation": "./src/migrations/update-17-3-0/update-nxw"
},
"19-2-3-set-project-name": {
"version": "19.2.3-beta.0",
"description": "Set project name in nx.json explicitly",
"implementation": "./src/migrations/update-19-2-1/set-project-name",
"x-repair-skip": true
}
}
}
28 changes: 28 additions & 0 deletions packages/nx/src/migrations/update-19-2-1/set-project-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { toProjectName } from '../../config/to-project-name';
import { ProjectConfiguration } from '../../config/workspace-json-project-json';
import { Tree } from '../../generators/tree';
import { readJson, writeJson } from '../../generators/utils/json';
import { getProjects } from '../../generators/utils/project-configuration';

export default function setProjectName(tree: Tree) {
// We are explicitly looking for project.json files here, so getProjects is fine.
const projects = getProjects(tree);

for (const { root } of projects.values()) {
if (!tree.exists(`${root}/project.json`)) {
continue;
}
const projectJson: ProjectConfiguration = readJson(
tree,
`${root}/project.json`
);
// In Nx 19.1+, the way the project name is inferred is different.
// For existing projects, if the name is not set, we can inline it
// based on the existing logic. This makes sure folks aren't caught
// off guard by the new behavior.
if (!projectJson.name) {
projectJson.name = toProjectName(root);
writeJson(tree, `${root}/project.json`, projectJson);
}
}
}
20 changes: 17 additions & 3 deletions packages/nx/src/plugins/project-json/build-nodes/project-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ProjectConfiguration } from '../../../config/workspace-json-project-jso
import { toProjectName } from '../../../config/to-project-name';
import { readJsonFile } from '../../../utils/fileutils';
import { NxPluginV2 } from '../../../project-graph/plugins';
import { PackageJson } from '../../../utils/package-json';

export const ProjectJsonProjectsPlugin: NxPluginV2 = {
name: 'nx/core/project-json',
Expand All @@ -15,6 +16,7 @@ export const ProjectJsonProjectsPlugin: NxPluginV2 = {
);

const project = buildProjectFromProjectJson(json, file);

return {
projects: {
[project.root]: project,
Expand All @@ -30,9 +32,21 @@ export function buildProjectFromProjectJson(
json: Partial<ProjectConfiguration>,
path: string
): ProjectConfiguration {
const packageJsonPath = join(dirname(path), 'package.json');
const { name, root, ...rest } = json;
return {
name: toProjectName(path),
root: dirname(path),
...json,
name:
name ?? readNameFromPackageJson(packageJsonPath) ?? toProjectName(path),
root: root ?? dirname(path),
...rest,
};
}

export function readNameFromPackageJson(path: string): string {
try {
const json = readJsonFile<PackageJson>(path);
return json.nx?.name ?? json.name;
} catch {
return undefined;
}
}
1 change: 1 addition & 0 deletions packages/nx/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from './package-manager';

export interface NxProjectPackageJsonConfiguration {
name?: string;
implicitDependencies?: string[];
tags?: string[];
namedInputs?: { [inputName: string]: (string | InputDefinition)[] };
Expand Down

0 comments on commit 22eb068

Please sign in to comment.