-
-
Save jonchurch/b7ac56f084cb4dfee049e104abb5617b to your computer and use it in GitHub Desktop.
Node script to list todos notes in code base
This file contains 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
/** | |
* 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('./.'); | |
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