1 | ;
|
2 |
|
3 | var path = require('path');
|
4 | var utils = require('./utils');
|
5 |
|
6 | /**
|
7 | * Return the dirname for the given `filepath`. Uses
|
8 | * the node.js [path] module.
|
9 | *
|
10 | * ```js
|
11 | * <%= dirname("a/b/c/d") %>
|
12 | * //=> 'a/b/c'
|
13 | * ```
|
14 | *
|
15 | * @param {String} `filepath`
|
16 | * @return {String} Returns the directory part of the file path.
|
17 | * @api public
|
18 | */
|
19 |
|
20 | exports.dirname = function dirname(filepath) {
|
21 | return path.dirname(filepath);
|
22 | };
|
23 |
|
24 | /**
|
25 | * Return the basename for the given `filepath`. Uses
|
26 | * the node.js [path] module.
|
27 | *
|
28 | * ```js
|
29 | * <%= basename("a/b/c/d.js") %>
|
30 | * //=> 'd.js'
|
31 | * ```
|
32 | *
|
33 | * @param {String} `filepath`
|
34 | * @return {String} Returns the basename part of the file path.
|
35 | * @api public
|
36 | */
|
37 |
|
38 | exports.basename = function basename(fp) {
|
39 | return path.basename(fp);
|
40 | };
|
41 |
|
42 | /**
|
43 | * Return the filename for the given `filepath`, excluding
|
44 | * extension.
|
45 | *
|
46 | * ```js
|
47 | * <%= basename("a/b/c/d.js") %>
|
48 | * //=> 'd'
|
49 | * ```
|
50 | *
|
51 | * @param {String} `filepath`
|
52 | * @return {String} Returns the file name part of the file path.
|
53 | * @api public
|
54 | */
|
55 |
|
56 | exports.filename = function filename(filepath) {
|
57 | return path.basename(filepath, path.extname(filepath));
|
58 | };
|
59 |
|
60 | /**
|
61 | * Return the file extension for the given `filepath`.
|
62 | * Uses the node.js [path] module.
|
63 | *
|
64 | * ```js
|
65 | * <%= extname("foo.js") %>
|
66 | * //=> '.js'
|
67 | * ```
|
68 | *
|
69 | * @param {String} `filepath`
|
70 | * @return {String} Returns a file extension
|
71 | * @api public
|
72 | */
|
73 |
|
74 | exports.extname = function extname(filepath) {
|
75 | return path.extname(filepath);
|
76 | };
|
77 |
|
78 | /**
|
79 | * Return the file extension for the given `filepath`,
|
80 | * excluding the `.`.
|
81 | *
|
82 | * ```js
|
83 | * <%= ext("foo.js") %>
|
84 | * //=> 'js'
|
85 | * ```
|
86 | *
|
87 | * @param {String} `filepath`
|
88 | * @return {String} Returns a file extension without dot.
|
89 | * @api public
|
90 | */
|
91 |
|
92 | exports.ext = function ext(filepath) {
|
93 | return path.extname(filepath).slice(1);
|
94 | };
|
95 |
|
96 | /**
|
97 | * Resolves the given paths to an absolute path. Uses
|
98 | * the node.js [path] module.
|
99 | *
|
100 | * ```js
|
101 | * <%= resolve('/foo/bar', './baz') %>
|
102 | * //=> '/foo/bar/baz'
|
103 | * ```
|
104 | *
|
105 | * @param {String} `filepath`
|
106 | * @return {String} Returns a resolve
|
107 | * @api public
|
108 | */
|
109 |
|
110 | exports.resolve = function resolve() {
|
111 | return path.resolve.apply(path, arguments);
|
112 | };
|
113 |
|
114 | /**
|
115 | * Get the relative path from file `a` to file `b`.
|
116 | * Typically `a` and `b` would be variables passed
|
117 | * on the context. Uses the node.js [path] module.
|
118 | *
|
119 | * ```js
|
120 | * <%= relative(a, b) %>
|
121 | * ```
|
122 | *
|
123 | * @param {String} `a` The "from" file path.
|
124 | * @param {String} `b` The "to" file path.
|
125 | * @return {String} Returns a relative path.
|
126 | * @api public
|
127 | */
|
128 |
|
129 | exports.relative = function relative(a, b) {
|
130 | return utils.relative(a, b);
|
131 | };
|
132 |
|
133 | /**
|
134 | * Get specific (joined) segments of a file path by passing a
|
135 | * range of array indices.
|
136 | *
|
137 | * ```js
|
138 | * <%= segments("a/b/c/d", "2", "3") %>
|
139 | * //=> 'c/d'
|
140 | *
|
141 | * <%= segments("a/b/c/d", "1", "3") %>
|
142 | * //=> 'b/c/d'
|
143 | *
|
144 | * <%= segments("a/b/c/d", "1", "2") %>
|
145 | * //=> 'b/c'
|
146 | * ```
|
147 | *
|
148 | * @param {String} `filepath` The file path to split into segments.
|
149 | * @return {String} Returns a single, joined file path.
|
150 | * @api public
|
151 | */
|
152 |
|
153 | exports.segments = function segments(filepath, a, b) {
|
154 | return filepath.split(/[\\\/]+/).slice(a, b).join('/');
|
155 | };
|
156 |
|
157 | /**
|
158 | * Join all arguments together and normalize the resulting
|
159 | * `filepath`. Uses the node.js [path] module.
|
160 | *
|
161 | * **Note**: there is also a `join()` array helper, dot notation
|
162 | * can be used with helpers to differentiate. Example: `<%= path.join() %>`.
|
163 | *
|
164 | *
|
165 | * ```js
|
166 | * <%= join("a", "b") %>
|
167 | * //=> 'a/b'
|
168 | * ```
|
169 | *
|
170 | * @param {String} `filepaths` List of file paths.
|
171 | * @return {String} Returns a single, joined file path.
|
172 | * @api public
|
173 | */
|
174 |
|
175 | exports.join = function join() {
|
176 | return path.join.apply(path, arguments);
|
177 | };
|
178 |
|
179 | /**
|
180 | * Returns true if a file path is an absolute path. An
|
181 | * absolute path will always resolve to the same location,
|
182 | * regardless of the working directory. Uses the node.js
|
183 | * [path] module.
|
184 | *
|
185 | * ```js
|
186 | * // posix
|
187 | * <%= isAbsolute('/foo/bar') %>
|
188 | * //=> 'true'
|
189 | * <%= isAbsolute('qux/') %>
|
190 | * //=> 'false'
|
191 | * <%= isAbsolute('.') %>
|
192 | * //=> 'false'
|
193 | *
|
194 | * // Windows
|
195 | * <%= isAbsolute('//server') %>
|
196 | * //=> 'true'
|
197 | * <%= isAbsolute('C:/foo/..') %>
|
198 | * //=> 'true'
|
199 | * <%= isAbsolute('bar\\baz') %>
|
200 | * //=> 'false'
|
201 | * <%= isAbsolute('.') %>
|
202 | * //=> 'false'
|
203 | * ```
|
204 | *
|
205 | * @param {String} `filepath`
|
206 | * @return {String} Returns a resolve
|
207 | * @api public
|
208 | */
|
209 |
|
210 | exports.isAbsolute = function isAbsolute(filepath) {
|
211 | return utils.isAbsolute(filepath);
|
212 | };
|
213 |
|
214 | /**
|
215 | * Returns true if a file path is an absolute path. An
|
216 | * absolute path will always resolve to the same location,
|
217 | * regardless of the working directory. Uses the node.js
|
218 | * [path] module.
|
219 | *
|
220 | * ```js
|
221 | * // posix
|
222 | * <%= isRelative('/foo/bar') %>
|
223 | * //=> 'false'
|
224 | * <%= isRelative('qux/') %>
|
225 | * //=> 'true'
|
226 | * <%= isRelative('.') %>
|
227 | * //=> 'true'
|
228 | *
|
229 | * // Windows
|
230 | * <%= isRelative('//server') %>
|
231 | * //=> 'false'
|
232 | * <%= isRelative('C:/foo/..') %>
|
233 | * //=> 'false'
|
234 | * <%= isRelative('bar\\baz') %>
|
235 | * //=> 'true'
|
236 | * <%= isRelative('.') %>
|
237 | * //=> 'true'
|
238 | * ```
|
239 | *
|
240 | * @param {String} `filepath`
|
241 | * @return {String} Returns a resolve
|
242 | * @api public
|
243 | */
|
244 |
|
245 | exports.isRelative = function isRelative(filepath) {
|
246 | return !utils.isAbsolute(filepath);
|
247 | };
|