-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
97 lines (81 loc) · 1.89 KB
/
build.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
import fs from 'node:fs'
import path from 'node:path'
import https from 'node:https'
import yauzl from 'yauzl'
import {tsvParse} from 'd3-dsv'
import concat from 'concat-stream'
import {bail} from 'bail'
const endpoint = 'https://www2.imm.dtu.dk/pubdb/edoc/imm6010.zip'
let found = false
https.get(endpoint, onresult)
/**
* @param {import('http').IncomingMessage} response
*/
function onresult(response) {
response
.pipe(fs.createWriteStream('archive.zip'))
.on('close', onclose)
.on('error', bail)
}
function onclose() {
yauzl.open('archive.zip', {lazyEntries: true}, onopen)
}
/**
* @param {Error?} error
* @param {import('yauzl').ZipFile} archive
*/
function onopen(error, archive) {
bail(error)
read()
archive.on('entry', onentry)
archive.on('end', onend)
/**
* @param {import('yauzl').Entry} entry
*/
function onentry(entry) {
if (path.basename(entry.fileName) !== 'AFINN-96.txt') {
return read()
}
found = true
archive.openReadStream(entry, onreadstream)
}
/**
* @param {Error?} error
* @param {import('stream').Readable} rs
*/
function onreadstream(error, rs) {
bail(error)
rs.pipe(concat(onconcat))
rs.on('end', read)
}
/**
* @param {Buffer} buf
*/
function onconcat(buf) {
/** @type {Record<string, number>} */
const data = {}
const rows = tsvParse('key\tvalue\n' + String(buf))
let index = -1
while (++index < rows.length) {
const row = rows[index]
if (row.value && row.key) {
data[row.key] = Number.parseInt(row.value, 10)
}
}
fs.writeFile(
'index.js',
'/**\n * AFINN-96\n * @type {Record<string, number>}\n*/\nexport const afinn96 = ' +
JSON.stringify(data, null, 2) +
'\n',
bail
)
}
function read() {
archive.readEntry()
}
}
function onend() {
if (!found) {
throw new Error('File not found')
}
}