generated from wighawag/template-ethereum-contracts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
scripts.js
154 lines (150 loc) · 4.63 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
#!/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 rawArgs = process.argv.slice(2);
function parseArgs(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} fied agrs, 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 () => {
const firstArg = rawArgs[0];
if (firstArg === 'run') {
const {fixedArgs, extra} = parseArgs(3, {});
await execute(
`cross-env HARDHAT_NETWORK=${fixedArgs[1]} ts-node --files ${
fixedArgs[2]
} ${extra.join(' ')}`
);
} else if (firstArg === 'deploy') {
const {fixedArgs, extra} = parseArgs(2, {});
await execute(
`hardhat --network ${fixedArgs[1]} deploy ${extra.join(' ')}`
);
} else if (firstArg === 'export') {
const {fixedArgs} = parseArgs(3, {});
await execute(
`hardhat --network ${fixedArgs[1]} export --export ${fixedArgs[2]}`
);
} else if (firstArg === 'fork:run') {
const {fixedArgs, options, extra} = parseArgs(3, {
deploy: 'boolean',
blockNumber: 'string',
'no-impersonation': 'boolean',
});
await execute(
`cross-env ${
options.deploy ? 'HARDHAT_DEPLOY_FIXTURE=true' : ''
} HARDHAT_DEPLOY_ACCOUNTS_NETWORK=${fixedArgs[1]} HARDHAT_FORK=${
fixedArgs[1]
} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} ts-node --files ${fixedArgs[2]} ${extra.join(' ')}`
);
} else if (firstArg === 'fork:deploy') {
const {fixedArgs, options, extra} = parseArgs(2, {
blockNumber: 'string',
'no-impersonation': 'boolean',
});
await execute(
`cross-env HARDHAT_DEPLOY_ACCOUNTS_NETWORK=${fixedArgs[1]} HARDHAT_FORK=${
fixedArgs[1]
} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} hardhat deploy ${extra.join(' ')}`
);
} else if (firstArg === 'fork:test') {
const {fixedArgs, options, extra} = parseArgs(2, {
blockNumber: 'string',
'no-impersonation': 'boolean',
});
await execute(
`cross-env HARDHAT_DEPLOY_ACCOUNTS_NETWORK=${fixedArgs[1]} HARDHAT_FORK=${
fixedArgs[1]
} ${
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(2, {
'no-impersonation': 'boolean',
});
await execute(
`cross-env HARDHAT_DEPLOY_ACCOUNTS_NETWORK=${fixedArgs[1]} HARDHAT_FORK=${
fixedArgs[1]
} ${
options.blockNumber ? `HARDHAT_FORK_NUMBER=${options.blockNumber}` : ''
} ${
options['no-impersonation']
? `HARDHAT_DEPLOY_NO_IMPERSONATION=true`
: ''
} hardhat node --watch --export contractsInfo.json ${extra.join(' ')}`
);
}
})();