Skip to content

Commit

Permalink
feat: patch all file if json didn't parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
playmean committed Apr 4, 2022
1 parent c9fc4d9 commit b41012c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,29 @@ async function bumpVersion(semver: SemVer, fromHash?: string) {
return parseSemver(`${lastSemver.version}+${shaPart}`)!;
}

function parseJsonOrNull<T = any>(input: string) {
try {
return JSON.parse(input) as T;
} catch {
return null;
}
}

async function patchVersion(
filePath: string,
objectPath: string,
version: SemVer,
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';
Expand Down

0 comments on commit b41012c

Please sign in to comment.