Created
June 14, 2024 20:54
-
-
Save calebdre/9554fd252e09fd05ece3e496b217c7b5 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
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