1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 | const path = require('path')
|
5 | const http = require('ipfs-utils/src/http')
|
6 | const url = 'https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv'
|
7 |
|
8 | const run = async () => {
|
9 | const rsp = await http.get(url)
|
10 | const lines = (await rsp.text()).split('\n')
|
11 | const names = []
|
12 | const codes = []
|
13 | const processed = lines
|
14 | .slice(1, lines.length - 1)
|
15 | .map(l => {
|
16 | const [name, tag, code] = l.split(',')
|
17 | return [name.trim(), tag.trim(), code.trim()]
|
18 | })
|
19 | .reduce((acc, l, index, arr) => {
|
20 | names.push(`'${l[0]}'`)
|
21 | codes.push(`${l[2].replace('\'', '')}`)
|
22 | acc += ` '${l[0]}': ${l[2].replace('\'', '')}`
|
23 |
|
24 | if (index !== arr.length - 1) {
|
25 | acc += ',\n'
|
26 | }
|
27 | return acc
|
28 | }, '')
|
29 |
|
30 | const typesTemplate = `// DO NOT CHANGE THIS FILE MANUALLY. IT IS GENERATED BY tools/update-table.js
|
31 |
|
32 | /**
|
33 | * Constant names for all available codecs
|
34 | */
|
35 | export type CodecConstant = ${names.map(n => `${n.toUpperCase().replace(/-/g, '_')}`).join(' | ')};
|
36 |
|
37 | /**
|
38 | * Names for all available codecs
|
39 | */
|
40 | export type CodecName = ${names.join(' | ')};
|
41 |
|
42 | /**
|
43 | * Number for all available codecs
|
44 | */
|
45 | export type CodecNumber = ${codes.join(' | ')};
|
46 |
|
47 | export type ConstantNumberMap = Record<CodecConstant, CodecNumber>
|
48 | export type NameUint8ArrayMap = Record<CodecName, Uint8Array>
|
49 | export type NumberNameMap = Record<CodecNumber, CodecName>
|
50 | export type NameNumberMap = Record<CodecName, CodecNumber>
|
51 | `
|
52 |
|
53 | const tableTemplate = `// DO NOT CHANGE THIS FILE. IT IS GENERATED BY tools/update-table.js
|
54 | /* eslint quote-props: off */
|
55 | 'use strict'
|
56 |
|
57 | /**
|
58 | * @type {import('./generated-types').NameNumberMap}
|
59 | */
|
60 | const baseTable = Object.freeze({
|
61 | ${processed}
|
62 | })
|
63 |
|
64 | module.exports = { baseTable }
|
65 | `
|
66 |
|
67 | fs.writeFileSync(path.join(__dirname, '../src/generated-types.ts'), typesTemplate)
|
68 | fs.writeFileSync(path.join(__dirname, '../src/base-table.js'), tableTemplate)
|
69 | }
|
70 |
|
71 | run()
|