-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (41 loc) · 1.46 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
const { default: fetch } = require('node-fetch')
const { default: chalk } = require('chalk')
const { table } = require('table')
const debug = require('debug')('sponsor.dev')
module.exports = async () => {
try {
const ci = Boolean(process.env.CI)
const disabled = Boolean(process.env.SPONSOR_DEV_DISABLED)
if (ci || disabled) {
return debug('Sponsor.dev is disabled, exit.')
}
const name = process.env.npm_package_name
debug('Got npm_package_name: %s', name)
if (!name) {
throw Error('Invalid package name.')
}
const remote = process.env.SPONSOR_DEV_REMOTE || 'https://sponsor.dev'
debug('Remote: %s', remote)
const url = new URL(`/api/npm/${name}`, remote)
const response = await fetch(url)
if (response.status === 200) {
const { sponsors } = await response.json()
const data = [[chalk.bold(`${name} is sponsored by`)]]
for (const { title, href } of sponsors) {
const lines = []
if (title) lines.push(title)
if (href) lines.push(chalk.blue.underline(href))
data.push([lines.join('\n')])
}
console.log(table(data))
} else if (response.status === 404) {
const data = [[chalk.bold(`${name} is looking for sponsors`)]]
data.push([chalk.blue.underline(`${remote}/npm/${name}`)])
console.log(table(data))
} else {
throw Error(`HTTP ${response.status} ${response.statusText}`)
}
} catch (error) {
debug(error.stack)
}
}