Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/package-json-eol.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 7 additions & 3 deletions packages/cli-v3/src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}

Expand Down
13 changes: 11 additions & 2 deletions packages/cli-v3/src/utilities/fileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down