forked from system-cpu/wxappUnpacker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwuLib.js
More file actions
267 lines (235 loc) · 8.06 KB
/
Copy pathwuLib.js
File metadata and controls
267 lines (235 loc) · 8.06 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const fs = require("fs");
const path = require("path");
const os = require('os');
const beautify = require('js-beautify').html;
let platform = os.platform();
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, (err) => {
if (err && err.code !== 'EEXIST') {
console.error(`创建目录失败: ${dir}`, err);
cb(err);
} else {
cb(null);
}
});
});
} else if (stats.isFile()) {
const error = new Error(`${dir} 是一个文件,无法在其中创建文件`);
console.error(error.message);
cb(error);
} else {
cb(null);
}
});
}
function save(name, content) {
return new Promise((resolve, reject) => {
mkdirs(path.dirname(name), function (err) {
if (err) return reject(err);
// 对HTML文件进行格式化
if (name.endsWith('.html') || name.endsWith('.wxml')) {
try {
// 确保content是字符串
let htmlContent = content;
if (Buffer.isBuffer(content)) {
htmlContent = content.toString('utf8');
}
// 检查内容是否为有效的字符串
if (typeof htmlContent === 'string' && htmlContent.trim()) {
content = beautify(htmlContent, {
indent_size: 2,
wrap_line_length: 80,
preserve_newlines: true,
max_preserve_newlines: 2,
unformatted: ['code', 'pre', 'em', 'strong', 'span'],
extra_liners: ['head', 'body', '/html']
});
} else {
console.warn(`跳过格式化: ${name} (内容为空或无效)`);
}
} catch (e) {
console.error(`格式化HTML文件失败: ${name}`, e);
// 发生错误时使用原始内容
if (Buffer.isBuffer(content)) {
content = content.toString('utf8');
}
}
}
// 确保写入的内容是Buffer或字符串
if (typeof content !== 'string' && !Buffer.isBuffer(content)) {
content = String(content);
}
fs.writeFile(name, content, function (err) {
if (err) return reject(err);
resolve();
});
});
});
}
function get(name, cb) {
if (!cb || typeof cb !== 'function') {
cb = (data) => { }; // 提供默认的回调函数
}
fs.readFile(name, function (err, data) {
if (err) {
console.error(`读取文件 ${name} 失败:`, err);
cb(null);
}
else cb(data);
});
}
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
};