Skip to content

Instantly share code, notes, and snippets.

@KrofDrakula
Last active November 17, 2023 15:29
Show Gist options
  • Save KrofDrakula/2843a937cd5837c15dc2aa8d8c56c8c6 to your computer and use it in GitHub Desktop.
Save KrofDrakula/2843a937cd5837c15dc2aa8d8c56c8c6 to your computer and use it in GitHub Desktop.
Scanning for usage of NPM packages

Run with deno:

deno task deps <path to source dir>
{
"tasks": {
"deps": "deno run --allow-read ./deps.ts"
},
"imports": {
"std/fs/": "https://deno.land/std@0.207.0/fs/",
"std/path/": "https://deno.land/std@0.207.0/path/"
}
}
import { walk } from "std/fs/walk.ts";
import { toFileUrl } from "std/path/mod.ts";
const importMatcher = /^import.*?from\s+['"](.*?)['"];?$/gm;
const sourceMatcher = /\.[tj]sx?$/;
const enumerateFiles = async (
packageMatch: (name: string) => boolean,
dir: URL
) => {
const packages = new Set<string>();
for await (const entry of walk(dir, {
includeFiles: true,
includeDirs: false,
match: [sourceMatcher],
})) {
const file = await Deno.open(entry.path, { read: true });
const reader = file.readable
.pipeThrough(new TextDecoderStream())
.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const matches = value.matchAll(importMatcher);
for (const match of matches) {
if (packageMatch(match[1])) packages.add(match[1]);
}
}
}
return packages;
};
const target = new URL(Deno.args[0], toFileUrl(Deno.cwd()) + "/");
const packages = await enumerateFiles(
(packageName) =>
!packageName.startsWith(".") && !packageName.startsWith("@blotoutio/"),
target
);
for (const code of packages) {
console.log(code.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment