Skip to content

Commit

Permalink
fix(core): modify pnpm normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Apr 19, 2024
1 parent 6b595f9 commit d4176c3
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 711 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -83,6 +83,7 @@
"@phenomnomnominal/tsquery": "~5.0.1",
"@playwright/test": "^1.36.1",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
"@pnpm/lockfile-file": "^9.0.0",
"@pnpm/lockfile-types": "^6.0.0",
"@reduxjs/toolkit": "1.9.0",
"@remix-run/dev": "^2.8.1",
Expand Down Expand Up @@ -324,7 +325,7 @@
"@widgetbot/react-embed": "^1.9.0",
"@yarnpkg/lockfile": "^1.1.0",
"@yarnpkg/parsers": "3.0.0-rc.46",
"@zkochan/js-yaml": "0.0.6",
"@zkochan/js-yaml": "0.0.7",
"axios": "^1.6.0",
"classnames": "^2.3.1",
"cliui": "^8.0.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/package.json
Expand Up @@ -33,9 +33,10 @@
},
"homepage": "https://nx.dev",
"dependencies": {
"@pnpm/lockfile-file": "^9.0.0",
"@yarnpkg/lockfile": "^1.1.0",
"@yarnpkg/parsers": "3.0.0-rc.46",
"@zkochan/js-yaml": "0.0.6",
"@zkochan/js-yaml": "0.0.7",
"axios": "^1.6.0",
"chalk": "^4.1.0",
"cli-cursor": "3.1.0",
Expand Down
38 changes: 24 additions & 14 deletions packages/nx/src/plugins/js/lock-file/pnpm-parser.ts
Expand Up @@ -5,7 +5,7 @@ import type {
ProjectSnapshot,
} from '@pnpm/lockfile-types';
import {
isV6Lockfile,
usesParenthesisVersionSeparator,
loadPnpmHoistedDepsDefinition,
parseAndNormalizePnpmLockfile,
stringifyToPnpmYaml,
Expand Down Expand Up @@ -49,9 +49,9 @@ export function getPnpmLockfileNodes(
lockFileHash: string
) {
const data = parsePnpmLockFile(lockFileContent, lockFileHash);
const isV6 = isV6Lockfile(data);
const useParensDivider = usesParenthesisVersionSeparator(data);

return getNodes(data, keyMap, isV6);
return getNodes(data, keyMap, useParensDivider);
}

export function getPnpmLockfileDependencies(
Expand All @@ -60,22 +60,22 @@ export function getPnpmLockfileDependencies(
ctx: CreateDependenciesContext
) {
const data = parsePnpmLockFile(lockFileContent, lockFileHash);
const isV6 = isV6Lockfile(data);
const useParensDivider = usesParenthesisVersionSeparator(data);

return getDependencies(data, keyMap, isV6, ctx);
return getDependencies(data, keyMap, useParensDivider, ctx);
}

function getNodes(
data: Lockfile,
keyMap: Map<string, ProjectGraphExternalNode>,
isV6: boolean
useParensDivider: boolean
): Record<string, ProjectGraphExternalNode> {
const nodes: Map<string, Map<string, ProjectGraphExternalNode>> = new Map();

Object.entries(data.packages).forEach(([key, snapshot]) => {
findPackageNames(key, snapshot, data).forEach((packageName) => {
const rawVersion = findVersion(key, packageName);
const version = parseBaseVersion(rawVersion, isV6);
const version = parseBaseVersion(rawVersion, useParensDivider);

// we don't need to keep duplicates, we can just track the keys
const existingNode = nodes.get(packageName)?.get(version);
Expand Down Expand Up @@ -117,7 +117,11 @@ function getNodes(
if (versionMap.size === 1) {
hoistedNode = versionMap.values().next().value;
} else {
const hoistedVersion = getHoistedVersion(hoistedDeps, packageName, isV6);
const hoistedVersion = getHoistedVersion(
hoistedDeps,
packageName,
useParensDivider
);
hoistedNode = versionMap.get(hoistedVersion);
}
if (hoistedNode) {
Expand All @@ -134,7 +138,7 @@ function getNodes(
function getHoistedVersion(
hoistedDependencies: Record<string, any>,
packageName: string,
isV6: boolean
useParensDivider: boolean
): string {
let version = getHoistedPackageVersion(packageName);

Expand All @@ -143,7 +147,10 @@ function getHoistedVersion(
k.startsWith(`/${packageName}/`)
);
if (key) {
version = parseBaseVersion(getVersion(key, packageName), isV6);
version = parseBaseVersion(
getVersion(key, packageName),
useParensDivider
);
} else {
// pnpm might not hoist every package
// similarly those packages will not be available to be used via import
Expand All @@ -157,7 +164,7 @@ function getHoistedVersion(
function getDependencies(
data: Lockfile,
keyMap: Map<string, ProjectGraphExternalNode>,
isV6: boolean,
useParensDivider: boolean,
ctx: CreateDependenciesContext
): RawProjectGraphDependency[] {
const results: RawProjectGraphDependency[] = [];
Expand All @@ -169,7 +176,7 @@ function getDependencies(
Object.entries(section).forEach(([name, versionRange]) => {
const version = parseBaseVersion(
findVersion(versionRange, name),
isV6
useParensDivider
);
const target =
ctx.externalNodes[`npm:${name}@${version}`] ||
Expand All @@ -192,8 +199,11 @@ function getDependencies(
return results;
}

function parseBaseVersion(rawVersion: string, isV6: boolean): string {
return isV6 ? rawVersion.split('(')[0] : rawVersion.split('_')[0];
function parseBaseVersion(
rawVersion: string,
useParensDivider: boolean
): string {
return useParensDivider ? rawVersion.split('(')[0] : rawVersion.split('_')[0];
}

export function stringifyPnpmLockfile(
Expand Down

0 comments on commit d4176c3

Please sign in to comment.