gulp-path-length
Version:
A Gulp plugin for enforcing a character limit on file paths
43 lines (36 loc) • 1.45 kB
JavaScript
var PluginError, gutil, pluginName, rewritePath, through;
through = require('through2');
gutil = require('gulp-util');
PluginError = gutil.PluginError;
pluginName = 'gulp-path-length';
rewritePath = require('./rewrite');
module.exports = function(options) {
var doRewrite, maxLength, _ref;
if (options == null) {
options = {};
}
maxLength = (_ref = options.maxLength) != null ? _ref : 256;
doRewrite = (options.rewrite != null) && (options.rewrite.replacement != null) && (options.rewrite.match != null);
return through.obj(function(file, enc, cb) {
var filePath;
if (options.rewrite != null) {
if ((options.rewrite.replacement != null) && (options.rewrite.match == null)) {
return cb(new PluginError(pluginName, 'Required option not set: "rewrite.match"'));
}
if ((options.rewrite.match != null) && (options.rewrite.replacement == null)) {
return cb(new PluginError(pluginName, 'Required option not set: "rewrite.replacement"'));
}
}
if (file.isStream()) {
returncb(new PluginError(pluginName, 'Streaming not supported'));
}
filePath = file.path;
if (doRewrite) {
filePath = rewritePath(filePath, options.rewrite.match, options.rewrite.replacement);
}
if (filePath.length > maxLength) {
return cb(new PluginError(pluginName, "File '" + filePath + "' path length greater than " + maxLength));
}
return cb(null, file);
});
};