Skip to content

Commit

Permalink
fix(core): properly error when projects have no name but with a packa…
Browse files Browse the repository at this point in the history
…ge.json
  • Loading branch information
AgentEnder committed May 1, 2024
1 parent c6674b0 commit 8ea3203
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
53 changes: 30 additions & 23 deletions packages/nx/src/plugins/package-json-workspaces/create-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,44 @@ export const createNodes: CreateNodes = [
combineGlobPatterns('package.json', '**/package.json'),
(p, _, { workspaceRoot }) => {
const readJson = (f) => readJsonFile(join(workspaceRoot, f));
const patterns = getGlobPatternsFromPackageManagerWorkspaces(
workspaceRoot,
readJson
);

const negativePatterns = patterns.filter((p) => p.startsWith('!'));
const positivePatterns = patterns.filter((p) => !p.startsWith('!'));

if (
// There are some negative patterns
negativePatterns.length > 0 &&
// No positive patterns
(positivePatterns.length === 0 ||
// Or only a single positive pattern that is the default coming from root package
(positivePatterns.length === 1 &&
positivePatterns[0] === 'package.json'))
) {
positivePatterns.push('**/package.json');
}
const matcher = buildPackageJsonWorkspacesMatcher(workspaceRoot, readJson);

if (
positivePatterns.some((positive) => minimatch(p, positive)) &&
!negativePatterns.some((negative) => minimatch(p, negative))
) {
if (matcher(p)) {
return createNodeFromPackageJson(p, workspaceRoot);
}
// The given package.json is not part of the workspaces configuration.
return {};
},
];

export function buildPackageJsonWorkspacesMatcher(
workspaceRoot: string,
readJson: (string) => any
) {
const patterns = getGlobPatternsFromPackageManagerWorkspaces(
workspaceRoot,
readJson
);

const negativePatterns = patterns.filter((p) => p.startsWith('!'));
const positivePatterns = patterns.filter((p) => !p.startsWith('!'));

if (
// There are some negative patterns
negativePatterns.length > 0 &&
// No positive patterns
(positivePatterns.length === 0 ||
// Or only a single positive pattern that is the default coming from root package
(positivePatterns.length === 1 && positivePatterns[0] === 'package.json'))
) {
positivePatterns.push('**/package.json');
}

return (p: string) =>
positivePatterns.some((positive) => minimatch(p, positive)) &&
!negativePatterns.some((negative) => minimatch(p, negative));
}

export function createNodeFromPackageJson(pkgJsonPath: string, root: string) {
const json: PackageJson = readJsonFile(join(root, pkgJsonPath));
const project = buildProjectConfigurationFromPackageJson(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,9 @@ export function readProjectConfigurationsFromRootMap(
if (!configuration.name) {
try {
const { name } = readJsonFile(join(root, 'package.json'));
if (!name) {
throw new Error("No name found for project at '" + root + "'.");
}
configuration.name = name;
} catch {
projectRootsWithNoName.push(root);
Expand Down

0 comments on commit 8ea3203

Please sign in to comment.