Repository navigation
Expand file tree
/
Copy pathwuLib.js
More file actions
226 lines (196 loc) · 6.49 KB
/
Copy pathwuLib.js
File metadata and controls
226 lines (196 loc) · 6.49 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
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) {
const aliases = new Map([
["--output-only", "o"],
["--keep-intermediate", "d"],
["--fast", "f"]
]);
const args = process.argv.slice(2);
if (args.includes("--help") || args.includes("-h")) {
console.log("Command Line Helper:\n\n" + helper);
return;
}
if (args.includes("--version") || args.includes("-v")) {
console.log(require("./package.json").version);
return;
}
const orders = [];
const inputs = [];
for (const arg of args) {
if (aliases.has(arg)) orders.push(aliases.get(arg));
else if (arg.startsWith("--main-dir=")) orders.push("s=" + arg.slice("--main-dir=".length));
else if (arg.startsWith("--")) throw new Error(`Unknown option: ${arg}`);
else if (arg.startsWith("-")) orders.push(arg.slice(1));
else inputs.push(arg);
}
if (inputs.length === 0) {
console.log("Command Line Helper:\n\n" + helper);
return;
}
console.time("Total use");
function endTime() {
ioEvent.check(() => console.timeEnd("Total use"));
}
let current = 0, faster = orders.includes("f"), fastCnt;
if (faster) {
fastCnt = new CntEvent;
fastCnt.add(endTime);
}
function doNext() {
if (current >= inputs.length) {
if (!faster) endTime();
} else {
const input = inputs[current++];
if (faster) fastCnt.encount(), cb(input, fastCnt.decount, orders), doNext();
else cb(input, doNext, orders);
}
}
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
};