Skip to content

Commit

Permalink
Fix relative paths in lockfile (#98)
Browse files Browse the repository at this point in the history
Co-authored-by: Danny Kim <[email protected]>
Co-authored-by: Evan Suhyeong Lee <[email protected]>
  • Loading branch information
3 people authored Jul 15, 2024
1 parent 22ef469 commit 9b85ad5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/lib/lockfile/helpers/generate-pnpm-lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export async function generatePnpmLockfile({

return [
".",
pnpmMapImporter(importer, {
pnpmMapImporter(".", importer, {
includeDevDependencies,
includePatchedDependencies,
directoryByPackageName,
Expand All @@ -130,7 +130,7 @@ export async function generatePnpmLockfile({

return [
importerId,
pnpmMapImporter(importer, {
pnpmMapImporter(importerId, importer, {
includeDevDependencies,
includePatchedDependencies,
directoryByPackageName,
Expand Down
32 changes: 27 additions & 5 deletions src/lib/lockfile/helpers/pnpm-map-importer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "node:path";
import type {
ProjectSnapshot,
ResolvedDependencies,
Expand All @@ -7,6 +8,7 @@ import { mapValues } from "remeda";

/** Convert dependency links */
export function pnpmMapImporter(
importerPath: string,
{ dependencies, devDependencies, ...rest }: ProjectSnapshot,
{
includeDevDependencies,
Expand All @@ -19,21 +21,41 @@ export function pnpmMapImporter(
): ProjectSnapshot {
return {
dependencies: dependencies
? pnpmMapDependenciesLinks(dependencies, directoryByPackageName)
? pnpmMapDependenciesLinks(
importerPath,
dependencies,
directoryByPackageName
)
: undefined,
devDependencies:
includeDevDependencies && devDependencies
? pnpmMapDependenciesLinks(devDependencies, directoryByPackageName)
? pnpmMapDependenciesLinks(
importerPath,
devDependencies,
directoryByPackageName
)
: undefined,
...rest,
};
}

function pnpmMapDependenciesLinks(
importerPath: string,
def: ResolvedDependencies,
directoryByPackageName: { [packageName: string]: string }
): ResolvedDependencies {
return mapValues(def, (value, key) =>
value.startsWith("link:") ? `link:./${directoryByPackageName[key]}` : value
);
return mapValues(def, (value, key) => {
if (!value.startsWith("link:")) {
return value;
}

const relativePath = path.relative(
importerPath,
directoryByPackageName[key]
);

return relativePath.startsWith(".")
? `link:${relativePath}`
: `link:./${relativePath}`;
});
}

0 comments on commit 9b85ad5

Please sign in to comment.