-
Notifications
You must be signed in to change notification settings - Fork 7
/
report.mjs
168 lines (146 loc) · 4.6 KB
/
report.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { promises as fs } from "fs";
import { glob } from "glob";
const versionCache = {};
async function findPackageJsonFiles(dir) {
return new Promise((resolve, reject) => {
glob(`${dir}/**/package.json`, (err, files) => {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
}
async function getLatestVersion(dep, tag) {
if (versionCache[dep]) {
return versionCache[dep];
}
const response = await fetch(`https://registry.npmjs.org/${dep}/${tag}`);
if (!response.ok) {
throw new Error(`Failed to fetch version for ${dep} with tag ${tag}`);
}
const data = await response.json();
const latestVersion = data.version;
versionCache[dep] = latestVersion;
return latestVersion;
}
async function getLitProtocolDeps(packageJsonPath, tag) {
const data = await fs.readFile(packageJsonPath, "utf8");
const json = JSON.parse(data);
const litDeps = {};
// Check dependencies and devDependencies for @lit-protocol packages
for (const depType of ["dependencies", "devDependencies"]) {
if (json[depType]) {
for (const [dep, version] of Object.entries(json[depType])) {
if (dep.startsWith("@lit-protocol")) {
litDeps[dep] = {
currentVersion: version,
latestVersion: await getLatestVersion(dep, tag),
};
}
}
}
}
return { name: json.name, dependencies: litDeps };
}
async function reportLitProtocolDeps(dir, tag) {
try {
// Find all package.json files in the given directory
const packageJsonFiles = await findPackageJsonFiles(dir);
const results = [];
// Iterate over each package.json file found
for (const file of packageJsonFiles) {
// Get the @lit-protocol dependencies for the current file
const deps = await getLitProtocolDeps(file, tag);
// Store the results
results.push(deps);
}
// Table headers
const headers = [
"📦 Package",
"Dependency",
"Current Version",
"Latest Version",
];
// Collect all rows to find the maximum width for each column
const rows = results.flatMap((result) =>
Object.entries(result.dependencies).map(
([dep, { currentVersion, latestVersion }]) => [
result.name,
dep,
currentVersion,
latestVersion,
]
)
);
// calculate the maximum width of each column
const calculateColumnWidths = (headers, rows) => {
const widths = headers.map((header) => header.length);
rows.forEach((row) => {
row.forEach((cell, i) => {
if (cell.length > widths[i]) {
widths[i] = cell.length;
}
});
});
return widths;
};
const columnWidths = calculateColumnWidths(headers, rows);
// pad a string to a given length
const padString = (str, length) => {
return str + " ".repeat(Math.max(0, length - str.length));
};
// color text
const colorText = (text, color) => {
const colors = {
green: "\x1b[32m",
yellow: "\x1b[33m",
reset: "\x1b[0m",
};
return colors[color] + text + colors.reset;
};
// Print the table header
console.log(
headers.map((header, i) => padString(header, columnWidths[i])).join(" | ")
);
console.log("-".repeat(columnWidths.reduce((a, b) => a + b + 3, -3)));
// Print each row of the table
rows.forEach((row) => {
const [pkg, dep, currentVersion, latestVersion] = row;
// Remove the caret (^) from the current version for comparison
const cleanedCurrentVersion = currentVersion.trim().replace(/^\^/, "");
const color =
cleanedCurrentVersion === latestVersion.trim() ? "green" : "yellow"; // Trim to avoid whitespace issues
const coloredLatestVersion = colorText(latestVersion, color);
console.log(
[
padString(pkg, columnWidths[0]),
padString(dep, columnWidths[1]),
padString(currentVersion, columnWidths[2]),
padString(coloredLatestVersion, columnWidths[3]),
].join(" | ") + "\x1b[0m" // Ensure reset color at the end of the line
);
});
} catch (error) {
// Log any errors that occur
console.error("Error:", error);
}
}
function parseArguments() {
const args = process.argv.slice(2);
const options = {
tag: "latest", // Default tag
dir: "./", // Default directory
};
args.forEach((arg) => {
if (arg.startsWith("--tag=")) {
options.tag = arg.split("=")[1];
} else {
options.dir = arg;
}
});
return options;
}
const options = parseArguments();
reportLitProtocolDeps(options.dir, options.tag);