forked from laravel/elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GulpPaths.js
143 lines (121 loc) · 3.63 KB
/
GulpPaths.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
var p = require('path');
var gutils = require('gulp-util');
var parsePath = require('parse-filepath');
/**
* Create a new GulpPaths constructor.
*/
var GulpPaths = function() {};
/**
* Set the Gulp src file(s) and path prefix.
*
* @param {string|Array} src
* @param {string|null} prefix
* @return {GulpPaths}
*/
GulpPaths.prototype.src = function(src, prefix) {
var self = this;
src = this.prefix(src, prefix);
if (Array.isArray(src)) {
// If any item in the src array is a folder
// then we will fetch all of the files.
src = src.map(function(path) {
if (self.parse(path).isDir) {
path += '/**/*';
}
return path;
});
this.src = { path: src, baseDir: prefix };
} else {
this.src = this.parse(src);
// If a directory is provided as the Gulp src,
// the user probably wants everything in it.
this.src.isDir && (this.src.path += '/**/*');
}
return this;
};
/**
* Set the Gulp output path.
*
* @param {string} output
* @param {string|null} defaultName
* @return {GulpPaths}
*/
GulpPaths.prototype.output = function(output, defaultName) {
this.output = this.parse(output);
// If the user didn't provide a path AND file name
// then we'll do our best to choose a default.
if ( ! this.output.name && defaultName) {
// We'll check to see if the provided src is not
// an array. If so, we'll use that single file
// name as the output name. But we must also
// change the extension (.sass -> .css).
if ( ! Array.isArray(this.src.path) && this.src.name.indexOf('*') == -1) {
defaultName = this.changeExtension(
this.src.name,
this.parse(defaultName).extension
);
}
this.output = this.parse(p.join(output, defaultName));
}
return this;
};
/**
* Change the file extension for a path.
*
* @param {string} path
* @param {string} newExtension
* @return {string}
*/
GulpPaths.prototype.changeExtension = function(path, newExtension) {
return gutils.replaceExtension(path, newExtension);
};
/**
* Apply a path prefix to the path(s).
*
* @param {string|Array} path
* @param {string|null} prefix
* @return {string|Array}
*/
GulpPaths.prototype.prefix = function(path, prefix) {
if ( ! prefix) return path;
var prefixOne = function(path) {
// Given any path that begins with a period, we
// can safely assume that the user wants to
// skip the prefix and begin at the root.
if (path.indexOf('./') == 0) {
return path;
}
// If path starts with "!" we need to negate him
if (path.indexOf('!') == 0) {
path = '!' + p.join(prefix, path.substring(1));
} else {
path = p.join(prefix, path);
}
return path.replace(/\/\//g, '/')
.replace(/\/\//g, '/')
.replace(p.join(prefix, prefix), prefix);
};
if (Array.isArray(path)) {
return path.map(prefixOne);
}
return prefixOne(path);
};
/**
* Parse the given file path.
*
* @param {string} path
* @return {object}
*/
GulpPaths.prototype.parse = function(path) {
var segments = parsePath(path);
return {
path : path,
name : segments.extname ? segments.basename : '',
extension : segments.extname,
isDir : ! (!! segments.extname),
baseDir : segments.extname
? segments.dirname
: p.join(segments.dirname, segments.basename)
};
};
module.exports = GulpPaths;