@@ -3,7 +3,7 @@ import { PluginClient } from '@remixproject/plugin'
33import * as chokidar from 'chokidar'
44import * as utils from '../utils'
55import * as fs from 'fs-extra'
6- import { basename , join } from 'path'
6+ import { join } from 'path'
77const { spawn } = require ( 'child_process' ) // eslint-disable-line
88
99export class HardhatClient extends PluginClient {
@@ -14,26 +14,26 @@ export class HardhatClient extends PluginClient {
1414 warnLog : boolean
1515 buildPath : string
1616
17- constructor ( private readOnly = false ) {
17+ constructor ( private readOnly = false ) {
1818 super ( )
1919 this . methods = [ 'compile' , 'sync' ]
2020 }
2121
22- setWebSocket ( websocket : WS ) : void {
22+ setWebSocket ( websocket : WS ) : void {
2323 this . websocket = websocket
2424 this . websocket . addEventListener ( 'close' , ( ) => {
2525 this . warnLog = false
2626 if ( this . watcher ) this . watcher . close ( )
2727 } )
2828 }
2929
30- sharedFolder ( currentSharedFolder : string ) : void {
30+ sharedFolder ( currentSharedFolder : string ) : void {
3131 this . currentSharedFolder = currentSharedFolder
3232 this . buildPath = utils . absolutePath ( 'artifacts/contracts' , this . currentSharedFolder )
3333 this . listenOnHardhatCompilation ( )
3434 }
3535
36- compile ( configPath : string ) {
36+ compile ( configPath : string ) {
3737 return new Promise ( ( resolve , reject ) => {
3838 if ( this . readOnly ) {
3939 const errMsg = '[Hardhat Compilation]: Cannot compile in read-only mode'
@@ -60,9 +60,10 @@ export class HardhatClient extends PluginClient {
6060 } )
6161 }
6262
63- private async processArtifact ( ) {
63+ private async processArtifact ( ) {
6464 // resolving the files
6565 const folderFiles = await fs . readdir ( this . buildPath )
66+ const targetsSynced = [ ]
6667 // name of folders are file names
6768 for ( const file of folderFiles ) { // ["artifacts/contracts/Greeter.sol/"]
6869 const contractFilePath = join ( this . buildPath , file )
@@ -87,15 +88,19 @@ export class HardhatClient extends PluginClient {
8788 const jsonStd = JSON . parse ( contentStd )
8889 compilationResult . target = jsonStd . sourceName
8990
90- // this is the full compilation result
91- console . log ( 'Processing Hardhat artifact for file: ' , file )
91+ targetsSynced . push ( compilationResult . target )
9292 const path = join ( contractFilePath , jsonDbg . buildInfo )
9393 const content = await fs . readFile ( path , { encoding : 'utf-8' } )
94-
94+
9595 await this . feedContractArtifactFile ( content , compilationResult )
9696 }
9797 if ( compilationResult . target ) {
98- this . emit ( 'compilationFinished' , compilationResult . target , { sources : compilationResult . input } , 'soljson' , compilationResult . output , compilationResult . solcVersion )
98+ // we are only interested in the contracts that are in the target of the compilation
99+ compilationResult . output = {
100+ ...compilationResult . output ,
101+ contracts : { [ compilationResult . target ] : compilationResult . output . contracts [ compilationResult . target ] }
102+ }
103+ this . emit ( 'compilationFinished' , compilationResult . target , { sources : compilationResult . input } , 'soljson' , compilationResult . output , compilationResult . solcVersion )
99104 }
100105 }
101106 }
@@ -104,29 +109,32 @@ export class HardhatClient extends PluginClient {
104109 this . call ( 'terminal' , 'log' , 'receiving compilation result from Hardhat' )
105110 this . warnLog = true
106111 }
112+ if ( targetsSynced . length ) {
113+ console . log ( `Processing artifacts for files: ${ [ ...new Set ( targetsSynced ) ] . join ( ', ' ) } ` )
114+ // @ts -ignore
115+ this . call ( 'terminal' , 'log' , `synced with Hardhat: ${ [ ...new Set ( targetsSynced ) ] . join ( ', ' ) } ` )
116+ }
107117 }
108118
109- listenOnHardhatCompilation ( ) {
119+ listenOnHardhatCompilation ( ) {
110120 try {
111121 this . watcher = chokidar . watch ( this . buildPath , { depth : 1 , ignorePermissionErrors : true , ignoreInitial : true } )
112-
122+
113123 this . watcher . on ( 'change' , ( ) => this . processArtifact ( ) )
114124 this . watcher . on ( 'add' , ( ) => this . processArtifact ( ) )
115125 // process the artifact on activation
116126 setTimeout ( ( ) => this . processArtifact ( ) , 1000 )
117127 } catch ( e ) {
118128 console . log ( e )
119- }
129+ }
120130 }
121131
122- async sync ( ) {
132+ async sync ( ) {
123133 console . log ( 'syncing from Hardhat' )
124134 this . processArtifact ( )
125- // @ts -ignore
126- this . call ( 'terminal' , 'log' , 'synced with Hardhat' )
127135 }
128136
129- async feedContractArtifactFile ( artifactContent , compilationResultPart ) {
137+ async feedContractArtifactFile ( artifactContent , compilationResultPart ) {
130138 const contentJSON = JSON . parse ( artifactContent )
131139 compilationResultPart . solcVersion = contentJSON . solcVersion
132140 for ( const file in contentJSON . input . sources ) {
@@ -139,10 +147,10 @@ export class HardhatClient extends PluginClient {
139147 compilationResultPart . output [ 'sources' ] [ file ] = contentJSON . output . sources [ file ]
140148 compilationResultPart . output [ 'contracts' ] [ file ] = contentJSON . output . contracts [ file ]
141149 if ( contentJSON . output . errors && contentJSON . output . errors . length ) {
142- compilationResultPart . output [ 'errors' ] = contentJSON . output . errors . filter ( error => error . sourceLocation . file === file )
150+ compilationResultPart . output [ 'errors' ] = contentJSON . output . errors . filter ( error => error . sourceLocation . file === file )
143151 }
144152 }
145153 }
146- }
154+ }
147155 }
148156}
0 commit comments