From b41012ca1cf7f39c6ad7f19caeb62c3dcb992f29 Mon Sep 17 00:00:00 2001 From: Aleksey Korolev Date: Mon, 4 Apr 2022 14:20:06 +0300 Subject: [PATCH] feat: patch all file if json didn't parsed --- src/main.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index fe3757c..fc17a01 100644 --- a/src/main.ts +++ b/src/main.ts @@ -75,6 +75,14 @@ async function bumpVersion(semver: SemVer, fromHash?: string) { return parseSemver(`${lastSemver.version}+${shaPart}`)!; } +function parseJsonOrNull(input: string) { + try { + return JSON.parse(input) as T; + } catch { + return null; + } +} + async function patchVersion( filePath: string, objectPath: string, @@ -82,14 +90,14 @@ async function patchVersion( newVersion: SemVer ) { const fileBody = await readFile(filePath, 'utf8'); + const parsedBody = parseJsonOrNull(fileBody); let patchedBody = fileBody; - if (objectPath === '') { + if (objectPath === '' || parsedBody === null) { patchedBody = fileBody.replaceAll(version.raw, newVersion.raw); } else { const indent = detectIndent(fileBody).indent; - const parsedBody = JSON.parse(fileBody); const modifiedBody = shvlSet(parsedBody, objectPath, newVersion.raw); patchedBody = JSON.stringify(modifiedBody, null, indent) + '\n';