Skip to content

Commit d7fb60f

Browse files
Initial URL replacer
1 parent 1f460d5 commit d7fb60f

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

addurl.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
const fs = require('fs');
3+
const path = require('path');
4+
const readline = require('readline');
5+
6+
7+
const rl = readline.createInterface({
8+
input: process.stdin,
9+
output: process.stdout
10+
});
11+
12+
13+
14+
const sourceDirectory = path.join(__dirname, 'src');
15+
16+
17+
function getAllFiles(dir, fileList = []) {
18+
const files = fs.readdirSync(dir);
19+
20+
files.forEach(file => {
21+
const filePath = path.join(dir, file);
22+
if (fs.statSync(filePath).isDirectory()) {
23+
24+
getAllFiles(filePath, fileList);
25+
} else {
26+
27+
fileList.push(filePath);
28+
}
29+
});
30+
return fileList;
31+
}
32+
33+
34+
function replaceInFile(filePath, oldString, newUrl) {
35+
try {
36+
let content = fs.readFileSync(filePath, 'utf8');
37+
const originalContent = content;
38+
39+
40+
const regex = new RegExp(oldString.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g');
41+
content = content.replace(regex, newUrl);
42+
} catch (error) {
43+
console.error(`Error processing file ${filePath}: ${error.message}`);
44+
}
45+
}
46+
47+
48+
function main() {
49+
console.log('URL must be exactly in this format: https://www.sploder.net');
50+
console.log('Make sure to include the protocol (http:// or https://) and do not include a trailing slash.\n');
51+
rl.question('Please enter the URL the launcher should open: ', (userUrl) => {
52+
if (!userUrl) {
53+
console.log('No URL provided. Exiting.');
54+
rl.close();
55+
return;
56+
}
57+
58+
console.log(`\nStarting replacement process for files in: ${sourceDirectory}`);
59+
console.log(`Replacing '_[[URL]]_' with: '${userUrl}'\n`);
60+
61+
try {
62+
63+
const allFiles = getAllFiles(sourceDirectory);
64+
65+
66+
const targetFiles = allFiles.filter(file => {
67+
const ext = path.extname(file).toLowerCase();
68+
return ext === '.html' || ext === '.js';
69+
});
70+
71+
if (targetFiles.length === 0) {
72+
console.log(`No .html or .js files found in '${sourceDirectory}'.`);
73+
} else {
74+
75+
targetFiles.forEach(file => {
76+
replaceInFile(file, '_[[URL]]_', userUrl);
77+
});
78+
console.log('\nReplacement process completed.');
79+
}
80+
} catch (error) {
81+
console.error(`An error occurred: ${error.message}`);
82+
if (error.code === 'ENOENT') {
83+
console.error(`Please ensure the directory '${sourceDirectory}' exists.`);
84+
}
85+
} finally {
86+
rl.close();
87+
}
88+
});
89+
}
90+
91+
92+
main();

0 commit comments

Comments
 (0)