Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve error handling when failed to load pnpm-sync.json #35

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions packages/pnpm-sync-lib/src/pnpmSyncPrepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
}
}
}

Expand Down
Loading