|
| 1 | +import crypto from 'crypto'; |
| 2 | +import axios from 'axios'; |
| 3 | +import handlebars from 'handlebars'; |
| 4 | + |
| 5 | +/** |
| 6 | + * The main package url. |
| 7 | + */ |
| 8 | +const URL = 'https://registry.npmjs.org/@mongosh/cli-repl'; |
| 9 | + |
| 10 | +/** |
| 11 | + * The homebrew formula template. |
| 12 | + */ |
| 13 | +const TEMPLATE = ` |
| 14 | +require "language/node" |
| 15 | +
|
| 16 | +class Mongosh < Formula |
| 17 | + desc "The MongoDB Shell" |
| 18 | + homepage "https://github.com/mongodb-js/mongosh#readme" |
| 19 | + url "{{tarball}}" |
| 20 | + version "{{version}}" |
| 21 | + sha256 "{{sha}}" |
| 22 | +
|
| 23 | + depends_on "node" |
| 24 | +
|
| 25 | + def install |
| 26 | + system "npm", "install", *Language::Node.std_npm_install_args(libexec) |
| 27 | + bin.install_symlink Dir["#{libexec}/bin/*"] |
| 28 | + end |
| 29 | +
|
| 30 | + test do |
| 31 | + system "#{bin}/mongosh --version" |
| 32 | + end |
| 33 | +end`; |
| 34 | + |
| 35 | +const getNpmMetadata = async() => { |
| 36 | + const { data } = await axios.get(URL); |
| 37 | + return data; |
| 38 | +}; |
| 39 | + |
| 40 | +const getSha256 = async(sha: string, tarball: string) => { |
| 41 | + return new Promise(async(resolve, reject) => { |
| 42 | + if (sha.length === 64) { |
| 43 | + resolve(sha); |
| 44 | + } else { |
| 45 | + const hash = crypto.createHash('sha256'); |
| 46 | + const { data } = await axios.get(tarball); |
| 47 | + console.log(data); |
| 48 | + |
| 49 | + data.on('readable', () => { |
| 50 | + const input = data.read(); |
| 51 | + if (input) { |
| 52 | + hash.update(input); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + data.on('end', () => { |
| 57 | + resolve(hash.digest('hex')); |
| 58 | + }); |
| 59 | + |
| 60 | + data.on('error', reject); |
| 61 | + } |
| 62 | + }); |
| 63 | +}; |
| 64 | + |
| 65 | +const generateFormula = async() => { |
| 66 | + const data = await getNpmMetadata(); |
| 67 | + const template = handlebars.compile(TEMPLATE); |
| 68 | + const version = data['dist-tags'].latest; |
| 69 | + const tarball = data.versions[version].dist.tarball; |
| 70 | + const sha = data.versions[version].dist.shasum; |
| 71 | + const rendered = template({ |
| 72 | + tarball: tarball, |
| 73 | + version: version, |
| 74 | + sha: await getSha256(sha, tarball) |
| 75 | + }); |
| 76 | + console.log('mongosh: created homebrew template:', rendered); |
| 77 | + return rendered; |
| 78 | +}; |
0 commit comments