forked from ethereum/solc-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solcjs
executable file
·140 lines (120 loc) · 3.54 KB
/
solcjs
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
138
139
140
#!/usr/bin/env node
// hold on to any exception handlers that existed prior to this script running, we'll be adding them back at the end
var originalUncaughtExceptionListeners = process.listeners("uncaughtException");
var fs = require('fs-extra');
var path = require('path');
var solc = require('./index.js');
// FIXME: remove annoying exception catcher of Emscripten
// see https://github.com/chriseth/browser-solidity/issues/167
process.removeAllListeners('uncaughtException');
var yargs = require('yargs')
.usage('Usage: $0 [options] [input_file...]')
.option('version', {
describe: 'Show version and exit.',
type: 'boolean'
})
.option('optimize', {
describe: 'Enable bytecode optimizer.',
type: 'boolean'
})
.option('bin', {
describe: 'Binary of the contracts in hex.',
type: 'boolean'
})
.option('abi', {
describe: 'ABI of the contracts.',
type: 'boolean'
})
.option('standard-json', {
describe: 'Turn on Standard JSON Input / Output mode.',
type: 'boolean'
})
.option('output-dir', {
alias: 'o',
describe: 'Output directory for the contracts.',
type: 'string'
})
.version(solc.version())
.showHelpOnFail(false, 'Specify --help for available options')
.help()
var argv = yargs.argv;
var files = argv._;
var destination = argv['output-dir'] || '.'
function abort (msg) {
console.error(msg || 'Error occured');
process.exit(1);
}
if (argv['standard-json']) {
var size = fs.fstatSync(process.stdin.fd).size;
if (size <= 0) {
abort('Empty input was read');
}
var input = fs.readSync(process.stdin.fd, size)[0];
console.log(solc.compileStandardWrapper(input));
process.exit(0);
} else if (files.length === 0) {
console.error('Must provide a file');
process.exit(1);
}
if (!(argv.bin || argv.abi)) {
abort('Invalid option selected, must specify either --bin or --abi');
}
var sources = {};
for (var i = 0; i < files.length; i++) {
try {
sources[ files[i] ] = { content: fs.readFileSync(files[i]).toString() };
} catch (e) {
abort('Error reading ' + files[i] + ': ' + e);
}
}
var output = JSON.parse(solc.compileStandardWrapper(JSON.stringify({
language: 'Solidity',
settings: {
optimizer: {
enabled: argv.optimize
},
outputSelection: {
'*': {
'*': [ 'abi', 'evm.bytecode' ]
}
}
},
sources: sources
})));
if (!output) {
abort('No output from compiler');
} else if (output['errors']) {
for (var error in output['errors']) {
var message = output['errors'][error]
if (message.severity === 'warning') {
console.log(message.formattedMessage)
} else {
console.error(message.formattedMessage)
}
}
}
fs.ensureDirSync (destination);
function writeFile (file, content) {
file = path.join(destination, file);
fs.writeFile(file, content, function (err) {
if (err) {
console.error('Failed to write ' + file + ': ' + err);
}
});
}
for (var fileName in output.contracts) {
for (var contractName in output.contracts[fileName]) {
var contractFileName = fileName + ':' + contractName;
contractFileName = contractFileName.replace(/[:./]/g, '_');
if (argv.bin) {
writeFile(contractFileName + '.bin', output.contracts[fileName][contractName].evm.bytecode.object);
}
if (argv.abi) {
writeFile(contractFileName + '.abi', JSON.stringify(output.contracts[fileName][contractName].abi));
}
}
}
// Put back original exception handlers.
originalUncaughtExceptionListeners.forEach(function (listener) {
process.addListener('uncaughtException', listener);
});