Skip to content

Commit 969145c

Browse files
committed
fix(cli): update command should preserve existing package.json order
1 parent 52e9bae commit 969145c

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

.changeset/cool-elephants-carry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"trigger.dev": patch
3+
---
4+
5+
fix(cli): update command should preserve existing package.json order

packages/cli-v3/src/commands/update.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { confirm, intro, isCancel, log, outro } from "@clack/prompts";
22
import { Command } from "commander";
33
import { detectPackageManager, installDependencies } from "nypm";
4-
import { basename, dirname, join, resolve } from "path";
4+
import { dirname, join, resolve } from "path";
55
import { PackageJson, readPackageJSON, type ResolveOptions, resolvePackageJSON } from "pkg-types";
66
import { z } from "zod";
77
import { CommonCommandOptions, OutroCommandError, wrapCommandAction } from "../cli/common.js";
88
import { chalkError, prettyError, prettyWarning } from "../utilities/cliOutput.js";
9-
import { removeFile, writeJSONFile } from "../utilities/fileSystem.js";
9+
import { removeFile, writeJSONFilePreserveOrder } from "../utilities/fileSystem.js";
1010
import { printStandloneInitialBanner, updateCheck } from "../utilities/initialBanner.js";
1111
import { logger } from "../utilities/logger.js";
1212
import { spinner } from "../utilities/windows.js";
@@ -227,7 +227,7 @@ export async function updateTriggerPackages(
227227

228228
// Backup package.json
229229
const packageJsonBackupPath = `${packageJsonPath}.bak`;
230-
await writeJSONFile(packageJsonBackupPath, readonlyPackageJson, true);
230+
await writeJSONFilePreserveOrder(packageJsonBackupPath, readonlyPackageJson, true);
231231

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

242242
// Update package.json
243243
mutatePackageJsonWithUpdatedPackages(packageJson, mismatches, cliVersion);
244-
await writeJSONFile(packageJsonPath, packageJson, true);
244+
await writeJSONFilePreserveOrder(packageJsonPath, packageJson, true);
245245

246246
async function revertPackageJsonChanges() {
247-
await writeJSONFile(packageJsonPath, readonlyPackageJson, true);
247+
await writeJSONFilePreserveOrder(packageJsonPath, readonlyPackageJson, true);
248248
await removeFile(packageJsonBackupPath);
249249
}
250250

packages/cli-v3/src/utilities/fileSystem.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,26 @@ export async function safeReadJSONFile(path: string) {
159159
}
160160
}
161161

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

172+
/**
173+
* Use this when you want to preserve the original key ordering (e.g. user's package.json)
174+
* @param path - The path to the file to write
175+
* @param json - The JSON object to write
176+
* @param pretty - Whether to pretty print the JSON
177+
*/
178+
export async function writeJSONFilePreserveOrder(path: string, json: any, pretty = false) {
179+
await safeWriteFile(path, JSON.stringify(json, undefined, pretty ? 2 : undefined));
180+
}
181+
166182
// Will create the directory if it doesn't exist
167183
export async function safeWriteFile(path: string, contents: string) {
168184
await fsModule.mkdir(pathModule.dirname(path), { recursive: true });

0 commit comments

Comments
 (0)