-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
274 lines (233 loc) · 7.23 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
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
268
269
270
271
272
273
274
'use strict';
const kBase = Symbol('base');
const kCwd = Symbol('cwd');
const kContents = Symbol('contents');
const kSymlink = Symbol('symlink');
const kDirent = Symbol('dirent');
const fs = require('fs');
const path = require('path');
const util = require('util');
const assert = require('assert');
const clone = require('clone-deep');
const {
builtinProperties,
handlers,
isBuffer,
isStream,
normalize,
pathError,
removeTrailingSep,
replaceExtname
} = require('./utils');
/**
* Create the Dirent class by optionally passing a clonable stream.
*/
const create = clonableStream => {
class Dirent extends fs.Dirent {
constructor(dirent = {}, type = '') {
super(null, type, '');
if (typeof dirent === 'string') {
dirent = { path: dirent };
}
this[kDirent] = dirent;
this.contents = dirent.contents || null;
this.stat = dirent.stat || null;
// replay history to get path normalization
const history = [].concat(dirent.history || []).concat(dirent.path || []);
this.history = [];
history.forEach(value => { this.path = value; });
this.cwd = dirent.cwd || process.cwd();
this.base = dirent.base;
for (const key of Object.keys(dirent)) {
if (!builtinProperties.has(key)) {
this[key] = dirent[key];
}
}
return new Proxy(this, handlers(Dirent, dirent));
}
isDirectory() {
if (!this.isNull()) return false;
if (typeof this[kDirent].isDirectory === 'function') {
return this[kDirent].isDirectory();
}
if (this.stat && typeof this.stat.isDirectory === 'function') {
return this.stat.isDirectory();
}
return super.isDirectory();
}
// gulp compat
isSymbolic() {
return this.isNull() && this.isSymbolicLink();
}
// alias for isSymbolicLink
isSymlink() {
return this.isSymbolicLink();
}
isBuffer() {
return isBuffer(this.contents);
}
isStream() {
return isStream(this.contents);
}
isNull() {
return this.contents === null;
}
inspect() {
return this[util.inspect.custom]();
}
clone(options) {
if (typeof options === 'boolean') options = { deep: options };
const opts = { deep: true, contents: true, ...options };
const { base, cwd } = this;
const history = this.history.slice();
const stat = this.stat ? Object.assign(new fs.Stats(), this.stat) : null;
const contents = (opts.contents && this.isBuffer())
? Buffer.from(this.contents)
: ((this.isStream() && this.contents.clone) ? this.contents.clone() : this.contents);
const file = new this.constructor({ cwd, base, stat, history, contents });
for (const key of Object.keys(this)) {
if (!builtinProperties.has(key)) {
file[key] = opts.deep ? clone(this[key], true) : this[key];
}
}
return file;
}
set contents(value) {
assert(this.constructor.isValidContents(value), 'Expected file.contents to be a Buffer, Stream, or null.');
if (clonableStream && isStream(value) && !clonableStream.isCloneable(value)) {
value = clonableStream(value);
}
this[kContents] = value;
}
get contents() {
return this[kContents];
}
set cwd(cwd) {
assert(cwd && typeof cwd === 'string', 'file.cwd should be a non-empty string');
this[kCwd] = removeTrailingSep(normalize(cwd));
}
get cwd() {
return this[kCwd];
}
set base(value) {
if (value == null) {
delete this[kBase];
return;
}
assert(value && typeof value === 'string', 'file.base should be a non-empty string, null, or undefined');
value = removeTrailingSep(normalize(value));
if (value === this[kCwd]) {
delete this[kBase];
return;
}
this[kBase] = value;
}
get base() {
return this[kBase] || this[kCwd];
}
set dirname(value) {
assert(this.path, pathError('dirname', 'set'));
this.path = path.join(value, this.basename);
}
get dirname() {
assert(this.path, pathError('dirname', 'get'));
return path.dirname(this.path);
}
set basename(value) {
assert(this.path, pathError('basename', 'set'));
this.path = path.join(this.dirname, value);
}
get basename() {
assert(this.path, pathError('basename', 'get'));
return path.basename(this.path);
}
set name(value) {
if (value && typeof value === 'string') {
this.basename = value;
}
}
get name() {
return this.basename;
}
set stem(value) {
assert(this.path, pathError('stem', 'set'));
this.path = path.join(this.dirname, value + this.extname);
}
get stem() {
assert(this.path, pathError('stem', 'get'));
return path.basename(this.path, this.extname);
}
set extname(value) {
assert(this.path, pathError('extname', 'set'));
this.path = replaceExtname(this.path, value);
}
get extname() {
assert(this.path, pathError('extname', 'get'));
return path.extname(this.path);
}
set symlink(value) {
assert(typeof value === 'string', 'Expected "file.symlink" to be a string.');
this[kSymlink] = removeTrailingSep(normalize(value));
}
get symlink() {
return this[kSymlink] || null;
}
set path(value) {
assert(typeof value === 'string', 'Expected "file.path" to be a string.');
value = removeTrailingSep(normalize(value));
if (value && value !== this.path) {
this.history.push(value);
}
}
get path() {
return this.history[this.history.length - 1] || null;
}
set absolute(value) {
throw new Error('"file.absolute" is a getter and may not be defined.');
}
get absolute() {
assert(this.path, pathError('absolute', 'get'));
return path.resolve(this.cwd, this.path);
}
set relative(value) {
throw new Error('"file.relative" is a getter and may not be defined.');
}
get relative() {
assert(this.path, pathError('relative', 'get'));
return path.relative(this.base, this.absolute);
}
set realpath(value) {
throw new Error('"file.realpath" is a getter and may not be defined.');
}
get realpath() {
assert(this.path, pathError('realpath', 'get'));
try {
return fs.realpathSync(this.absolute);
} catch (err) {
/* ignore error */
return null;
}
}
[util.inspect.custom]() {
const filepath = this.path ? (!this.relative.includes('../') ? this.relative : this.absolute) : null;
const inspect = filepath ? [`"${filepath}"`] : [];
if (this.isBuffer()) {
inspect.push(this.contents.inspect());
}
if (this.isStream()) {
const name = this.contents.constructor.name;
const suffix = name.endsWith('Stream') ? '' : 'Stream';
inspect.push(`<${name === 'Class' ? '' : name}${suffix}>`);
}
return `<Dirent ${inspect.join(' ')}>`;
}
static isValidContents(value) {
return value === null || isBuffer(value) || isStream(value);
}
static create(clonableStream) {
return create(clonableStream);
}
}
return Dirent;
};
module.exports = create(/*clonableStream*/);