@@ -44,6 +44,7 @@ program
4444 'When using a package manager to install libraries, use this option to specify directories where packages are installed. ' +
4545 'Can be used multiple times to provide multiple locations.'
4646 )
47+ . option ( '--overwrite' , 'If artifacts already exist on disk, overwrite them.' , false )
4748 . option ( '-o, --output-dir <output-directory>' , 'Output directory for the contracts.' )
4849 . option ( '-p, --pretty-json' , 'Pretty-print all JSON output.' , false )
4950 . option ( '-v, --verbose' , 'More detailed console output.' , false ) ;
@@ -224,26 +225,33 @@ if (!output) {
224225
225226fs . mkdirSync ( destination , { recursive : true } ) ;
226227
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+
229235 fs . writeFile ( file , content , function ( err ) {
230236 if ( err ) {
231- console . error ( ' Failed to write ' + file + ': ' + err ) ;
237+ throw new Error ( ` Failed to write ${ file } : ${ err } ` ) ;
232238 }
233239 } ) ;
234240}
235241
236242for ( const fileName in output . contracts ) {
237243 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+ }
244248
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 ;
247255 }
248256 }
249257}
0 commit comments