Skip to content

Commit bbab02b

Browse files
committed
solcjs: Add --pretty-json option
1 parent fc6671c commit bbab02b

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

solcjs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ program
3838
.option('--abi', 'ABI of the contracts.')
3939
.option('--standard-json', 'Turn on Standard JSON Input / Output mode.')
4040
.option('--base-path <path>', 'Automatically resolve all imports inside the given path.')
41-
.option('-o, --output-dir <output-directory>', 'Output directory for the contracts.');
41+
.option('-o, --output-dir <output-directory>', 'Output directory for the contracts.')
42+
.option('-p, --pretty-json', 'Pretty-print all JSON output.', false);
4243

4344
program.parse(process.argv);
4445
const options = program.opts();
@@ -91,18 +92,26 @@ function stripBasePath(sourcePath) {
9192
return withUnixPathSeparators(relativeSourcePath);
9293
}
9394

95+
function toFormattedJson(input) {
96+
return JSON.stringify(input, null, program.prettyJson ? 4 : 0);
97+
}
98+
99+
function reformatJsonIfRequested(inputJson) {
100+
return (program.prettyJson ? toFormattedJson(JSON.parse(inputJson)) : inputJson);
101+
}
102+
94103
var callbacks = undefined
95104
if (options.basePath || !options.standardJson)
96105
callbacks = {'import': readFileCallback};
97106

98107
if (options.standardJson) {
99108
var input = fs.readFileSync(process.stdin.fd).toString('utf8');
100-
var output = solc.compile(input, callbacks);
109+
var output = reformatJsonIfRequested(solc.compile(input, callbacks));
101110

102111
try {
103112
var inputJSON = smtchecker.handleSMTQueries(JSON.parse(input), JSON.parse(output), smtsolver.smtSolver);
104113
if (inputJSON) {
105-
output = solc.compile(JSON.stringify(inputJSON), callbacks);
114+
output = reformatJsonIfRequested(solc.compile(JSON.stringify(inputJSON), callbacks));
106115
}
107116
}
108117
catch (e) {
@@ -118,7 +127,7 @@ if (options.standardJson) {
118127
outputJSON.errors = []
119128
}
120129
outputJSON.errors.push(addError);
121-
output = JSON.stringify(outputJSON);
130+
output = toFormattedJson(outputJSON);
122131
}
123132

124133
console.log(output);
@@ -195,7 +204,7 @@ for (var fileName in output.contracts) {
195204
}
196205

197206
if (options.abi) {
198-
writeFile(contractFileName + '.abi', JSON.stringify(output.contracts[fileName][contractName].abi));
207+
writeFile(contractFileName + '.abi', toFormattedJson(output.contracts[fileName][contractName].abi));
199208
}
200209
}
201210
}

0 commit comments

Comments
 (0)