Commit 705d5670 authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

Merge pull request #1770 from ethereum-optimism/sc/gen-artifacts-ts

feat: rewrite generate-artifacts in ts
parents a87e3d0d 8d024666
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
"build:contracts": "hardhat compile --show-stack-traces", "build:contracts": "hardhat compile --show-stack-traces",
"build:dump": "ts-node bin/take-dump.ts", "build:dump": "ts-node bin/take-dump.ts",
"autogen:markdown": "node scripts/generate-markdown.js", "autogen:markdown": "node scripts/generate-markdown.js",
"autogen:artifacts": "node scripts/generate-artifacts.js && ts-node scripts/generate-deployed-artifacts.ts", "autogen:artifacts": "ts-node scripts/generate-artifacts.ts && ts-node scripts/generate-deployed-artifacts.ts",
"test": "yarn test:contracts", "test": "yarn test:contracts",
"test:contracts": "hardhat test --show-stack-traces", "test:contracts": "hardhat test --show-stack-traces",
"test:coverage": "NODE_OPTIONS=--max_old_space_size=8192 hardhat coverage && istanbul check-coverage --statements 88 --branches 76 --functions 84 --lines 88", "test:coverage": "NODE_OPTIONS=--max_old_space_size=8192 hardhat coverage && istanbul check-coverage --statements 88 --branches 76 --functions 84 --lines 88",
......
#!/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')}
export const getContractArtifact = (name: string): any => {
return {
${evmArtifactPaths
.map((artifactPath) => {
const artifact = require(artifactPath)
return `${artifact.contractName}: ${artifact.contractName}`
})
.join(',\n')}
}[name]
}
`
fs.writeFileSync(`./src/contract-artifacts.ts`, content)
})().catch(console.error)
import path from 'path'
import glob from 'glob'
import fs from 'fs'
/**
* Script for automatically generating a file which has a series of `require` statements for
* importing JSON contract artifacts. We do this to preserve browser compatibility.
*/
const main = async () => {
const contractArtifactsFolder = path.resolve(
__dirname,
`../artifacts/contracts`
)
const artifactPaths = glob
.sync(`${contractArtifactsFolder}/**/*.json`)
.filter((match) => {
// Filter out the debug outputs.
return !match.endsWith('.dbg.json')
})
const content = `
/* eslint-disable @typescript-eslint/no-var-requires, no-empty */
/*
THIS FILE IS AUTOMATICALLY GENERATED.
DO NOT EDIT.
*/
${artifactPaths
.map((artifactPath) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const artifact = require(artifactPath)
const relPath = path.relative(__dirname, artifactPath)
return `
let ${artifact.contractName}
try {
${artifact.contractName} = require('${relPath}')
} catch {}
`
})
.join('\n')}
export const getContractArtifact = (name: string): any => {
return {
${artifactPaths
.map((artifactPath) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const artifact = require(artifactPath)
return `${artifact.contractName}`
})
.join(',\n')}
}[name]
}
`
fs.writeFileSync(
path.resolve(__dirname, `../src/contract-artifacts.ts`),
content
)
}
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment