Skip to content

Commit f3a10e4

Browse files
committed
refactor(checkObsolote): make it more readable
1 parent 2cc5baa commit f3a10e4

File tree

1 file changed

+98
-47
lines changed

1 file changed

+98
-47
lines changed

checkObsolete.js

Lines changed: 98 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,117 @@
1-
var _ = require("lodash");
2-
var exec = require("child_process").execSync;
3-
var fs = require("fs");
4-
var path = require("path");
1+
const { execSync } = require("child_process");
2+
const fs = require("fs");
53
const JSON5 = require("json5");
4+
const path = require("path");
65

7-
let args = process.argv.length <= 2 ? [] : process.argv.slice(2, process.argv.length);
8-
9-
var frameworks = [].concat(
10-
fs.readdirSync("./frameworks/keyed").map((f) => ["keyed", f]),
11-
fs.readdirSync("./frameworks/non-keyed").map((f) => ["non-keyed", f])
12-
);
6+
const DEBUG = false;
137

14-
let now = new Date();
15-
let obsoleteDate = new Date(now.getFullYear() - 1, now.getMonth(), now.getDay());
8+
const missingPackageWarnings = [];
9+
const manualChecks = [];
1610

17-
let warnings = [];
18-
let manually = [];
11+
/**
12+
* Returns an array with arrays of types and names of frameworks
13+
* @example getFramewokrs()
14+
* @returns [{type:"keyed", name:"vue"},{type:"keyed", name:"qwik"},{type:"non-keyed", name:"svelte"}]
15+
*/
16+
function getFrameworks() {
17+
const keyedFrameworks = fs
18+
.readdirSync("./frameworks/keyed")
19+
.map((framework) => ({ type: "keyed", name: framework }));
20+
const nonKeyedFrameworks = fs
21+
.readdirSync("./frameworks/non-keyed")
22+
.map((framework) => ({ type: "non-keyed", name: framework }));
23+
return [...keyedFrameworks, ...nonKeyedFrameworks];
24+
}
1925

20-
function maybeObsolete(package) {
21-
let output;
26+
/**
27+
* @param {string} packageName
28+
*/
29+
function maybeObsolete(packageName) {
2230
try {
23-
let output = exec(`npm view ${package} time`, {
31+
const npmCmd = `npm view ${packageName} time`;
32+
const output = execSync(npmCmd, {
2433
stdio: ["ignore", "pipe", "ignore"],
2534
}).toString();
26-
let r = JSON5.parse(output);
27-
return [new Date(r.modified) < obsoleteDate, package, new Date(r.modified).toISOString().substring(0, 10)];
35+
const timeData = JSON5.parse(output);
36+
37+
const now = new Date();
38+
const obsoleteDate = new Date(
39+
now.getFullYear() - 1,
40+
now.getMonth(),
41+
now.getDay()
42+
);
43+
44+
const modifiedDate = new Date(timeData.modified);
45+
const isObsolete = modifiedDate < obsoleteDate;
46+
const formattedDate = modifiedDate.toISOString().substring(0, 10);
47+
48+
return [isObsolete, packageName, formattedDate];
2849
} catch (error) {
29-
console.error(`Failed to execute npm view for ${package}. Error Code ${error.status} and message: ${error.message}`);
30-
return [false, package, null];
50+
console.error(
51+
`Failed to execute npm view for ${packageName}. Error Code ${error.status} and message: ${error.message}`
52+
);
53+
return [false, packageName, null];
3154
}
3255
}
3356

34-
const DEBUG = false;
57+
function checkFrameworks() {
58+
const frameworks = getFrameworks();
59+
60+
for (const { type, name } of frameworks) {
61+
const frameworkPath = path.join("frameworks", type, name);
62+
const packageJSONPath = path.join(frameworkPath, "package.json");
63+
64+
if (!fs.existsSync(packageJSONPath)) {
65+
missingPackageWarnings.push(
66+
`WARN: skipping ${type}/${name} since there's no package.json`
67+
);
68+
continue;
69+
}
70+
71+
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, "utf-8"));
72+
const mainPackages =
73+
packageJSON?.["js-framework-benchmark"]?.frameworkVersionFromPackage;
3574

36-
for (f of frameworks) {
37-
let [dir, name] = f;
38-
let path = `frameworks/${dir}/${name}`;
39-
if (!fs.existsSync(path + "/package.json")) {
40-
warnings.push(`WARN: skipping ${dir}/${name} since there's no package.json`);
41-
} else {
42-
let packageJSON = JSON.parse(fs.readFileSync(path + "/package.json"));
43-
let mainPackages = packageJSON?.["js-framework-benchmark"]?.frameworkVersionFromPackage;
44-
if (mainPackages) {
45-
if (DEBUG) console.log(`Checking ${dir}/${name} ${mainPackages}`);
46-
let packages = mainPackages.split(":");
47-
let maybeObsoleteResults = packages.map((p) => maybeObsolete(p));
48-
if (DEBUG) console.log(`Results for ${dir}/${name} ${maybeObsoleteResults}`);
49-
maybeObsoleteResult = maybeObsoleteResults.some((r) => r[0]);
50-
if (maybeObsoleteResult) {
75+
if (!mainPackages) {
76+
manualChecks.push(`${type}/${name} has no frameworkVersionFromPackage`);
77+
continue;
78+
}
79+
80+
if (DEBUG) {
81+
console.log(`Checking ${type}/${name} ${mainPackages}`);
82+
}
83+
84+
const packages = mainPackages.split(":");
85+
const isPackageObsolete = packages.map(maybeObsolete);
86+
87+
if (DEBUG) {
88+
console.log(`Results for ${type}/${name} ${isPackageObsolete}`);
89+
}
90+
91+
const anyPackageObsolete = isPackageObsolete.some((r) => r[0]);
92+
if (anyPackageObsolete) {
93+
const formattedPackages = isPackageObsolete
94+
.map((result) => result.slice(1).join(":"))
95+
.join(", ");
96+
97+
console.log(
98+
`Last npm update for ${type}/${name} ${mainPackages} is older than a year: ${formattedPackages}`
99+
);
100+
} else {
101+
if (DEBUG)
51102
console.log(
52-
`Last npm update for ${dir}/${name} ${mainPackages} is older than a year: ${maybeObsoleteResults
53-
.map((a) => a.slice(1).join(":"))
54-
.join(", ")}`
103+
`Last npm update for ${type}/${name} ${mainPackages} is newer than a year`
55104
);
56-
} else {
57-
if (DEBUG) console.log(` Last npm update for ${dir}/${name} ${mainPackages} is newer than a year`);
58-
}
59-
} else {
60-
manually.push(`${dir}/${name} has no frameworkVersionFromPackage`);
61105
}
62106
}
107+
108+
if (missingPackageWarnings.length > 0)
109+
console.warn("\nWarnings:\n" + missingPackageWarnings.join("\n"));
110+
if (manualChecks.length > 0)
111+
console.warn(
112+
"\nThe following frameworks must be checked manually\n" +
113+
manualChecks.join("\n")
114+
);
63115
}
64116

65-
if (warnings.length > 0) console.log("\nWarnings:\n" + warnings.join("\n"));
66-
if (manually.length > 0) console.log("\nThe following frameworks must be checked manually\n" + manually.join("\n"));
117+
checkFrameworks();

0 commit comments

Comments
 (0)