-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·59 lines (50 loc) · 1.18 KB
/
cli.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
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const stringify = require("json-stable-stringify");
const sortAbi = require(".");
const [filename] = process.argv.slice(2);
const processAbi = (str) => stringify(sortAbi(JSON.parse(str)), { space: 2 });
function sortFile() {
const filePath = path.resolve(filename);
fs.writeFileSync(filePath, processAbi(fs.readFileSync(filePath, "utf8")));
}
function readAllFromStdIn(cb) {
let input = "";
process.stdin.setEncoding("utf-8");
process.stdin
.on("data", function (chunk) {
input += chunk;
})
.on("end", function () {
cb(null, input);
})
.on("error", function (err) {
cb(err);
});
setTimeout(function () {
if (!input) {
cb(null);
}
}, 100);
}
function sortStdIn() {
readAllFromStdIn(function (err, input) {
if (err) {
throw err;
}
if (!input) {
console.error("Usage:");
console.error(" abi-sort abi.json");
console.error(" abi-sort < abi.json > sorted.json");
process.exit(1);
}
process.stdout.write(processAbi(input));
});
}
if (filename) {
sortFile();
} else {
sortStdIn();
}