1
1
#!/usr/bin/env node
2
2
3
+ const commander = require ( 'commander' ) ;
4
+ const fs = require ( 'fs' ) ;
5
+ const os = require ( 'os' ) ;
6
+ const path = require ( 'path' ) ;
7
+ const solc = require ( './index.js' ) ;
8
+ const smtchecker = require ( './smtchecker.js' ) ;
9
+ const smtsolver = require ( './smtsolver.js' ) ;
10
+
3
11
// hold on to any exception handlers that existed prior to this script running, we'll be adding them back at the end
4
- var originalUncaughtExceptionListeners = process . listeners ( "uncaughtException" ) ;
5
-
6
- var fs = require ( 'fs' ) ;
7
- var os = require ( 'os' ) ;
8
- var path = require ( 'path' ) ;
9
- var solc = require ( './index.js' ) ;
10
- var smtchecker = require ( './smtchecker.js' ) ;
11
- var smtsolver = require ( './smtsolver.js' ) ;
12
+ const originalUncaughtExceptionListeners = process . listeners ( 'uncaughtException' ) ;
12
13
// FIXME: remove annoying exception catcher of Emscripten
13
14
// see https://github.com/chriseth/browser-solidity/issues/167
14
15
process . removeAllListeners ( 'uncaughtException' ) ;
15
- var commander = require ( 'commander' ) ;
16
16
17
17
const program = new commander . Command ( ) ;
18
- const commanderParseInt = function ( value ) {
18
+ const commanderParseInt = function ( value ) {
19
19
const parsedValue = parseInt ( value , 10 ) ;
20
20
if ( isNaN ( parsedValue ) ) {
21
- throw new commander . InvalidArgumentError ( " Not a valid integer." ) ;
21
+ throw new commander . InvalidArgumentError ( ' Not a valid integer.' ) ;
22
22
}
23
23
return parsedValue ;
24
24
} ;
@@ -51,46 +51,47 @@ program
51
51
program . parse ( process . argv ) ;
52
52
const options = program . opts ( ) ;
53
53
54
- var files = program . args ;
55
- var destination = options . outputDir || '.'
54
+ const files = program . args ;
55
+ const destination = options . outputDir || '.' ;
56
56
57
57
function abort ( msg ) {
58
58
console . error ( msg || 'Error occured' ) ;
59
59
process . exit ( 1 ) ;
60
60
}
61
61
62
- function readFileCallback ( sourcePath ) {
63
- const prefixes = [ options . basePath ? options . basePath : "" ] . concat (
62
+ function readFileCallback ( sourcePath ) {
63
+ const prefixes = [ options . basePath ? options . basePath : '' ] . concat (
64
64
options . includePath ? options . includePath : [ ]
65
65
) ;
66
66
for ( const prefix of prefixes ) {
67
- const prefixedSourcePath = ( prefix ? prefix + '/' : "" ) + sourcePath ;
67
+ const prefixedSourcePath = ( prefix ? prefix + '/' : '' ) + sourcePath ;
68
68
69
69
if ( fs . existsSync ( prefixedSourcePath ) ) {
70
70
try {
71
- return { ' contents' : fs . readFileSync ( prefixedSourcePath ) . toString ( 'utf8' ) }
71
+ return { contents : fs . readFileSync ( prefixedSourcePath ) . toString ( 'utf8' ) } ;
72
72
} catch ( e ) {
73
- return { error : 'Error reading ' + prefixedSourcePath + ': ' + e } ;
73
+ return { error : 'Error reading ' + prefixedSourcePath + ': ' + e } ;
74
74
}
75
75
}
76
76
}
77
- return { error : 'File not found inside the base path or any of the include paths.' }
77
+ return { error : 'File not found inside the base path or any of the include paths.' } ;
78
78
}
79
79
80
- function withUnixPathSeparators ( filePath ) {
81
- if ( os . platform ( ) !== 'win32' )
82
- // On UNIX-like systems forward slashes in paths are just a part of the file name.
80
+ function withUnixPathSeparators ( filePath ) {
81
+ // On UNIX-like systems forward slashes in paths are just a part of the file name.
82
+ if ( os . platform ( ) !== 'win32' ) {
83
83
return filePath ;
84
+ }
84
85
85
- return filePath . replace ( / \\ / g, "/" ) ;
86
+ return filePath . replace ( / \\ / g, '/' ) ;
86
87
}
87
88
88
- function makeSourcePathRelativeIfPossible ( sourcePath ) {
89
+ function makeSourcePathRelativeIfPossible ( sourcePath ) {
89
90
const absoluteBasePath = ( options . basePath ? path . resolve ( options . basePath ) : path . resolve ( '.' ) ) ;
90
91
const absoluteIncludePaths = (
91
- options . includePath ?
92
- options . includePath . map ( ( prefix ) => { return path . resolve ( prefix ) ; } ) :
93
- [ ]
92
+ options . includePath
93
+ ? options . includePath . map ( ( prefix ) => { return path . resolve ( prefix ) ; } )
94
+ : [ ]
94
95
) ;
95
96
96
97
// Compared to base path stripping logic in solc this is much simpler because path.resolve()
@@ -104,58 +105,52 @@ function makeSourcePathRelativeIfPossible(sourcePath) {
104
105
for ( const absolutePrefix of [ absoluteBasePath ] . concat ( absoluteIncludePaths ) ) {
105
106
const relativeSourcePath = path . relative ( absolutePrefix , absoluteSourcePath ) ;
106
107
107
- if ( ! relativeSourcePath . startsWith ( '../' ) )
108
- return withUnixPathSeparators ( relativeSourcePath ) ;
108
+ if ( ! relativeSourcePath . startsWith ( '../' ) ) { return withUnixPathSeparators ( relativeSourcePath ) ; }
109
109
}
110
110
111
111
// File is not located inside base path or include paths so use its absolute path.
112
112
return withUnixPathSeparators ( absoluteSourcePath ) ;
113
113
}
114
114
115
- function toFormattedJson ( input ) {
115
+ function toFormattedJson ( input ) {
116
116
return JSON . stringify ( input , null , program . prettyJson ? 4 : 0 ) ;
117
117
}
118
118
119
- function reformatJsonIfRequested ( inputJson ) {
119
+ function reformatJsonIfRequested ( inputJson ) {
120
120
return ( program . prettyJson ? toFormattedJson ( JSON . parse ( inputJson ) ) : inputJson ) ;
121
121
}
122
122
123
- var callbacks = undefined
124
- if ( options . basePath || ! options . standardJson )
125
- callbacks = { 'import' : readFileCallback } ;
123
+ let callbacks ;
124
+ if ( options . basePath || ! options . standardJson ) { callbacks = { import : readFileCallback } ; }
126
125
127
126
if ( options . standardJson ) {
128
- var input = fs . readFileSync ( process . stdin . fd ) . toString ( 'utf8' ) ;
129
- if ( program . verbose )
130
- console . log ( '>>> Compiling:\n' + reformatJsonIfRequested ( input ) + "\n" )
131
- var output = reformatJsonIfRequested ( solc . compile ( input , callbacks ) ) ;
127
+ const input = fs . readFileSync ( process . stdin . fd ) . toString ( 'utf8' ) ;
128
+ if ( program . verbose ) { console . log ( '>>> Compiling:\n' + reformatJsonIfRequested ( input ) + '\n' ) ; }
129
+ let output = reformatJsonIfRequested ( solc . compile ( input , callbacks ) ) ;
132
130
133
131
try {
134
- var inputJSON = smtchecker . handleSMTQueries ( JSON . parse ( input ) , JSON . parse ( output ) , smtsolver . smtSolver ) ;
132
+ const inputJSON = smtchecker . handleSMTQueries ( JSON . parse ( input ) , JSON . parse ( output ) , smtsolver . smtSolver ) ;
135
133
if ( inputJSON ) {
136
- if ( program . verbose )
137
- console . log ( '>>> Retrying compilation with SMT:\n' + toFormattedJson ( inputJSON ) + "\n" )
134
+ if ( program . verbose ) { console . log ( '>>> Retrying compilation with SMT:\n' + toFormattedJson ( inputJSON ) + '\n' ) ; }
138
135
output = reformatJsonIfRequested ( solc . compile ( JSON . stringify ( inputJSON ) , callbacks ) ) ;
139
136
}
140
- }
141
- catch ( e ) {
142
- var addError = {
143
- component : "general" ,
137
+ } catch ( e ) {
138
+ const addError = {
139
+ component : 'general' ,
144
140
formattedMessage : e . toString ( ) ,
145
141
message : e . toString ( ) ,
146
- type : " Warning"
142
+ type : ' Warning'
147
143
} ;
148
144
149
- var outputJSON = JSON . parse ( output ) ;
145
+ const outputJSON = JSON . parse ( output ) ;
150
146
if ( ! outputJSON . errors ) {
151
- outputJSON . errors = [ ]
147
+ outputJSON . errors = [ ] ;
152
148
}
153
149
outputJSON . errors . push ( addError ) ;
154
150
output = toFormattedJson ( outputJSON ) ;
155
151
}
156
152
157
- if ( program . verbose )
158
- console . log ( '>>> Compilation result:' )
153
+ if ( program . verbose ) { console . log ( '>>> Compilation result:' ) ; }
159
154
console . log ( output ) ;
160
155
process . exit ( 0 ) ;
161
156
} else if ( files . length === 0 ) {
@@ -171,14 +166,15 @@ if (!options.basePath && options.includePath && options.includePath.length > 0)
171
166
abort ( '--include-path option requires a non-empty base path.' ) ;
172
167
}
173
168
174
- if ( options . includePath )
175
- for ( const includePath of options . includePath )
176
- if ( ! includePath )
177
- abort ( 'Empty values are not allowed in --include-path.' ) ;
169
+ if ( options . includePath ) {
170
+ for ( const includePath of options . includePath ) {
171
+ if ( ! includePath ) { abort ( 'Empty values are not allowed in --include-path.' ) ; }
172
+ }
173
+ }
178
174
179
- var sources = { } ;
175
+ const sources = { } ;
180
176
181
- for ( var i = 0 ; i < files . length ; i ++ ) {
177
+ for ( let i = 0 ; i < files . length ; i ++ ) {
182
178
try {
183
179
sources [ makeSourcePathRelativeIfPossible ( files [ i ] ) ] = {
184
180
content : fs . readFileSync ( files [ i ] ) . toString ( )
@@ -193,37 +189,36 @@ const cliInput = {
193
189
settings : {
194
190
optimizer : {
195
191
enabled : options . optimize ,
196
- runs : options . optimizeRuns ,
192
+ runs : options . optimizeRuns
197
193
} ,
198
194
outputSelection : {
199
195
'*' : {
200
- '*' : [ 'abi' , 'evm.bytecode' ]
196
+ '*' : [ 'abi' , 'evm.bytecode' ]
201
197
}
202
198
}
203
199
} ,
204
200
sources : sources
205
201
} ;
206
- if ( program . verbose )
207
- console . log ( '>>> Compiling:\n' + toFormattedJson ( cliInput ) + "\n" )
208
- var output = JSON . parse ( solc . compile ( JSON . stringify ( cliInput ) , callbacks ) ) ;
202
+ if ( program . verbose ) { console . log ( '>>> Compiling:\n' + toFormattedJson ( cliInput ) + '\n' ) ; }
203
+ const output = JSON . parse ( solc . compile ( JSON . stringify ( cliInput ) , callbacks ) ) ;
209
204
210
205
let hasError = false ;
211
206
212
207
if ( ! output ) {
213
208
abort ( 'No output from compiler' ) ;
214
- } else if ( output [ ' errors' ] ) {
215
- for ( var error in output [ ' errors' ] ) {
216
- var message = output [ ' errors' ] [ error ]
209
+ } else if ( output . errors ) {
210
+ for ( const error in output . errors ) {
211
+ const message = output . errors [ error ] ;
217
212
if ( message . severity === 'warning' ) {
218
- console . log ( message . formattedMessage )
213
+ console . log ( message . formattedMessage ) ;
219
214
} else {
220
- console . error ( message . formattedMessage )
221
- hasError = true
215
+ console . error ( message . formattedMessage ) ;
216
+ hasError = true ;
222
217
}
223
218
}
224
219
}
225
220
226
- fs . mkdirSync ( destination , { recursive : true } ) ;
221
+ fs . mkdirSync ( destination , { recursive : true } ) ;
227
222
228
223
function writeFile ( file , content ) {
229
224
file = path . join ( destination , file ) ;
@@ -234,9 +229,9 @@ function writeFile (file, content) {
234
229
} ) ;
235
230
}
236
231
237
- for ( var fileName in output . contracts ) {
238
- for ( var contractName in output . contracts [ fileName ] ) {
239
- var contractFileName = fileName + ':' + contractName ;
232
+ for ( const fileName in output . contracts ) {
233
+ for ( const contractName in output . contracts [ fileName ] ) {
234
+ let contractFileName = fileName + ':' + contractName ;
240
235
contractFileName = contractFileName . replace ( / [: ./ \\ ] / g, '_' ) ;
241
236
242
237
if ( options . bin ) {
0 commit comments