-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathbootsplash.js
More file actions
295 lines (256 loc) · 9.19 KB
/
bootsplash.js
File metadata and controls
295 lines (256 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const fs = require('fs');
const path = require('path');
const { executeCommand } = require('../utils/commands');
const { log } = require('../utils/logger');
/**
* Set up react-native-bootsplash (splash screen) and
* @forward-software/react-native-toolbox (app icons) in the generated project.
*
* @param {string} projectPath - Path to the generated project
* @param {boolean} isExpo - Whether the project uses Expo
* @param {boolean} useExpoRouter - Whether the project uses Expo Router
* @param {boolean} useAuthFlow - Whether auth flow is enabled
*/
async function setupBootsplash(projectPath, isExpo, useExpoRouter, useAuthFlow, pm) {
log.info('Installing react-native-bootsplash and react-native-toolbox...');
// Install packages
await executeCommand(pm.add('react-native-bootsplash'), { cwd: projectPath });
await executeCommand(
pm.addDev('@forward-software/react-native-toolbox'),
{ cwd: projectPath },
);
// Create assets directory
const assetsDir = path.join(projectPath, 'assets');
if (!fs.existsSync(assetsDir)) {
fs.mkdirSync(assetsDir, { recursive: true });
}
// Copy VoltRN logo as splash and icon placeholders
const logoSvgSource = path.join(__dirname, '..', 'assets', 'voltrn-logo.svg');
const logoPngSource = path.join(__dirname, '..', 'assets', 'voltrn-logo.png');
const logoSvg = fs.readFileSync(logoSvgSource, 'utf8');
fs.writeFileSync(path.join(assetsDir, 'splashscreen.svg'), logoSvg);
fs.writeFileSync(path.join(assetsDir, 'icon.svg'), logoSvg);
fs.copyFileSync(logoPngSource, path.join(assetsDir, 'icon.png'));
// Add npm scripts to project's package.json
addAssetScripts(projectPath);
// Configure Expo plugin if needed
if (isExpo) {
configureExpoPlugin(projectPath);
}
// Run initial splash and icon generation (RN CLI only. Expo does it during prebuild)
if (!isExpo) {
await runSplashGeneration(projectPath);
await runIconGeneration(projectPath);
}
// Wire up BootSplash.hide() in the app entry point
wireBootSplashHide(projectPath, isExpo, useExpoRouter, useAuthFlow);
log.success('Splash screen and app icon setup complete');
}
/**
* Add assets:splash and assets:icons scripts to the project's package.json
*/
function addAssetScripts(projectPath) {
const pkgPath = path.join(projectPath, 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
if (!pkg.scripts) {
pkg.scripts = {};
}
pkg.scripts['assets:splash'] =
'npx react-native-bootsplash generate assets/splashscreen.svg --platforms=android,ios --background=1A1A2E --logo-width=128 --assets-output=assets/bootsplash';
pkg.scripts['assets:icons'] =
'npx @forward-software/react-native-toolbox icons assets/icon.svg';
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
}
/**
* Add react-native-bootsplash as an Expo plugin in app.json
*/
function configureExpoPlugin(projectPath) {
const appJsonPath = path.join(projectPath, 'app.json');
if (!fs.existsSync(appJsonPath)) {
return;
}
const appJson = JSON.parse(fs.readFileSync(appJsonPath, 'utf8'));
if (!appJson.expo) {
return;
}
if (!appJson.expo.plugins) {
appJson.expo.plugins = [];
}
appJson.expo.plugins.push([
'react-native-bootsplash',
{
logo: './assets/splashscreen.svg',
logoWidth: 128,
background: '#1A1A2E',
assetsOutput: 'assets/bootsplash',
},
]);
appJson.expo.icon = './assets/icon.png';
if (!appJson.expo.android) {
appJson.expo.android = {};
}
appJson.expo.android.adaptiveIcon = {
foregroundImage: './assets/icon.png',
backgroundColor: '#1A1A2E',
};
fs.writeFileSync(appJsonPath, JSON.stringify(appJson, null, 2));
log.info('Added react-native-bootsplash plugin to app.json');
}
/**
* Run the initial splash screen generation for RN CLI projects
*/
async function runSplashGeneration(projectPath) {
try {
await executeCommand(
'npx react-native-bootsplash generate assets/splashscreen.svg --platforms=android,ios --background=1A1A2E --logo-width=128 --assets-output=assets/bootsplash',
{ cwd: projectPath },
);
log.success('Splash screen assets generated');
} catch (error) {
log.warning(
'Could not generate splash screen assets automatically. Run "npm run assets:splash" manually after setup.',
);
}
}
/**
* Run the initial app icon generation for RN CLI projects
*/
async function runIconGeneration(projectPath) {
try {
await executeCommand(
'npx @forward-software/react-native-toolbox icons assets/icon.svg',
{ cwd: projectPath },
);
log.success('App icon assets generated');
} catch (error) {
log.warning(
'Could not generate app icon assets automatically. Run "npm run assets:icons" manually after setup.',
);
}
}
/**
* Wire up BootSplash.hide() in the app entry point.
*
* For Expo Router (auth): replaces Expo SplashScreen with BootSplash in _layout.tsx
* For Expo Router (no auth): adds BootSplash.hide() to _layout.tsx
* For React Navigation: adds BootSplash.hide() to App.tsx
*/
function wireBootSplashHide(projectPath, isExpo, useExpoRouter, useAuthFlow) {
if (useExpoRouter) {
patchExpoRouterLayout(projectPath, useAuthFlow);
} else {
patchAppTsx(projectPath);
}
}
/**
* Patch App.tsx (React Navigation: both Expo and RN CLI) to add BootSplash.hide()
*/
function patchAppTsx(projectPath) {
const appTsxPath = path.join(projectPath, 'App.tsx');
if (!fs.existsSync(appTsxPath)) {
log.warning('App.tsx not found. Skipping BootSplash.hide() injection');
return;
}
let content = fs.readFileSync(appTsxPath, 'utf8');
// Add imports
const bootsplashImport =
"import BootSplash from 'react-native-bootsplash';\nimport { useEffect } from 'react';";
// Insert import after the first import line
const firstImportEnd = content.indexOf('\n');
content =
content.slice(0, firstImportEnd + 1) +
bootsplashImport +
'\n' +
content.slice(firstImportEnd + 1);
// Add useEffect inside the App function body
const functionBodyMatch = content.match(/function App\(\).*?\{[\s]*\n/);
if (functionBodyMatch) {
const insertPos =
content.indexOf(functionBodyMatch[0]) + functionBodyMatch[0].length;
const hideEffect = ` useEffect(() => {
BootSplash.hide({ fade: true });
}, []);\n\n`;
content =
content.slice(0, insertPos) + hideEffect + content.slice(insertPos);
}
fs.writeFileSync(appTsxPath, content);
log.info('Added BootSplash.hide() to App.tsx');
}
/**
* Patch Expo Router root _layout.tsx to use BootSplash instead of Expo SplashScreen
*/
function patchExpoRouterLayout(projectPath, useAuthFlow) {
const layoutPath = path.join(projectPath, 'app', '_layout.tsx');
if (!fs.existsSync(layoutPath)) {
log.warning(
'app/_layout.tsx not found. Skipping BootSplash.hide() injection',
);
return;
}
let content = fs.readFileSync(layoutPath, 'utf8');
if (useAuthFlow) {
// Auth flow already has SplashScreen logic. Replace with BootSplash
// Replace Expo SplashScreen import
content = content.replace(
/import\s*\{([^}]*)\bSplashScreen\b([^}]*)\}\s*from\s*'expo-router'/,
(match, before, after) => {
const remaining = (before + after)
.split(',')
.map((s) => s.trim())
.filter((s) => s && s !== 'SplashScreen')
.join(', ');
return `import { ${remaining} } from 'expo-router';\nimport BootSplash from 'react-native-bootsplash'`;
},
);
// Replace SplashScreen.preventAutoHideAsync()
content = content.replace(
/\/\/.*Prevent.*splash.*\n?.*SplashScreen\.preventAutoHideAsync\(\);?\n?/i,
'',
);
// Replace SplashScreen.hideAsync() with BootSplash.hide()
content = content.replace(
/SplashScreen\.hideAsync\(\)/g,
'BootSplash.hide({ fade: true })',
);
} else {
// No auth. Add BootSplash import and hide call
const bootsplashImport =
"import BootSplash from 'react-native-bootsplash';";
// Add import after existing imports
const lastImportMatch = content.match(/^import .+$/gm);
if (lastImportMatch) {
const lastImport = lastImportMatch[lastImportMatch.length - 1];
const lastImportPos = content.lastIndexOf(lastImport) + lastImport.length;
content =
content.slice(0, lastImportPos) +
'\n' +
bootsplashImport +
"\nimport { useEffect } from 'react';\n" +
content.slice(lastImportPos);
}
// Find the main layout/function and add useEffect with BootSplash.hide()
// Look for function body patterns used in the generated layouts
const funcPatterns = [
/function RootLayoutNav\(\).*?\{[\s]*\n/,
/function Layout\(\).*?\{[\s]*\n/,
/export default function.*?\{[\s]*\n/,
];
for (const pattern of funcPatterns) {
const match = content.match(pattern);
if (match) {
const insertPos = content.indexOf(match[0]) + match[0].length;
const hideEffect = ` useEffect(() => {
BootSplash.hide({ fade: true });
}, []);\n\n`;
content =
content.slice(0, insertPos) + hideEffect + content.slice(insertPos);
break;
}
}
}
fs.writeFileSync(layoutPath, content);
log.info('Added BootSplash.hide() to app/_layout.tsx');
}
module.exports = {
setupBootsplash,
};