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 29, 2024
1 parent 4baa3a5 commit 1f09f2a
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 742 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
40 changes: 26 additions & 14 deletions packages/nx/src/plugins/js/lock-file/pnpm-parser.ts
Original file line number Diff line number Diff line change
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 useParenSeparator = usesParenthesisVersionSeparator(data);

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

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

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

function matchPropValue(
Expand Down Expand Up @@ -114,7 +114,7 @@ function isLockFileKey(depVersion: string) {
function getNodes(
data: Lockfile,
keyMap: Map<string, ProjectGraphExternalNode>,
isV6: boolean
useParenSeparator: boolean
): Record<string, ProjectGraphExternalNode> {
const nodes: Map<string, Map<string, ProjectGraphExternalNode>> = new Map();

Expand Down Expand Up @@ -205,7 +205,7 @@ function getNodes(
if (!rawVersion) {
continue;
}
const version = parseBaseVersion(rawVersion, isV6);
const version = parseBaseVersion(rawVersion, useParenSeparator);
if (!version) {
continue;
}
Expand Down Expand Up @@ -239,7 +239,11 @@ function getNodes(
if (versionMap.size === 1) {
hoistedNode = versionMap.values().next().value;
} else {
const hoistedVersion = getHoistedVersion(hoistedDeps, packageName, isV6);
const hoistedVersion = getHoistedVersion(
hoistedDeps,
packageName,
useParenSeparator
);
hoistedNode = versionMap.get(hoistedVersion);
}
if (hoistedNode) {
Expand All @@ -256,7 +260,7 @@ function getNodes(
function getHoistedVersion(
hoistedDependencies: Record<string, any>,
packageName: string,
isV6: boolean
useParenSeparator: boolean
): string {
let version = getHoistedPackageVersion(packageName);

Expand All @@ -265,7 +269,10 @@ function getHoistedVersion(
k.startsWith(`/${packageName}/`)
);
if (key) {
version = parseBaseVersion(getVersion(key, packageName), isV6);
version = parseBaseVersion(
getVersion(key, packageName),
useParenSeparator
);
} else {
// pnpm might not hoist every package
// similarly those packages will not be available to be used via import
Expand All @@ -279,7 +286,7 @@ function getHoistedVersion(
function getDependencies(
data: Lockfile,
keyMap: Map<string, ProjectGraphExternalNode>,
isV6: boolean,
useParenSeparator: boolean,
ctx: CreateDependenciesContext
): RawProjectGraphDependency[] {
const results: RawProjectGraphDependency[] = [];
Expand All @@ -291,7 +298,7 @@ function getDependencies(
Object.entries(section).forEach(([name, versionRange]) => {
const version = parseBaseVersion(
findVersion(versionRange, name),
isV6
useParenSeparator
);
const target =
ctx.externalNodes[`npm:${name}@${version}`] ||
Expand All @@ -314,8 +321,13 @@ function getDependencies(
return results;
}

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

export function stringifyPnpmLockfile(
Expand Down

0 comments on commit 1f09f2a

Please sign in to comment.