forked from larack8/wxappUnpacker
-
Notifications
You must be signed in to change notification settings - Fork 5
/
wuLib.js
204 lines (176 loc) · 5.96 KB
/
wuLib.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
const fs = require("fs");
const path = require("path");
class CntEvent {
constructor() {
this.cnt = 0;
this.emptyEvent = [];
this.encount = this.encount.bind(this);
this.decount = this.decount.bind(this);
this.add = this.add.bind(this);
}
encount(delta = 1) {
this.cnt += delta;
}
decount() {
if (this.cnt > 0) --this.cnt;
if (this.cnt == 0) {
for (let info of this.emptyEvent) info[0](...info[1]);
this.emptyEvent = [];
}
}
add(cb, ...attach) {
this.emptyEvent.push([cb, attach]);
}
check(cb, ...attach) {
if (this.cnt == 0) cb(...attach);
else this.add(cb, ...attach);
}
}
class LimitedRunner {
constructor(limit) {
this.limit = limit;
this.cnt = 0;
this.funcs = [];
}
run(func) {
if (this.cnt < this.limit) {
this.cnt++;
setTimeout(func, 0);
} else {
this.funcs.push(func);
}
}
done() {
if (this.cnt > 0) this.cnt--;
if (this.funcs.length > 0) {
this.cnt++;
setTimeout(this.funcs.shift(), 0);
}
}
runWithCb(func, ...args) {
let cb = args.pop(), self = this;
function agent(...args) {
self.done();
return cb.apply(this, args);
}
args.push(agent);
this.run(() => func(...args));
}
}
let ioEvent = new CntEvent;
let ioLimit = new LimitedRunner(4096);
function mkdirs(dir, cb) {
ioLimit.runWithCb(fs.stat.bind(fs), dir, (err, stats) => {
if (err) mkdirs(path.dirname(dir), () => fs.mkdir(dir, cb));
else if (stats.isFile()) throw Error(dir + " was created as a file, so we cannot put file into it.");
else cb();
});
}
function save(name, content) {
ioEvent.encount();
mkdirs(path.dirname(name), () => ioLimit.runWithCb(fs.writeFile.bind(fs), name, content, err => {
if (err) throw Error("Save file error: " + err);
ioEvent.decount();
}));
}
function get(name, cb, opt = {encoding: 'utf8'}) {
ioEvent.encount();
ioLimit.runWithCb(fs.readFile.bind(fs), name, opt, (err, data) => {
if (err) throw Error("Read file error: " + err);
else cb(data);
ioEvent.decount();
});
}
function del(name) {
ioEvent.encount();
ioLimit.runWithCb(fs.unlink.bind(fs), name, ioEvent.decount);
}
function changeExt(name, ext = "") {
return name.slice(0, name.lastIndexOf(".")) + ext;
}
function scanDirByExt(dir, ext, cb) {
let result = [], scanEvent = new CntEvent;
function helper(dir) {
scanEvent.encount();
ioLimit.runWithCb(fs.readdir.bind(fs), dir, (err, files) => {
if (err) throw Error("Scan dir error: " + err);
for (let file of files) {
scanEvent.encount();
let name = path.resolve(dir, file);
fs.stat(name, (err, stats) => {
if (err) throw Error("Scan dir error: " + err);
if (stats.isDirectory()) helper(name);
else if (stats.isFile() && name.endsWith(ext)) result.push(name);
scanEvent.decount();
});
}
scanEvent.decount();
});
}
scanEvent.add(cb, result);
helper(dir, ext, scanEvent);
}
function toDir(to, from) {//get relative path without posix/win32 problem
if (from[0] == ".") from = from.slice(1);
if (to[0] == ".") to = to.slice(1);
from = from.replace(/\\/g, '/');
to = to.replace(/\\/g, '/');
let a = Math.min(to.length, from.length);
for (let i = 1, m = Math.min(to.length, from.length); i <= m; i++) if (!to.startsWith(from.slice(0, i))) {
a = i - 1;
break;
}
let pub = from.slice(0, a);
let len = pub.lastIndexOf("/") + 1;
let k = from.slice(len);
let ret = "";
for (let i = 0; i < k.length; i++) if (k[i] == '/') ret += '../';
return ret + to.slice(len);
}
function commonDir(pathA, pathB) {
if (pathA[0] == ".") pathA = pathA.slice(1);
if (pathB[0] == ".") pathB = pathB.slice(1);
pathA = pathA.replace(/\\/g, '/');
pathB = pathB.replace(/\\/g, '/');
let a = Math.min(pathA.length, pathB.length);
for (let i = 1, m = Math.min(pathA.length, pathB.length); i <= m; i++) if (!pathA.startsWith(pathB.slice(0, i))) {
a = i - 1;
break;
}
let pub = pathB.slice(0, a);
let len = pub.lastIndexOf("/") + 1;
return pathA.slice(0, len);
}
function commandExecute(cb, helper) {
console.time("Total use");
function endTime() {
ioEvent.check(() => console.timeEnd("Total use"));
}
let orders = [];
for (let order of process.argv) if (order.startsWith("-")) orders.push(order.slice(1));
let iter = process.argv[Symbol.iterator](), nxt = iter.next(), called = false, faster = orders.includes("f"),
fastCnt;
if (faster) {
fastCnt = new CntEvent;
fastCnt.add(endTime);
}
function doNext() {
let nxt = iter.next();
while (!nxt.done && nxt.value.startsWith("-")) nxt = iter.next();
if (nxt.done) {
if (!called) console.log("Command Line Helper:\n\n" + helper);
else if (!faster) endTime();
} else {
called = true;
if (faster) fastCnt.encount(), cb(nxt.value, fastCnt.decount, orders), doNext();
else cb(nxt.value, doNext, orders);
}
}
while (!nxt.done && !nxt.value.endsWith(".js")) nxt = iter.next();
doNext();
}
module.exports = {
mkdirs: mkdirs, get: get, save: save, toDir: toDir, del: del, addIO: ioEvent.add,
changeExt: changeExt, CntEvent: CntEvent, scanDirByExt: scanDirByExt, commonDir: commonDir,
commandExecute: commandExecute
};