UNPKG

5.38 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path');
4var 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
20exports.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
38exports.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
56exports.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
74exports.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
92exports.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
110exports.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
129exports.relative = function relative(a, b) {
130 if (typeof b === 'undefined' && typeof process !== 'undefine' && typeof process.cwd === 'function') {
131 b = a;
132 a = process.cwd();
133 }
134 a = a || '';
135 b = b || '';
136 var rel = utils.relative(a, b);
137 return rel === '.' ? './' : rel;
138};
139
140/**
141 * Get specific (joined) segments of a file path by passing a
142 * range of array indices.
143 *
144 * ```js
145 * <%= segments("a/b/c/d", "2", "3") %>
146 * //=> 'c/d'
147 *
148 * <%= segments("a/b/c/d", "1", "3") %>
149 * //=> 'b/c/d'
150 *
151 * <%= segments("a/b/c/d", "1", "2") %>
152 * //=> 'b/c'
153 * ```
154 *
155 * @param {String} `filepath` The file path to split into segments.
156 * @return {String} Returns a single, joined file path.
157 * @api public
158 */
159
160exports.segments = function segments(filepath, a, b) {
161 return filepath.split(/[\\\/]+/).slice(a, b).join('/');
162};
163
164/**
165 * Join all arguments together and normalize the resulting
166 * `filepath`. Uses the node.js [path] module.
167 *
168 * **Note**: there is also a `join()` array helper, dot notation
169 * can be used with helpers to differentiate. Example: `<%= path.join() %>`.
170 *
171 *
172 * ```js
173 * <%= join("a", "b") %>
174 * //=> 'a/b'
175 * ```
176 *
177 * @param {String} `filepaths` List of file paths.
178 * @return {String} Returns a single, joined file path.
179 * @api public
180 */
181
182exports.join = function join() {
183 return path.join.apply(path, arguments);
184};
185
186/**
187 * Returns true if a file path is an absolute path. An
188 * absolute path will always resolve to the same location,
189 * regardless of the working directory. Uses the node.js
190 * [path] module.
191 *
192 * ```js
193 * // posix
194 * <%= isAbsolute('/foo/bar') %>
195 * //=> 'true'
196 * <%= isAbsolute('qux/') %>
197 * //=> 'false'
198 * <%= isAbsolute('.') %>
199 * //=> 'false'
200 *
201 * // Windows
202 * <%= isAbsolute('//server') %>
203 * //=> 'true'
204 * <%= isAbsolute('C:/foo/..') %>
205 * //=> 'true'
206 * <%= isAbsolute('bar\\baz') %>
207 * //=> 'false'
208 * <%= isAbsolute('.') %>
209 * //=> 'false'
210 * ```
211 *
212 * @param {String} `filepath`
213 * @return {String} Returns a resolve
214 * @api public
215 */
216
217exports.isAbsolute = function isAbsolute(filepath) {
218 return utils.isAbsolute(filepath);
219};
220
221/**
222 * Returns true if a file path is an absolute path. An
223 * absolute path will always resolve to the same location,
224 * regardless of the working directory. Uses the node.js
225 * [path] module.
226 *
227 * ```js
228 * // posix
229 * <%= isRelative('/foo/bar') %>
230 * //=> 'false'
231 * <%= isRelative('qux/') %>
232 * //=> 'true'
233 * <%= isRelative('.') %>
234 * //=> 'true'
235 *
236 * // Windows
237 * <%= isRelative('//server') %>
238 * //=> 'false'
239 * <%= isRelative('C:/foo/..') %>
240 * //=> 'false'
241 * <%= isRelative('bar\\baz') %>
242 * //=> 'true'
243 * <%= isRelative('.') %>
244 * //=> 'true'
245 * ```
246 *
247 * @param {String} `filepath`
248 * @return {String} Returns a resolve
249 * @api public
250 */
251
252exports.isRelative = function isRelative(filepath) {
253 return !utils.isAbsolute(filepath);
254};