Created
July 19, 2023 05:50
-
-
Save Super-Chama/1af48918978e926753d71c4f9ce7dc8b to your computer and use it in GitHub Desktop.
Analyse all ES6 imports inside directory
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 glob = require("glob"); | |
function traverseAndWriteImports(directory, outputFile) { | |
const filePattern = path.join(directory, "**/*.{js,ts}"); | |
const files = glob.sync(filePattern, { | |
ignore: "**/node_modules/**", | |
nodir: true, | |
}); | |
const imports = []; | |
files.forEach((file) => { | |
const content = fs.readFileSync(file, "utf-8"); | |
const importStatements = content.match(/import\s+.+\s+from\s+['"].+['"]/g); | |
if (importStatements) { | |
importStatements.forEach((importStatement) => { | |
imports.push(importStatement); | |
}); | |
} | |
}); | |
fs.writeFileSync(outputFile, imports.join("\n"), "utf-8"); | |
console.log(`All imports have been written to ${outputFile}`); | |
} | |
const directory = "./"; | |
const outputFile = "./output.txt"; | |
traverseAndWriteImports(directory, outputFile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment