Skip to content

Commit e7dd6fa

Browse files
authored
refactor: add utility function for writing JSON files (#2469)
1 parent bc175f1 commit e7dd6fa

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

android/autolink.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
readTextFile,
1010
writeTextFile,
1111
} from "../scripts/helpers.js";
12-
import { mkdir_p } from "../scripts/utils/filesystem.mjs";
12+
import { mkdir_p, writeJSONFile } from "../scripts/utils/filesystem.mjs";
1313

1414
/**
1515
* @typedef {import("@react-native-community/cli-types").Config} Config
@@ -97,7 +97,7 @@ async function loadConfig(json, projectRoot) {
9797
const prunedConfig = pruneDependencies(config);
9898

9999
ensureDirForFile(json);
100-
writeTextFile(json, JSON.stringify(prunedConfig, undefined, 2) + "\n");
100+
writeJSONFile(json, prunedConfig);
101101
writeTextFile(stateFile, state);
102102
return prunedConfig;
103103
}
@@ -117,12 +117,11 @@ async function main(projectRoot, output) {
117117
);
118118
const dependencies = pickAndroidDependencies(config);
119119

120-
const json = JSON.stringify(dependencies, undefined, 2);
121120
if (!output) {
122-
console.log(json);
121+
console.log(JSON.stringify(dependencies, undefined, 2));
123122
} else {
124123
ensureDirForFile(output);
125-
writeTextFile(output, json + "\n");
124+
writeJSONFile(output, dependencies);
126125
}
127126
}
128127

ios/assetsCatalog.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import * as nodefs from "node:fs";
44
import * as path from "node:path";
55
import { sourceForAppConfig } from "../scripts/appConfig.mjs";
66
import { readJSONFile } from "../scripts/helpers.js";
7-
import { cp_r, mkdir_p, rm_r } from "../scripts/utils/filesystem.mjs";
7+
import {
8+
cp_r,
9+
mkdir_p,
10+
rm_r,
11+
writeJSONFile,
12+
} from "../scripts/utils/filesystem.mjs";
813
import { isObject, projectPath } from "./utils.mjs";
914

1015
/**
@@ -146,6 +151,6 @@ export function generateAssetsCatalogs(
146151

147152
const contents = { images, info: template["info"] };
148153
const dest = path.join(appIconSet, "Contents.json");
149-
fs.writeFileSync(dest, JSON.stringify(contents, undefined, 2));
154+
writeJSONFile(dest, contents, fs);
150155
}
151156
}

scripts/internal/prepare-viewfinder.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env -S node --experimental-transform-types --no-warnings
22

33
import { spawnSync } from "node:child_process";
4-
import * as fs from "node:fs";
54
import * as path from "node:path";
65
import { URL, fileURLToPath } from "node:url";
76
import { readJSONFile } from "../helpers.js";
7+
import { writeJSONFile } from "../utils/filesystem.mjs";
88

99
const APP_IDENTIFIER = "com.microsoft.ReactNativeViewfinder";
1010
const PACKAGE_MANAGER = "yarn";
@@ -34,7 +34,7 @@ function configureAppManifest() {
3434
},
3535
};
3636

37-
fs.writeFileSync(APP_MANIFEST, JSON.stringify(manifest, null, 2) + "\n");
37+
writeJSONFile(APP_MANIFEST, manifest);
3838
}
3939

4040
/**

scripts/internal/set-react-version.mts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
* Reminder that this script is meant to be runnable without installing
33
* dependencies. It can therefore not rely on any external libraries.
44
*/
5-
import { promises as fs } from "node:fs";
6-
import * as os from "node:os";
5+
import * as fs from "node:fs";
76
import * as path from "node:path";
87
import * as util from "node:util";
98
import {
@@ -14,6 +13,7 @@ import {
1413
v,
1514
} from "../helpers.js";
1615
import type { Manifest } from "../types.js";
16+
import { writeJSONFile } from "../utils/filesystem.mjs";
1717
import { fetchPackageMetadata, npmRegistryBaseURL } from "../utils/npm.mjs";
1818

1919
const VALID_TAGS = ["canary-macos", "canary-windows", "nightly"];
@@ -36,12 +36,12 @@ function searchReplaceInFile(
3636
filename: string,
3737
searchValue: string | RegExp,
3838
replaceValue: string
39-
): Promise<void> {
39+
): void {
4040
const current = readTextFile(filename);
4141
const updated = current.replace(searchValue, replaceValue);
42-
return updated === current
43-
? Promise.resolve()
44-
: fs.writeFile(filename, updated);
42+
if (updated !== current) {
43+
fs.writeFileSync(filename, updated);
44+
}
4545
}
4646

4747
/**
@@ -331,7 +331,6 @@ export async function setReactVersion(
331331
coreOnly: boolean,
332332
overrides: Record<string, string> = {}
333333
): Promise<void> {
334-
let fd: fs.FileHandle | undefined;
335334
try {
336335
const profile = { ...(await getProfile(version, coreOnly)), ...overrides };
337336
console.dir(profile, { depth: null });
@@ -363,18 +362,12 @@ export async function setReactVersion(
363362
}
364363

365364
const tmpFile = manifestPath + ".tmp";
366-
fd = await fs.open(tmpFile, "w", 0o644);
367-
await fd.write(JSON.stringify(manifest, undefined, 2));
368-
await fd.write(os.EOL);
369-
await fd.close();
370-
fd = undefined;
371-
await fs.rename(tmpFile, manifestPath);
365+
writeJSONFile(tmpFile, manifest);
366+
fs.renameSync(tmpFile, manifestPath);
372367
}
373368
} catch (e) {
374369
console.error(e);
375370
process.exitCode = 1;
376-
} finally {
377-
fd?.close();
378371
}
379372
}
380373

scripts/utils/filesystem.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,14 @@ export function mkdir_p(p, fs = nodefs) {
2626
export function rm_r(p, fs = nodefs) {
2727
fs.rmSync(p, RM_R_OPTIONS);
2828
}
29+
30+
/**
31+
* @param {string} path
32+
* @param {unknown} obj
33+
*/
34+
export function writeJSONFile(path, obj, fs = nodefs) {
35+
const fd = fs.openSync(path, "w", 0o644);
36+
fs.writeSync(fd, JSON.stringify(obj, undefined, 2));
37+
fs.writeSync(fd, "\n");
38+
fs.closeSync(fd);
39+
}

0 commit comments

Comments
 (0)