-
Notifications
You must be signed in to change notification settings - Fork 2
/
_scripts.js
473 lines (461 loc) · 20.2 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/env node
'use strict';
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const {spawn, exec} = require('child_process');
const fs = require('fs');
const commandlineArgs = process.argv.slice(2);
function wait(numSeconds) {
return new Promise(resolve => {
setTimeout(resolve, numSeconds * 1000);
});
}
function getCurrentBranch() {
return new Promise((resolve, reject) => {
if (process.env.VERCEL_GIT_COMMIT_REF) {
return resolve(process.env.VERCEL_GIT_COMMIT_REF);
}
try {
exec('git rev-parse --abbrev-ref HEAD', (error, stdout, stderr) => {
if (error !== null) {
reject('git error: ' + error + stderr);
} else {
resolve(stdout.toString().trim());
}
});
} catch (e) {
reject(e);
}
});
}
async function getNetworkName() {
let networkName = process.env.NETWORK_NAME;
if (!networkName) {
console.log(`no NETWORK_NAME specified`);
try {
let branch = await getCurrentBranch();
console.log(`branch: ${branch}`);
if (branch.indexOf('/') !== -1) {
const components = branch.split('/');
branch = components[0];
} else if (branch.indexOf('_') !== -1) {
const components = branch.split('_');
branch = components[0];
}
if (fs.existsSync(`contracts/deployments/${branch}`)) {
networkName = branch;
}
console.log(`networkName: ${networkName}`);
} catch (e) {
console.error(e);
}
}
// console.log(`networkName: ${networkName}`);
return networkName;
}
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);
});
}
function getEnv(network) {
let env = 'dotenv -e .env -e contracts/.env -- ';
if (network && network !== 'localhost') {
if (fs.existsSync(`.env.web.${network}`) || fs.existsSync(`.env.${network}`)) {
env = `dotenv `;
if (fs.existsSync(`.env.${network}`)) {
env += ` -e .env.${network}`;
}
if (fs.existsSync(`.env.web.${network}`)) {
env += ` -e .env.web.${network}`;
}
env += ` -e .env -e contracts/.env -- `;
}
}
// console.log({ENV: env});
return env;
}
async function performAction(rawArgs) {
const firstArg = rawArgs[0];
const args = rawArgs.slice(1);
// console.log({firstArg, args});
if (firstArg == 'contracts:dev') {
const {fixedArgs, extra, options} = parseArgs(args, 0, {reset: 'boolean'});
if (options.reset) {
await execute('rimraf agent-service/src/contracts.json');
await execute('rimraf contracts/deployments/localhost && rimraf web/src/lib/contracts.json');
}
await execute(
`dotenv -e .env -e contracts/.env -- npm --prefix contracts run dev -- --export ../web/src/lib/contracts.json`
);
} else if (firstArg == 'contracts:node') {
await execute(`dotenv -e .env -e contracts/.env -- npm --prefix contracts run dev:node -- --no-deploy`);
} else if (firstArg == 'contracts:local:dev') {
const {fixedArgs, extra, options} = parseArgs(args, 0, {reset: 'boolean'});
if (options.reset) {
await execute('rimraf agent-service/src/contracts.json');
await execute('rimraf contracts/deployments/localhost && rimraf web/src/lib/contracts.json');
}
console.log(`waiting on local node...`);
await execute(`wait-on tcp:127.0.0.1:8545`);
await performAction([`contracts:execute`, 'localhost', 'contracts/scripts/fundingFromCoinbase.ts']);
await wait(1); // slight delay to ensure ethereum node is actually ready
await execute(
`dotenv -e .env -e contracts/.env -- npm --prefix contracts run local:dev -- --export ../web/src/lib/contracts.json`
);
} else if (firstArg === 'contracts:deploy') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
await execute(
`${env}npm --prefix contracts run deploy ${network} -- --export ../web/src/lib/contracts.json ${extra.join(' ')}`
);
} else if (firstArg === 'contracts:void:deploy') {
await execute(`dotenv -e .env -e contracts/.env -- npm --prefix contracts run void:deploy`);
} else if (firstArg === 'contracts:fork:deploy') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix contracts run fork:deploy ${network} -- ${extra.join(' ')}`);
} else if (firstArg === 'contracts:fork:dev') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix contracts run fork:dev ${network} -- ${extra.join(' ')}`);
} else if (firstArg === 'contracts:fork:node') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix contracts run fork:node ${network} -- ${extra.join(' ')}`);
} else if (firstArg === 'contracts:export') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
if (!network) {
console.error(`need to specify the network as first argument`);
return;
}
const env = getEnv(network);
await execute(`${env}npm --prefix contracts run export ${network} -- ../web/src/lib/contracts.json`);
} else if (firstArg === 'contracts:metadata') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
if (!network) {
console.error(`need to specify the network as first argument`);
return;
}
const env = getEnv(network);
await execute(`${env}npm --prefix contracts run metadata ${network}`);
} else if (firstArg === 'contracts:metadata') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
if (!network) {
console.error(`need to specify the network as first argument`);
return;
}
const env = getEnv(network);
await execute(`${env}npm --prefix contracts run metadata ${network}`);
} else if (firstArg === 'contracts:seed') {
const {fixedArgs, extra, options} = parseArgs(args, 1, {waitContracts: 'boolean'});
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
if (options.waitContracts) {
console.log(`waiting for web/src/lib/contracts.json...`);
await execute(`wait-on web/src/lib/contracts.json`);
}
await execute(`${env}npm --prefix contracts run execute ${network} scripts/seed.ts ${extra.join(' ')}`);
} else if (firstArg === 'contracts:execute') {
const {fixedArgs, extra, options} = parseArgs(args, 1, {waitContracts: 'boolean'});
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
if (options.waitContracts) {
console.log(`waiting for web/src/lib/contracts.json...`);
await execute(`wait-on web/src/lib/contracts.json`);
}
await execute(`${env}npm --prefix contracts run execute ${network} ${extra.join(' ')}`);
} else if (firstArg === 'contracts:fork:execute') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix contracts run fork:execute ${network} ${extra.join(' ')}`);
} else if (firstArg === 'contracts:etherscan-verify') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
if (!network) {
console.error(`need to specify the network as first argument`);
return;
}
const env = getEnv(network);
await execute(
`${env}npm --prefix contracts run hardhat -- --network ${network} etherscan-verify ${extra.join(' ')}`
);
} else if (firstArg === 'contracts:sourcify') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
if (!network) {
console.error(`need to specify the network as first argument`);
return;
}
const env = getEnv(network);
await execute(`${env}npm --prefix contracts run hardhat -- --network ${network} sourcify ${extra.join(' ')}`);
} else if (firstArg === 'tenderly:push') {
const {fixedArgs} = parseArgs(args, 1, {});
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix contracts run tenderly:push ${network}`);
} else if (firstArg === 'subgraph:build') {
await execute(`dotenv -- npm --prefix subgraph run generate ../contracts/deployments/localhost mainnet`);
await execute(`dotenv -- npm --prefix subgraph run codegen`);
await execute(`dotenv -- npm --prefix subgraph run build`);
} else if (firstArg === 'subgraph:dev') {
await execute(`dotenv -- npm --prefix subgraph run setup`);
await execute(`wait-on web/src/lib/contracts.json`);
await execute(`dotenv -- npm --prefix subgraph run dev ../contracts/deployments/localhost mainnet`);
} else if (firstArg === 'subgraph:deploy') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
let deployCommand = 'deploy';
if (network && network !== 'localhost') {
deployCommand = 'hosted:deploy';
}
await execute(`wait-on web/src/lib/contracts.json`);
await execute(`${env}npm --prefix subgraph run ${deployCommand} ../contracts/deployments/${network}`);
} else if (firstArg === 'subgraph:etherplay') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
const env = getEnv(network);
if (network === 'localhost') {
throw new Error(`can't be on localhost`);
}
await execute(`wait-on web/src/lib/contracts.json`);
await execute(`${env}npm --prefix subgraph run etherplay:deploy ../contracts/deployments/${network}`);
} else if (firstArg === 'agent-service:dev') {
console.log(`waiting for web/src/lib/contracts.json...`);
await execute(`wait-on web/src/lib/contracts.json`);
const {fixedArgs} = parseArgs(args, 1);
const network = fixedArgs[0] || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix agent-service run dev ../contracts/deployments/localhost`);
} else if (firstArg === 'agent-service:build') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || (await getNetworkName()) || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix agent-service run build ../contracts/deployments/${network}`);
} else if (firstArg === 'agent-service:deploy') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
if (!network) {
console.error(`need to specify the network as first argument`);
return;
}
const env = getEnv(network);
// await performAction(['agent-service:build', network]);
await execute(`${env}npm --prefix agent-service run deploy ../contracts/deployments/${network}`);
} else if (firstArg === 'account-service:dev') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || (await getNetworkName()) || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix account-service run dev ${network}`);
} else if (firstArg === 'account-service:build') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || (await getNetworkName()) || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix account-service run build ${network}`);
} else if (firstArg === 'account-service:deploy') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || (await getNetworkName()) || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix account-service run deploy ${network}`);
} else if (firstArg === 'web:dev') {
const {fixedArgs, options, extra} = parseArgs(args, 1, {
skipContracts: 'boolean',
waitContracts: 'boolean',
inspect: 'boolean'
});
const network = fixedArgs[0] || 'localhost';
if (!options.skipContracts) {
await performAction(['contracts:export', network]);
}
if (options.waitContracts) {
await execute(`wait-on web/src/lib/contracts.json`);
}
const env = getEnv(network);
if (options.inspect) {
await execute(`${env}npm --prefix web run dev:inspect -- ${extra.join(' ')}`);
} else {
await execute(`${env}npm --prefix web run dev -- ${extra.join(' ')}`);
}
} else if (firstArg === 'web:build') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || (await getNetworkName()) || 'localhost';
const env = getEnv(network);
await execute(`${env}npm --prefix web run prepare`);
await performAction(['contracts:export', network || 'localhost']);
await execute(`${env}npm run common:build`);
await execute(`${env}npm --prefix web run build`);
} else if (firstArg === 'web:build:generic') {
require('dotenv').config({path: '.env.web.generic'});
process.env.MODE = 'dev';
await execute(`npm --prefix contracts run void:deploy -- --export ../web/src/lib/contracts.json`);
await execute(`npm --prefix web run prepare`);
await execute(`npm run common:build`);
await execute(`npm --prefix web run build`);
} else if (firstArg === 'web:serve') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
const env = getEnv(network);
await execute(`${env}npm --prefix web run serve`);
} else if (firstArg === 'web:build:serve') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || 'localhost';
await performAction(['web:build', network || 'localhost']);
await performAction(['web:serve', network || 'localhost']);
} else if (firstArg === 'web:deploy') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0];
if (!network) {
console.error(`need to specify the network as first argument`);
return;
}
const env = getEnv(network);
await performAction(['web:build', network]);
await execute(`${env}npm --prefix web run deploy`);
} else if (firstArg === 'deploy') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || (await getNetworkName());
if (!network) {
console.error(`need to specify the network as first argument (or via env: NETWORK_NAME)`);
return;
}
await performAction(['contracts:deploy', network]);
await performAction(['subgraph:deploy', network]);
await performAction(['agent-service:deploy', network]);
await performAction(['account-service:deploy', network]);
await performAction(['web:deploy', network]);
} else if (firstArg === 'deploy:noweb') {
const {fixedArgs, extra} = parseArgs(args, 1, {});
const network = fixedArgs[0] || (await getNetworkName());
if (!network) {
console.error(`need to specify the network as first argument (or via env: NETWORK_NAME)`);
return;
}
await performAction(['contracts:deploy', network]);
await performAction(['subgraph:deploy', network]);
await performAction(['agent-service:deploy', network]);
await performAction(['account-service:deploy', network]);
} else if (firstArg === 'stop') {
await execute(`docker compose down -v --remove-orphans`);
} else if (firstArg === 'externals') {
await execute(`docker compose down -v --remove-orphans`);
await execute(`docker compose up`);
} else if (firstArg === 'externals:geth') {
await execute(`docker compose down -v --remove-orphans`);
await execute(`docker compose -f docker-compose.yml -f docker-compose.geth.yml up`);
} else if (firstArg === 'dev') {
const {extra} = parseArgs(args, 0, {});
execute(`newsh "npm run common:dev"`);
execute(`newsh "npm run web:dev localhost -- --skipContracts --waitContracts ${extra.join(' ')}"`);
execute(`newsh "npm run agent-service:dev"`);
execute(`newsh "npm run account-service:dev"`);
execute(`newsh "npm run contracts:node"`);
execute(`newsh "npm run contracts:local:dev -- --reset"`);
execute(`newsh "npm run subgraph:dev"`);
await performAction(['common:build']);
await performAction(['contracts:seed', 'localhost', '--waitContracts']);
} else if (firstArg === 'start') {
const {extra} = parseArgs(args, 0, {});
await execute(`docker compose down -v --remove-orphans`); // required else we run in race conditions
execute(`newsh "npm run externals"`);
execute(`newsh "npm run common:dev"`);
execute(`newsh "npm run web:dev localhost -- --skipContracts --waitContracts ${extra.join(' ')}"`);
execute(`newsh "npm run agent-service:dev"`);
execute(`newsh "npm run account-service:dev"`);
execute(`newsh "npm run contracts:node"`);
execute(`newsh "npm run contracts:local:dev -- --reset"`);
execute(`newsh "npm run subgraph:dev"`);
await performAction(['common:build']);
await performAction(['contracts:seed', 'localhost', '--waitContracts']);
} else if (firstArg === 'start:nonode') {
const {extra} = parseArgs(args, 0, {});
await execute(`docker compose down -v --remove-orphans`); // required else we run in race conditions
execute(`newsh "npm run externals"`);
execute(`newsh "npm run common:dev"`);
execute(`newsh "npm run web:dev localhost -- --skipContracts --waitContracts ${extra.join(' ')}"`);
execute(`newsh "npm run agent-service:dev"`);
execute(`newsh "npm run account-service:dev"`);
execute(`newsh "npm run subgraph:dev"`);
await performAction(['common:build']);
await performAction(['contracts:seed', 'localhost', '--waitContracts']);
} else if (firstArg === 'start:nonode') {
const {extra} = parseArgs(args, 0, {});
await execute(`docker compose down -v --remove-orphans`); // required else we run in race conditions
execute(`newsh "npm run externals"`);
execute(`newsh "npm run common:dev"`);
execute(`newsh "npm run web:dev localhost -- --skipContracts --waitContracts ${extra.join(' ')}"`);
execute(`newsh "npm run agent-service:dev"`);
execute(`newsh "npm run account-service:dev"`);
execute(`newsh "npm run subgraph:dev"`);
await performAction(['common:build']);
await performAction(['contracts:seed', 'localhost', '--waitContracts']);
} else if (firstArg === 'start:geth') {
const {extra} = parseArgs(args, 0, {});
await execute(`docker compose down -v --remove-orphans`); // required else we run in race conditions
execute(`newsh "npm run externals:geth"`);
execute(`newsh "npm run common:dev"`);
execute(`newsh "npm run web:dev localhost -- --skipContracts --waitContracts ${extra.join(' ')}"`);
execute(`newsh "npm run agent-service:dev"`);
execute(`newsh "npm run account-service:dev"`);
execute(`newsh "npm run contracts:local:dev -- --reset"`);
execute(`newsh "npm run subgraph:dev"`);
await performAction(['common:build']);
await performAction(['contracts:seed', 'localhost', '--waitContracts']);
}
}
performAction(commandlineArgs);