Skip to content
Merged
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/cool-elephants-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---

fix(cli): update command should preserve existing package.json order
10 changes: 5 additions & 5 deletions packages/cli-v3/src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { confirm, intro, isCancel, log, outro } from "@clack/prompts";
import { Command } from "commander";
import { detectPackageManager, installDependencies } from "nypm";
import { basename, dirname, join, resolve } from "path";
import { dirname, join, resolve } from "path";
import { PackageJson, readPackageJSON, type ResolveOptions, resolvePackageJSON } from "pkg-types";
import { z } from "zod";
import { CommonCommandOptions, OutroCommandError, wrapCommandAction } from "../cli/common.js";
import { chalkError, prettyError, prettyWarning } from "../utilities/cliOutput.js";
import { removeFile, writeJSONFile } from "../utilities/fileSystem.js";
import { removeFile, writeJSONFilePreserveOrder } from "../utilities/fileSystem.js";
import { printStandloneInitialBanner, updateCheck } from "../utilities/initialBanner.js";
import { logger } from "../utilities/logger.js";
import { spinner } from "../utilities/windows.js";
Expand Down Expand Up @@ -227,7 +227,7 @@ export async function updateTriggerPackages(

// Backup package.json
const packageJsonBackupPath = `${packageJsonPath}.bak`;
await writeJSONFile(packageJsonBackupPath, readonlyPackageJson, true);
await writeJSONFilePreserveOrder(packageJsonBackupPath, readonlyPackageJson, true);

const exitHandler = async (sig: any) => {
log.warn(
Expand All @@ -241,10 +241,10 @@ export async function updateTriggerPackages(

// Update package.json
mutatePackageJsonWithUpdatedPackages(packageJson, mismatches, cliVersion);
await writeJSONFile(packageJsonPath, packageJson, true);
await writeJSONFilePreserveOrder(packageJsonPath, packageJson, true);

async function revertPackageJsonChanges() {
await writeJSONFile(packageJsonPath, readonlyPackageJson, true);
await writeJSONFilePreserveOrder(packageJsonPath, readonlyPackageJson, true);
await removeFile(packageJsonBackupPath);
}

Expand Down
16 changes: 16 additions & 0 deletions packages/cli-v3/src/utilities/fileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,26 @@ export async function safeReadJSONFile(path: string) {
}
}

/**
* Use this for deterministic builds. Uses `json-stable-stringify` to sort keys alphabetically.
* @param path - The path to the file to write
* @param json - The JSON object to write
* @param pretty - Whether to pretty print the JSON
*/
export async function writeJSONFile(path: string, json: any, pretty = false) {
await safeWriteFile(path, stringify(json, pretty ? { space: 2 } : undefined) ?? "");
}

/**
* Use this when you want to preserve the original key ordering (e.g. user's package.json)
* @param path - The path to the file to write
* @param json - The JSON object to write
* @param pretty - Whether to pretty print the JSON
*/
export async function writeJSONFilePreserveOrder(path: string, json: any, pretty = false) {
await safeWriteFile(path, JSON.stringify(json, undefined, pretty ? 2 : undefined));
}

// Will create the directory if it doesn't exist
export async function safeWriteFile(path: string, contents: string) {
await fsModule.mkdir(pathModule.dirname(path), { recursive: true });
Expand Down