Skip to content

Commit d5255e2

Browse files
Add a URL replacer
1 parent d7fb60f commit d5255e2

1 file changed

Lines changed: 77 additions & 34 deletions

File tree

addurl.js

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
const fs = require('fs');
32
const path = require('path');
43
const readline = require('readline');
@@ -39,54 +38,98 @@ function replaceInFile(filePath, oldString, newUrl) {
3938

4039
const regex = new RegExp(oldString.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g');
4140
content = content.replace(regex, newUrl);
41+
42+
if (content !== originalContent) {
43+
fs.writeFileSync(filePath, content, 'utf8');
44+
}
4245
} catch (error) {
4346
console.error(`Error processing file ${filePath}: ${error.message}`);
4447
}
4548
}
4649

4750

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-
}
51+
function isValidUrl(url) {
52+
const protocolRegex = /^(http|https):\/\//;
53+
const trailingSlashRegex = /\/$/;
54+
const domainRegex = /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
5755

58-
console.log(`\nStarting replacement process for files in: ${sourceDirectory}`);
59-
console.log(`Replacing '_[[URL]]_' with: '${userUrl}'\n`);
56+
if (!protocolRegex.test(url)) {
57+
console.error('Error: URL must begin with http:// or https://');
58+
return false;
59+
}
6060

61-
try {
61+
if (trailingSlashRegex.test(url)) {
62+
console.error('Error: URL must not have a trailing slash.');
63+
return false;
64+
}
6265

63-
const allFiles = getAllFiles(sourceDirectory);
66+
const domain = url.split('//')[1];
67+
if (!domainRegex.test(domain)) {
68+
console.error('Error: URL must contain a proper domain format (e.g., domain.tld).');
69+
return false;
70+
}
71+
72+
return true;
73+
}
74+
75+
76+
function processFiles(userUrl) {
77+
if (!isValidUrl(userUrl)) {
78+
rl.close();
79+
return;
80+
}
81+
82+
try {
6483

84+
const allFiles = getAllFiles(sourceDirectory);
6585

66-
const targetFiles = allFiles.filter(file => {
67-
const ext = path.extname(file).toLowerCase();
68-
return ext === '.html' || ext === '.js';
86+
87+
const targetFiles = allFiles.filter(file => {
88+
const ext = path.extname(file).toLowerCase();
89+
return ext === '.html' || ext === '.js';
90+
});
91+
92+
if (targetFiles.length === 0) {
93+
console.log(`No .html or .js files found in '${sourceDirectory}'.`);
94+
} else {
95+
96+
targetFiles.forEach(file => {
97+
replaceInFile(file, '_[[URL]]_', userUrl);
6998
});
99+
console.log('URL added successfully.');
100+
}
101+
} catch (error) {
102+
console.error(`An error occurred: ${error.message}`);
103+
if (error.code === 'ENOENT') {
104+
console.error(`Please ensure the directory '${sourceDirectory}' exists.`);
105+
}
106+
} finally {
107+
rl.close();
108+
}
109+
}
70110

71-
if (targetFiles.length === 0) {
72-
console.log(`No .html or .js files found in '${sourceDirectory}'.`);
73-
} else {
74111

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.`);
112+
function main() {
113+
const args = process.argv.slice(2);
114+
const urlArgIndex = args.indexOf('--url');
115+
const greenColor = '\x1b[32m';
116+
const resetColor = '\x1b[0m';
117+
if (urlArgIndex !== -1 && args[urlArgIndex + 1]) {
118+
const userUrl = args[urlArgIndex + 1];
119+
processFiles(userUrl);
120+
} else {
121+
console.log(`URL must be exactly in this format: ${greenColor}https://www.sploder.net${resetColor}`);
122+
console.log('Make sure to include the protocol (http:// or https://) and do not include a trailing slash.\n');
123+
rl.question('Please enter the URL the launcher should open: ', (userUrl) => {
124+
if (!userUrl) {
125+
console.log('No URL provided. Exiting.');
126+
rl.close();
127+
return;
84128
}
85-
} finally {
86-
rl.close();
87-
}
88-
});
129+
processFiles(userUrl);
130+
});
131+
}
89132
}
90133

91134

92-
main();
135+
main();

0 commit comments

Comments
 (0)