-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
91 lines (91 loc) · 3.12 KB
/
test.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bua = require("./index");
const PATH = require("path");
const fs = require("fs");
const pack = new bua.Pack(), extract = new bua.Extract();
LetsPack('node_modules', (err) => {
if (err)
return console.log(err);
pack.finalize();
});
pack.output.pipe(fs.createWriteStream('node_modules.bua')).on('finish', () => {
console.log('packed', PATH.resolve('node_modules.bua'));
LetsExtract();
fs.createReadStream('node_modules.bua').pipe(extract.input).on('finish', () => {
console.log('extracted', PATH.resolve('node_modules_bua'));
});
});
function LetsExtract() {
extract.entry((header, stream, next) => {
const time = new Date(header.mtime);
header.name = header.name.replace(/^node_modules/, 'node_modules_bua');
if (header.type === 'file')
stream.pipe(fs.createWriteStream(header.name, { mode: header.mode || 0o644 })).on('finish', () => fs.utimes(header.name, 0, time, (err) => {
if (err)
return next(err);
next();
}));
else
(function dirHandler() {
let dirMked = false, streamDrained = false;
stream.skip(() => {
if (dirMked)
return next();
streamDrained = true;
});
fs.mkdir(header.name, { recursive: true, mode: header.mode || 0o755 }, (err) => {
if (err)
return next(err);
fs.utimes(header.name, 0, time, (err) => {
if (err)
return next(err);
if (streamDrained)
return next();
dirMked = true;
});
});
})();
});
}
function LetsPack(path, cb) {
fs.stat(path, (err, stats) => {
if (err)
return cb(err);
if (stats.isDirectory()) {
pack.writeHeader({
name: path,
size: 0,
mtime: Math.floor(stats.mtimeMs),
mode: stats.mode,
type: 'directory'
});
fs.readdir(path, (err, files) => {
if (err)
return cb(err);
const l = files.length;
(function next(i = 0) {
if (i === l)
return cb(null);
LetsPack(PATH.join(path, files[i]), (err) => {
if (err)
return cb(err);
next(i + 1);
});
})();
});
}
else if (stats.isFile())
fs.createReadStream(path)
.pipe(pack.entry({
name: path,
size: stats.size,
mtime: Math.floor(stats.mtimeMs),
mode: stats.mode,
type: 'file'
}, cb));
else
cb(null);
});
}
//# sourceMappingURL=test.js.map