forked from lulzsun/blitz-app-adblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.js
115 lines (92 loc) · 3.14 KB
/
io.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
const fs = require('fs');
const http = require('http');
const https = require('https');
const path = require('path');
async function downloadFile(url, filePath) {
const proto = !url.charAt(4).localeCompare('s') ? https : http;
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(filePath);
let fileInfo = null;
const request = proto.get(url, response => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to get '${url}' (${response.statusCode})`));
return;
}
fileInfo = {
mime: response.headers['content-type'],
size: parseInt(response.headers['content-length'], 10),
};
response.pipe(file);
});
// The destination stream is ended by the time it's called
file.on('finish', () => resolve(fileInfo));
request.on('error', err => {
fs.unlink(filePath, () => reject(err));
});
file.on('error', err => {
fs.unlink(filePath, () => reject(err));
});
request.end();
});
}
function modifyFileAfterContext (data, filePath, context) {
const file = fs.readFileSync(filePath).toString().split('\n')
let line = -1
for (let i = 0; i < file.length; i++) {
const lineString = file[i]
if (context.trim() === lineString.trim()) {
line = i
break
}
}
if (line === -1) {
throw new Error('Current Blitz version doesn\'t contain the given injection context.')
}
if (file[line + 1] !== data) {
file.splice(line + 1, 0, data)
const text = file.join('\n')
fs.writeFileSync(filePath, text)
console.log(`${filePath} => Writing to line ${line + 2}: ${data}`)
}
}
function modifyFileAtLine(data, filePath, line, compare=-1) {
var file = fs.readFileSync(filePath).toString().split("\n");
if(compare != -1) {
if(!(data === file[line-1]) && !(compare === file[line-1])) {
throw new Error(
`Current Blitz version caused patch comparison check to fail. Look for a new patcher release or create a new issue on Github!
\n\n
Finding:\n
\t'${file[line-1]}'\n
Instead of:
\t'${compare}'`);
}
}
file.splice(line-1, 1, data);
var text = file.join('\n');
fs.writeFileSync(filePath, text);
console.log(`${filePath} => Writing to line ${line}: ${data}`);
}
function copyFile(src, dest) {
fs.copyFileSync(src, dest);
}
function deleteFolder(dir_path) {
if (fs.existsSync(dir_path)) {
fs.readdirSync(dir_path).forEach(function(entry) {
var entry_path = path.join(dir_path, entry);
if (fs.lstatSync(entry_path).isDirectory()) {
deleteFolder(entry_path);
} else {
fs.unlinkSync(entry_path);
}
});
fs.rmdirSync(dir_path);
}
}
module.exports = {
downloadFile: downloadFile,
modifyFileAfterContext: modifyFileAfterContext,
modifyFileAtLine: modifyFileAtLine,
copyFile: copyFile,
deleteFolder: deleteFolder,
};