-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
index.js
165 lines (144 loc) · 4.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"use strict"
const fs = require(`fs`)
const got = require(`got`)
const path = require(`path`)
require(`dotenv`).config()
const keywords = [`gatsby-plugin`, `gatsby-source`, `gatsby-transformer`]
const pluginsFile = path.join(__dirname, `plugins.json`)
const loadPlugins = async () => require(pluginsFile)
const savePlugins = plugins =>
new Promise((resolve, reject) => {
let output = `[\n\t`
output += plugins
.sort((a, b) => {
if (a.name < b.name) {
return -1
}
if (a.name > b.name) {
return 1
}
return 0
})
.map(p => JSON.stringify(p))
.join(`,\n\t`)
output += `\n]\n`
fs.writeFile(pluginsFile, output, err => {
if (err) reject(err)
resolve()
})
})
const searchNPM = () => {
const promises = keywords.map(keyword => {
const url = `https://api.npms.io/v2/search?q=${keyword}+keywords:-gatsby-plugin+not:deprecated&size=250`
return got(url).then(response => JSON.parse(response.body))
})
return Promise.all(promises)
}
const transformResults = results => {
const merged = results.reduce((t, r) => t.concat(r.results), [])
const deduped = merged.reduce((t, r) => {
t[r.package.name] = t[r.package.name] || r.package
return t
}, {})
return Object.keys(deduped).map(d => deduped[d])
}
const filterNotBlacklisted = (packages, plugins) => {
const blacklisted = plugins.filter(p => p.blacklist).map(p => p.name)
return packages.filter(pkg => blacklisted.indexOf(pkg.name) < 0)
}
const filterNotNotified = (packages, plugins) => {
const notified = plugins.filter(p => p.notified).map(p => p.name)
return packages.filter(pkg => notified.indexOf(pkg.name) < 0)
}
const removePackagesWithoutRepository = packages =>
packages.filter(pkg => !!pkg.links.repository)
const removeBadNameFormats = packages => packages.filter(p => hasGoodName(p))
const hasGoodName = pkg => {
const name = pkg.name
const isScopedPackage = name.startsWith(`@`)
if (!isScopedPackage) {
return startsWithAllowedPrefix(name)
}
const nameWithoutScope = name.slice(0, name.indexOf(`/`))
return startsWithAllowedPrefix(nameWithoutScope)
}
const startsWithAllowedPrefix = name =>
keywords.some(keyword => name.startsWith(keyword))
const removePackagesWithoutReadme = packages =>
packages.filter(pkg => hasReadMe(pkg))
const hasReadMe = pkg => {
if (pkg.links.homepage || pkg.readme) return true
if (pkg.links.repository) {
return got(pkg.links.repository + `/blob/master/README.md`)
.then(response => response.statusCode === 200)
.catch(_ => false)
} else return false
}
const updatePlugins = (updates, plugins) => {
const res = plugins.map(p => Object.assign({}, p))
updates.forEach(u => {
const idx = res.findIndex(r => r.name === u.name)
if (idx >= 0) {
res[idx] = Object.assign({}, res[idx], u)
} else {
res.push(u)
}
})
return updates
}
const filterArchived = plugins => {
if (!process.env.GITHUB_API_TOKEN) {
throw new Error(
`Please use instructions in README.md to setup GITHUB_API_TOKEN`
)
}
const promises = plugins.map(plugin => {
const [username, packageName] = plugin.links.repository.split(`/`).slice(-2)
const url = `https://api.github.com/repos/${username}/${packageName}`
return got(url, {
headers: {
Authorization: `token ${process.env.GITHUB_API_TOKEN}`,
},
})
.then(response => JSON.parse(response.body))
.then(repo => {
return { [packageName]: repo.archived }
})
.catch(_ => {
return { [packageName]: false }
})
})
return Promise.all(promises)
.then(resultsArray =>
resultsArray.reduce((obj, result) => Object.assign(obj, result))
)
.then(result => plugins.filter(plugin => !result[plugin.name]))
}
const main = () => {
loadPlugins()
.then(plugins =>
searchNPM()
.then(transformResults)
.then(packages => filterNotBlacklisted(packages, plugins))
.then(packages => filterNotNotified(packages, plugins))
.then(packages => removePackagesWithoutRepository(packages))
.then(packages => removeBadNameFormats(packages))
.then(packages => removePackagesWithoutReadme(packages))
.then(packages => filterArchived(packages))
.then(packages =>
packages.map(p => {
// TODO: notify / comment on GitHub
console.info(`Notify package "${p.name}"`)
// return update status
// will turn notified to true once notifications are created
return { name: p.name, blacklist: false, notified: false }
})
)
.then(updates => updatePlugins(updates, plugins))
.then(savePlugins)
)
.catch(err => {
console.error(err)
})
}
main()