Skip to content

Instantly share code, notes, and snippets.

@JustenRickert
Created August 27, 2019 22:30
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 JustenRickert/77fa4269648bd9e198d5d7b3a9bf7542 to your computer and use it in GitHub Desktop.
Save JustenRickert/77fa4269648bd9e198d5d7b3a9bf7542 to your computer and use it in GitHub Desktop.
import fs from "fs";
import { uniq } from "lodash";
import { parse } from "@babel/parser";
import traverse from "@babel/traverse";
const getRelevantFiles = (filename, relevantFiles = []) => {
if (relevantFiles.includes(filename)) return relevantFiles;
const ast = parse(fs.readFileSync(filename, "utf-8"));
traverse(ast, {
ImportDeclarations(path) {
const filename = getDependencyFilenameFromNode(path.node);
relevantFiles.push(filename);
const relevantFilesToDependency = getRelevantFiles(
filename,
relevantFiles
);
relevantFiles.push(...relevantFilesToDependency);
}
});
return uniq(relevantFiles);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment