Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(scripts): discover unused packages #6364

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 85 additions & 20 deletions scripts/validation/deprecated-packages.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,98 @@
const fs = require("node:fs");
const fse = require("fs-extra");
const path = require("node:path");

const root = path.join(__dirname, "..", "..");
const packages = path.join(root, "packages");
const libs = path.join(root, "lib");
const clients = path.join(root, "clients");

const excluded = [];
/**
* Throw and Error if any deprecated package is not marked private.
*/
{
const excluded = [];

for (const folder of fs.readdirSync(packages)) {
const pkgJson = require(path.join(packages, folder, "package.json"));
if (excluded.includes(pkgJson.name)) {
continue;
}
if (pkgJson.private === true) {
fse.moveSync(path.join(packages, folder), path.join(root, "deprecated", "packages", folder));
const deprecatedPackages = path.join(root, "deprecated", "packages");
for (const folder of fs.readdirSync(deprecatedPackages)) {
const pkgJson = require(path.join(deprecatedPackages, folder, "package.json"));

if (excluded.includes(pkgJson.name)) {
continue;
}

if (pkgJson.private !== true) {
throw new Error("package in deprecated folder is not marked private:", folder);
} else {
}
}
}

const deprecatedPackages = path.join(root, "deprecated", "packages");
for (const folder of fs.readdirSync(deprecatedPackages)) {
const pkgJson = require(path.join(deprecatedPackages, folder, "package.json"));
/**
* Analyze package dependency graph.
*/
{
const packagesData = fs.readdirSync(packages).map((pkg) => require(path.join(packages, pkg, "package.json")));
const libsData = fs.readdirSync(libs).map((pkg) => require(path.join(libs, pkg, "package.json")));
const clientsData = fs.readdirSync(clients).map((pkg) => require(path.join(clients, pkg, "package.json")));

if (excluded.includes(pkgJson.name)) {
continue;
}
const allPackages = [...packagesData, ...libsData, ...clientsData];

if (pkgJson.private !== true) {
throw new Error("package in deprecated folder is not marked private:", folder);
} else {
console.log(`"${pkgJson.name}"`);
}
const graph = new (class PackageGraph {
/**
* @param graph - map of pkg name to pkg.json objects.
*/
constructor(graph) {
this.graph = graph;
for (const md of Object.values(graph)) {
md.dependents = md.dependents ?? {};
for (const dependency of Object.keys({ ...md.dependencies, ...md.devDependencies })) {
if (!graph[dependency]) {
// is external.
continue;
}
graph[dependency].dependents = graph[dependency].dependents ?? {};
graph[dependency].dependents[md.name] = 1;
}
}
}

entryPoints() {
const { graph } = this;
return Object.values(graph)
.filter((p) => {
if (p.name.startsWith("@aws-sdk/client-")) {
return false;
}
// return Object.keys(p.dependents).length === 0;
return true;
})
.sort((a, b) => {
return Object.keys(a.dependents).length - Object.keys(b.dependents).length;
})
.map((p) => {
const deps = Object.keys(p.dependents);
switch (p.name) {
case "@aws-sdk/karma-credential-loader":
return p.name + ": -- devtool --";
}
if (deps.length === 0) {
return p.name + ": ---- NONE ----";
}
if (deps.length > 100) {
return p.name + `: ** ${deps.length} packages (clients) **`;
}
if (deps.length > 5) {
return p.name + `: ** ${deps.length} packages **`;
}
return p.name + ": \n\t" + deps.join("\n\t");
});
}
})(
allPackages.reduce((gr, pkg) => {
gr[pkg.name] = pkg;
return gr;
}, {})
);

console.log(graph.entryPoints().join("\n"));
}
Loading