Skip to content

Commit

Permalink
chore: cache versions to increase npm updater performance (#825) (#828)
Browse files Browse the repository at this point in the history
Co-authored-by: Manuel Carrasco Moñino <[email protected]>
  • Loading branch information
vaadin-bot and manolo authored Apr 23, 2021
1 parent 652510a commit 562a836
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
36 changes: 22 additions & 14 deletions scripts/lib/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const https = require("https");
const xml2js = require('xml2js');
const fs = require('fs');
const exec = require('util').promisify(require('child_process').exec);
const cachedBowerVersions = {};
const cachedNpmVersions = {};

async function run(cmd) {
const { stdout, stderr } = await exec(cmd);
Expand Down Expand Up @@ -106,32 +108,38 @@ async function getAnnotations(){
}

async function getLatestNpmVersion(package, version, major, minor) {
cmd = `npm view ${package} versions --json`;
const json = await JSON.parse(await run(cmd))
const versions = json.filter(version => version.startsWith(`${major}.${minor}`));
const next = versions.pop();
console.log(`Checking next version for ${package} ${version} ${next}`);
return next;
if (!cachedNpmVersions[package]) {
cmd = `npm view ${package} versions --json`;
const json = await JSON.parse(await run(cmd))
const versions = json.filter(version => version.startsWith(`${major}.${minor}`));
const next = versions.pop();
console.log(`Checking next Npm version for ${package} ${version} ${next}`);
cachedNpmVersions[package] = next;
}
return cachedNpmVersions[package];
}

async function getLatestBowerVersion(package, version, major, minor) {
cmd = `bower info ${package} --json`;
const json = await JSON.parse(await run(cmd));
const versions = json.versions.filter(version => version.startsWith(`${major}.${minor}`));
const next = versions[0]
console.log(`Checking next version for ${package} ${version} ${next}`);
return next;
if (!cachedBowerVersions[package]) {
cmd = `bower info ${package} --json`;
const json = await JSON.parse(await run(cmd));
const versions = json.versions.filter(version => version.startsWith(`${major}.${minor}`));
const next = versions[0]
console.log(`Checking next Bower version for ${package} ${version} ${next}`);
cachedBowerVersions[package] = next;
}
return cachedBowerVersions[package];
}

async function computeVertionToUpdate(data) {
async function computeVersionToUpdate(data) {
return (data['updatedVersion'] = await getLatestNpmVersion(data.package, data.version, data.major, data.minor));
}

module.exports = {
getVersions,
getVersionsCsv,
getVersionsJson,
computeVertionToUpdate,
computeVersionToUpdate,
getLatestNpmVersion,
getAnnotations,
checkoutPlatorm,
Expand Down
9 changes: 5 additions & 4 deletions scripts/updateNpmVer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fs = require('fs');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const replace = require('replace-in-file');
const {getAnnotations, computeVertionToUpdate} = require('./lib/versions.js');
const {getAnnotations, computeVersionToUpdate} = require('./lib/versions.js');

let exclude=[];

Expand Down Expand Up @@ -48,7 +48,7 @@ async function run(cmd) {
/**
* Allow exclude certain component package update, use ',' as separator
*/
async function excludeComponents() {
function excludeComponents() {
for (i = 2;process.argv[i]; i++) {
switch(process.argv[i]) {
case '--exclude':
Expand All @@ -67,15 +67,16 @@ async function excludeComponents() {
async function main() {
console.log("Updating the NpmPackage annotation.")
const annotations = await getAnnotations();

if (process.argv.length > 2) {
exclude = await excludeComponents();
exclude = excludeComponents();
}

for (i = 0; i < annotations.length; i++) {
if (exclude.includes(annotations[i].package)) {
console.log('\x1b[33m', "skip updating " + annotations[i].package + " package");
} else {
await computeVertionToUpdate(annotations[i]);
await computeVersionToUpdate(annotations[i]);
await updateFiles(annotations[i]);
}
}
Expand Down

0 comments on commit 562a836

Please sign in to comment.