|
| 1 | +// use this script as 'node ./generate-checksum-table.js ./binaries' where Neuron binaries sit in ./binaries |
| 2 | +const fs = require('node:fs') |
| 3 | +const path = require('node:path') |
| 4 | +const crypto = require('node:crypto') |
| 5 | + |
| 6 | +class Package { |
| 7 | + _name |
| 8 | + _directory |
| 9 | + |
| 10 | + get arch() { |
| 11 | + const name = this._name |
| 12 | + if (name.endsWith('.exe') || name.includes('-x64') || name.includes('x86_64')) { |
| 13 | + return 'x64' |
| 14 | + } |
| 15 | + |
| 16 | + if (name.includes('-arm64')) { |
| 17 | + return 'arm64' |
| 18 | + } |
| 19 | + |
| 20 | + throw new Error(`Unknown arch: ${name}`) |
| 21 | + } |
| 22 | + |
| 23 | + get os() { |
| 24 | + const name = this._name |
| 25 | + if (name.endsWith('.exe')) { |
| 26 | + return 'Windows' |
| 27 | + } |
| 28 | + |
| 29 | + if (name.includes('-mac-') || name.endsWith('.dmg')) { |
| 30 | + return 'macOS' |
| 31 | + } |
| 32 | + |
| 33 | + if (name.endsWith('.AppImage')) { |
| 34 | + return 'Linux' |
| 35 | + } |
| 36 | + |
| 37 | + throw new Error(`Unknown OS: ${name}`) |
| 38 | + } |
| 39 | + |
| 40 | + get package() { |
| 41 | + const ext = path.extname(this._name).slice(1) |
| 42 | + if (ext === 'dmg') return 'DMG' |
| 43 | + return ext |
| 44 | + } |
| 45 | + |
| 46 | + get url() { |
| 47 | + const version = this._name.split('-')[1] |
| 48 | + return `https://github.com/nervosnetwork/neuron/releases/download/${version}/${this._name}` |
| 49 | + } |
| 50 | + |
| 51 | + get checksum() { |
| 52 | + const binary = fs.readFileSync(path.join(this._directory, this._name)) |
| 53 | + const hash = crypto.createHash('sha256') |
| 54 | + hash.update(binary) |
| 55 | + return hash.digest('hex') |
| 56 | + } |
| 57 | + |
| 58 | + constructor(directory, name) { |
| 59 | + this._directory = directory |
| 60 | + this._name = name |
| 61 | + } |
| 62 | + |
| 63 | + toEntry() { |
| 64 | + return `${this.os} | ${this.arch} | [${this.package}](${this.url}) | <code>${this.checksum}</code>\n` |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const getChecksumTable = (directory) => { |
| 69 | + let table = `OS | Arch | Package | SHA256 Checksum\n-- | -- | -- | --\n` |
| 70 | + |
| 71 | + const files = fs.readdirSync(directory).filter((f) => ['.dmg', '.zip', '.exe', '.AppImage'].includes(path.extname(f))) |
| 72 | + files |
| 73 | + .map((f) => { |
| 74 | + return new Package(directory, f) |
| 75 | + }) |
| 76 | + .sort((a, b) => { |
| 77 | + if (a.os !== b.os) { |
| 78 | + if (a.os === 'Windows') return -1 |
| 79 | + if (a.os === 'Linux') return 1 |
| 80 | + } |
| 81 | + |
| 82 | + if (a.package !== b.package) { |
| 83 | + return a.package === 'zip' ? -1 : 1 |
| 84 | + } |
| 85 | + |
| 86 | + if (a.arch !== b.arch) { |
| 87 | + return a.arch === 'x64' ? -1 : 1 |
| 88 | + } |
| 89 | + }) |
| 90 | + .forEach((p) => { |
| 91 | + table += p.toEntry() |
| 92 | + }) |
| 93 | + |
| 94 | + return table |
| 95 | +} |
| 96 | + |
| 97 | +if (process.argv.length < 3) { |
| 98 | + throw new Error(`Directory of binaries is required, use command as 'node ./generate-checksum-table.js ./binaries`) |
| 99 | +} |
| 100 | + |
| 101 | +const directory = process.argv[2] |
| 102 | + |
| 103 | +const checksumTable = getChecksumTable(directory) |
| 104 | + |
| 105 | +console.info(checksumTable) |
| 106 | + |
| 107 | +const output = path.join(directory, './checksums.txt') |
| 108 | +fs.writeFileSync(output, checksumTable, { flag: 'wx' }) |
| 109 | + |
| 110 | +console.info(`Checksum table has been generated at ${output}`) |
0 commit comments