Skip to content

Commit

Permalink
fix(core): handle packageManager property with createPackageJson (#26726
Browse files Browse the repository at this point in the history
)

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 <[email protected]>
Co-authored-by: Stephen Wade <[email protected]>
  • Loading branch information
3 people authored Jun 28, 2024
1 parent 336d371 commit fdd89a6
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down Expand Up @@ -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: '[email protected]',
};
}
if (path === 'libs/lib1/package.json') {
return {
...projectPackageJson(),
packageManager: '[email protected]',
};
}
})
);

expect(
createPackageJson('lib1', graph, {
root: '',
})
).toEqual({
dependencies: {
random: '1.0.0',
typescript: '^4.8.4',
},
name: 'other-name',
packageManager: '[email protected]',
version: '1.2.3',
});
expect(JSON.stringify(consoleWarnSpy.mock.calls)).toMatch(
/Package Manager Mismatch/
);
});
});
});

Expand Down
19 changes: 18 additions & 1 deletion packages/nx/src/plugins/js/package-json/create-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
filterUsingGlobPatterns,
getTargetInputs,
} from '../../../hasher/task-hasher';
import { output } from '../../../utils/output';

interface NpmDeps {
readonly dependencies: Record<string, string>;
Expand Down Expand Up @@ -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')
);

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit fdd89a6

Please sign in to comment.