-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
86 lines (79 loc) · 2.23 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const _ = require("lodash");
const minimist = require("minimist");
const winston = require("winston");
const logger = winston.createLogger({
levels: winston.config.syslog.levels,
level: "notice",
format: winston.format.simple(),
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
)
})
]
});
function exportAbiForFile(filename) {
if (filename.endsWith(".json")) {
const data = fs.readFileSync(filename);
const jsonData = JSON.parse(data);
if (jsonData.hasOwnProperty("abi")) {
logger.info("Found ABI in: " + filename);
return jsonData["abi"];
}
}
return null;
}
function exportAbiForDirectory(directory) {
var res = [];
const fileList = fs.readdirSync(directory);
logger.info("Looking for json files in: " + directory);
fileList.forEach(filename => {
jsonAbi = exportAbiForFile(path.join(directory, filename));
if (jsonAbi !== null) {
res = _.union(res, jsonAbi);
}
});
return res;
}
function printHelp() {
console.log(
"Example: " +
process.argv[1] +
" -d /home/user/myproject/build/contracts/ -v\n" +
"Options:\n" +
" -d / --directory: location of the build files, [build/contracts] by default\n" +
" -o / --output: output file, [build/ABI.json] by default\n" +
" -v / --verbose"
);
}
function main() {
var args = minimist(process.argv.slice(2), {
alias: { d: "directory", v: "verbose", o: "output" },
boolean: ["verbose"],
default: { d: "build/contracts", o: "build/ABI.json" }
});
if (args.verbose) {
logger.level = "info";
}
fs.stat(args.directory, (err, stats) => {
if (!err && stats.isDirectory()) {
abi = exportAbiForDirectory(args.directory);
fs.writeFileSync(args.output, JSON.stringify(abi, null, 2));
logger.notice("ABI extracted and output file wrote to: " + args.output);
} else {
logger.error(
'"' +
args.directory +
'" must be a directory, you might need the -d or --directory argument'
);
printHelp();
process.exit(2);
}
});
}
main();