Skip to content

Commit

Permalink
improve downloads-by-versions script
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Mar 31, 2021
1 parent bb9b7f5 commit 9652799
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"clean-dependencies": "node scripts/clean-dependencies",
"refresh": "npm run clean-dependencies && npm i && npm t",
"downloads": "node scripts/downloads-by-versions",
"downloads-main-only": "node scripts/downloads-by-versions --main-only=true",
"usage-100": "node scripts/usage -l=100",
"usage-1000": "node scripts/usage -l=1000",
"outdated": "node scripts/check-dependencies",
Expand Down
27 changes: 18 additions & 9 deletions scripts/downloads-by-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@ const { cyan, green } = require('chalk');
const fetch = require('node-fetch');
const coerce = require('semver/functions/coerce');

(async () => {
const res = await fetch('https://www.npmjs.com/package/core-js');
const PURE = !process.argv.includes('--main-only=true');

async function getStat(pkg) {
const res = await fetch(`https://www.npmjs.com/package/${ pkg }`);
const html = await res.text();
const [, json] = html.match(/>window\.__context__ = ([^<]+)<\//);
const context = JSON.parse(json);
const downloadsByPatch = context.context.versionsDownloads;
return JSON.parse(json).context.versionsDownloads;
}

(async () => {
const core = await getStat('core-js');
const pure = await getStat('core-js-pure');
const downloadsByPatch = {};
const downloadsByMinor = {};
const downloadsByMajor = {};
let total = 0;

for (const [version, downloads] of Object.entries(downloadsByPatch)) {
const semver = coerce(version);
for (const [patch, downloadsMain] of Object.entries(core)) {
const downloadsPure = PURE && pure[patch] || 0;
const semver = coerce(patch);
const { major } = semver;
const minor = `${ major }.${ semver.minor }`;
downloadsByMinor[minor] = (downloadsByMinor[minor] || 0) + downloads;
downloadsByMajor[major] = (downloadsByMajor[major] || 0) + downloads;
total += downloads;
downloadsByPatch[patch] = downloadsMain + downloadsPure;
downloadsByMinor[minor] = (downloadsByMinor[minor] || 0) + downloadsMain + downloadsPure;
downloadsByMajor[major] = (downloadsByMajor[major] || 0) + downloadsMain + downloadsPure;
total += downloadsMain + downloadsPure;
}

function log(kind, map) {
Expand Down

0 comments on commit 9652799

Please sign in to comment.