Skip to content

Instantly share code, notes, and snippets.

@Super-Chama
Created July 19, 2023 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Super-Chama/1af48918978e926753d71c4f9ce7dc8b to your computer and use it in GitHub Desktop.
Save Super-Chama/1af48918978e926753d71c4f9ce7dc8b to your computer and use it in GitHub Desktop.
Analyse all ES6 imports inside directory
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