|
@ -1,22 +1,21 @@ |
|
|
const got = require('../got.js') |
|
|
const got = require('../got.js') |
|
|
const byteSize = require('byte-size') |
|
|
|
|
|
|
|
|
|
|
|
module.exports = async (topic, ...args) => { |
|
|
module.exports = async (topic, ...args) => { |
|
|
const endpoint = `https://packagephobia.now.sh/api.json?p=${args.join('/')}` |
|
|
const endpoint = `https://packagephobia.now.sh/v2/api.json?p=${args.join('/')}` |
|
|
const { installSize, publishSize } = await got(endpoint).then(res => res.body) |
|
|
const { install, publish } = await got(endpoint).then(res => res.body) |
|
|
|
|
|
|
|
|
switch (topic) { |
|
|
switch (topic) { |
|
|
case 'publish': |
|
|
case 'publish': |
|
|
return { |
|
|
return { |
|
|
subject: 'publish size', |
|
|
subject: 'publish size', |
|
|
status: formatSize(publishSize), |
|
|
status: publish.pretty, |
|
|
color: formatColor(publishSize) |
|
|
color: publish.color.replace('#', '') |
|
|
} |
|
|
} |
|
|
case 'install': |
|
|
case 'install': |
|
|
return { |
|
|
return { |
|
|
subject: 'install size', |
|
|
subject: 'install size', |
|
|
status: formatSize(installSize), |
|
|
status: install.pretty, |
|
|
color: formatColor(installSize) |
|
|
color: install.color.replace('#', '') |
|
|
} |
|
|
} |
|
|
default: |
|
|
default: |
|
|
return { |
|
|
return { |
|
@ -26,51 +25,3 @@ module.exports = async (topic, ...args) => { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const formatSize = (size) => { |
|
|
|
|
|
return byteSize(size, { |
|
|
|
|
|
units: 'iec', |
|
|
|
|
|
precision: 2 |
|
|
|
|
|
}).toString().replace(/iB\b/, 'B') |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 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' |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const formatColor = 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 |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|