2016-10-10 03:43:44 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Stream = require('stream');
|
|
|
|
var Path = require('path');
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
function gulpRename(obj, options) {
|
|
|
|
|
|
|
|
options = options || {};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
var stream = new Stream.Transform({objectMode: true});
|
|
|
|
|
|
|
|
function parsePath(path) {
|
2018-09-20 02:56:13 +02:00
|
|
|
var extname = options.multiExt ? Path.basename(path).slice(Path.basename(path).indexOf('.')) : Path.extname(path);
|
2016-10-10 03:43:44 +02:00
|
|
|
return {
|
|
|
|
dirname: Path.dirname(path),
|
|
|
|
basename: Path.basename(path, extname),
|
|
|
|
extname: extname
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
stream._transform = function (originalFile, unused, callback) {
|
|
|
|
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
var file = originalFile.clone({contents: false});
|
2016-10-10 03:43:44 +02:00
|
|
|
var parsedPath = parsePath(file.relative);
|
|
|
|
var path;
|
|
|
|
|
|
|
|
var type = typeof obj;
|
|
|
|
|
|
|
|
if (type === 'string' && obj !== '') {
|
|
|
|
|
|
|
|
path = obj;
|
|
|
|
|
|
|
|
} else if (type === 'function') {
|
|
|
|
|
2018-09-20 02:56:13 +02:00
|
|
|
obj(parsedPath, file);
|
2016-10-10 03:43:44 +02:00
|
|
|
path = Path.join(parsedPath.dirname, parsedPath.basename + parsedPath.extname);
|
|
|
|
|
|
|
|
} else if (type === 'object' && obj !== undefined && obj !== null) {
|
|
|
|
|
|
|
|
var dirname = 'dirname' in obj ? obj.dirname : parsedPath.dirname,
|
|
|
|
prefix = obj.prefix || '',
|
|
|
|
suffix = obj.suffix || '',
|
|
|
|
basename = 'basename' in obj ? obj.basename : parsedPath.basename,
|
|
|
|
extname = 'extname' in obj ? obj.extname : parsedPath.extname;
|
|
|
|
|
|
|
|
path = Path.join(dirname, prefix + basename + suffix + extname);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
callback(new Error('Unsupported renaming parameter type supplied'), undefined);
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
file.path = Path.join(file.base, path);
|
|
|
|
|
|
|
|
// Rename sourcemap if present
|
|
|
|
if (file.sourceMap) {
|
|
|
|
file.sourceMap.file = file.relative;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, file);
|
|
|
|
};
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = gulpRename;
|