From 9652799dcef8d2e2a4ad7691835e0db92442b0e0 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Wed, 31 Mar 2021 22:39:30 +0700 Subject: [PATCH] improve `downloads-by-versions` script --- package.json | 1 + scripts/downloads-by-versions.js | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 97606ff677a3..d6cee4c73384 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/downloads-by-versions.js b/scripts/downloads-by-versions.js index ff899d1b4abf..718a8c1b754d 100644 --- a/scripts/downloads-by-versions.js +++ b/scripts/downloads-by-versions.js @@ -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) {