-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
42 lines (40 loc) · 1.17 KB
/
index.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
// index.js
////////////////////////////////////////////////////////////
// NPM Modules
////////////////////////////////////////////////////////////
var path = require('path')
var PluginError = require('gulp-util').PluginError
var through = require('through2')
////////////////////////////////////////////////////////////
// Setup
////////////////////////////////////////////////////////////
var myName = 'gulp-regex-rename'
function handleErr(err, handler, cb) {
handler.emit('error', new PluginError(myName, err))
return cb()
}
////////////////////////////////////////////////////////////
// Logic
////////////////////////////////////////////////////////////
module.exports = function(regex, str) {
return through.obj(function(file, unused, cb) {
if (file.isStream()) {
return handleErr('Streaming not supported', this, cb)
}
if (
regex instanceof RegExp === false ||
typeof str !== 'string'
) {
return handleErr('Incorrect params', this, cb)
}
try {
file.path = path.join(
file.base,
file.relative.replace(regex, str)
)
} catch(err) {
return handleErr(err, this, cb)
}
return cb(null, file)
})
}