Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar-gavhane committed Jul 23, 2020
1 parent 9942ce5 commit 5d90bec
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# folders
node_modules
.vscode

# files
yarn.lock
package-lock.json
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 120
}
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
# WIP
<div align="center">
<h1>pkg-details</h1>
<img height="80" width="80" alt="unicorn" src='https://emojicdn.elk.sh/📦' />
<p>CLI tool to get pkg details</p>

![npm bundle size](https://img.shields.io/bundlephobia/minzip/pkg-details?label=size&style=flat-square)
![npm version](https://img.shields.io/npm/v/pkg-details?style=flat-square)
![npm downloads](https://img.shields.io/npm/dm/pkg-details?style=flat-square)
![gitHub tests](https://img.shields.io/github/workflow/status/sagar-gavhane/pkg-details/tests?label=tests&style=flat-square)

</div>

### Installing

This module is distributed via `npm` which is bundled with `node` and should be installed as one of your project's dependence.

```bash
npm install -g pkg-details
# or
yarn global add pkg-details
```

### Usage

<!-- update usage -->

```bash
npx pkg-details react
>
react@v16.13.1 | MIT | deps: 3 | versions: 275 | minified: 6.30 KB | gzip: 2.56 KB
React is a JavaScript library for building user interfaces.

versions:
latest: 16.13.1
next: 0.0.0-7f28234f8
experimental: 0.0.0-experimental-4c8c98ab9
canary: 0.0.0-57333ca33
unstable: 0.0.0-da834083c

downloads:
weekly: 7687396
monthly: 29955078

links:
README: https://reactjs.org/
npm: https://www.npmjs.com/package/react
repository: https://github.com/facebook/react.git

dates:
last published: 4 months
created at: over 8 years

maintainers:
- acdlite [npm@andrewclark.io]
- brianvaughn [briandavidvaughn@gmail.com]
- fb [opensource+npm@fb.com]
- gaearon [dan.abramov@gmail.com]
- lunaruan [lunaris.ruan@gmail.com]
- sebmarkbage [sebastian@calyptus.eu]
- sophiebits [npm@sophiebits.com]
- trueadm [dg@domgan.com]
```

### Contributing

Pull requests are always welcome! :)

### License

MIT
98 changes: 98 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
25 changes: 24 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,31 @@
"version": "1.0.0",
"description": "get npm package details",
"main": "index.js",
"bin": {
"pkg-details": "index.js"
},
"repository": "git@github.com:sagar-gavhane/pkg-details.git",
"author": "sagar",
"license": "MIT",
"private": false
"private": false,
"keywords": [
"pkg details",
"details",
"pkg-info",
"npm-info"
],
"scripts": {
"release": "np"
},
"devDependencies": {
"np": "^6.3.2"
},
"dependencies": {
"chalk": "^4.1.0",
"date-fns": "^2.15.0",
"got": "^11.5.1",
"minimist": "^1.2.5",
"ora": "^4.0.5",
"terminal-link": "^2.1.1"
}
}
25 changes: 25 additions & 0 deletions services/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const got = require('got')

const getPackage = async (packageName) => {
return await got(`https://registry.npmjs.org/${packageName}`, {
responseType: 'json',
}).then((r) => r.body)
}

const getPackageStats = async (packageName) => {
return await got(`https://bundlephobia.com/api/size?package=${packageName}`, {
responseType: 'json',
}).then((r) => r.body)
}

const getDownloadStats = async (packageName, startDate, endDate) => {
return await got(`https://api.npmjs.org/downloads/range/${startDate}:${endDate}/${packageName}`, {
responseType: 'json',
}).then((r) => r.body)
}

module.exports = {
getPackage,
getPackageStats,
getDownloadStats,
}
5 changes: 5 additions & 0 deletions utils/br.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const clear = () => {
console.log('')
}

module.exports = clear
9 changes: 9 additions & 0 deletions utils/clear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const clear = () => {
const readline = require('readline')
const blank = '\n'.repeat(process.stdout.rows)
console.log(blank)
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
}

module.exports = clear
5 changes: 5 additions & 0 deletions utils/downloadSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const downloadSum = (downloads) => {
return downloads.reduce((prev, currentValue) => prev + currentValue.downloads, 0)
}

module.exports = downloadSum
7 changes: 7 additions & 0 deletions utils/readablizeBytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const readablizeBytes = (bytes) => {
var s = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB']
var e = Math.floor(Math.log(bytes) / Math.log(1024))
return (bytes / Math.pow(1024, e)).toFixed(2) + ' ' + s[e]
}

module.exports = readablizeBytes

0 comments on commit 5d90bec

Please sign in to comment.