UNPKG

zhilehuo-gulp-markedfile-fliter

Version:

select files which have special mark

78 lines (75 loc) 2.37 kB
'use strict'; var through = require('through2'); var gutil = require('gulp-util'); var chalk = require('chalk'); var PluginError = gutil.PluginError; // 常量 const PLUGIN_NAME = 'gulp-filterfile'; function filterfile(search,type,deleteMarkAfterSelect){ //deleteMarkAfterSelect 处理完后是否删除或替换该标记 var type = type||"regex"; var selectedFileCount = 0; var stream = through.obj(function(file, enc, cb) { if (file.isStream()) { this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!')); return callback(); } if (file.isBuffer()) { if(type == "text"){ if(String(file.contents).indexOf(search) > 0){ // 确保文件进入下一个 gulp 插件 selectedFileCount++; if(deleteMarkAfterSelect) file.contents = new Buffer(String(file.contents).replace(search,deleteMarkAfterSelect)); this.push(file); } } if(type == 'regex') { var reg = new RegExp(search+"( +status:(\\d) time:[0-9: -]{19} ){0,1}", "g"); //status 没有代表没处理过 1代表处理过了 0代表还需要处理 if(String(file.contents).match(reg)){ var status = reg.exec(String(file.contents))[2]; if(!status || status == 0) { file.contents = new Buffer(String(file.contents).replace(reg,search+" status:1 time:"+getNowDate()+" ")); selectedFileCount++; this.push(file); } } } } // 告诉 stream 引擎,我们已经处理完了这个文件 cb(); }) //console.log('\tselected ' + chalk.green(selectedFileCount) + ' files totally'); return stream; }; function getNowDate() { var date = new Date(); var sign1 = "-"; var sign2 = ":"; var year = date.getFullYear() // 年 var month = date.getMonth() + 1; // 月 var day = date.getDate(); // 日 var hour = date.getHours(); // 时 var minutes = date.getMinutes(); // 分 var seconds = date.getSeconds() //秒 // 给一位数数据前面加 “0” if (month >= 1 && month <= 9) { month = "0" + month; } if (day >= 0 && day <= 9) { day = "0" + day; } if (hour >= 0 && hour <= 9) { hour = "0" + hour; } if (minutes >= 0 && minutes <= 9) { minutes = "0" + minutes; } if (seconds >= 0 && seconds <= 9) { seconds = "0" + seconds; } var currentdate = year + sign1 + month + sign1 + day + " " + hour + sign2 + minutes + sign2 + seconds; return currentdate; } module.exports = filterfile;