Created
May 19, 2024 19:59
-
-
Save amsv01/ecd1cbc7c55e6738f0c220e6962d559c to your computer and use it in GitHub Desktop.
Node file utils
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
const fs = require('fs') | |
const path = require('path') | |
// Function to get all .ts files recursively | |
const getAllTsFiles = (dir, fileList = []) => { | |
const files = fs.readdirSync(dir) | |
files.forEach((file) => { | |
const filePath = path.join(dir, file) | |
if (fs.statSync(filePath).isDirectory()) { | |
fileList = getAllTsFiles(filePath, fileList) | |
} else if (file.endsWith('.ts')) { | |
fileList.push(filePath) | |
} | |
}) | |
return fileList | |
} | |
// Function to concatenate file contents | |
const concatenateFiles = (files) => { | |
let combinedContent = '' | |
files.forEach((file) => { | |
const content = fs.readFileSync(file, 'utf-8') | |
combinedContent += `// ${file}\n${content}\n\n// -----------------------------------\n` | |
}) | |
return combinedContent | |
} | |
// Main function | |
const main = (dir, outputFilePath) => { | |
const tsFiles = getAllTsFiles(dir) | |
tsFiles.sort((a, b) => a.localeCompare(b, undefined, { numeric: true })) | |
const combinedContent = concatenateFiles(tsFiles) | |
fs.writeFileSync(outputFilePath, combinedContent) | |
console.log(`Combined content written to ${outputFilePath}`) | |
} | |
// Get arguments from the command line | |
const args = process.argv.slice(2) | |
const directoryPath = args[0] || './src' | |
const outputFile = args[1] || 'combined-src.txt' | |
main(directoryPath, outputFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment