-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
163 lines (132 loc) · 3.11 KB
/
index.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
'use strict';
const fs = require('fs');
const fsu = require('fsu');
const path = require('path');
const Buffer = require('buffer').Buffer;
const Stream = require('stream').Stream;
const Transform = require('stream').Transform;
const isArray = Array.isArray;
const File = function(file) {
if (!file) {
file = {};
}
this.id = file.id;
this.dst(file.path || file.dst())
.setContent(file.content || null);
this.src = {
path: this._dst.path,
basename: this._dst.basename,
dirname: this._dst.dirname
};
this._dependencies = file._dependencies || new Set();
this._error = file._error;
};
File.prototype.isBuffer = function() {
return typeof this.content === 'object' && this.content instanceof Buffer;
};
File.prototype.isStream = function() {
return typeof this.content === 'object' && this.content instanceof Stream;
};
File.prototype.isNull = function() {
return this.content === null;
};
File.prototype.pipe = function(stream, options) {
if (!options) {
options = {};
}
let end = options.end;
if (this.isStream()) {
return this.content.pipe(stream, { end: end });
}
//buffer
if (this.isBuffer()) {
if (end) {
stream.end(this.content);
} else {
stream.write(this.content);
}
return stream;
}
//null
return this.read().pipe(stream, options);
};
File.prototype.read = function() {
if (this.isNull()) {
this.content = fs.createReadStream(this.src.path);
this._error && this.content.on('error', this._error);
}
return this;
};
File.prototype.write = function(options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
if (!options) {
options = {};
}
if (!options.force) {
options.force = true;
}
const save = fsu.createWriteStreamUnique(this._dst.path, options);
this._error && save.on('error', this._error);
cb && save.on('finish', cb);
return this.pipe(save, { end: true });
};
File.prototype.getContent = function(cb) {
if (this.isBuffer()) {
cb(this.content);
return this;
}
let buffer = [];
let reader = new Transform({
transform: (chunk, encoding, next) => {
buffer.push(chunk);
next();
},
flush: done => {
cb(this.setContent(buffer.join('')).content);
done();
}
});
this.pipe(reader);
return this;
};
File.prototype.setContent = function(content) {
if (typeof content === 'string') {
content = Buffer.from(content);
}
this.content = content || null;
return this;
};
File.prototype.dst = function(dst) {
if (dst) {
this._dst = {
path: dst,
basename: path.basename(dst),
dirname: path.dirname(dst)
};
return this;
}
return this._dst;
};
File.prototype.dependencies = function(deps) {
if (deps) {
if (isArray(deps)) {
this._dependencies.add.apply(this._dependencies, deps);
} else {
this._dependencies.add(deps);
}
return this;
}
return this._dependencies;
};
File.prototype.clear = function() {
this.dependencies.clear();
return this;
};
File.prototype.error = function(cb) {
this._error = cb;
return this;
};
module.exports = File;