UNPKG

node-tidy

Version:

A CLI tool to find and remove unused NPM packages and Install Missing packages in your Node.js projects.

32 lines (31 loc) 1.35 kB
import fs from "fs"; import { globSync } from "glob"; import getDependencies from "./getDependencies"; import { builtinModules } from "module"; async function findMissingPackages() { console.log("Scanning for missing packages..."); const files = globSync("**/*.{js,jsx,ts,tsx}", { ignore: ["node_modules/**", "dist/**"], }); const usedPackages = new Set(); const importRegex = /\b(?:import(?:["'\s]*[\w*{}\n, ]+from\s*)?["']([^"']+)["']|require\(["']([^"']+)["']\))/g; for (const file of files) { const content = fs.readFileSync(file, "utf8"); let match; while ((match = importRegex.exec(content)) !== null) { const packageName = (match[1] || match[2] || "").split("/")[0]; // Extract the package name if (packageName && !packageName.startsWith(".")) { usedPackages.add(packageName); } } } const installedPackages = new Set([ ...getDependencies().dependencies, ...getDependencies().devDependencies, ]); const nodeBuiltinModules = new Set(builtinModules); // Find packages used in code but not installed const missingPackages = Array.from(usedPackages).filter((pkg) => !installedPackages.has(pkg) && !nodeBuiltinModules.has(pkg)); return missingPackages; } export default findMissingPackages;