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:
parent
336d371ceb
commit
fdd89a6fe2
@ -397,7 +397,7 @@ describe('createPackageJson', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('parsing "package.json" versions', () => {
|
describe('parsing "package.json"', () => {
|
||||||
const appDependencies = [
|
const appDependencies = [
|
||||||
{ source: 'app1', target: 'npm:@nx/devkit', type: 'static' },
|
{ source: 'app1', target: 'npm:@nx/devkit', type: 'static' },
|
||||||
{ source: 'app1', target: 'npm:typescript', 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/
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -16,6 +16,7 @@ import {
|
|||||||
filterUsingGlobPatterns,
|
filterUsingGlobPatterns,
|
||||||
getTargetInputs,
|
getTargetInputs,
|
||||||
} from '../../../hasher/task-hasher';
|
} from '../../../hasher/task-hasher';
|
||||||
|
import { output } from '../../../utils/output';
|
||||||
|
|
||||||
interface NpmDeps {
|
interface NpmDeps {
|
||||||
readonly dependencies: Record<string, string>;
|
readonly dependencies: Record<string, string>;
|
||||||
@ -43,7 +44,7 @@ export function createPackageJson(
|
|||||||
const projectNode = graph.nodes[projectName];
|
const projectNode = graph.nodes[projectName];
|
||||||
const isLibrary = projectNode.type === 'lib';
|
const isLibrary = projectNode.type === 'lib';
|
||||||
|
|
||||||
const rootPackageJson = readJsonFile(
|
const rootPackageJson: PackageJson = readJsonFile(
|
||||||
join(options.root ?? workspaceRoot, 'package.json')
|
join(options.root ?? workspaceRoot, 'package.json')
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -180,6 +181,22 @@ export function createPackageJson(
|
|||||||
packageJson.peerDependenciesMeta
|
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;
|
return packageJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user