-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
80 lines (63 loc) · 2.32 KB
/
utils.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
'use strict';
const { getOwnPropertyDescriptor } = Reflect;
const fs = require('fs');
const path = require('path');
const isObject = value => value !== null && typeof value === 'object';
const isWindows = process.platform === 'win32';
exports.handlers = (Dirent, dirent) => ({
get(file, prop) {
if (exports.builtinProperties.has(prop)) return file[prop];
// use "dirent.constructor" for subclassing
const ctorProtoDesc = getOwnPropertyDescriptor(dirent.constructor.prototype, prop);
if (ctorProtoDesc && typeof ctorProtoDesc.value === 'function' && !(dirent instanceof fs.Dirent)) {
return ctorProtoDesc[prop];
}
// then check Dirent
const protoDesc = getOwnPropertyDescriptor(Dirent.prototype, prop);
if (protoDesc && typeof protoDesc.value === 'function') {
return file[prop];
}
// then "file"
const fileDesc = getOwnPropertyDescriptor(file.constructor.prototype, prop);
if (fileDesc && typeof fileDesc.value !== 'function') {
return file[prop];
}
// then "stat" and instance properties
const objects = [file.stat, dirent, dirent.stat];
const obj = objects.find(obj => obj && prop in obj) || file;
return obj[prop];
}
});
exports.builtinProperties = new Set(['constructor', 'contents', 'stat', 'history', 'path', 'base', 'cwd']);
exports.isBuffer = value => {
if (isObject(value) && value.constructor && typeof value.constructor.isBuffer === 'function') {
return value.constructor.isBuffer(value);
}
return false;
};
exports.isStream = value => {
if (isObject(value) && typeof value.pipe === 'function') {
return typeof value.on === 'function';
}
return false;
};
exports.removeTrailingSep = str => {
let i = str.length;
while (i > 1 && (str[i - 1] === '/' || (isWindows && str[i - 1] === '\\'))) i--;
return str.slice(0, i);
};
exports.normalize = input => {
return input ? path.normalize(input) : '';
};
exports.replaceExtname = (filepath, extname) => {
if (typeof filepath !== 'string') return filepath;
if (filepath === '') return filepath;
const { dir, name } = path.parse(filepath);
if (!extname.startsWith('.')) {
extname = `.${extname}`;
}
return path.join(dir, `${name}${extname}`);
};
exports.pathError = (prop, key = 'set') => {
return `Expected "file.path" to be a string, cannot ${key} "file.${prop}".`;
};