-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (75 loc) · 2.75 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
#!/usr/bin/env node
const got = require('got')
const { join } = require('path')
const colors = require('colors')
const emoji = require('node-emoji')
const { dependencies, devDependencies } = require(join(process.cwd(), 'package.json'))
const aliases = {}
const notStarred = {}
const cannotStar = []
const allDependencies = Object.keys({ ...dependencies, ...devDependencies })
if (!process.env.GITHUB_API_TOKEN) {
console.log(colors.red(`${emoji.get('warning')} We use GITHUB_API_TOKEN environment variable to call the Github API.`))
console.log(colors.red(`Please run ${colors.bold('export GITHUB_API_TOKEN="YOUR_TOKEN"')} and try again.`))
process.exit()
}
async function callGithubApi (query) {
const { body } = await got.post('https://api.github.com/graphql', {
body: { query },
headers: { authorization: `Bearer ${process.env.GITHUB_API_TOKEN}` },
json: true,
})
return body.data
}
async function fetchPackagesInformation () {
const { body } = await got.post('https://api.npms.io/v2/package/mget', {
body: allDependencies,
json: true,
})
return Object.entries(body).reduce((pkgs, [pkgName, pkgInfo]) => {
try {
const url = pkgInfo.collected.metadata.repository.url.split('/')
const owner = url[3]
const repository = url[4].substring(0, url[4].lastIndexOf('.'))
pkgs.push({ name: pkgName, owner, repository })
} catch (e) {
cannotStar.push(pkgName)
}
return pkgs
}, [])
}
async function thanksDependencies () {
const pkgs = await fetchPackagesInformation()
let query = ''
pkgs.forEach(({ name, owner, repository }, index) => {
query += `_${index}: repository(owner:"${owner}",name:"${repository}"){id,viewerHasStarred}\n`
aliases[`_${index}`] = { name, owner, repository }
})
const repos = await callGithubApi(`query{${query}}`)
query = ''
Object.entries(repos).forEach(([alias, repo], index) => {
if (!repo) {
cannotStar.push(aliases[alias].name)
return
}
if (!repo.viewerHasStarred) {
query += `_${index}: addStar(input:{clientMutationId:"${alias}",starrableId:"${repo.id}"}){clientMutationId}\n`
notStarred[alias] = repo
}
})
if (Object.keys(notStarred).length <= 0) {
console.log(`You already starred all your GitHub dependencies. ${emoji.get('heart')}\n`)
} else {
await callGithubApi(`mutation{${query}}`)
console.log(`Stars sent to :`)
Object.entries(notStarred).forEach(([alias, repo]) => {
console.log(` - ${emoji.get('star')} ${colors.blue(aliases[alias].name)}`)
})
}
cannotStar.forEach((name) => {
console.log(` - Cannot ${emoji.get('star')} ${colors.blue(name)} ${emoji.get('cry')}`)
})
console.log(`\nThanks to you! ${emoji.get('heart')}`)
process.exit()
}
thanksDependencies()