From 78afa0b6db490bbfa4a217cca2ebd30e30babe22 Mon Sep 17 00:00:00 2001 From: Cheng Liu Date: Sat, 8 Jun 2024 00:44:13 -0700 Subject: [PATCH 1/2] feat: improve error handling when failed to load pnpm-sync.json --- packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts b/packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts index de19e28..1780551 100644 --- a/packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts +++ b/packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts @@ -214,34 +214,40 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr // if .pnpm-sync.json already exists, read it first if (fs.existsSync(pnpmSyncJsonPath)) { - const existingPnpmSyncJsonFile: IPnpmSyncJson = JSON.parse( - fs.readFileSync(pnpmSyncJsonPath).toString() - ); + let existingPnpmSyncJsonFile: IPnpmSyncJson | undefined; + try { + existingPnpmSyncJsonFile = JSON.parse(fs.readFileSync(pnpmSyncJsonPath).toString()); + } catch (e) { + // no-catch + // Regenerate .pnpm-sync.json when failed to load the current one + } - const actualPnpmSyncJsonVersion: string = existingPnpmSyncJsonFile?.version; - if (actualPnpmSyncJsonVersion === expectedPnpmSyncJsonVersion) { - // If a lockfileId is provided - // then all entries with this lockfileId should be deleted - // they will be regenerated later - if (lockfileId) { - const filteredTargetFolders = existingPnpmSyncJsonFile.postbuildInjectedCopy.targetFolders.filter( - (targetFolder) => targetFolder?.lockfileId !== lockfileId - ); - existingPnpmSyncJsonFile.postbuildInjectedCopy.targetFolders = filteredTargetFolders; - } - pnpmSyncJsonFile = existingPnpmSyncJsonFile; - } else { - logMessageCallback({ - message: `The .pnpm-sync.json file in ${pnpmSyncJsonFolder} has an incompatible version; pnpm-sync will regenerate it.`, - messageKind: LogMessageKind.VERBOSE, - details: { - messageIdentifier: LogMessageIdentifier.PREPARE_REPLACING_FILE, - pnpmSyncJsonPath, - projectFolder, - actualVersion: actualPnpmSyncJsonVersion, - expectedVersion: expectedPnpmSyncJsonVersion + if (existingPnpmSyncJsonFile) { + const actualPnpmSyncJsonVersion: string = existingPnpmSyncJsonFile.version; + if (actualPnpmSyncJsonVersion === expectedPnpmSyncJsonVersion) { + // If a lockfileId is provided + // then all entries with this lockfileId should be deleted + // they will be regenerated later + if (lockfileId) { + const filteredTargetFolders = existingPnpmSyncJsonFile.postbuildInjectedCopy.targetFolders.filter( + (targetFolder) => targetFolder?.lockfileId !== lockfileId + ); + existingPnpmSyncJsonFile.postbuildInjectedCopy.targetFolders = filteredTargetFolders; } - }); + pnpmSyncJsonFile = existingPnpmSyncJsonFile; + } else { + logMessageCallback({ + message: `The .pnpm-sync.json file in ${pnpmSyncJsonFolder} has an incompatible version; pnpm-sync will regenerate it.`, + messageKind: LogMessageKind.VERBOSE, + details: { + messageIdentifier: LogMessageIdentifier.PREPARE_REPLACING_FILE, + pnpmSyncJsonPath, + projectFolder, + actualVersion: actualPnpmSyncJsonVersion, + expectedVersion: expectedPnpmSyncJsonVersion + } + }); + } } } From af493bcbec641174acd5149cf5d4caaf5150c0d8 Mon Sep 17 00:00:00 2001 From: Chao Guo <10736839+g-chao@users.noreply.github.com> Date: Mon, 10 Jun 2024 12:08:31 -0700 Subject: [PATCH 2/2] chore: bump version --- packages/pnpm-sync-lib/package.json | 2 +- packages/pnpm-sync/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/pnpm-sync-lib/package.json b/packages/pnpm-sync-lib/package.json index 6446732..fb55d1d 100644 --- a/packages/pnpm-sync-lib/package.json +++ b/packages/pnpm-sync-lib/package.json @@ -1,6 +1,6 @@ { "name": "pnpm-sync-lib", - "version": "0.2.7", + "version": "0.2.8", "description": "API library for integrating \"pnpm-sync\" with your toolchain", "repository": { "type": "git", diff --git a/packages/pnpm-sync/package.json b/packages/pnpm-sync/package.json index c63032e..ecdfbc1 100644 --- a/packages/pnpm-sync/package.json +++ b/packages/pnpm-sync/package.json @@ -1,6 +1,6 @@ { "name": "pnpm-sync", - "version": "0.2.7", + "version": "0.2.8", "description": "Recopy injected dependencies whenever a project is rebuilt in your PNPM workspace", "keywords": [ "rush",