-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·98 lines (81 loc) · 3.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
const chalk = require('chalk')
const argv = require('minimist')(process.argv.slice(2))
const terminalLink = require('terminal-link')
const ora = require('ora')
const { subDays, format, parseISO, formatDistance } = require('date-fns')
const { getPackage, getPackageStats, getDownloadStats } = require('./services/api')
const clear = require('./utils/clear')
const br = require('./utils/br')
const downloadSum = require('./utils/downloadSum')
const readablizeBytes = require('./utils/readablizeBytes')
const spinner = ora('Loading package details...')
// handle unhandled rejections
process.on('unhandledRejection', (err) => {
// clear()
spinner.clear()
console.log(chalk.red(`Failed while fetching package details :(`))
process.exit()
})
const log = console.log
const tl = (...args) => chalk.blueBright(terminalLink(...args))
if (!Array.isArray(argv._) || !argv._.length) {
log(chalk.bold.red('Please provide package name.'))
return
}
const pkgName = argv._[0]
const init = async () => {
// fetch package stats
spinner.start()
const [package, packageStats, weeklyDownloadStats, monthlyDownloadStats] = await Promise.all([
await getPackage(pkgName),
await getPackageStats(pkgName),
await getDownloadStats(pkgName, format(subDays(new Date(), 7), 'yyyy-MM-dd'), format(new Date(), 'yyyy-MM-dd')),
await getDownloadStats(pkgName, format(subDays(new Date(), 28), 'yyyy-MM-dd'), format(new Date(), 'yyyy-MM-dd')),
])
spinner.stop()
const npmhome = `https://www.npmjs.com/package/${pkgName}`
const homepage = package.homepage || npmhome
const tlConfig = { fallback: false }
const latestVersion = `${package['dist-tags'].latest}`
const numberOfDeps = Object.keys(package.versions[latestVersion].dependencies).length
const numberOfVersions = Object.keys(package.versions).length
const repositoryUrl = package.repository.url.replace('git+', '')
const lastPublished = package.time[latestVersion]
// printout
clear()
br()
log(
chalk.green.underline.bold(`${pkgName}@v${latestVersion}`),
`| ${package.license} | deps: ${chalk.cyan(numberOfDeps)} | versions: ${chalk.cyan(numberOfVersions)}`,
`| minified: ${chalk.cyan(readablizeBytes(packageStats.size))} | gzip: ${chalk.cyan(
readablizeBytes(packageStats.gzip)
)}`
)
console.log(`${package.description}`)
br()
console.log(chalk.yellow.bold('versions:'))
for (const key in package['dist-tags']) {
console.log(`${key}: ${chalk.cyan(package['dist-tags'][key])}`)
}
br()
console.log(chalk.yellow.bold('downloads:'))
console.log(`weekly: ${chalk.cyan(downloadSum(weeklyDownloadStats.downloads))}`)
console.log(`monthly: ${chalk.cyan(downloadSum(monthlyDownloadStats.downloads))}`)
br()
console.log(chalk.yellow.bold('links:'))
console.log(`README: ${tl(homepage, homepage, tlConfig)}`)
console.log(`npm: ${tl(npmhome, npmhome, tlConfig)}`)
console.log(`repository: ${tl(repositoryUrl, repositoryUrl, tlConfig)}`)
br()
console.log(chalk.yellow.bold('dates:'))
// 2020-03-19T19:53:13.309Z
console.log(`last published: ${formatDistance(parseISO(lastPublished), new Date())}`)
console.log(`created at: ${formatDistance(parseISO(package.time.created), new Date())}`)
br()
console.log(chalk.yellow.bold('maintainers:'))
package.maintainers.forEach((maintainer) => {
console.log(`- ${maintainer.name} [${tl(maintainer.email, `mailto:${maintainer.email}`)}]`)
})
}
init()