find-match-in-dir

This commit is contained in:
Blank
2019-11-18 12:02:43 +08:00
parent 1aa7c3ac47
commit f793ae527c
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/evn node
const crypto = require('crypto');
const path = require('path')
const fs = require('fs')
const reg = new RegExp('tt([ (]).+','g')
var result = []
let arg = process.argv[2];
if (!arg) {
console.error("need arg (file/dir)")
return
}
if (!path.isAbsolute(arg)) {
arg = path.resolve(arg)
}
function doIt(absPath) {
fs.stat(absPath, function (err, stats) {
if (err) console.error(err.message)
if (stats.isDirectory()) {
allCoffee(arg)
} else {
if (path.extname(arg) !== '.coffee') {
console.error("need file with ext 'coffee'")
return
}
findCoffeeRgex(arg)
}
console.log(result)
})
}
function allCoffee(dirPath) {
fs.readdirSync(dirPath, {encoding: "utf-8", withFileTypes: true}).forEach((dirent) => {
if (dirent.isFile()) {
if (dirent.name.trim().toLowerCase().endsWith(".coffee")){
let filePath = path.join(dirPath, dirent.name)
if (!path.isAbsolute(filePath)) {
filePath = path.resolve(filePath)
}
findCoffeeRgex(filePath)
}
}else if (dirent.isDirectory()){
allCoffee(path.join(dirPath, dirent.name))
}
})
}
function findCoffeeRgex(coffeePath) {
let content = fs.readFileSync(coffeePath, 'utf-8')
let res = content.match(reg)
if(res != null){
(result = result + res)
}
}
doIt(arg)