-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
77 lines (62 loc) · 2.94 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
67
68
69
70
71
72
73
74
75
76
77
import fs from 'fs'
import path from 'path'
import deepmerge from 'deepmerge'
import { homedir } from 'os'
import { extendConfig } from 'hardhat/config'
import { HardhatPluginError } from 'hardhat/plugins'
import { HardhatConfig, NetworkConfig, NetworksConfig, HardhatUserConfig } from 'hardhat/types'
// This import is needed to let the TypeScript compiler know that it should include your type
// extensions in your npm package's types file.
import './type-extensions'
const HARDHAT_CONFIG_DIR = '.hardhat'
const HARDHAT_NETWORK_DEFAULT_CONFIG_FILES = ['networks.json', 'networks.js', 'networks.ts']
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 foundPaths = getDefaultLocalNetworksConfigPaths().filter(fs.existsSync)
if (foundPaths.length > 1) fail(`Multiple default config files found: ${foundPaths.join(', ')}. Please pick one.`)
return foundPaths.length === 1 ? foundPaths[0] : undefined
}
export function getDefaultLocalNetworksConfigPaths() {
return HARDHAT_NETWORK_DEFAULT_CONFIG_FILES.map(file => path.join(getLocalConfigDir(), file))
}
export function getLocalConfigDir() {
return path.join(homedir(), HARDHAT_CONFIG_DIR)
}
function fail(message: string): void {
throw new HardhatPluginError('hardhat-local-networks-config-plugin', message)
}