From 2223563251626b83beb3a850497388d64ac36cec Mon Sep 17 00:00:00 2001 From: Chao Guo <10736839+g-chao@users.noreply.github.com> Date: Tue, 9 Apr 2024 15:58:55 -0700 Subject: [PATCH] feat: use PNPM logic to process depPathToFilename --- .../pnpm-sync-lib/etc/pnpm-sync-lib.api.md | 7 ++-- packages/pnpm-sync-lib/package.json | 4 +++ packages/pnpm-sync-lib/src/interfaces.ts | 6 ++-- packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts | 36 +++++++++---------- packages/pnpm-sync/package.json | 1 - packages/pnpm-sync/src/start.ts | 2 -- pnpm-lock.yaml | 19 ++++++---- tests/pnpm-sync-api-test/package.json | 1 - 8 files changed, 40 insertions(+), 36 deletions(-) diff --git a/packages/pnpm-sync-lib/etc/pnpm-sync-lib.api.md b/packages/pnpm-sync-lib/etc/pnpm-sync-lib.api.md index 060d9d9..2435613 100644 --- a/packages/pnpm-sync-lib/etc/pnpm-sync-lib.api.md +++ b/packages/pnpm-sync-lib/etc/pnpm-sync-lib.api.md @@ -59,7 +59,6 @@ export interface IPnpmSyncCopyOptions { // @beta (undocumented) export interface IPnpmSyncPrepareOptions { - depPathToFilename: (depPath: string) => string; dotPnpmFolder: string; ensureFolder: (folderPath: string) => Promise; lockfilePath: string; @@ -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; @@ -133,7 +132,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) diff --git a/packages/pnpm-sync-lib/package.json b/packages/pnpm-sync-lib/package.json index e77cbb8..42022f8 100644 --- a/packages/pnpm-sync-lib/package.json +++ b/packages/pnpm-sync-lib/package.json @@ -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", diff --git a/packages/pnpm-sync-lib/src/interfaces.ts b/packages/pnpm-sync-lib/src/interfaces.ts index a56053f..823cd43 100644 --- a/packages/pnpm-sync-lib/src/interfaces.ts +++ b/packages/pnpm-sync-lib/src/interfaces.ts @@ -32,7 +32,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_REPLACING_FILE = 'prepare-replacing-file', PREPARE_WRITING_FILE = 'prepare-writing-file', @@ -55,9 +55,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; diff --git a/packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts b/packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts index cce795f..eda2f9e 100644 --- a/packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts +++ b/packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts @@ -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, @@ -27,12 +29,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. @@ -67,7 +63,7 @@ export interface IPnpmSyncPrepareOptions { * @beta */ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Promise { - const { ensureFolder, readPnpmLockfile, logMessageCallback, depPathToFilename } = options; + const { ensureFolder, readPnpmLockfile, logMessageCallback } = options; let { lockfilePath, dotPnpmFolder } = options; // get the pnpm-lock.yaml path @@ -91,27 +87,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> = getInjectedDependencyToVersion(pnpmLockfile); diff --git a/packages/pnpm-sync/package.json b/packages/pnpm-sync/package.json index 5b8dee4..643285f 100644 --- a/packages/pnpm-sync/package.json +++ b/packages/pnpm-sync/package.json @@ -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", diff --git a/packages/pnpm-sync/src/start.ts b/packages/pnpm-sync/src/start.ts index 7884417..08dfcde 100644 --- a/packages/pnpm-sync/src/start.ts +++ b/packages/pnpm-sync/src/start.ts @@ -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(); @@ -75,7 +74,6 @@ program await pnpmSyncPrepareAsync({ lockfilePath: lockfile, dotPnpmFolder: store, - depPathToFilename, ensureFolder: FileSystem.ensureFolderAsync, readPnpmLockfile: async ( lockfilePath: string, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d4fad87..ebd542e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,9 +8,6 @@ importers: packages/pnpm-sync: dependencies: - '@pnpm/dependency-path': - specifier: ~2.1.8 - version: 2.1.8 '@pnpm/lockfile-file': specifier: ~8.1.4 version: 8.1.7(@pnpm/logger@5.0.0) @@ -56,6 +53,13 @@ importers: version: 5.3.3 packages/pnpm-sync-lib: + dependencies: + '@pnpm/dependency-path': + specifier: 2.1.8 + version: 2.1.8 + yaml: + specifier: 2.4.1 + version: 2.4.1 devDependencies: '@rushstack/eslint-config': specifier: ^3.6.2 @@ -149,9 +153,6 @@ importers: tests/pnpm-sync-api-test: dependencies: - '@pnpm/dependency-path': - specifier: ~2.1.8 - version: 2.1.8 '@pnpm/lockfile-file': specifier: ~8.1.4 version: 8.1.7(@pnpm/logger@5.0.0) @@ -4944,6 +4945,12 @@ packages: /yallist@4.0.0: resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==} + /yaml@2.4.1: + resolution: {integrity: sha512-pIXzoImaqmfOrL7teGUBt/T7ZDnyeGBWyXQBvOVhLkWLN37GXv8NMLK406UY6dS51JfcQHsmcW5cJ441bHg6Lg==} + engines: {node: '>= 14'} + hasBin: true + dev: false + /yocto-queue@0.1.0: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} engines: {node: '>=10'} diff --git a/tests/pnpm-sync-api-test/package.json b/tests/pnpm-sync-api-test/package.json index 300adf5..2cf5247 100644 --- a/tests/pnpm-sync-api-test/package.json +++ b/tests/pnpm-sync-api-test/package.json @@ -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",