generated from wighawag/template-on-chain-art
-
Notifications
You must be signed in to change notification settings - Fork 2
/
_scripts.js
203 lines (195 loc) · 6.86 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env node
'use strict';
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const {spawn} = require('child_process');
const path = require('path');
require('dotenv').config();
const fs = require('fs');
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, stdioToFile, options) {
return new Promise((resolve, reject) => {
const onExit = (error) => {
if (error) {
return reject(error);
}
resolve();
};
let writeStream;
if (stdioToFile) {
writeStream = fs.createWriteStream(stdioToFile, 'utf-8');
}
const shell = spawn(command.split(' ')[0], command.split(' ').slice(1), {
stdio: stdioToFile ? undefined : 'inherit',
shell: options ? options.shell : true,
detached: options ? options.detached : undefined,
}).on('exit', onExit);
if (writeStream) {
shell.stdout.pipe(writeStream);
}
});
}
async function performAction(rawArgs) {
const firstArg = rawArgs[0];
const args = rawArgs.slice(1);
if (firstArg === 'run') {
const {fixedArgs, extra} = parseArgs(args, 2, {});
let filepath = fixedArgs[1];
const folder = path.basename(__dirname);
if (filepath.startsWith(folder + '/') || filepath.startsWith(folder + '\\')) {
filepath = filepath.slice(folder.length + 1);
}
await execute(
`cross-env HARDHAT_DEPLOY_LOG=true HARDHAT_NETWORK=${fixedArgs[0]} ts-node --files ${filepath} ${extra.join(
' '
)}`
);
} else if (firstArg === 'geth') {
await execute(`docker-compose down -v --remove-orphans`);
execute(`docker-compose up`, 'geth.log', {shell: false});
await execute(`wait-on tcp:localhost:8545`);
await performAction([`run`, 'localhost', 'scripts/fundingFromCoinbase.ts']);
} else if (firstArg === 'geth:stop') {
await execute(`docker-compose down -v --remove-orphans`);
} else if (firstArg === 'geth:dev') {
try {
await execute(`docker-compose down -v --remove-orphans`).catch((e) => console.log(e));
} catch (err) {
console.error(`down error`, err);
}
try {
execute(`docker-compose up`, 'geth.log', {shell: false}).catch((e) => console.log(e));
} catch (err) {
console.error(`up error`, err);
}
await execute(`wait-on tcp:localhost:8545`);
await performAction([`run`, 'localhost', 'scripts/fundingFromCoinbase.ts']);
try {
fs.rmSync('deployments/localhost', {recursive: true});
} catch (err) {}
execute(`npm run serve`, 'web.log', {shell: false});
await execute(`npm run local:dev`);
} else if (firstArg === 'deploy') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
await execute(`hardhat --network ${fixedArgs[0]} deploy --report-gas ${extra.join(' ')}`);
} else if (firstArg === 'verify') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
if (!network) {
console.error(`need to specify the network as first argument`);
return;
}
await execute(`hardhat --network ${network} etherscan-verify ${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',
});
let filepath = fixedArgs[1];
const folder = path.basename(__dirname);
if (filepath.startsWith(folder + '/') || filepath.startsWith(folder + '\\')) {
filepath = filepath.slice(folder.length + 1);
}
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 ${filepath} ${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 --report-gas ${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 --hostname 0.0.0.0 ${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 --hostname 0.0.0.0 --watch --export contractsInfo.json ${extra.join(' ')}`
);
} else if (firstArg === 'tenderly:push') {
const {fixedArgs} = parseArgs(args, 1, {});
await execute(`hardhat --network ${fixedArgs[0]} tenderly:push`);
}
}
performAction(commandlineArgs);