Skip to content

Commit

Permalink
feat: convert V5->V6 instead of vice-versa
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Qun committed May 14, 2024
1 parent e0144b3 commit cfcf9d4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class LockfileDependency {
console.error('Peer dependencies info missing!', node);
}
} else {
this.entryId = '/' + this.name + '/' + this.version;
this.entryId = '/' + this.name + '@' + this.version;
}
}

Expand Down
6 changes: 3 additions & 3 deletions apps/lockfile-explorer/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { IAppContext } from '@rushstack/lockfile-explorer-web/lib/AppContex
import { Colorize } from '@rushstack/terminal';
import type { Lockfile } from '@pnpm/lockfile-types';

import { convertLockfileV6DepPathToV5DepPath } from './utils';
import { convertOldDepPathToNewDepPath } from './utils';
import { init } from './init';
import type { IAppState } from './state';
import { type ICommandLine, parseCommandLine } from './commandLine';
Expand Down Expand Up @@ -121,10 +121,10 @@ function startApp(debugMode: boolean): void {
throw new Error('The current lockfile version is not supported.');
}

if (packages && shrinkwrapFileMajorVersion === 6) {
if (packages && shrinkwrapFileMajorVersion === 5) {
const updatedPackages: Lockfile['packages'] = {};
for (const [dependencyPath, dependency] of Object.entries(packages)) {
updatedPackages[convertLockfileV6DepPathToV5DepPath(dependencyPath)] = dependency;
updatedPackages[convertOldDepPathToNewDepPath(dependencyPath)] = dependency;
}
doc.packages = updatedPackages;
}
Expand Down
31 changes: 17 additions & 14 deletions apps/lockfile-explorer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
import * as dp from '@pnpm/dependency-path';

/**
* Example: "" --> ""
* Example: "" --> ""
* This operation exactly mirrors the behavior of PNPM's own implementation:
* https://github.com/pnpm/pnpm/blob/73ebfc94e06d783449579cda0c30a40694d210e4/lockfile/lockfile-file/src/experiments/inlineSpecifiersLockfileConverters.ts#L72
*/
export function convertLockfileV5DepPathToV6DepPath(newDepPath: string): string {
if (newDepPath.startsWith('file:')) return newDepPath;
const index = newDepPath.indexOf('/', 2);
if (newDepPath.includes('(') && index > dp.indexOfPeersSuffix(newDepPath)) return newDepPath;
return `${newDepPath.substring(0, index)}@${newDepPath.substring(index + 1)}`;
}

export function convertLockfileV6DepPathToV5DepPath(newDepPath: string): string {
if (!newDepPath.includes('@', 2) || newDepPath.startsWith('file:')) return newDepPath;
const index = newDepPath.indexOf('@', newDepPath.indexOf('/@') + 2);
if (newDepPath.includes('(') && index > dp.indexOfPeersSuffix(newDepPath)) return newDepPath;
return `${newDepPath.substring(0, index)}/${newDepPath.substring(index + 1)}`;
export function convertOldDepPathToNewDepPath(oldDepPath: string): string {
const parsedDepPath = dp.parse(oldDepPath);
if (!parsedDepPath.name || !parsedDepPath.version) return oldDepPath;
let newDepPath = `/${parsedDepPath.name}@${parsedDepPath.version}`;
if (parsedDepPath.peersSuffix) {
if (parsedDepPath.peersSuffix.startsWith('(')) {
newDepPath += parsedDepPath.peersSuffix;
} else {
newDepPath += `_${parsedDepPath.peersSuffix}`;
}
}
if (parsedDepPath.host) {
newDepPath = `${parsedDepPath.host}${newDepPath}`;
}
return newDepPath;
}

0 comments on commit cfcf9d4

Please sign in to comment.