我正在使用检查特定的字符串,如果找到了该字符串,我想抛出一个类似于“文件abc中找到的字符串”这样的错误。文件param包含包含文件名+缓冲区的整个对象,但我不知道如何从gulp中的文件对象中提取文件名?
.pipe(contains({
search: 'myString',
onFound: function (string, file, cb) {
console.log(file);
var error = 'Your file "' + file + '" contains "' + string + '", it should not.';
cb(new gutil.PluginError('gulp-contains', error));
}
}))现在,这一行给出了“您的文件对象对象包含someString,它不应该”的输出。另外,console.log(文件)记录输出如下
<File "myFile.js" <Buffer 66 75 6e 63 74 69 6f 6e 20 28 75 73 65 72 2c 20 63 6f 6e 74 65 78 74 2c 20 63 61 6c 6c 62 61 63 6b 29 20 7b 0d 0a 20 20 20 20 63 6f 6e 73 6f 6c 65 2e ... >>我只想要部件" myFile.js“,这样我的输出字符串将是”您的文件myFile.js包含someString,它不应该“
发布于 2018-07-12 11:24:11
这里的文件是Node.js中的一个文件对象。您可以使用file.path获取路径。
.pipe(contains({
search: 'myString',
onFound: function (string, file, cb) {
console.log(file);
var sFile = require('path').parse(file.path).base;
var error = 'Your file "' + sFile + '" contains "' + string + '", it should not.';
cb(new gutil.PluginError('gulp-contains', error));
}
}))https://stackoverflow.com/questions/51303941
复制相似问题