-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuilder.js
More file actions
157 lines (135 loc) · 4.38 KB
/
builder.js
File metadata and controls
157 lines (135 loc) · 4.38 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
/**
* Custom builder
* 1- Starting from main.js, reads all placeholders recursively to inject the .js files
* 2- Removes all comment lines after // ==/UserScript==
* 3- Removes all console.log lines
* 4- <<temporary>> Removes all shared.devlog lines
*/
let arguments = process.argv;
const defaults = {
removeComments: false,
removeLogs: false,
removeSharedDevLog: false
};
if (arguments.includes('--no-console-log')) {
defaults.removeLogs = true;
}
if (arguments.includes('--no-faucet-log')) {
defaults.removeSharedDevLog = true;
}
if (arguments.includes('--no-comments')) {
defaults.removeComments = true;
}
const fs = require('fs');
const workDir = 'src/';
const mainFile = 'index.js';
/**
* removeTrailingLines: returns the string without empty lines at the end
*/
String.prototype.removeTrailingLines = function() {
let str = this;
while(str.endsWith('\n')) {
str = str.slice(0, -1);
}
return str;
};
/**
* addIndentation: adds whitespaces
*/
String.prototype.addIndentation = function(spaces = 0) {
let str = this;
let strLines = str.split('\n').map(line => `${' '.repeat(spaces)}${line}`);
return strLines.join('\n');
};
/**
* replacePlaceholders: calls mixer for each {{file}} placeholder
*/
String.prototype.replacePlaceholders = function() {
let str = this;
const phRegex = /([ \t]*){{([^}]+)}}/g;
return str.replace(phRegex, (_, indentation, fName) => mixer(fName.trim(), indentation.length));
};
String.prototype.replaceHtmlPlaceholders = function() {
console.log('@replaceHtmlPlaceholders');
let str = this;
const phRegex = /([ \t]*)\/\*\*([^*]+)\*\*\//g;
console.log(phRegex);
return str.replace(phRegex, (_, indentation, fName) => htmlReader(fName.trim(), indentation.length));
};
/**
* mixer: Reads the content of file, trims it,
* removes empty lines at the end and
* replaces {{file}} placeholdes recursively
*/
const mixer = (file, indentation) => fs.readFileSync(workDir + file, 'utf-8').trim().removeTrailingLines().addIndentation(indentation).replacePlaceholders();
// const htmlReader = (file, indentation) => `\`${fs.readFileSync(workDir + 'ui/html/' + file, 'utf-8').trim().removeTrailingLines().addIndentation(indentation)}\``;
const htmlReader = (file, indentation) => `${fs.readFileSync(workDir + 'ui/html/' + file, 'utf-8').trim().removeTrailingLines().addIndentation(indentation)}`;
let contents = mixer(mainFile);
contents = contents.replaceHtmlPlaceholders();
let filteredLines = contents.split('\n');
if (defaults.removeComments) {
// Remove all commented lines after '==/UserScript=='
const endMetaLineNumber = filteredLines.findIndex(x => x.includes('==/UserScript=='));
if (endMetaLineNumber < 0) {
console.error('Metadata end line not found');
return;
}
filteredLines = filteredLines.filter((line, index) => {
if (index <= endMetaLineNumber) {
return true;
}
let tempLine = line.trim();
if (tempLine.startsWith('//')) {
return false;
} else {
return true;
}
});
}
if (defaults.removeLogs) {
// Remove all lines that contain console.log
filteredLines = filteredLines.filter((line) => {
let tempLine = line.trim();
if (tempLine.startsWith('console.log') && tempLine.includes(');')) {
return false;
} else {
return true;
}
});
}
if (defaults.removeSharedDevLog) {
// Remove all lines that contain shared.devlog
filteredLines = filteredLines.filter((line, index) => {
let tempLine = line.trim();
if ((tempLine.startsWith('shared.devlog') || tempLine.startsWith('shared.addError')) && tempLine.includes(');')) {
return false;
} else {
return true;
}
});
}
// Remove whitespaces if line is empty
filteredLines = filteredLines.map((line) => {
let tempLine = line.trim();
if (tempLine == '') {
return tempLine;
} else {
return line;
}
});
// Remove double empty lines
filteredLines = filteredLines.filter((line, idx) => {
if (idx == 0) {
return true;
}
let previous = filteredLines[idx - 1];
if (previous == '' && line == '') {
return false;
}
return true;
});
// Join the modified lines back into a single string
const modifiedContents =filteredLines.join('\n');
// Write the modified contents to /dist
fs.writeFileSync('dist/autoclaim-dist.user.js', modifiedContents);
// fs.writeFileSync('dist/autoclaim-v3-beta.user.js', modifiedContents);