Skip to content

Commit 1ecce4b

Browse files
authored
Merge pull request #534 from ethereum/solcjs-pretty-json-and-verbose-options
[solcjs] `--pretty-json` and `--verbose` options
2 parents fc6671c + aa49a83 commit 1ecce4b

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

solcjs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ 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)
43+
.option('-v, --verbose', 'More detailed console output.', false);
4244

4345
program.parse(process.argv);
4446
const options = program.opts();
@@ -91,18 +93,30 @@ function stripBasePath(sourcePath) {
9193
return withUnixPathSeparators(relativeSourcePath);
9294
}
9395

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

98108
if (options.standardJson) {
99109
var input = fs.readFileSync(process.stdin.fd).toString('utf8');
100-
var output = solc.compile(input, callbacks);
110+
if (program.verbose)
111+
console.log('>>> Compiling:\n' + reformatJsonIfRequested(input) + "\n")
112+
var output = reformatJsonIfRequested(solc.compile(input, callbacks));
101113

102114
try {
103115
var inputJSON = smtchecker.handleSMTQueries(JSON.parse(input), JSON.parse(output), smtsolver.smtSolver);
104116
if (inputJSON) {
105-
output = solc.compile(JSON.stringify(inputJSON), callbacks);
117+
if (program.verbose)
118+
console.log('>>> Retrying compilation with SMT:\n' + toFormattedJson(inputJSON) + "\n")
119+
output = reformatJsonIfRequested(solc.compile(JSON.stringify(inputJSON), callbacks));
106120
}
107121
}
108122
catch (e) {
@@ -118,9 +132,11 @@ if (options.standardJson) {
118132
outputJSON.errors = []
119133
}
120134
outputJSON.errors.push(addError);
121-
output = JSON.stringify(outputJSON);
135+
output = toFormattedJson(outputJSON);
122136
}
123137

138+
if (program.verbose)
139+
console.log('>>> Compilation result:')
124140
console.log(output);
125141
process.exit(0);
126142
} else if (files.length === 0) {
@@ -142,7 +158,7 @@ for (var i = 0; i < files.length; i++) {
142158
}
143159
}
144160

145-
var output = JSON.parse(solc.compile(JSON.stringify({
161+
const cliInput = {
146162
language: 'Solidity',
147163
settings: {
148164
optimizer: {
@@ -156,7 +172,10 @@ var output = JSON.parse(solc.compile(JSON.stringify({
156172
}
157173
},
158174
sources: sources
159-
}), callbacks));
175+
};
176+
if (program.verbose)
177+
console.log('>>> Compiling:\n' + toFormattedJson(cliInput) + "\n")
178+
var output = JSON.parse(solc.compile(JSON.stringify(cliInput), callbacks));
160179

161180
let hasError = false;
162181

@@ -195,7 +214,7 @@ for (var fileName in output.contracts) {
195214
}
196215

197216
if (options.abi) {
198-
writeFile(contractFileName + '.abi', JSON.stringify(output.contracts[fileName][contractName].abi));
217+
writeFile(contractFileName + '.abi', toFormattedJson(output.contracts[fileName][contractName].abi));
199218
}
200219
}
201220
}

0 commit comments

Comments
 (0)