-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpath.js
247 lines (220 loc) · 5.13 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
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
'use strict';
const path = require('path');
const relative = require('relative');
/**
* Return the dirname for the given `filepath`. Uses
* the node.js [path] module.
*
* ```js
* <%= dirname("a/b/c/d") %>
* //=> 'a/b/c'
* ```
* @param {String} `filepath`
* @return {String} Returns the directory part of the file path.
* @api public
*/
exports.dirname = path.dirname;
/**
* Return the basename for the given `filepath`. Uses
* the node.js [path] module.
*
* ```js
* <%= basename("a/b/c/d.js") %>
* //=> 'd.js'
* ```
* @param {String} `filepath`
* @return {String} Returns the basename part of the file path.
* @api public
*/
exports.basename = path.basename;
/**
* Returns the filename for the given `filepath`, excluding
* extension. Aliased as `stem`.
*
* ```js
* <%= filename("a/b/c/d.js") %>
* //=> 'd'
* ```
* @param {String} `filepath`
* @return {String} Returns the file name part of the file path.
* @api public
*/
exports.filename = filepath => {
return path.basename(filepath, path.extname(filepath));
};
/**
* Alias for [filename](#filename).
*
* ```js
* <%= stem("a/b/c/d.js") %>
* //=> 'd'
* ```
* @param {String} `filepath`
* @return {String} Returns the file name part of the file path.
* @api public
*/
exports.stem = exports.filename;
/**
* Return the file extension for the given `filepath`.
* Uses the node.js [path] module.
*
* ```js
* <%= extname("foo.js") %>
* //=> '.js'
* ```
* @param {String} `filepath`
* @return {String} Returns a file extension
* @api public
*/
exports.extname = path.extname;
/**
* Return the file extension for the given `filepath`,
* excluding the `.`.
*
* ```js
* <%= ext("foo.js") %>
* //=> 'js'
* ```
* @param {String} `filepath`
* @return {String} Returns a file extension without dot.
* @api public
*/
exports.ext = filepath => path.extname(filepath).replace(/^\./, '');
/**
* Resolves the given paths to an absolute path. Uses
* the node.js [path] module.
*
* ```js
* <%= resolve('/foo/bar', './baz') %>
* //=> '/foo/bar/baz'
* ```
* @param {String} `filepath`
* @return {String} Returns a resolve
* @api public
*/
exports.resolve = path.resolve;
/**
* Get the relative path from file `a` to file `b`.
* Typically `a` and `b` would be variables passed
* on the context. Uses the node.js [path] module.
*
* ```js
* <%= relative(a, b) %>
* ```
* @param {String} `a` The "from" file path.
* @param {String} `b` The "to" file path.
* @return {String} Returns a relative path.
* @api public
*/
exports.relative = (a, b) => {
if (b === void 0) {
b = a;
if (typeof process !== 'undefined' && typeof process.cwd === 'function') {
a = process.cwd();
} else {
a = '.';
}
}
a = a || '';
b = b || '';
let rel = relative(a, b);
return rel === '.' ? './' : rel;
};
/**
* Get specific (joined) segments of a file path by passing a
* range of array indices.
*
* ```js
* <%= segments("a/b/c/d", "2", "3") %>
* //=> 'c/d'
*
* <%= segments("a/b/c/d", "1", "3") %>
* //=> 'b/c/d'
*
* <%= segments("a/b/c/d", "1", "2") %>
* //=> 'b/c'
* ```
* @param {String} `filepath` The file path to split into segments.
* @return {String} Returns a single, joined file path.
* @api public
*/
exports.segments = (filepath, a, b) => {
return filepath.split(/[\\/]+/).slice(a, b).join('/');
};
/**
* Join all arguments together and normalize the resulting
* `filepath`. Uses the node.js [path] module.
*
* **Note**: there is also a `join()` array helper, dot notation
* can be used with helpers to differentiate. Example: `<%= path.join() %>`.
*
*
* ```js
* <%= join("a", "b") %>
* //=> 'a/b'
* ```
* @param {String} `filepaths` List of file paths.
* @return {String} Returns a single, joined file path.
* @api public
*/
exports.join = path.join;
/**
* Returns true if a file path is an absolute path. An
* absolute path will always resolve to the same location,
* regardless of the working directory. Uses the node.js
* [path] module.
*
* ```js
* // posix
* <%= isAbsolute('/foo/bar') %>
* //=> 'true'
* <%= isAbsolute('qux/') %>
* //=> 'false'
* <%= isAbsolute('.') %>
* //=> 'false'
*
* // Windows
* <%= isAbsolute('//server') %>
* //=> 'true'
* <%= isAbsolute('C:/foo/..') %>
* //=> 'true'
* <%= isAbsolute('bar\\baz') %>
* //=> 'false'
* <%= isAbsolute('.') %>
* //=> 'false'
* ```
* @param {String} `filepath`
* @return {String} Returns a resolve
* @api public
*/
exports.isAbsolute = path.isAbsolute;
/**
* Returns true if a file path is an absolute path. An
* absolute path will always resolve to the same location,
* regardless of the working directory. Uses the node.js
* [path] module.
*
* ```js
* // posix
* <%= isRelative('/foo/bar') %>
* //=> 'false'
* <%= isRelative('qux/') %>
* //=> 'true'
* <%= isRelative('.') %>
* //=> 'true'
*
* // Windows
* <%= isRelative('//server') %>
* //=> 'false'
* <%= isRelative('C:/foo/..') %>
* //=> 'false'
* <%= isRelative('bar\\baz') %>
* //=> 'true'
* <%= isRelative('.') %>
* //=> 'true'
* ```
* @param {String} `filepath`
* @return {String} Returns a resolve
* @api public
*/
exports.isRelative = filepath => !path.isAbsolute(filepath);