fix(core): handle packageManager property with createPackageJson (#26726)

This PR was originally authored by @stephenwade. It adds the `packageManager` property to the `createPackageJson` function that ensures the generated `package.json` uses the same package manager that was used when generating it.

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

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

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

Fixes #

---------

Co-authored-by: Stephen Wade <stephen@stephenwade.me>
Co-authored-by: Stephen Wade <stephen.wade@avb.net>
This commit is contained in:
Miroslav Jonaš 2024-06-28 13:48:39 +02:00 committed by GitHub
parent 336d371ceb
commit fdd89a6fe2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 111 additions and 2 deletions

View File

@ -397,7 +397,7 @@ describe('createPackageJson', () => {
});
});
describe('parsing "package.json" versions', () => {
describe('parsing "package.json"', () => {
const appDependencies = [
{ source: 'app1', target: 'npm:@nx/devkit', type: 'static' },
{ source: 'app1', target: 'npm:typescript', type: 'static' },
@ -654,6 +654,98 @@ describe('createPackageJson', () => {
},
});
});
it('should add packageManager if missing', () => {
spies.push(
jest.spyOn(fs, 'existsSync').mockImplementation((path) => {
if (path === 'libs/lib1/package.json') {
return true;
}
if (path === 'package.json') {
return true;
}
})
);
spies.push(
jest
.spyOn(fileutilsModule, 'readJsonFile')
.mockImplementation((path) => {
if (path === 'package.json') {
return {
...rootPackageJson(),
packageManager: 'yarn',
};
}
if (path === 'libs/lib1/package.json') {
return projectPackageJson();
}
})
);
expect(
createPackageJson('lib1', graph, {
root: '',
})
).toEqual({
dependencies: {
random: '1.0.0',
typescript: '^4.8.4',
},
name: 'other-name',
packageManager: 'yarn',
version: '1.2.3',
});
});
it('should replace packageManager if not in sync with root and show warning', () => {
spies.push(
jest.spyOn(fs, 'existsSync').mockImplementation((path) => {
if (path === 'libs/lib1/package.json') {
return true;
}
if (path === 'package.json') {
return true;
}
})
);
const consoleWarnSpy = jest.spyOn(process.stdout, 'write');
spies.push(consoleWarnSpy);
spies.push(
jest
.spyOn(fileutilsModule, 'readJsonFile')
.mockImplementation((path) => {
if (path === 'package.json') {
return {
...rootPackageJson(),
packageManager: 'yarn@1.2',
};
}
if (path === 'libs/lib1/package.json') {
return {
...projectPackageJson(),
packageManager: 'yarn@4.3',
};
}
})
);
expect(
createPackageJson('lib1', graph, {
root: '',
})
).toEqual({
dependencies: {
random: '1.0.0',
typescript: '^4.8.4',
},
name: 'other-name',
packageManager: 'yarn@1.2',
version: '1.2.3',
});
expect(JSON.stringify(consoleWarnSpy.mock.calls)).toMatch(
/Package Manager Mismatch/
);
});
});
});

View File

@ -16,6 +16,7 @@ import {
filterUsingGlobPatterns,
getTargetInputs,
} from '../../../hasher/task-hasher';
import { output } from '../../../utils/output';
interface NpmDeps {
readonly dependencies: Record<string, string>;
@ -43,7 +44,7 @@ export function createPackageJson(
const projectNode = graph.nodes[projectName];
const isLibrary = projectNode.type === 'lib';
const rootPackageJson = readJsonFile(
const rootPackageJson: PackageJson = readJsonFile(
join(options.root ?? workspaceRoot, 'package.json')
);
@ -180,6 +181,22 @@ export function createPackageJson(
packageJson.peerDependenciesMeta
);
if (rootPackageJson.packageManager) {
if (
packageJson.packageManager &&
packageJson.packageManager !== rootPackageJson.packageManager
) {
output.warn({
title: 'Package Manager Mismatch',
bodyLines: [
`The project ${projectName} has explicitly specified "packageManager" config of "${packageJson.packageManager}" but the workspace is using "${rootPackageJson.packageManager}".`,
`Please remove the project level "packageManager" config or align it with the workspace root package.json.`,
],
});
}
packageJson.packageManager = rootPackageJson.packageManager;
}
return packageJson;
}