Skip to content

Commit

Permalink
feat: use PNPM logic to process depPathToFilename
Browse files Browse the repository at this point in the history
  • Loading branch information
g-chao committed Apr 9, 2024
1 parent ec306e1 commit ef53ede
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 38 deletions.
7 changes: 3 additions & 4 deletions packages/pnpm-sync-lib/etc/pnpm-sync-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export interface IPnpmSyncCopyOptions {

// @beta (undocumented)
export interface IPnpmSyncPrepareOptions {
depPathToFilename: (depPath: string) => string;
dotPnpmFolder: string;
ensureFolder: (folderPath: string) => Promise<void>;
lockfilePath: string;
Expand All @@ -81,9 +80,9 @@ export type LogMessageDetails = {
lockfilePath: string;
dotPnpmFolder: string;
} | {
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_FORMAT;
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION;
lockfilePath: string;
lockfileVersion: string | undefined;
pnpmVersion: string | undefined;
} | {
messageIdentifier: LogMessageIdentifier.PREPARE_PROCESSING;
lockfilePath: string;
Expand Down Expand Up @@ -120,7 +119,7 @@ export enum LogMessageIdentifier {
// (undocumented)
COPY_STARTING = "copy-starting",
// (undocumented)
PREPARE_ERROR_UNSUPPORTED_FORMAT = "prepare-error-unsupported-format",
PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION = "prepare-error-unsupported-pnpm-version",
// (undocumented)
PREPARE_FINISHING = "prepare-finishing",
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions packages/pnpm-sync-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"lint": "eslint src/**",
"prepublishOnly": "heft build --clean"
},
"dependencies": {
"@pnpm/dependency-path": "2.1.8",
"yaml": "2.4.1"
},
"devDependencies": {
"@rushstack/eslint-config": "^3.6.2",
"@rushstack/eslint-patch": "^1.7.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/pnpm-sync-lib/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export enum LogMessageKind {
export enum LogMessageIdentifier {
// pnpmSyncPrepareAsync() messages
PREPARE_STARTING = 'prepare-starting',
PREPARE_ERROR_UNSUPPORTED_FORMAT = 'prepare-error-unsupported-format',
PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION = 'prepare-error-unsupported-pnpm-version',
PREPARE_PROCESSING = 'prepare-processing',
PREPARE_WRITING_FILE = 'prepare-writing-file',
PREPARE_FINISHING = 'prepare-finishing',
Expand All @@ -52,9 +52,9 @@ export type LogMessageDetails =
dotPnpmFolder: string;
}
| {
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_FORMAT;
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION;
lockfilePath: string;
lockfileVersion: string | undefined;
pnpmVersion: string | undefined;
}
| {
messageIdentifier: LogMessageIdentifier.PREPARE_PROCESSING;
Expand Down
36 changes: 17 additions & 19 deletions packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path';
import fs from 'fs';
import YAML from 'yaml';
import process from 'node:process';
import { depPathToFilename } from '@pnpm/dependency-path';

import {
ILockfile,
Expand All @@ -26,12 +28,6 @@ export interface IPnpmSyncPrepareOptions {
*/
dotPnpmFolder: string;

/**
* Environment-provided API to avoid an NPM dependency.
* The "pnpm-sync" NPM package provides a reference implementation.
*/
depPathToFilename: (depPath: string) => string;

/**
* Environment-provided API to avoid an NPM dependency.
* The "pnpm-sync" NPM package provides a reference implementation.
Expand Down Expand Up @@ -66,7 +62,7 @@ export interface IPnpmSyncPrepareOptions {
* @beta
*/
export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Promise<void> {
const { ensureFolder, readPnpmLockfile, logMessageCallback, depPathToFilename } = options;
const { ensureFolder, readPnpmLockfile, logMessageCallback } = options;
let { lockfilePath, dotPnpmFolder } = options;

// get the pnpm-lock.yaml path
Expand All @@ -90,27 +86,29 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr
throw Error('The input pnpm-lock.yaml path or the input .pnpm folder path is not correct!');
}

const startTime = process.hrtime.bigint();
const pnpmModulesYamlPath: string = path.resolve(dotPnpmFolder, '..');
const pnpmModulesYaml = YAML.parse(fs.readFileSync(`${pnpmModulesYamlPath}/.modules.yaml`, 'utf8'));
const pnpmVersion: string | undefined = pnpmModulesYaml?.packageManager?.split('@')[1];

// read the pnpm-lock.yaml
const pnpmLockfile: ILockfile | undefined = await readPnpmLockfile(lockfilePath, {
ignoreIncompatible: true
});

// currently, only support lockfileVersion 6.x, which is pnpm v8
const lockfileVersion: string | undefined = pnpmLockfile?.lockfileVersion.toString();
if (!lockfileVersion || !lockfileVersion.startsWith('6')) {
// currently, only support pnpm v8
if (!pnpmVersion || !pnpmVersion.startsWith('8')) {
logMessageCallback({
message: `The pnpm-lock.yaml format is not supported; pnpm-sync requires lockfile version 6`,
message: `The pnpm version is not supported; pnpm-sync requires pnpm version 8.x`,
messageKind: LogMessageKind.ERROR,
details: {
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_FORMAT,
messageIdentifier: LogMessageIdentifier.PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION,
lockfilePath,
lockfileVersion
pnpmVersion: pnpmVersion
}
});
return;
}
const startTime = process.hrtime.bigint();

// read the pnpm-lock.yaml
const pnpmLockfile: ILockfile | undefined = await readPnpmLockfile(lockfilePath, {
ignoreIncompatible: true
});

// find injected dependency and all its available versions
const injectedDependencyToVersion: Map<string, Set<string>> = getInjectedDependencyToVersion(pnpmLockfile);
Expand Down
1 change: 0 additions & 1 deletion packages/pnpm-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
},
"dependencies": {
"@pnpm/lockfile-file": "~8.1.4",
"@pnpm/dependency-path": "~2.1.8",
"@pnpm/logger": "~5.0.0",
"@rushstack/node-core-library": "^3.61.0",
"@rushstack/package-extractor": "^0.6.12",
Expand Down
2 changes: 0 additions & 2 deletions packages/pnpm-sync/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { FileSystem, Async } from '@rushstack/node-core-library';
import { PackageExtractor } from '@rushstack/package-extractor';
import { readWantedLockfile, Lockfile } from '@pnpm/lockfile-file';
import { depPathToFilename } from '@pnpm/dependency-path';

const program: Command = new Command();

Expand Down Expand Up @@ -75,7 +74,6 @@ program
await pnpmSyncPrepareAsync({
lockfilePath: lockfile,
dotPnpmFolder: store,
depPathToFilename,
ensureFolder: FileSystem.ensureFolderAsync,
readPnpmLockfile: async (
lockfilePath: string,
Expand Down
19 changes: 13 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/pnpm-sync-api-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"main": "lib/index.js",
"dependencies": {
"@pnpm/lockfile-file": "~8.1.4",
"@pnpm/dependency-path": "~2.1.8",
"@pnpm/logger": "~5.0.0",
"@rushstack/node-core-library": "^3.61.0",
"@rushstack/package-extractor": "^0.6.12",
Expand Down
2 changes: 0 additions & 2 deletions tests/pnpm-sync-api-test/src/test/pnpmSyncPrepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'fs';
import path from 'path';
import { FileSystem, Path } from '@rushstack/node-core-library';
import { readWantedLockfile, Lockfile } from '@pnpm/lockfile-file';
import { depPathToFilename } from '@pnpm/dependency-path';
import {
type ILockfile,
type ILockfilePackage,
Expand Down Expand Up @@ -69,7 +68,6 @@ describe('pnpm-sync-api test', () => {
await pnpmSyncPrepareAsync({
lockfilePath: lockfilePath,
dotPnpmFolder: dotPnpmFolder,
depPathToFilename,
ensureFolder: FileSystem.ensureFolderAsync,
readPnpmLockfile: async (
lockfilePath: string,
Expand Down

0 comments on commit ef53ede

Please sign in to comment.