-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·137 lines (120 loc) · 3.35 KB
/
index.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
#!/usr/bin/env node
/* eslint-disable no-console */
const chalk = require('chalk');
const fs = require('fs');
const stringify = require('csv-stringify');
const { program } = require('commander');
const lib = require('./lib.js');
const pkg = require('./package.json');
// Utility methods to store tabular data
const convertToCsv = (data, header) => {
return new Promise((resolve, reject) => {
stringify(
data,
{
header: header != null ? header : true,
quoted: true,
},
(error, output) => {
if (error) {
reject(error);
} else {
resolve(output);
}
},
);
});
};
const convertToJson = (data) => {
return new Promise((resolve, reject) => {
try {
resolve(JSON.stringify(data));
} catch {
reject(new Error('Invalid JSON'));
}
});
};
// Main method executing the analysis command
async function main() {
const { analyses, configuration } = lib.___;
let result;
program
.storeOptionsAsProperties()
.version(pkg.version)
.description(pkg.description)
.option(
'-e, --endpoint <url>',
'graphQL subgraph endpoint',
configuration.endpoint,
)
.option(
'-f, --format <csv|json>',
'file format of output file',
configuration.format,
)
.option('-o, --output <path>', 'optional file output for tabular data')
.option(
'-s, --relayer_address <str>',
'address of relayer funder',
configuration.relayerAddress,
);
Object.keys(analyses).forEach((name) => {
program
.command(name)
.description(analyses[name].description)
.action(async (_, options, commands) => {
console.log(
chalk.bold(`Analyse "${name}" (${analyses[name].description}):`),
);
// Set configuration
lib.setConfiguration({
endpoint: options.parent.endpoint,
format: options.parent.format,
relayerAddress: options.parent.relayer_address,
log: console.log,
});
// Execute command!
try {
result = await analyses[name].command(options);
} catch (error) {
console.error(chalk.red(error));
process.exit(1);
}
});
});
await program.parseAsync(process.argv);
if (!result) {
console.error(chalk.red('Error: Invalid result!'));
process.exit(1);
}
console.log(`Done processing ${result.length} data entries total!`);
if (program.output) {
fs.unlink(program.output, (err) => {
if (err) {
console.log(`File ${program.output} didn't exist.`);
} else {
console.log(`File ${program.output} was deleted.`);
}
});
let writeHeader = true;
try {
const pagination = 1000;
const convertFunc = configuration.format === 'json' ? convertToJson : convertToCsv;
var a = result;
while(a.length) {
const fileContent = await convertFunc(a.splice(0, pagination), writeHeader); //This would process certain amount of elements at a time
fs.appendFile(program.output, fileContent, (error) => {
if (error) {
throw error;
}
console.log(`Stored ${pagination} results in ${program.output}`);
});
writeHeader = false;
}
} catch (error) {
console.error(chalk.red(error));
process.exit(1);
}
}
}
main();