Skip to content

Instantly share code, notes, and snippets.

@julianburr
Last active July 24, 2017 17:43
Show Gist options
  • Save julianburr/5fa7d4e3e22773f035ee7a62e97f1d59 to your computer and use it in GitHub Desktop.
Save julianburr/5fa7d4e3e22773f035ee7a62e97f1d59 to your computer and use it in GitHub Desktop.
Node script to list todos notes in code base
/**
* Author: Julian Burr <https://github.com/julianburr>
* License: https://creativecommons.org/publicdomain/zero/1.0/
*
* This script basically just runs through all source files and
* prints out a list of files and lines where it found the TODO
* keyword
*
* If you want to look for other keywords, just changed the
* searchRegEx to your needs :)
*
* If placed in your projects you can set it up as a script in your
* package json and run it like this:
* npm run todos
*/
var fs = require('fs-extra');
// Just needed to make it look nicer :D
var chalk = require('chalk');
var base = null;
var todoCnt = 0;
var searchRegEx = /TODO(.*)/g;
/**
* Create a function that reads a directory so we can call it recursively
* beginning from the projects source root
*/
function readDir(dirPath){
var list = fs.readdirSync(dirPath);
list.forEach(function(fileName){
var fullPath = dirPath + '/' + fileName;
var stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
readDir(fullPath);
} else if (stats.isFile()) {
var data = fs.readFileSync(fullPath, "utf-8");
var matchFile = data.match(searchRegEx);
if (matchFile && matchFile.length > 0) {
console.log('\n' + chalk.underline(fullPath + ': '))
data.split('\n').forEach(function(lineContent, line){
var matchLine = lineContent.match(searchRegEx);
if (matchLine && matchLine.length > 0) {
matchLine.forEach(function(match){
todoCnt++;
console.log(chalk.gray(' *:' + (line + ' ').substring(0, 6)) + match)
});
}
});
}
}
})
}
readDir('./src');
if (todoCnt > 0) {
console.log(chalk.red(chalk.bold('\n✖ ' + todoCnt + ' todos found in code\n')));
} else {
console.log(chalk.green(chalk.bold('\nGreat, seems you are todo-free\n')));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment