From a530ea8ed19d4dac7d33c9607c7742df45a66598 Mon Sep 17 00:00:00 2001 From: Nico Domino Date: Mon, 29 Apr 2024 20:17:09 +0200 Subject: [PATCH] fix: preserve newline at the end of `package.json` after update (#14) * fix: add newline at the end of package.json after update * fix: update with dynamic line-endings --- packages/monorepo-release/src/utils.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/monorepo-release/src/utils.ts b/packages/monorepo-release/src/utils.ts index 825c6c4..8a5ff33 100644 --- a/packages/monorepo-release/src/utils.ts +++ b/packages/monorepo-release/src/utils.ts @@ -8,24 +8,25 @@ import { Config, defaultConfig } from "./config.js" async function read( directory: string, -): Promise }>> { + raw?: boolean +): Promise }> | string> { const content = await fs.readFile( path.join(process.cwd(), directory, "package.json"), "utf8", ) - return JSON.parse(content) + return raw ? content : JSON.parse(content) } async function update( directory: string, data: Partial, ): Promise { - const original = await pkgJson.read(directory) - const content = JSON.stringify({ ...original, ...data }, null, 2) + const original = await pkgJson.read(directory, true) as string + let content = JSON.stringify({ ...JSON.parse(original), ...data }, null, 2) + content += original.endsWith("\r\n") ? "\r\n" : "\n" await fs.writeFile( path.join(process.cwd(), directory, "package.json"), content, - "utf8", ) } @@ -69,8 +70,8 @@ export function pluralize( typeof count === "number" ? count : count instanceof Set || count instanceof Map - ? count.size - : count.length + ? count.size + : count.length const pluralForm = pluralRules.select(count) switch (pluralForm) {