This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
nodefs.js
148 lines (121 loc) · 3.69 KB
/
nodefs.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
const PATH = require("path");
const Stream = require("stream").Stream;
module.exports = function(vfs, base) {
var resolvePath = base
? function(path) {
if (path.substring(0, base.length) === base) {
return path;
}
return PATH.join(base, path);
}
: function(path) { return path; };
function readFile(path, encoding, callback) {
if (!callback) {
callback = encoding;
encoding = null;
}
var options = {};
if (encoding)
options.encoding = encoding;
vfs.readfile(resolvePath(path), options, function(err, meta) {
if (err)
return callback(err);
var data = "";
meta.stream.on("data", function(d) {
data += d;
})
var done;
meta.stream.on("error", function(e) {
if (done) return;
done = true;
callback(e);
});
meta.stream.on("end", function() {
if (done) return;
done = true;
callback(null, data);
});
});
}
function writeFile(path, data, encoding, callback) {
if (!callback) {
callback = encoding;
encoding = null;
}
var options = {};
if (encoding)
options.encoding = encoding;
var stream = options.stream = new Stream();
stream.readable = true;
vfs.mkfile(resolvePath(path), options, function(err, meta) {
if (err)
return callback(err);
callback(null);
});
stream.emit("data", data);
stream.emit("end");
}
function readdir(path, callback) {
vfs.readdir(resolvePath(path), {encoding: null}, function(err, meta) {
if (err)
return callback(err);
var stream = meta.stream;
var files = [];
stream.on("data", function(stat) {
files.push(stat.name);
});
var called;
stream.on("error", function(err) {
if (called) return;
called = true;
callback(err);
});
stream.on("end", function() {
if (called) return;
called = true;
callback(null, files);
});
});
}
function exists(path, callback) {
vfs.stat(resolvePath(path), {}, function(err, stat) {
return callback(stat && !stat.err);
});
}
function stat(path, callback) {
vfs.stat(resolvePath(path), {}, callback);
}
function rename(from, to, callback) {
vfs.rename(resolvePath(to), {from: resolvePath(from)}, callback);
}
function mkdirP(path, mode, callback) {
if (!callback) {
callback = mode;
mode = null;
}
vfs.execFile("mkdir", {args: ["-p", resolvePath(path)]}, callback);
}
function mkdir(path, callback) {
vfs.execFile("mkdir", {args: [resolvePath(path)]}, callback);
}
function rmfile(path, callback) {
vfs.rmfile(resolvePath(path), {}, callback || function(){}); // shouldn't vfs handle callback == null?
}
function rmdir(path, options, callback) {
vfs.rmdir(resolvePath(path), options, callback || function(){});
}
return {
readFile: readFile,
writeFile: writeFile,
readdir: readdir,
exists: exists,
stat: stat,
rename: rename,
mkdirP: mkdirP,
mkdir: mkdir,
unlink: rmfile,
rmfile: rmfile,
rmdir: rmdir,
vfs: vfs
}
}