Skip to content

Instantly share code, notes, and snippets.

@calebdre
Created June 14, 2024 20:54
Show Gist options
  • Save calebdre/9554fd252e09fd05ece3e496b217c7b5 to your computer and use it in GitHub Desktop.
Save calebdre/9554fd252e09fd05ece3e496b217c7b5 to your computer and use it in GitHub Desktop.
type FileInfo {
content: string
path: string
filename: string
}
function findTsFiles(directory: string, exclude_dirs: string[]): void {
const fileList: FileInfo[] = []
function traverseDirectory(currentDir: string) {
const files = fs.readdirSync(currentDir)
files.forEach((file) => {
const filePath = path.join(currentDir, file)
const stats = fs.statSync(filePath)
if (stats.isDirectory()) {
if (!exclude_dirs.includes(file)) {
traverseDirectory(filePath)
}
} else if (stats.isFile() && (file.endsWith('.ts') || file.endsWith('.tsx'))) {
const fileInfo: FileInfo = {
content: fs.readFileSync(filePath, 'utf-8'),
path: currentDir,
filename: file
}
fileList.push(fileInfo)
}
})
}
traverseDirectory(directory)
// save to a json file so we don't have to do this on every run
const jsonContent = JSON.stringify(fileList, null, 2)
fs.writeFileSync('files.json', jsonContent)
return fileList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment