This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
build.js
208 lines (194 loc) · 6.09 KB
/
build.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const fs = require('fs');
const http = require('http');
const https = require('https');
const fileCache = new Map();
function readFile (path) {
if (fileCache.has(path)) return fileCache.get(path);
return new Promise((resolve, reject) => {
if (path.startsWith('http')) {
let data = '';
const callback = res => {
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
data = data.toString();
fileCache.set(path, data);
if (res.complete) resolve(data);
else reject(data);
});
};
if (path.startsWith('https')) https.get(path, callback);
else http.get(path, callback);
} else {
try {
const data = fs.readFileSync('./src/' + path).toString();
fileCache.set(path, data);
resolve(data);
} catch (e) {
reject(e);
}
}
});
}
function resolvePath (path, dir) {
if (path.startsWith('http://') || path.startsWith('https://') || path.startsWith('file://')) return path;
if (path.startsWith('/')) path = dir + path;
else path = dir + '/' + path;
return new URL(path).toString();
}
function prettyMeta (metas) {
const list = [];
let maxLen = 0;
for (const line of metas) {
const its = line.split(' ');
if (its.length < 2) continue;
list.push(its);
maxLen = Math.max(maxLen, its[1].length + 1);
}
const ret = [];
for (const its of list) {
let s = its[0] + ' ' + its[1];
if (its.length > 2) {
s += ' '.repeat(maxLen - its[1].length);
for (let i = 2; i < its.length; i++) {
s += its[i];
if (i + 1 === its.length) break;
s += ' ';
}
}
ret.push(s);
}
return ret;
}
function wrap (metas, notMeta) {
return '// ==UserScript==\n' + prettyMeta(metas).join('\n') + '\n// ==/UserScript==\n\n' + notMeta;
}
const multipleKeySet = new Set(['@match', '@exclude-match', '@include', '@exclude', '@require', '@resource', '@connect', '@grant', '@compatible', '@incompatible']);
function mergeMeta (metasArr) {
const keyMap = new Map();
for (let i = metasArr.length - 1; i >= 0; i--) {
for (const line of metasArr[i]) {
const its = line.split(' ');
if (its.length < 2) continue;
const tag = its[1];
if (multipleKeySet.has(tag)) {
if (!keyMap.has(tag)) keyMap.set(tag, new Set());
keyMap.get(tag).add(line);
} else if (!keyMap.has(tag)) {
keyMap.set(tag, line);
}
}
}
const ret = [];
for (const v of keyMap.values()) {
if (v instanceof Set) {
for (const it of v) {
ret.push(it);
}
} else {
ret.push(v);
}
}
return ret;
}
const userScriptRegExp = /\/\/\s*==UserScript==\s*([\s\S]*?)\/\/\s*==\/UserScript==/;
const processingMap = new Map();
async function processMeta (path, replaceUrlFn, onlyMeta = false) {
console.log('processMeta', path, onlyMeta);
if (!processingMap.has(replaceUrlFn)) processingMap.set(replaceUrlFn, new Set());
const pathSet = processingMap.get(replaceUrlFn);
if (pathSet.has(path)) return [[], ''];
pathSet.add(path);
const data = await readFile(replaceUrlFn instanceof Function ? replaceUrlFn(path, false) : path);
const r = userScriptRegExp.exec(data);
if (!r) return [[], data];
const notMeta = data.slice(r.index + r[0].length);
const meta = r[1].replace(/[\r\n]+/g, '\n').replace(/ +/g, ' ');
const overrideMetas = [];
const metas = [];
const notMetas = [];
for (let line of meta.split('\n')) {
const its = line.split(' ');
if (its.length < 2) continue;
const tag = its[1];
switch (tag) {
case '@require':
{
if (its.length < 3) continue;
const url = its[2];
const m = await processMeta(url, replaceUrlFn, onlyMeta);
overrideMetas.push(m[0]);
if (onlyMeta) {
if (replaceUrlFn instanceof Function) {
line = line.replace(url, replaceUrlFn(url));
}
} else {
notMetas.push(m[1]);
continue;
}
break;
}
case '@resource':
{
if (its.length < 4) continue;
const url = its[3];
if (replaceUrlFn instanceof Function) {
line = line.replace(url, replaceUrlFn(url));
}
break;
}
default:
if (!tag.endsWith('URL')) break;
// eslint-disable-line no-fallthrough
case '@homepage':
case '@website':
case '@source':
case '@icon':
case '@defaulticon':
case '@icon64':
{
if (its.length < 3) continue;
const url = its[2];
if (replaceUrlFn instanceof Function) {
line = line.replace(url, replaceUrlFn(url));
}
break;
}
}
if (line) metas.push(line);
}
overrideMetas.push(metas);
notMetas.push(notMeta);
pathSet.delete(path);
return [mergeMeta(overrideMetas), notMetas.join('\n')];
}
function createReplaceFn (dir, name) {
return (url, res = true) => {
url = url.replace('{replace}', name);
if (res) return resolvePath(url, dir);
return url;
};
}
function copy (src, dst) {
console.log('Copy from', src, 'to', dst);
if (fs.statSync(src).isDirectory()) {
if (!fs.existsSync(dst))fs.mkdirSync(dst);
fs.readdirSync(src).forEach(path => copy(src + '/' + path, dst + '/' + path));
} else {
fs.copyFileSync(src, dst);
}
}
(async function () {
copy('./src', './dist');
const replaceGithub = createReplaceFn('https://raw.githubusercontent.com/SeaLoong/BLRHH/dist', 'github');
let dist = await processMeta('./meta.js', replaceGithub);
fs.writeFileSync('./dist/installer.github.user.js', wrap(dist[0], dist[1]));
dist = await processMeta('./meta.js', replaceGithub, true);
fs.writeFileSync('./dist/meta.github.js', wrap(dist[0], dist[1]));
const replaceJsdelivr = createReplaceFn('https://cdn.jsdelivr.net/gh/SeaLoong/BLRHH@dist', 'jsdelivr');
dist = await processMeta('./meta.js', replaceJsdelivr);
fs.writeFileSync('./dist/installer.jsdelivr.user.js', wrap(dist[0], dist[1]));
dist = await processMeta('./meta.js', replaceJsdelivr, true);
fs.writeFileSync('./dist/meta.jsdelivr.js', wrap(dist[0], dist[1]));
})();