Last active
September 19, 2018 06:10
-
-
Save black-black-cat/c55ae1d195aa6b3a9377596a53e6e15f to your computer and use it in GitHub Desktop.
递归查找文本内容
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
#!/usr/bin/env node | |
var path = require("path"); | |
var fs = require("fs"); | |
var filePath = path.resolve(__dirname, '../../src/') // process.argv[2]; | |
var lookingForString = 'debugger' //process.argv[3]; | |
recursiveReadFile(filePath); | |
function recursiveReadFile (fileName) { | |
if (!fs.existsSync(fileName)) return; | |
if (isFile(fileName)) { | |
check(fileName); | |
} | |
if (isDirectory(fileName)) { | |
var files = fs.readdirSync(fileName); | |
files.forEach(function (val, key) { | |
var temp = path.join(fileName, val); | |
if (isDirectory(temp)) recursiveReadFile(temp); | |
if (isFile(temp)) check(temp); | |
}) | |
} | |
} | |
function check (fileName) { | |
var data = readFile(fileName); | |
var exc = new RegExp(lookingForString); | |
if (exc.test(data)) { | |
console.log(fileName); | |
process.exit(1); | |
} | |
} | |
function isDirectory (fileName) { | |
if (fs.existsSync(fileName)) return fs.statSync(fileName).isDirectory(); | |
} | |
function isFile (fileName) { | |
if (fs.existsSync(fileName)) return fs.statSync(fileName).isFile(); | |
} | |
function readFile (fileName) { | |
if (fs.existsSync(fileName)) return fs.readFileSync(fileName, "utf-8"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment