Skip to content

Commit

Permalink
feat(core): handle yarn workspace packages when pruning (#13170)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Nov 16, 2022
1 parent 78d5d7b commit 45cba68
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
23 changes: 23 additions & 0 deletions packages/nx/src/utils/lock-file/yarn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ describe('yarn LockFile utility', () => {
removeComment(berryLockFileDevkitAndYargs)
);
});

it('shold correctly prune lockfile with multiple packages and custom name', () => {
const result = pruneYarnLockFile(
parsedLockFile,
['yargs', '@nrwl/devkit', 'typescript'],
'custom-name'
);
expect(result.lockFileMetadata.workspacePackages).toMatchInlineSnapshot(`
Object {
"custom-name@workspace:^": Object {
"dependencies": Object {
"@nrwl/devkit": "14.7.5",
"typescript": "~4.8.2",
"yargs": "^17.4.0",
},
"languageName": "unknown",
"linkType": "soft",
"resolution": "custom-name@workspace:^",
"version": "0.0.0-use.local",
},
}
`);
});
});
});

Expand Down
63 changes: 36 additions & 27 deletions packages/nx/src/utils/lock-file/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,41 @@ function pruneWorkspacePackages(
): LockFileDependencies {
const result: LockFileDependencies = {};

Object.entries(workspacePackages).forEach(
([packageKey, { dependencies, ...value }]) => {
const isRootPackage = packageKey.indexOf('@workspace:.') !== -1;
const prunedWorkspaceDependencies = pruneWorkspacePackageDependencies(
dependencies,
packages,
prunedDependencies,
isRootPackage
);
result[packageKey] = {
...value,
dependencies: sortObject(prunedWorkspaceDependencies),
};
}
);
let workspaceProjKey = '';

if (projectName) {
workspaceProjKey =
Object.keys(workspacePackages).find((key) =>
key.startsWith(`${projectName}@workspace:`)
) || `${projectName}@workspace:^`;
} else {
workspaceProjKey = Object.keys(workspacePackages).find(
(key) => key.indexOf('@workspace:.') !== -1
);
}

if (workspaceProjKey) {
const prunedWorkspaceDependencies = pruneWorkspacePackageDependencies(
workspacePackages[workspaceProjKey]?.dependencies || {},
packages,
prunedDependencies
);
result[workspaceProjKey] = {
version: '0.0.0-use.local',
resolution: workspaceProjKey,
languageName: 'unknown',
linkType: 'soft',
dependencies: sortObject(prunedWorkspaceDependencies),
};
}

return result;
}

function pruneWorkspacePackageDependencies(
dependencies: Record<string, string>,
packages: string[],
prunedDependencies: LockFileData['dependencies'],
isRoot: boolean
prunedDependencies: LockFileData['dependencies']
): Record<string, string> {
const result: Record<string, string> = {};

Expand All @@ -345,16 +356,14 @@ function pruneWorkspacePackageDependencies(
}
);
// add all missing deps to root workspace package
if (isRoot) {
packages.forEach((p) => {
if (!result[p]) {
// extract first version expression from package's structure
const metaVersion = Object.values(prunedDependencies[p])[0]
.packageMeta[0] as string;
result[p] = metaVersion.split('@npm:')[1];
}
});
}
packages.forEach((p) => {
if (!result[p]) {
// extract first version expression from package's structure
const metaVersion = Object.values(prunedDependencies[p])[0]
.packageMeta[0] as string;
result[p] = metaVersion.split('@npm:')[1];
}
});

return result;
}
Expand Down

1 comment on commit 45cba68

@vercel
Copy link

@vercel vercel bot commented on 45cba68 Nov 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.