UNPKG

1.94 kBJavaScriptView Raw
1'use strict'
2
3const fs = require('fs')
4const path = require('path')
5const http = require('ipfs-utils/src/http')
6const url = 'https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv'
7
8const 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 */
35export type CodecConstant = ${names.map(n => `${n.toUpperCase().replace(/-/g, '_')}`).join(' | ')};
36
37/**
38 * Names for all available codecs
39 */
40export type CodecName = ${names.join(' | ')};
41
42/**
43 * Number for all available codecs
44 */
45export type CodecNumber = ${codes.join(' | ')};
46
47export type ConstantNumberMap = Record<CodecConstant, CodecNumber>
48export type NameUint8ArrayMap = Record<CodecName, Uint8Array>
49export type NumberNameMap = Record<CodecNumber, CodecName>
50export 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 */
60const baseTable = Object.freeze({
61${processed}
62})
63
64module.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
71run()