Skip to content

Commit 64079c7

Browse files
committed
fix: Improve solution
1 parent 8de207e commit 64079c7

File tree

2 files changed

+71
-35
lines changed

2 files changed

+71
-35
lines changed

internal/shrinkwrap-extractor/lib/convertPackageLockToShrinkwrap.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export default async function convertPackageLockToShrinkwrap(workspaceRootDir, t
4848
path: workspaceRootDir,
4949
});
5050
const tree = await arb.loadVirtual();
51-
const cliNode = Array.from(tree.tops).find((node) => node.packageName === targetPackageName);
51+
const tops = Array.from(tree.tops.values());
52+
const cliNode = tops.find((node) => node.packageName === targetPackageName);
5253
if (!cliNode) {
5354
throw new Error(`Target package "${targetPackageName}" not found in workspace`);
5455
}
@@ -71,7 +72,7 @@ export default async function convertPackageLockToShrinkwrap(workspaceRootDir, t
7172
throw new Error(`Duplicate root package entry for "${targetPackageName}"`);
7273
}
7374
} else {
74-
packageLoc = normalizePackageLocation(packageLoc);
75+
packageLoc = normalizePackageLocation(packageLoc, node, targetPackageName, tree.packageName);
7576
}
7677
if (packageLoc !== "" && !pkg.resolved) {
7778
// For all but the root package, ensure that "resolved" and "integrity" fields are present
@@ -104,24 +105,27 @@ export default async function convertPackageLockToShrinkwrap(workspaceRootDir, t
104105
}
105106

106107
/**
107-
* Normalize package locations from workspace-specific paths to standard npm paths
108-
* Examples:
108+
* Normalize package locations from workspace-specific paths to standard npm paths.
109+
* Examples (assuming @ui5/cli is the targetPackageName):
109110
* - packages/cli/node_modules/foo -> node_modules/foo
110-
* - packages/fs/node_modules/bar -> node_modules/bar
111+
* - packages/fs/node_modules/bar -> node_modules/@ui5/fs/node_modules/bar
111112
*
112113
* @param {string} location - Package location from arborist
114+
* @param {object} node - Package node from arborist
115+
* @param {string} targetPackageName - Target package name for shrinkwrap file
116+
* @param {string} rootPackageName - Root / workspace package name
113117
* @returns {string} - Normalized location for npm-shrinkwrap.json
114118
*/
115-
function normalizePackageLocation(location) {
116-
// Remove workspace prefix (packages/*/...) and convert to standard node_modules path
117-
// Match: packages/<workspace-name>/node_modules/... or packages/<workspace-name>
118-
const workspacePattern = /^packages\/[^/]+\/(node_modules\/.+)$/;
119-
const match = location.match(workspacePattern);
120-
if (match) {
121-
return match[1];
119+
function normalizePackageLocation(location, node, targetPackageName, rootPackageName) {
120+
const topPackageName = node.top.packageName;
121+
if (topPackageName === targetPackageName) {
122+
// Remove location for packages within target package (e.g. @ui5/cli)
123+
return location.substring(node.top.location.length + 1);
124+
} else if (topPackageName !== rootPackageName) {
125+
// Add package within node_modules of actual package name (e.g. @ui5/fs)
126+
return `node_modules/${topPackageName}/${location.substring(node.top.location.length + 1)}`;
122127
}
123-
// If it's just the workspace package itself (packages/<name>), keep as-is
124-
// Otherwise return the location unchanged
128+
// If it's already within the root workspace package, keep as-is
125129
return location;
126130
}
127131

internal/shrinkwrap-extractor/test/expected/package.b/npm-shrinkwrap.json

Lines changed: 53 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)