Commit 5cf646ba authored by Mark Tyneway's avatar Mark Tyneway

hardhat-deploy-config: add getter

Add a getter to the `hre.deployConfig` object
that gives access to alternative network's deploy
configs. This is useful because multiple networks
can be linked and share config. In particular when
L2 networks are used with L1 networks, the entire
deploy config has be be shared with both networks
so it results in a copy paste.
parent 43b70f43
---
'@eth-optimism/hardhat-deploy-config': patch
---
Add getter for other network's deploy config
...@@ -27,32 +27,42 @@ const normalizePath = ( ...@@ -27,32 +27,42 @@ const normalizePath = (
} }
export const loadDeployConfig = (hre: HardhatRuntimeEnvironment): any => { export const loadDeployConfig = (hre: HardhatRuntimeEnvironment): any => {
let config: any const getDeployConfig = (dir: string, network: string): any => {
try { let config: any
const base = `${hre.config.paths.deployConfig}/${hre.network.name}` try {
if (fs.existsSync(`${base}.ts`)) { const base = `${dir}/${network}`
// eslint-disable-next-line @typescript-eslint/no-var-requires if (fs.existsSync(`${base}.ts`)) {
config = require(`${base}.ts`).default // eslint-disable-next-line @typescript-eslint/no-var-requires
} else if (fs.existsSync(`${base}.json`)) { config = require(`${base}.ts`).default
// eslint-disable-next-line @typescript-eslint/no-var-requires } else if (fs.existsSync(`${base}.json`)) {
config = require(`${base}.json`) // eslint-disable-next-line @typescript-eslint/no-var-requires
} else { config = require(`${base}.json`)
throw new Error('not found') } else {
throw new Error('not found')
}
} catch (err) {
throw new Error(
`error while loading deploy config for network: ${hre.network.name}, ${err}`
)
} }
} catch (err) { return config
throw new Error( }
`error while loading deploy config for network: ${hre.network.name}, ${err}`
) const paths = hre.config.paths.deployConfig
const conf = getDeployConfig(paths, hre.network.name)
const spec = parseDeployConfig(hre, conf)
spec.getDeployConfig = (network: string) => {
return getDeployConfig(paths, network)
} }
return new Proxy(parseDeployConfig(hre, config), { return new Proxy(spec, {
get: (target, prop) => { get: (target, prop) => {
if (target.hasOwnProperty(prop)) { if (target.hasOwnProperty(prop)) {
return target[prop] return target[prop]
} }
// Explicitly throw if the property is not found since I can't yet figure out a good way to // Explicitly throw if the property is not found
// handle the necessary typings.
throw new Error( throw new Error(
`property does not exist in deploy config: ${String(prop)}` `property does not exist in deploy config: ${String(prop)}`
) )
......
...@@ -24,8 +24,8 @@ declare module 'hardhat/types/config' { ...@@ -24,8 +24,8 @@ declare module 'hardhat/types/config' {
declare module 'hardhat/types/runtime' { declare module 'hardhat/types/runtime' {
interface HardhatRuntimeEnvironment { interface HardhatRuntimeEnvironment {
deployConfig: { deployConfig: {
// TODO: Is there any good way to type this?
[key: string]: any [key: string]: any
getDeployConfig(arg0: string): { [key: string]: any }
} }
} }
} }
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