Skip to content

Commit c0a74b6

Browse files
committed
perf, feat(checkObsolotence): add cache
Add a cache of versions and updates for the same frameworks.
1 parent f3a10e4 commit c0a74b6

File tree

1 file changed

+62
-19
lines changed

1 file changed

+62
-19
lines changed

checkObsolete.js

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ const path = require("path");
55

66
const DEBUG = false;
77

8-
const missingPackageWarnings = [];
9-
const manualChecks = [];
10-
118
/**
129
* Returns an array with arrays of types and names of frameworks
1310
* @example getFramewokrs()
@@ -23,16 +20,52 @@ function getFrameworks() {
2320
return [...keyedFrameworks, ...nonKeyedFrameworks];
2421
}
2522

23+
const frameworks = getFrameworks();
24+
25+
/**
26+
* Looks for duplicate frameworks
27+
* @param {{type: string, name: string}[]} frameworks
28+
* @returns {string[]}
29+
*/
30+
function findDuplicateFrameworks(frameworks) {
31+
const names = frameworks.map((framework) => framework.name); // Creates an array with framework names only
32+
const duplicateNames = names.filter(
33+
(name, index) => names.indexOf(name) !== index
34+
); // Filters out repetitive framework names
35+
36+
return duplicateNames;
37+
}
38+
39+
const duplicateFrameworks = findDuplicateFrameworks(frameworks);
40+
const frameworksCache = new Map();
41+
2642
/**
2743
* @param {string} packageName
2844
*/
2945
function maybeObsolete(packageName) {
3046
try {
3147
const npmCmd = `npm view ${packageName} time`;
32-
const output = execSync(npmCmd, {
33-
stdio: ["ignore", "pipe", "ignore"],
34-
}).toString();
35-
const timeData = JSON5.parse(output);
48+
let timeData;
49+
50+
if (duplicateFrameworks.includes(packageName)) {
51+
if (frameworksCache.has(packageName)) {
52+
const output = frameworksCache.get(packageName);
53+
timeData = JSON5.parse(output);
54+
return;
55+
}
56+
57+
const output = execSync(npmCmd, {
58+
stdio: ["ignore", "pipe", "ignore"],
59+
}).toString();
60+
timeData = JSON5.parse(output);
61+
62+
frameworksCache.set(packageName, JSON5.stringify(timeData));
63+
} else {
64+
const output = execSync(npmCmd, {
65+
stdio: ["ignore", "pipe", "ignore"],
66+
}).toString();
67+
timeData = JSON5.parse(output);
68+
}
3669

3770
const now = new Date();
3871
const obsoleteDate = new Date(
@@ -45,18 +78,23 @@ function maybeObsolete(packageName) {
4578
const isObsolete = modifiedDate < obsoleteDate;
4679
const formattedDate = modifiedDate.toISOString().substring(0, 10);
4780

48-
return [isObsolete, packageName, formattedDate];
81+
return { isObsolete, packageName, lastUpdate: formattedDate };
4982
} catch (error) {
5083
console.error(
5184
`Failed to execute npm view for ${packageName}. Error Code ${error.status} and message: ${error.message}`
5285
);
53-
return [false, packageName, null];
86+
return { isObsolete: false, packageName, lastUpdate: null };
5487
}
5588
}
5689

57-
function checkFrameworks() {
58-
const frameworks = getFrameworks();
90+
const missingPackageWarnings = [];
91+
const manualChecks = [];
5992

93+
/**
94+
* Checks frameworks in frameworks/keyed and frameworks/non-keyed for obsolescence,
95+
* the presence of package.json and the presence of the frameworkVersionFromPackage property
96+
*/
97+
function checkFrameworks() {
6098
for (const { type, name } of frameworks) {
6199
const frameworkPath = path.join("frameworks", type, name);
62100
const packageJSONPath = path.join(frameworkPath, "package.json");
@@ -88,20 +126,25 @@ function checkFrameworks() {
88126
console.log(`Results for ${type}/${name} ${isPackageObsolete}`);
89127
}
90128

91-
const anyPackageObsolete = isPackageObsolete.some((r) => r[0]);
129+
const anyPackageObsolete = isPackageObsolete.some(
130+
(packageFramework) => packageFramework.isObsolete
131+
);
132+
92133
if (anyPackageObsolete) {
93134
const formattedPackages = isPackageObsolete
94-
.map((result) => result.slice(1).join(":"))
135+
.map((result) => `${result.packageName}:${result.lastUpdate}`)
95136
.join(", ");
96137

97138
console.log(
98-
`Last npm update for ${type}/${name} ${mainPackages} is older than a year: ${formattedPackages}`
139+
`Last npm update for ${type}/${name} - ${mainPackages} is older than a year: ${formattedPackages}`
140+
);
141+
continue;
142+
}
143+
144+
if (DEBUG) {
145+
console.log(
146+
`Last npm update for ${type}/${name} ${mainPackages} is newer than a year`
99147
);
100-
} else {
101-
if (DEBUG)
102-
console.log(
103-
`Last npm update for ${type}/${name} ${mainPackages} is newer than a year`
104-
);
105148
}
106149
}
107150

0 commit comments

Comments
 (0)