Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Add script to check that NPM website has correct versions #145

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/check-npm-website.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Usage:

npm run build
npm run clean
npm run get-definitely-typed
npm run parse
npm run calculate-versions
node ./bin/check-npm-website.js
*/

import fetch = require("node-fetch");
import { TypingsData, fullPackageName, readTypesDataFile, typingsFromData } from "./lib/common";
import { done, nAtATime } from "./lib/util";
import Versions, { changedPackages } from "./lib/versions";

if (!module.parent) {
done(main());
}

/**
* Downloads package packages from npmjs.com and checks that it reflects the latest published version.
*/
async function main(): Promise<void> {
const [typeData, versions] = await Promise.all([readTypesDataFile(), Versions.load()]);
const typings = typingsFromData(typeData);
const changed = await changedPackages(typings);
const outdated: TypingsData[] = [];

await nAtATime(25, typings, async typing => {
if (!changed.includes(typing)) {
if (!await checkSingle(typing, versions.versionInfo(typing).version)) {
outdated.push(typing);
}
}
});

console.log(`The following are outdated: ${outdated.map(t => t.typingsPackageName)}`);
}

/** Returns true if npm is up-to-date. */
async function checkSingle(typing: TypingsData, correctVersion: number): Promise<boolean> {
const name = typing.typingsPackageName;
const url = `https://www.npmjs.com/package/${fullPackageName(name)}`;
console.log(`Checking ${name}...`);
const content = await fetchTextWithRetries(url);

const rgx = /<strong>\d+\.\d+\.(\d+)<\/strong>\s+is the latest/;
const match = rgx.exec(content);
if (match === null) {
throw new Error(`${name} has unexpected content:\n${content}`);
}

const websiteVersion = match[1];
const upToDate = websiteVersion === correctVersion.toString();
if (!upToDate) {
console.log(`OUTDATED: ${name}: Expected ${correctVersion}, got ${websiteVersion}`);
}
return upToDate;
}

async function fetchTextWithRetries(url: string, retries: number = 5): Promise<string> {
while (retries > 0) {
const response = await fetch(url);
// Gateway Time-out
if (response.status !== 200) {
console.log(`Retrying ${url}...`);
retries--;
}
else {
return response.text();
}
}
throw new Error(`Can't fetch ${url} even after retrying`);
}