forked from Kode/khamake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath.js
62 lines (51 loc) · 1.33 KB
/
Path.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
"use strict";
const path = require('path');
class Path {
constructor(path) {
this.path = path;
}
startsWith(other) {
var me = this.path;
var he = other.path;
if (he == '.') return true;
if (me[0] == '.' && me[1] == '/') me = me.substr(2);
if (he[0] == '.' && he[1] == '/') he = he.substr(2);
for (var i = 0; i < he.length; ++i) {
if (me[i] != he[i]) return false;
}
return true;
}
relativize(other) {
return new Path(path.relative(this.path, other.path));
}
resolve(subpath) {
if (typeof (subpath) !== 'string') subpath = subpath.path;
if (path.isAbsolute(subpath)) return new Path(subpath);
return new Path(path.join(this.path, subpath));
}
parent() {
if (this.path == ".") return this.toAbsolutePath().parent();
else {
for (var i = this.path.length - 1; i >= 0; --i) {
if (this.path[i] == '/' || this.path[i] == '\\') {
return require('./Paths.js').get(this.path.substr(0, i));
}
}
}
return this;
}
getFileName() {
return path.basename(this.path);
}
toString() {
return path.normalize(this.path);
}
isAbsolute() {
return (this.path.length > 0 && this.path[0] == '/') || (this.path.length > 1 && this.path[1] == ':');
}
toAbsolutePath() {
if (this.isAbsolute()) return this;
return new Path(path.resolve(process.cwd(), this.path));
}
}
module.exports = Path;