generate-artifacts.js 1.31 KB
Newer Older
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
#!/usr/bin/env node
const path = require('path')
const glob = require('glob')
const fs = require('fs')

;(async () => {
  console.log(`writing contract artifacts typescript file`)

  const getContractJsonFiles = (artifactsFolder) => {
    return glob.sync(`${artifactsFolder}/**/*.json`).filter((match) => {
      return !match.endsWith('.dbg.json')
    })
  }

  const evmArtifactPaths = getContractJsonFiles(
    path.resolve(__dirname, `../artifacts/contracts`)
  )

  const content = `
  /* eslint-disable @typescript-eslint/no-var-requires, no-empty */
  /*
  THIS FILE IS AUTOMATICALLY GENERATED.
  DO NOT EDIT.
  */

  ${evmArtifactPaths
    .map((artifactPath) => {
      const artifact = require(artifactPath)
      const relPath = path.relative(__dirname, artifactPath)
      return `
      let ${artifact.contractName}
      try {
        ${artifact.contractName} = require('${relPath}')
      } catch {}
      `
    })
    .join('\n')}

39 40 41 42 43 44 45 46 47
  export const getContractArtifact = (name: string): any => {
    return {
      ${evmArtifactPaths
        .map((artifactPath) => {
          const artifact = require(artifactPath)
          return `${artifact.contractName}: ${artifact.contractName}`
        })
        .join(',\n')}
    }[name]
48 49 50 51 52
  }
  `

  fs.writeFileSync(`./src/contract-artifacts.ts`, content)
})().catch(console.error)