Skip to content

Commit 1062e4b

Browse files
authored
fix(create-app): handle non commented out cliFile (#545)
* fix(create-app): handle non commented out cliFile * chore: missing line * chore: sort imports * chore: add changeset
1 parent 72bcd0b commit 1062e4b

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
lines changed

.changeset/pink-mails-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-rock': patch
3+
---
4+
5+
Updated the cliFile regex and add a warning when the file content cannot be modified.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import * as fs from 'node:fs';
2+
import * as path from 'node:path';
3+
import { cleanup, getTempDirectory, writeFiles } from '@rock-js/test-helpers';
4+
import * as tools from '@rock-js/tools';
5+
import { updateAndroidBuildGradle } from "../initInExistingProject.js";
6+
7+
const directory = getTempDirectory('test_updateAndroidBuildGradle');
8+
9+
afterEach(() => {
10+
cleanup(directory);
11+
});
12+
13+
describe('updateAndroidBuildGradle', () => {
14+
const workingCases = [
15+
{
16+
name: 'cliFile is commented out',
17+
content: `
18+
react {
19+
// cliFile = file("../../node_modules/react-native/cli.js")
20+
}
21+
`,
22+
expected: `
23+
react {
24+
cliFile = file("../../node_modules/rock/dist/src/bin.js")
25+
}
26+
`,
27+
},
28+
{
29+
name: 'cliFile is not commented out',
30+
content: `
31+
reactNativeDir = file("../../node_modules/react-native")
32+
33+
react {
34+
cliFile = file("\${reactNativeDir}/cli.js")
35+
}
36+
`,
37+
expected: `
38+
reactNativeDir = file("../../node_modules/react-native")
39+
40+
react {
41+
cliFile = file("../../node_modules/rock/dist/src/bin.js")
42+
}
43+
`,
44+
},
45+
{
46+
name: 'cliFile is using a variable',
47+
content: `
48+
reactNativeDir = file("../../node_modules/react-native")
49+
50+
react {
51+
cliFile = file("\${reactNativeDir}/cli.js")
52+
}
53+
`,
54+
expected: `
55+
reactNativeDir = file("../../node_modules/react-native")
56+
57+
react {
58+
cliFile = file("../../node_modules/rock/dist/src/bin.js")
59+
}
60+
`,
61+
},
62+
];
63+
64+
it.each(workingCases)('should update the Android build.gradle file when $name', ({ content, expected }) => {
65+
const files = {
66+
'android/app/build.gradle': content,
67+
};
68+
writeFiles(directory, files);
69+
updateAndroidBuildGradle(directory, 'android');
70+
71+
expect(fs.readFileSync(path.join(directory, 'android/app/build.gradle'), 'utf8')).toStrictEqual(expected)
72+
});
73+
74+
it('should not update the Android build.gradle file when cliFile is already set', () => {
75+
const content = `
76+
react {
77+
cliFile = file("../../node_modules/rock/dist/src/bin.js")
78+
}
79+
`;
80+
const files = {
81+
'android/app/build.gradle': content,
82+
};
83+
84+
writeFiles(directory, files);
85+
updateAndroidBuildGradle(directory, 'android');
86+
87+
expect(fs.readFileSync(path.join(directory, 'android/app/build.gradle'), 'utf8')).toStrictEqual(content)
88+
});
89+
90+
it('should display a warning when unable to update the Android build.gradle file', () => {
91+
const warn = vi.spyOn(tools.logger, 'warn');
92+
93+
const content = `
94+
react {}
95+
`;
96+
97+
const files = {
98+
'android/app/build.gradle': content,
99+
};
100+
101+
writeFiles(directory, files);
102+
updateAndroidBuildGradle(directory, 'android');
103+
104+
expect(fs.readFileSync(path.join(directory, 'android/app/build.gradle'), 'utf8')).toStrictEqual(content)
105+
106+
expect(warn).toHaveBeenCalledWith(
107+
expect.stringContaining('Unable to update')
108+
)
109+
});
110+
});

packages/create-app/src/lib/utils/initInExistingProject.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,35 @@ export default {
166166
fs.writeFileSync(rockConfigPath, content);
167167
}
168168

169-
function updateAndroidBuildGradle(projectRoot: string, sourceDir: string) {
169+
export function updateAndroidBuildGradle(projectRoot: string, sourceDir: string) {
170170
const filePath = path.join(projectRoot, sourceDir, 'app', 'build.gradle');
171171
if (!fs.existsSync(filePath)) {
172172
return;
173173
}
174174
const desired = 'cliFile = file("../../node_modules/rock/dist/src/bin.js")';
175175
const content = fs.readFileSync(filePath, 'utf8');
176+
177+
if (content.includes(desired)) {
178+
logger.debug(`${filePath} already contains "${desired}"`);
179+
return;
180+
}
181+
176182
const replaced = content.replace(
177-
/\/\/\s+cliFile\s*=\s*file\([^)]*\)/g,
183+
/(?:\/\/\s+)?cliFile\s*=\s*file\([^)]*\)/g,
178184
desired,
179185
);
180186

181-
if (!content.includes(desired) && replaced !== content) {
182-
fs.writeFileSync(filePath, replaced);
187+
if (replaced === content) {
188+
logger.warn(
189+
`Unable to update ${color.bold(filePath)}.
190+
Please update the "CLI file" build phase manually with:
191+
cliFile = file("../../node_modules/rock/dist/src/bin.js")
192+
`,
193+
);
183194
return;
184195
}
196+
197+
fs.writeFileSync(filePath, replaced);
185198
}
186199

187200
function updateAndroidSettingsGradle(projectRoot: string, sourceDir: string) {

0 commit comments

Comments
 (0)