This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (83 loc) · 2.52 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
// partially inspired from AlessandroMinoccheri/package-info
// TODO: <22-02-19> - Marko: replace got with 'isomorphic-unfetch'
const got = require('got')
const { getDepLength, isValidPkgName } = require('./utils')
module.exports = function pkginfo(
pckg,
opts = ['name', 'version', 'description'],
) {
// I'm nice like that
if (typeof pckg === 'string') {
pckg = { name: pckg }
}
if (isValidPkgName(pckg.name)) {
return got(`https://registry.npmjs.org/${pckg.name.toLowerCase()}`)
.then(data => {
const dataParsed = JSON.parse(data.body)
const name = dataParsed.name
const version = pckg.version || dataParsed['dist-tags'].latest
const latest = dataParsed['dist-tags'].latest
const description = dataParsed.description
const license = dataParsed.license
let homepage = ''
let author = ''
let repository = ''
// edge: when there is no version
if (!dataParsed.versions[version]) {
return null
}
const dependencies = getDepLength(dataParsed, version)
const devDependencies = getDepLength(
dataParsed,
version,
'devDependencies',
)
if (dataParsed.homepage !== undefined) {
homepage = dataParsed.homepage
}
if (dataParsed.repository !== undefined) {
repository = dataParsed.repository.url
}
// get author and maintainers
if (dataParsed.author && dataParsed.author.name) {
// call it a day I have author
author = dataParsed.author.name
} else if (Array.isArray(dataParsed.maintainers)) {
// TODO: maybe make them unique
author = dataParsed.maintainers
.reduce((acc, { name: n }) => (n ? `${n} ${acc}` : acc), '')
.trim()
} else {
// is not neede, author is empty str
}
const allProps = {
homepage,
latest,
name,
description,
version,
author,
license,
devDependencies,
dependencies,
repository,
}
const output = {}
for (const option of opts) {
const value = allProps[option]
if (value) {
output[option] = value
}
}
return output
})
.catch(err => {
if (err.statusCode === 404) {
return null
}
throw err
})
} else {
return Promise.reject(new Error('package name required'))
}
}