-
Notifications
You must be signed in to change notification settings - Fork 48
/
_scripts.js
162 lines (157 loc) · 5.04 KB
/
_scripts.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
'use strict'
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const { spawn } = require('child_process')
require('dotenv').config()
const commandlineArgs = process.argv.slice(2)
function parseArgs(rawArgs, numFixedArgs, expectedOptions) {
const fixedArgs = []
const options = {}
const extra = []
const alreadyCounted = {}
for (let i = 0; i < rawArgs.length; i++) {
const rawArg = rawArgs[i]
if (rawArg.startsWith('--')) {
const optionName = rawArg.slice(2)
const optionDetected = expectedOptions[optionName]
if (!alreadyCounted[optionName] && optionDetected) {
alreadyCounted[optionName] = true
if (optionDetected === 'boolean') {
options[optionName] = true
} else {
i++
options[optionName] = rawArgs[i]
}
} else {
if (fixedArgs.length < numFixedArgs) {
throw new Error(
`expected ${numFixedArgs} fixed args, got only ${fixedArgs.length}`
)
} else {
extra.push(rawArg)
}
}
} else {
if (fixedArgs.length < numFixedArgs) {
fixedArgs.push(rawArg)
} else {
for (const opt of Object.keys(expectedOptions)) {
alreadyCounted[opt] = true
}
extra.push(rawArg)
}
}
}
return { options, extra, fixedArgs }
}
function execute(command) {
return new Promise((resolve, reject) => {
const onExit = (error) => {
if (error) {
return reject(error)
}
resolve()
}
spawn(command.split(' ')[0], command.split(' ').slice(1), {
stdio: 'inherit',
shell: true,
}).on('exit', onExit)
})
}
async function performAction(rawArgs) {
const firstArg = rawArgs[0]
const args = rawArgs.slice(1)
if (firstArg === 'run') {
const { fixedArgs, extra } = parseArgs(args, 2, {})
await execute(
`cross-env HARDHAT_DEPLOY_LOG=true HARDHAT_NETWORK=${
fixedArgs[0]
} ts-node --files ${fixedArgs[1]} ${extra.join(' ')}`
)
} else if (firstArg === 'deploy') {
const { fixedArgs, extra } = parseArgs(args, 1, {})
await execute(`hardhat --network ${fixedArgs[0]} deploy ${extra.join(' ')}`)
} else if (firstArg === 'export') {
const { fixedArgs } = parseArgs(args, 2, {})
await execute(
`hardhat --network ${fixedArgs[0]} export --export ${fixedArgs[1]}`
)
} else if (firstArg === 'fork:run') {
const { fixedArgs, options, extra } = parseArgs(args, 2, {
deploy: 'boolean',
blockNumber: 'string',
'no-impersonation': 'boolean',
})
await execute(
`cross-env ${
options.deploy ? 'HARDHAT_DEPLOY_FIXTURE=true' : ''
} HARDHAT_DEPLOY_LOG=true HARDHAT_FORK=${fixedArgs[0]} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} ts-node --files ${fixedArgs[1]} ${extra.join(' ')}`
)
} else if (firstArg === 'fork:deploy') {
const { fixedArgs, options, extra } = parseArgs(args, 1, {
blockNumber: 'string',
'no-impersonation': 'boolean',
})
await execute(
`cross-env HARDHAT_FORK=${fixedArgs[0]} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} hardhat deploy ${extra.join(' ')}`
)
} else if (firstArg === 'fork:node') {
const { fixedArgs, options, extra } = parseArgs(args, 1, {
blockNumber: 'string',
'no-impersonation': 'boolean',
})
await execute(
`cross-env HARDHAT_FORK=${fixedArgs[0]} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} hardhat node ${extra.join(' ')}`
)
} else if (firstArg === 'fork:test') {
const { fixedArgs, options, extra } = parseArgs(args, 1, {
blockNumber: 'string',
'no-impersonation': 'boolean',
})
await execute(
`cross-env HARDHAT_FORK=${fixedArgs[0]} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} HARDHAT_DEPLOY_FIXTURE=true HARDHAT_COMPILE=true mocha --bail --recursive test ${extra.join(
' '
)}`
)
} else if (firstArg === 'fork:dev') {
const { fixedArgs, options, extra } = parseArgs(args, 1, {
blockNumber: 'string',
'no-impersonation': 'boolean',
})
await execute(
`cross-env HARDHAT_FORK=${fixedArgs[0]} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} hardhat node --watch --export contractsInfo.json ${extra.join(' ')}`
)
}
}
performAction(commandlineArgs)