Skip to content

Commit 6bbbf03

Browse files
committed
Create preload.js
1 parent f3ecc93 commit 6bbbf03

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

preload.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const directoryPath = path.join(__dirname, 'public/src');
5+
6+
// Function to search for all .js files in the directory and its subdirectories
7+
const getJsFiles = (dir) => {
8+
let jsFiles = [];
9+
const files = fs.readdirSync(dir);
10+
11+
files.forEach((file) => {
12+
const fullPath = path.join(dir, file);
13+
const stat = fs.statSync(fullPath);
14+
15+
if (stat.isDirectory()) {
16+
jsFiles = jsFiles.concat(getJsFiles(fullPath));
17+
}
18+
else if (file.endsWith('.js')) {
19+
jsFiles.push(fullPath);
20+
}
21+
});
22+
23+
return jsFiles;
24+
};
25+
26+
// Function to modify files
27+
const modifyFile = (filePath) => {
28+
let fileContent = fs.readFileSync(filePath, 'utf8');
29+
30+
const preloadPattern = /preload\s*\(\s*\)\s*\n\s*{/g;
31+
32+
// Check if file contains 'preload () {'
33+
if (preloadPattern.test(fileContent)) {
34+
console.log(`Modifying file: ${filePath}`);
35+
36+
// Replace 'preload () {' with 'preload () {\n this.load.setBaseURL('https://cdn.phaserfiles.com/v385');'
37+
fileContent = fileContent.replace(preloadPattern, match => `${match}\n this.load.setBaseURL('https://cdn.phaserfiles.com/v385');`);
38+
39+
// Write the modified content back to the file
40+
fs.writeFileSync(filePath, fileContent, 'utf8');
41+
}
42+
};
43+
44+
// Main function to execute the script
45+
const main = () => {
46+
const jsFiles = getJsFiles(directoryPath);
47+
48+
jsFiles.forEach((file) => {
49+
modifyFile(file);
50+
});
51+
52+
console.log('Script has completed.');
53+
};
54+
55+
main();

0 commit comments

Comments
 (0)