Skip to content

Commit 93961d4

Browse files
committed
fix(config): don't overwrite react-native.config.js if it may be configured
1 parent d7a8c0f commit 93961d4

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

scripts/configure.mjs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ export function mergeConfig(lhs, rhs) {
108108
};
109109
}
110110

111+
/**
112+
* Returns whether `react-native.config.js` needs to be updated.
113+
* @param {string} packagePath
114+
* @returns {boolean}
115+
*/
116+
function shouldUpdateReactNativeConfig(packagePath, fs = nodefs) {
117+
const configPath = path.join(packagePath, "react-native.config.js");
118+
const config = readTextFile(configPath, fs);
119+
return (
120+
!/["'`]react-native-test-app["'`]/.test(config) ||
121+
!config.includes("configureProjects")
122+
);
123+
}
124+
111125
/**
112126
* Sort the keys in specified object.
113127
* @param {Record<string, unknown>} obj
@@ -609,15 +623,14 @@ export function updatePackageManifest(
609623

610624
/**
611625
* Writes all specified files to disk.
612-
* @param {Configuration["files"]} files
626+
* @param {[string, string | FileCopy][]} files
613627
* @param {string} destination
614628
* @returns {Promise<void[]>}
615629
*/
616630
export function writeAllFiles(files, destination, fs = nodefs.promises) {
617631
const options = { recursive: true, mode: 0o755 };
618632
return Promise.all(
619-
Object.keys(files).map(async (filename) => {
620-
const content = files[filename];
633+
files.map(async ([filename, content]) => {
621634
if (!content) {
622635
return;
623636
}
@@ -665,8 +678,24 @@ export function configure(params, fs = nodefs) {
665678
}
666679

667680
const { files, oldFiles } = config;
681+
const templateFiles = Object.entries(files).filter(([filename]) => {
682+
switch (filename) {
683+
case "react-native.config.js": {
684+
const needsUpdate = shouldUpdateReactNativeConfig(packagePath);
685+
if (!needsUpdate) {
686+
warn(
687+
`skipped modifying '${filename}' because it may already be configured for 'react-native-test-app'`
688+
);
689+
}
690+
return needsUpdate;
691+
}
692+
693+
default:
694+
return true;
695+
}
696+
});
668697

669-
writeAllFiles(files, packagePath).then(() => {
698+
writeAllFiles(templateFiles, packagePath).then(() => {
670699
const packageManifest = path.join(packagePath, "package.json");
671700
if (!fs.existsSync(packageManifest)) {
672701
// We cannot assume that the app itself is an npm package. Some libraries

test/configure/writeAllFiles.test.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ describe("writeAllFiles()", () => {
1717
it("writes all files to disk", async () => {
1818
setMockFiles({ "file-on-disk": "0" });
1919
await writeAllFiles(
20-
{
21-
file0: { source: "file-on-disk" },
22-
file1: "1",
23-
file2: "2",
24-
},
20+
[
21+
["file0", { source: "file-on-disk" }],
22+
["file1", "1"],
23+
["file2", "2"],
24+
],
2525
"test"
2626
);
2727

@@ -32,11 +32,11 @@ describe("writeAllFiles()", () => {
3232

3333
it("ignores files with no content", async () => {
3434
await writeAllFiles(
35-
{
36-
file1: "1",
37-
file2: "2",
38-
file3: "",
39-
},
35+
[
36+
["file1", "1"],
37+
["file2", "2"],
38+
["file3", ""],
39+
],
4040
"."
4141
);
4242

@@ -48,25 +48,23 @@ describe("writeAllFiles()", () => {
4848
it("rethrows write exceptions", async () => {
4949
await rejects(
5050
writeAllFiles(
51-
{
52-
file0: {
53-
// This entry will throw an exception
54-
source: "Bad Arnold movies.txt",
55-
},
56-
file1: "1",
57-
file2: "2",
58-
},
51+
[
52+
// This entry will throw an exception
53+
["file0", { source: "Bad Arnold movies.txt" }],
54+
["file1", "1"],
55+
["file2", "2"],
56+
],
5957
"."
6058
)
6159
);
6260

6361
await rejects(
6462
writeAllFiles(
65-
{
66-
file1: "1",
67-
file2: "2",
68-
"": "3", // This entry will throw an exception
69-
},
63+
[
64+
["file1", "1"],
65+
["file2", "2"],
66+
["", "3"], // This entry will throw an exception
67+
],
7068
"."
7169
)
7270
);

0 commit comments

Comments
 (0)