-
Notifications
You must be signed in to change notification settings - Fork 21
/
update-table.js
executable file
·71 lines (58 loc) · 1.88 KB
/
update-table.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
'use strict'
const fs = require('fs')
const path = require('path')
const http = require('ipfs-utils/src/http')
const url = 'https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv'
const run = async () => {
const rsp = await http.get(url)
const lines = (await rsp.text()).split('\n')
const names = []
const codes = []
const processed = lines
.slice(1, lines.length - 1)
.map(l => {
const [name, tag, code] = l.split(',')
return [name.trim(), tag.trim(), code.trim()]
})
.reduce((acc, l, index, arr) => {
names.push(`'${l[0]}'`)
codes.push(`${l[2].replace('\'', '')}`)
acc += ` '${l[0]}': ${l[2].replace('\'', '')}`
if (index !== arr.length - 1) {
acc += ',\n'
}
return acc
}, '')
const typesTemplate = `// DO NOT CHANGE THIS FILE MANUALLY. IT IS GENERATED BY tools/update-table.js
/**
* Constant names for all available codecs
*/
export type CodecConstant = ${names.map(n => `${n.toUpperCase().replace(/-/g, '_')}`).join(' | ')}
/**
* Names for all available codecs
*/
export type CodecName = ${names.join(' | ')}
/**
* Number for all available codecs
*/
export type CodecCode = ${codes.join(' | ')}
export type ConstantCodeMap = Record<CodecConstant, CodecCode>
export type NameUint8ArrayMap = Record<CodecName, Uint8Array>
export type CodeNameMap = Record<CodecCode, CodecName>
export type NameCodeMap = Record<CodecName, CodecCode>
`
const tableTemplate = `// DO NOT CHANGE THIS FILE. IT IS GENERATED BY tools/update-table.js
/* eslint quote-props: off */
'use strict'
/**
* @type {import('./generated-types').NameCodeMap}
*/
const baseTable = Object.freeze({
${processed}
})
module.exports = { baseTable }
`
fs.writeFileSync(path.join(__dirname, '../src/generated-types.d.ts'), typesTemplate)
fs.writeFileSync(path.join(__dirname, '../src/generated-table.js'), tableTemplate)
}
run()