-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
228 lines (208 loc) · 5.39 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import fss from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
import getNpmTarballUrl from 'get-npm-tarball-url'
import { npmHighImpact } from 'npm-high-impact'
import { publint } from 'publint'
import pLimit from 'p-limit'
/*
Results enum (severity):
0: No issues
1: Has suggestions
2: Has warnings
3: Has errors
*/
const cachedResultsFileUrl = new URL('./cache/_results.json', import.meta.url)
const usedCacheFileBaseNames = ['_results.json']
const useCacheOnly = process.argv.includes('--cache')
try {
await fs.mkdir('./cache', { recursive: true })
if (process.argv.includes('--bench')) {
await benchCommand()
} else {
await mainCommand()
}
} catch (e) {
console.error(e)
process.exitCode = 1
} finally {
// cleanup unused cache
const cachedFiles = await fs.readdir('./cache')
await Promise.all(
cachedFiles.map(async (file) => {
if (!usedCacheFileBaseNames.includes(file)) {
await fs.rm(new URL(`./cache/${file}`, import.meta.url))
}
})
)
}
async function mainCommand() {
const packages = npmHighImpact.slice(0, 200)
const limit = pLimit(5)
const processed = await Promise.all(
packages.map((pkg) =>
limit(async () => {
try {
const pkgData = await fetchPkgData(pkg)
if (!pkgData) {
console.log('No data for', pkg)
return null
}
const { tarball, version } = pkgData
const { messages } = await publint({ pack: { tarball } })
const severity =
messages.length === 0
? 0
: Math.max(...messages.map((m) => messageToSeverity(m)))
// Provide feedback
console.log(pkg, severity)
return { version, severity }
} catch (e) {
console.error(`Failed to lint ${pkg}`, e)
return null
}
})
)
)
/** @type {Record<string, number>} */
const result = {}
for (let i = 0; i < packages.length; i++) {
const p = processed[i]
if (p) {
result[`${packages[i]}@${p.version}`] = p.severity
}
}
await fs.writeFile(cachedResultsFileUrl, JSON.stringify(result, null, 2))
}
async function benchCommand() {
console.log('Fetching packages...')
const packages = npmHighImpact.slice(0, 200)
const limit = pLimit(5)
const pkgData = await Promise.all(
packages.map((pkg) =>
limit(async () => {
try {
const data = await fetchPkgData(pkg)
if (!data) {
console.log('No data for', pkg)
return null
}
return { ...data, pkg }
} catch (e) {
console.error(`Failed to fetch ${pkg}`, e)
return null
}
})
)
)
console.log('Linting packages...')
const start = performance.now()
await Promise.all(
pkgData.map((d) =>
limit(async () => {
if (!d) return
try {
const { tarball } = d
await publint({ pack: { tarball } })
} catch (e) {
console.error(`Failed to lint ${d.pkg}`, e)
return null
}
})
)
)
const duration = performance.now() - start
console.log(`Linted ${packages.length} packages in ${duration.toFixed(2)}ms`)
}
/**
* @param {string} pkg
*/
async function fetchPkgData(pkg) {
const version = useCacheOnly
? await getPkgVersionFromCache(pkg)
: await fetchPkgLatestVersion(pkg)
if (!version) return null
const cachedFileUrl = getCacheTarFileUrl(pkg, version)
usedCacheFileBaseNames.push(path.basename(cachedFileUrl.href))
/** @type {ArrayBuffer} */
let tarball
if (fss.existsSync(cachedFileUrl)) {
const buffer = (await fs.readFile(cachedFileUrl)).buffer
if (buffer instanceof ArrayBuffer) {
tarball = buffer
} else {
throw new Error(`Unexpected SharedArrayBuffer type for ${cachedFileUrl}`)
}
} else if (!useCacheOnly) {
tarball = await fetchPkg(pkg, version)
await fs.writeFile(cachedFileUrl, Buffer.from(tarball))
} else {
return null
}
return { tarball, version }
}
/**
* @param {string} pkg
* @param {string} version
*/
function getCacheTarFileUrl(pkg, version) {
return new URL(
`./cache/${pkg.replace('/', '__')}-${version}.tgz`,
import.meta.url
)
}
/**
* @param {string} pkg
* @param {string} version
* @returns {Promise<ArrayBuffer>}
*/
async function fetchPkg(pkg, version) {
const tarballUrl = getNpmTarballUrl(pkg, version)
const fetchResult = await fetch(tarballUrl, {
headers: {
'User-Agent': 'publint-analysis'
}
})
return await fetchResult.arrayBuffer()
}
/**
* @param {string} pkg
* @returns {Promise<string>}
*/
async function fetchPkgLatestVersion(pkg) {
return await fetch(
`https://registry.npmjs.org/${encodeURIComponent(pkg)}/latest`,
{
headers: {
'User-Agent': 'publint-analysis'
}
}
)
.then((v) => v.ok && v.json())
.then((v) => v.version)
}
/**
* @param {string} pkg
*/
async function getPkgVersionFromCache(pkg) {
const cachedFiles = await fs.readdir('./cache')
const pkgName = pkg.replace('/', '__')
for (const file of cachedFiles) {
if (file.startsWith(pkgName)) {
return /-(\d.*)\.tgz/.exec(file)?.[1]
}
}
}
/**
* @param {import('publint').Message} message
*/
function messageToSeverity(message) {
switch (message.type) {
case 'error':
return 3
case 'warning':
return 2
case 'suggestion':
return 1
}
}