Skip to content

Commit 5099891

Browse files
authored
get deterministic serialization using stableStringify (#1064)
1 parent 43eb94d commit 5099891

File tree

4 files changed

+127
-3
lines changed

4 files changed

+127
-3
lines changed

.changeset/giant-owls-exercise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ensembleui/js-commons": patch
3+
---
4+
5+
get deterministic serialization using stableStringify

packages/js-commons/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"dayjs": "^1.11.10",
4343
"eslint-config-custom": "workspace:*",
4444
"firebase": "9.10.0",
45+
"json-stable-stringify": "^1.2.1",
4546
"lodash-es": "^4.17.21",
4647
"tsconfig": "workspace:*",
4748
"tsup": "^7.2.0"

packages/js-commons/src/core/local-files.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
pick,
1717
values,
1818
} from "lodash-es";
19+
import stableStringify from "json-stable-stringify";
1920
import type { LocalApplicationTransporter } from "./transporter";
2021
import { ArtifactProps, EnsembleDocumentType } from "./dto";
2122
import type {
@@ -272,7 +273,7 @@ export const localStoreAsset = async (
272273
await writeFile(assetPath, fileData); // write the file to disk
273274
await writeFile(
274275
join(assetDir, `${fileName}.json`),
275-
JSON.stringify(assetDocument),
276+
stableStringify(assetDocument, { space: 2 }) || "",
276277
); // write file's metadata separately in json
277278

278279
const { relativePath } = await saveArtifact(assetDocument, appMetadata, {
@@ -359,7 +360,9 @@ export const saveArtifact = async (
359360
if (!isAssetOrFont(artifact)) {
360361
await writeFile(
361362
join(artifactSubDir, pathToWrite),
362-
isConfigOrSecret(artifact) ? JSON.stringify(artifact) : artifact.content,
363+
isConfigOrSecret(artifact)
364+
? stableStringify(artifact, { space: 2 }) || ""
365+
: artifact.content,
363366
"utf-8",
364367
);
365368
}
@@ -437,7 +440,7 @@ const writeJsonData = async (
437440
data: unknown,
438441
hideFile = false,
439442
): Promise<void> => {
440-
await writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
443+
await writeFile(filePath, stableStringify(data, { space: 2 }) || "", "utf-8");
441444

442445
if (hideFile) hideOnWindow(filePath);
443446
};

pnpm-lock.yaml

Lines changed: 115 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)