@@ -44,6 +44,7 @@ program
44
44
'When using a package manager to install libraries, use this option to specify directories where packages are installed. ' +
45
45
'Can be used multiple times to provide multiple locations.'
46
46
)
47
+ . option ( '--overwrite' , 'If artifacts already exist on disk, overwrite them.' , false )
47
48
. option ( '-o, --output-dir <output-directory>' , 'Output directory for the contracts.' )
48
49
. option ( '-p, --pretty-json' , 'Pretty-print all JSON output.' , false )
49
50
. option ( '-v, --verbose' , 'More detailed console output.' , false ) ;
@@ -224,26 +225,33 @@ if (!output) {
224
225
225
226
fs . mkdirSync ( destination , { recursive : true } ) ;
226
227
227
- function writeFile ( file , content ) {
228
- file = path . join ( destination , file ) ;
228
+ function writeFile ( file , extension , content ) {
229
+ file = path . join ( destination , `${ file } .${ extension } ` ) ;
230
+
231
+ if ( fs . existsSync ( file ) && ! options . overwrite ) {
232
+ throw new Error ( `Refusing to overwrite existing file ${ file } (use --overwrite to force).` ) ;
233
+ }
234
+
229
235
fs . writeFile ( file , content , function ( err ) {
230
236
if ( err ) {
231
- console . error ( ' Failed to write ' + file + ': ' + err ) ;
237
+ throw new Error ( ` Failed to write ${ file } : ${ err } ` ) ;
232
238
}
233
239
} ) ;
234
240
}
235
241
236
242
for ( const fileName in output . contracts ) {
237
243
for ( const contractName in output . contracts [ fileName ] ) {
238
- let contractFileName = fileName + ':' + contractName ;
239
- contractFileName = contractFileName . replace ( / [: ./ \\ ] / g, '_' ) ;
240
-
241
- if ( options . bin ) {
242
- writeFile ( contractFileName + '.bin' , output . contracts [ fileName ] [ contractName ] . evm . bytecode . object ) ;
243
- }
244
+ try {
245
+ if ( options . bin ) {
246
+ writeFile ( contractName , 'bin' , output . contracts [ fileName ] [ contractName ] . evm . bytecode . object ) ;
247
+ }
244
248
245
- if ( options . abi ) {
246
- writeFile ( contractFileName + '.abi' , toFormattedJson ( output . contracts [ fileName ] [ contractName ] . abi ) ) ;
249
+ if ( options . abi ) {
250
+ writeFile ( contractName , 'abi' , toFormattedJson ( output . contracts [ fileName ] [ contractName ] . abi ) ) ;
251
+ }
252
+ } catch ( err ) {
253
+ console . error ( err . message ) ;
254
+ hasError = true ;
247
255
}
248
256
}
249
257
}
0 commit comments