Created
February 6, 2020 13:54
-
-
Save andrefa/f7351d7402606cd8325862dd3bb495e8 to your computer and use it in GitHub Desktop.
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
const fs = require('fs') | |
const path = require('path') | |
const voidFn = () => {} | |
class Utils { | |
constructor() { | |
this.gitignoreFileName = '.gitignore' | |
} | |
isDirectory(file) { | |
return fs.statSync(file).isDirectory() | |
} | |
readDirectory(directory) { | |
return fs.readdirSync(directory).map( file => path.join(directory, file)) | |
} | |
hasExtension(f, ext) { | |
return f.split('.').pop() === ext | |
} | |
iterateRecursively({ directory, dirAction = voidFn, fileAction = voidFn }) { | |
const files = this.readDirectory(directory) | |
files.forEach( file => { | |
if (this.isDirectory(file)) { | |
this.iterateRecursively({directory: file, dirAction, fileAction}) | |
dirAction(file) | |
} else { | |
fileAction(file) | |
} | |
}) | |
} | |
} | |
const utils = new Utils() | |
utils.iterateRecursively({ | |
directory: '.', | |
fileAction: (file) => { | |
if (utils.hasExtension(file, 'gitignore')) { | |
const ignored = fs.readFileSync(file).toString() | |
console.log(ignored) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment