-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildHeader.js
More file actions
92 lines (86 loc) · 3.03 KB
/
buildHeader.js
File metadata and controls
92 lines (86 loc) · 3.03 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
const fs = require('fs');
let htmlFiles = ["ballblast.html", "dash.html", "editor.html", "index.html", "map.html"];
let jsFiles = ["serviceWorker.js", "dev/engine/editor/contentBrowser.ts"];
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What is the file directory? ', (fileDirectory) => {
// Trim to remove extra whitespace and newlines
fileDirectory = fileDirectory.trim();
let arr = fileDirectory.split('/');
arr.reverse()
htmlFilesReplace(arr);
jsFilesReplace(arr)
// Close the readline interface
rl.close();
});
function htmlFilesReplace(arr) {
let filesProcessed = 0;
for (let html of htmlFiles) {
fs.readFile('docs/' + html, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
let found = null;
for (let i of arr) {
if (data.includes(i)) {
found = i;
break;
}
}
if (!found) {
throw new Error("file cant be added, directory doesn't exist")
}
let regex = new RegExp(found + '.*?script>', 'gm')
let array = [...data.matchAll(regex)];
let modifiedData = data.replace(array[array.length - 1][0], array[array.length - 1][0] + `\n\t<script src="${arr.reverse().join('/')}"></script>`);
arr.reverse()
// Write the modified content back to the file
fs.writeFile('docs/' + html, modifiedData, 'utf8', (err) => {
if (err) {
console.error(err);
return;
}
filesProcessed++;
if (filesProcessed === htmlFiles.length) {
console.log("All files processed and modified successfully.");
}
});
});
}
}
function jsFilesReplace(arr) {
let filesProcessed = 0;
for (let js of jsFiles) {
fs.readFile(js, 'utf8', (err, data) => {
let found = null;
for (let i of arr) {
if (data.includes(i)) {
found = i;
break;
}
}
if (!found) {
throw new Error("file cant be added, directory doesn't exist")
}
let regex = new RegExp(found + `.*?,`, 'm')
let array = [...data.match(regex)];
let subst = `${array[0]}\n\t\t"${arr.reverse().join('/')}",`;
arr.reverse()
modifiedData = data.replace(regex, subst);
fs.writeFile(js, modifiedData, 'utf8', (err) => {
if (err) {
console.error(err);
return;
}
filesProcessed++;
if (filesProcessed === jsFiles.length) {
console.log("All files processed and modified successfully.");
}
});
});
}
}