diff --git a/libs/index.md b/libs/index.md index 8e92f9b..01add15 100644 --- a/libs/index.md +++ b/libs/index.md @@ -78,6 +78,8 @@ Available color names: | david-dm dev dependencies | ![](/david/dev/zeit/pkg) | [/david/dev/zeit/pkg](/david/dev/zeit/pkg) | david-dm peer dependencies | ![](/david/peer/epoberezkin/ajv-keywords) | [/david/peer/epoberezkin/ajv-keywords](/david/peer/epoberezkin/ajv-keywords) | david-dm optional dependencies | ![](/david/optional/epoberezkin/ajv-keywords) | [/david/optional/epoberezkin/ajv-keywords](/david/optional/epoberezkin/ajv-keywords) +| packagephobia publish size | ![](/packagephobia/publish/webpack) | [/packagephobia/publish/webpack](/packagephobia/publish/webpack) +| packagephobia install size | ![](/packagephobia/install/webpack) | [/packagephobia/install/webpack](/packagephobia/install/webpack) | uptime robot status | ![](/uptime-robot/status/m780731617-a9e038618dc1aee36a44c4af) | [/uptime-robot/status/m780731617-a9e038618dc1aee36a44c4af](/uptime-robot/status/m780731617-a9e038618dc1aee36a44c4af) | uptime robot uptime (day) | ![](/uptime-robot/day/m780731617-a9e038618dc1aee36a44c4af) | [/uptime-robot/lasy-day/m780731617-a9e038618dc1aee36a44c4af](/uptime-robot/day/m780731617-a9e038618dc1aee36a44c4af) | uptime robot uptime (week) | ![](/uptime-robot/week/m780731617-a9e038618dc1aee36a44c4af) | [/uptime-robot/lasy-week/m780731617-a9e038618dc1aee36a44c4af](/uptime-robot/week/m780731617-a9e038618dc1aee36a44c4af) diff --git a/libs/live-fns/_index.js b/libs/live-fns/_index.js index 22faa80..2103647 100644 --- a/libs/live-fns/_index.js +++ b/libs/live-fns/_index.js @@ -9,6 +9,7 @@ module.exports = { 'github': require('./github.js'), 'homebrew': require('./homebrew.js'), 'npm': require('./npm.js'), + 'packagephobia': require('./packagephobia.js'), 'travis': require('./travis.js'), 'uptime-robot': require('./uptime-robot.js') } diff --git a/libs/live-fns/packagephobia.js b/libs/live-fns/packagephobia.js new file mode 100644 index 0000000..e22f6a3 --- /dev/null +++ b/libs/live-fns/packagephobia.js @@ -0,0 +1,70 @@ +const axios = require('../axios.js') +const prettyBytes = require('pretty-bytes') + +module.exports = async function (topic, pkg) { + const endpoint = `https://packagephobia.now.sh/api.json?p=${pkg}` + const { data } = await axios(endpoint) + const { installSize, publishSize } = data + + switch (topic) { + case 'publish': + return { + subject: 'publish size', + status: prettyBytes(publishSize), + color: getHexColor(publishSize) + } + case 'install': + return { + subject: 'install size', + status: prettyBytes(installSize), + color: getHexColor(installSize) + } + default: + return { + subject: 'packagephobia', + status: 'unknown topic', + color: 'grey' + } + } +} + +/** + * Color schema from + * https://github.com/styfle/packagephobia/blob/master/src/util/npm-parser.ts + */ + +const KB = 1024 +const oneHundredKb = 100 * KB +const megabyte = 1024 * KB +const fiveMb = 5 * megabyte +const thirtyMb = 30 * megabyte +const oneHundredMb = 100 * megabyte +const fiveHundredMb = 500 * megabyte + +const color = { + brightgreen: '44CC11', + limegreen: '97CA00', + yellow: 'DFB317', + orange: 'FE7D37', + red: 'E05D44', + blue: '007EC6', + pink: 'FF69B4' +} + +function getHexColor (bytes) { + if (bytes < oneHundredKb) { + return color.brightgreen + } else if (bytes < megabyte) { + return color.limegreen + } else if (bytes < fiveMb) { + return color.blue + } else if (bytes < thirtyMb) { + return color.yellow + } else if (bytes < oneHundredMb) { + return color.orange + } else if (bytes < fiveHundredMb) { + return color.red + } else { + return color.pink + } +} diff --git a/package-lock.json b/package-lock.json index 23102ee..73f1d6b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3249,6 +3249,11 @@ "integrity": "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=", "dev": true }, + "pretty-bytes": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.1.0.tgz", + "integrity": "sha512-wa5+qGVg9Yt7PB6rYm3kXlKzgzgivYTLRandezh43jjRqgyDyP+9YxfJpJiLs9yKD1WeU8/OvtToWpW7255FtA==" + }, "pretty-error": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/pretty-error/-/pretty-error-2.1.1.tgz", diff --git a/package.json b/package.json index 05b5e2f..28e3b69 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "micro": "^9.3.2", "micro-fork": "^0.1.0", "millify": "^2.0.1", + "pretty-bytes": "^5.1.0", "serve-marked": "0.3.0" }, "devDependencies": {