Created
September 15, 2019 17:12
-
-
Save ggirou/146eb57f93af5942b9765cf408047e2e to your computer and use it in GitHub Desktop.
Gulp Regex scanner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var regexScanner = require('./regex-scanner'); | |
gulp.task('find-usage:myprefix', function() { | |
// Répertoire où se situe les sources | |
var sourceFolder= './application/'; | |
// Les fichiers sources à vérifier | |
var filesToCheck= [ | |
sourceFolder+ '**/*.ts', | |
sourceFolder +'**/*.html', | |
sourceFolder +'**/*.less' | |
]; | |
// Liste d'expressions régulières àrechercher | |
var regexpKeywords= [ | |
'\\.module\\([^)]+\\)', | |
'\\.directive\\([^)]+\\)' | |
// ... | |
]; | |
gulp.src(filesToCheck, {base: sourceFolder}) | |
.pipe(regexScanner(regexpKeywords)) | |
.pipe(regexScanner.reporter()); // TODO adapterle reporter pour l'intégrer dans TFS | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var through = require('through2'); | |
var gutil = require('gulp-util'); | |
var PluginError = gutil.PluginError; | |
const PLUGIN_NAME = 'gulp-regex-scanner'; | |
function regexScanner(regexp) { | |
if (!regexp) { | |
throw new PluginError(PLUGIN_NAME, 'Missing regexp to search!'); | |
} | |
if (Array.isArray(regexp)) { | |
regexp = "(" + regexp.join(')|(') + ")"; | |
} | |
regexp = new RegExp(regexp, 'g'); | |
// creating a stream through which each file will pass | |
return through.obj(function (file, enc, cb) { | |
if (file.isStream()) { | |
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!')); | |
return cb(); | |
} | |
file.matches = file.matches || []; | |
if (file.isBuffer()) { | |
var lines = file.contents.toString().split('\n'); | |
lines.forEach(function (line, index) { | |
var matches = line.match(regexp); | |
if (matches) { | |
matches.forEach(function (match) { | |
file.matches.push({ | |
file: file.relative, | |
lineNumber: index + 1, | |
match: match, | |
line: line | |
}); | |
}) | |
} | |
}); | |
} | |
// make sure the file goes through the next gulp plugin | |
this.push(file); | |
// tell the stream engine that we are done with this file | |
cb(); | |
}); | |
} | |
regexScanner.reporter = function (rpt) { | |
if (!rpt || rpt === 'default') { | |
// Reporter par défaut, log dans la console | |
rpt = function (result) { | |
result.matches.forEach(function (match) { | |
gutil.log(match.file + ":" + match.lineNumber + ":" + match.line.replace(match.match, gutil.colors.red(match.match))); | |
}); | |
} | |
} | |
return through.obj(function (file, enc, cb) { | |
var matches = file.matches; | |
if (Array.isArray(matches) && matches.length > 0) { | |
var result = {matches: matches}; | |
rpt(result); | |
} | |
cb(null, file); | |
}); | |
}; | |
// exporting the plugin main function | |
module.exports = regexScanner; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment