-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
66 lines (54 loc) · 2.46 KB
/
index.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import fs from 'fs'
import path from 'path'
import { homedir } from 'os'
import deepmerge from 'deepmerge'
import { extendConfig } from 'hardhat/config'
import { HardhatConfig, NetworkConfig, NetworksConfig, HardhatUserConfig } from 'hardhat/types'
const HARDHAT_CONFIG_DIR = '.hardhat'
const HARDHAT_NETWORK_CONFIG_FILE = 'networks.json'
export interface LocalNetworksConfig {
networks: NetworksConfig
defaultConfig: NetworkConfig
}
extendConfig((hardhatConfig: HardhatConfig, userConfig: HardhatUserConfig): void => {
const localNetworksConfig = readLocalNetworksConfig(userConfig)
const userNetworkConfigs = userConfig.networks || []
Object.entries(userNetworkConfigs).forEach(([networkName, userNetworkConfig]) => {
hardhatConfig.networks[networkName] = (deepmerge.all([
hardhatConfig.networks[networkName] || {},
localNetworksConfig.defaultConfig,
localNetworksConfig.networks[networkName] || {},
userNetworkConfig as object
]) as NetworkConfig)
})
Object.entries(localNetworksConfig.networks).forEach(([networkName, localNetworkConfig]) => {
if (!hardhatConfig.networks[networkName]) {
hardhatConfig.networks[networkName] = (deepmerge.all([
hardhatConfig.networks[networkName] || {},
localNetworksConfig.defaultConfig,
localNetworkConfig
]) as NetworkConfig)
}
})
});
export function readLocalNetworksConfig(userConfig: HardhatUserConfig): LocalNetworksConfig {
const localNetworksConfigPath = parseLocalNetworksConfigPath(userConfig)
const localNetworksConfig = localNetworksConfigPath ? require(localNetworksConfigPath) : {}
if (!localNetworksConfig.networks) localNetworksConfig.networks = []
if (!localNetworksConfig.defaultConfig) localNetworksConfig.defaultConfig = {}
return localNetworksConfig
}
export function parseLocalNetworksConfigPath(userConfig: HardhatUserConfig): string | undefined {
const localNetworksConfigPath = userConfig.localNetworksConfig
if (typeof localNetworksConfigPath === 'string' && fs.existsSync(localNetworksConfigPath)) {
return localNetworksConfigPath
}
const defaultLocalNetworksConfigPath = getDefaultLocalNetworksConfigPath()
return fs.existsSync(defaultLocalNetworksConfigPath) ? defaultLocalNetworksConfigPath : undefined
}
export function getDefaultLocalNetworksConfigPath() {
return path.join(getLocalConfigDir(), HARDHAT_NETWORK_CONFIG_FILE)
}
export function getLocalConfigDir() {
return path.join(homedir(), HARDHAT_CONFIG_DIR)
}