Skip to content

refactor: add utility function for writing JSON files #2469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 4 additions & 5 deletions android/autolink.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
readTextFile,
writeTextFile,
} from "../scripts/helpers.js";
import { mkdir_p } from "../scripts/utils/filesystem.mjs";
import { mkdir_p, writeJSONFile } from "../scripts/utils/filesystem.mjs";

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

ensureDirForFile(json);
writeTextFile(json, JSON.stringify(prunedConfig, undefined, 2) + "\n");
writeJSONFile(json, prunedConfig);
writeTextFile(stateFile, state);
return prunedConfig;
}
Expand All @@ -117,12 +117,11 @@ async function main(projectRoot, output) {
);
const dependencies = pickAndroidDependencies(config);

const json = JSON.stringify(dependencies, undefined, 2);
if (!output) {
console.log(json);
console.log(JSON.stringify(dependencies, undefined, 2));
} else {
ensureDirForFile(output);
writeTextFile(output, json + "\n");
writeJSONFile(output, dependencies);
}
}

Expand Down
9 changes: 7 additions & 2 deletions ios/assetsCatalog.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import * as nodefs from "node:fs";
import * as path from "node:path";
import { sourceForAppConfig } from "../scripts/appConfig.mjs";
import { readJSONFile } from "../scripts/helpers.js";
import { cp_r, mkdir_p, rm_r } from "../scripts/utils/filesystem.mjs";
import {
cp_r,
mkdir_p,
rm_r,
writeJSONFile,
} from "../scripts/utils/filesystem.mjs";
import { isObject, projectPath } from "./utils.mjs";

/**
Expand Down Expand Up @@ -146,6 +151,6 @@ export function generateAssetsCatalogs(

const contents = { images, info: template["info"] };
const dest = path.join(appIconSet, "Contents.json");
fs.writeFileSync(dest, JSON.stringify(contents, undefined, 2));
writeJSONFile(dest, contents);
}
}
4 changes: 2 additions & 2 deletions scripts/internal/prepare-viewfinder.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env -S node --experimental-transform-types --no-warnings

import { spawnSync } from "node:child_process";
import * as fs from "node:fs";
import * as path from "node:path";
import { URL, fileURLToPath } from "node:url";
import { readJSONFile } from "../helpers.js";
import { writeJSONFile } from "../utils/filesystem.mjs";

const APP_IDENTIFIER = "com.microsoft.ReactNativeViewfinder";
const PACKAGE_MANAGER = "yarn";
Expand Down Expand Up @@ -34,7 +34,7 @@ function configureAppManifest() {
},
};

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

/**
Expand Down
23 changes: 8 additions & 15 deletions scripts/internal/set-react-version.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* Reminder that this script is meant to be runnable without installing
* dependencies. It can therefore not rely on any external libraries.
*/
import { promises as fs } from "node:fs";
import * as os from "node:os";
import * as fs from "node:fs";
import * as path from "node:path";
import * as util from "node:util";
import {
Expand All @@ -14,6 +13,7 @@ import {
v,
} from "../helpers.js";
import type { Manifest } from "../types.js";
import { writeJSONFile } from "../utils/filesystem.mjs";
import { fetchPackageMetadata, npmRegistryBaseURL } from "../utils/npm.mjs";

const VALID_TAGS = ["canary-macos", "canary-windows", "nightly"];
Expand All @@ -36,12 +36,12 @@ function searchReplaceInFile(
filename: string,
searchValue: string | RegExp,
replaceValue: string
): Promise<void> {
): void {
const current = readTextFile(filename);
const updated = current.replace(searchValue, replaceValue);
return updated === current
? Promise.resolve()
: fs.writeFile(filename, updated);
if (updated !== current) {
fs.writeFileSync(filename, updated);
}
}

/**
Expand Down Expand Up @@ -331,7 +331,6 @@ export async function setReactVersion(
coreOnly: boolean,
overrides: Record<string, string> = {}
): Promise<void> {
let fd: fs.FileHandle | undefined;
try {
const profile = { ...(await getProfile(version, coreOnly)), ...overrides };
console.dir(profile, { depth: null });
Expand Down Expand Up @@ -363,18 +362,12 @@ export async function setReactVersion(
}

const tmpFile = manifestPath + ".tmp";
fd = await fs.open(tmpFile, "w", 0o644);
await fd.write(JSON.stringify(manifest, undefined, 2));
await fd.write(os.EOL);
await fd.close();
fd = undefined;
await fs.rename(tmpFile, manifestPath);
writeJSONFile(tmpFile, manifest);
fs.renameSync(tmpFile, manifestPath);
}
} catch (e) {
console.error(e);
process.exitCode = 1;
} finally {
fd?.close();
}
}

Expand Down
11 changes: 11 additions & 0 deletions scripts/utils/filesystem.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ export function mkdir_p(p, fs = nodefs) {
export function rm_r(p, fs = nodefs) {
fs.rmSync(p, RM_R_OPTIONS);
}

/**
* @param {string} path
* @param {unknown} obj
*/
export function writeJSONFile(path, obj, fs = nodefs) {
const fd = fs.openSync(path, "w", 0o644);
fs.writeSync(fd, JSON.stringify(obj, undefined, 2));
fs.writeSync(fd, "\n");
fs.closeSync(fd);
}
Loading