diff --git a/.changeset/package-json-eol.md b/.changeset/package-json-eol.md new file mode 100644 index 00000000000..2a45cfb133a --- /dev/null +++ b/.changeset/package-json-eol.md @@ -0,0 +1,5 @@ +--- +"trigger.dev": patch +--- + +Preserve the trailing newline in `package.json` when `trigger deploy`/`dev` updates the `@trigger.dev/*` package versions, instead of always writing the file without one. diff --git a/packages/cli-v3/src/commands/update.ts b/packages/cli-v3/src/commands/update.ts index dc423bcf240..c5fca56244b 100644 --- a/packages/cli-v3/src/commands/update.ts +++ b/packages/cli-v3/src/commands/update.ts @@ -1,4 +1,5 @@ import { confirm, intro, isCancel, log, outro } from "@clack/prompts"; +import { readFile } from "node:fs/promises"; import type { Command } from "commander"; import { detectPackageManager, installDependencies } from "nypm"; import { dirname, join, resolve } from "path"; @@ -269,9 +270,12 @@ Set them to ${cliVersion} in package.json and reinstall, or run the CLI version const installSpinner = spinner(); installSpinner.start("Updating dependencies in package.json"); + // Preserve the existing end-of-file newline when rewriting package.json + const hasTrailingNewline = (await readFile(packageJsonPath, "utf8")).endsWith("\n"); + // Backup package.json const packageJsonBackupPath = `${packageJsonPath}.bak`; - await writeJSONFilePreserveOrder(packageJsonBackupPath, readonlyPackageJson, true); + await writeJSONFilePreserveOrder(packageJsonBackupPath, readonlyPackageJson, true, hasTrailingNewline); const exitHandler = async (sig: any) => { log.warn( @@ -285,10 +289,10 @@ Set them to ${cliVersion} in package.json and reinstall, or run the CLI version // Update package.json mutatePackageJsonWithUpdatedPackages(packageJson, mismatches, cliVersion); - await writeJSONFilePreserveOrder(packageJsonPath, packageJson, true); + await writeJSONFilePreserveOrder(packageJsonPath, packageJson, true, hasTrailingNewline); async function revertPackageJsonChanges() { - await writeJSONFilePreserveOrder(packageJsonPath, readonlyPackageJson, true); + await writeJSONFilePreserveOrder(packageJsonPath, readonlyPackageJson, true, hasTrailingNewline); await removeFile(packageJsonBackupPath); } diff --git a/packages/cli-v3/src/utilities/fileSystem.ts b/packages/cli-v3/src/utilities/fileSystem.ts index 6f287fe7027..e2580ab066b 100644 --- a/packages/cli-v3/src/utilities/fileSystem.ts +++ b/packages/cli-v3/src/utilities/fileSystem.ts @@ -160,9 +160,18 @@ export async function writeJSONFile(path: string, json: any, pretty = false) { * @param path - The path to the file to write * @param json - The JSON object to write * @param pretty - Whether to pretty print the JSON + * @param trailingNewline - Whether to end the file with a newline */ -export async function writeJSONFilePreserveOrder(path: string, json: any, pretty = false) { - await safeWriteFile(path, JSON.stringify(json, undefined, pretty ? 2 : undefined)); +export async function writeJSONFilePreserveOrder( + path: string, + json: any, + pretty = false, + trailingNewline = false +) { + await safeWriteFile( + path, + JSON.stringify(json, undefined, pretty ? 2 : undefined) + (trailingNewline ? "\n" : "") + ); } // Will create the directory if it doesn't exist