From b7a104a25d0d0dea765fbc824917de9271119025 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 28 Sep 2022 13:06:17 +0200 Subject: [PATCH] add sync to truffle client --- libs/remixd/src/services/truffleClient.ts | 70 +++++++++++++---------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/libs/remixd/src/services/truffleClient.ts b/libs/remixd/src/services/truffleClient.ts index 2921b9c5b3..8ba93cdb75 100644 --- a/libs/remixd/src/services/truffleClient.ts +++ b/libs/remixd/src/services/truffleClient.ts @@ -12,6 +12,7 @@ export class TruffleClient extends PluginClient { currentSharedFolder: string watcher: chokidar.FSWatcher warnLog: boolean + buildPath: string constructor (private readOnly = false) { super() @@ -28,6 +29,7 @@ export class TruffleClient extends PluginClient { sharedFolder (currentSharedFolder: string): void { this.currentSharedFolder = currentSharedFolder + this.buildPath = utils.absolutePath('build/contracts', this.currentSharedFolder) this.listenOnTruffleCompilation() } @@ -58,38 +60,39 @@ export class TruffleClient extends PluginClient { }) } - listenOnTruffleCompilation () { - try { - const buildPath = utils.absolutePath('build/contracts', this.currentSharedFolder) - this.watcher = chokidar.watch(buildPath, { depth: 3, ignorePermissionErrors: true, ignoreInitial: true }) - const compilationResult = { - input: {}, - output: { - contracts: {}, - sources: {} - }, - solcVersion: null - } - const processArtifact = async () => { - const folderFiles = await fs.readdir(buildPath) - // name of folders are file names - for (const file of folderFiles) { - if (file.endsWith('.json')) { - const content = await fs.readFile(join(buildPath, file), { encoding: 'utf-8' }) - await this.feedContractArtifactFile(file, content, compilationResult) - } - } - if (!this.warnLog) { - // @ts-ignore - this.call('terminal', 'log', 'receiving compilation result from truffle') - this.warnLog = true - } - this.emit('compilationFinished', '', { sources: compilationResult.input }, 'soljson', compilationResult.output, compilationResult.solcVersion) + private async processArtifact () { + const folderFiles = await fs.readdir(this.buildPath) + const compilationResult = { + input: {}, + output: { + contracts: {}, + sources: {} + }, + solcVersion: null + } + // name of folders are file names + for (const file of folderFiles) { + if (file.endsWith('.json')) { + const content = await fs.readFile(join(this.buildPath, file), { encoding: 'utf-8' }) + await this.feedContractArtifactFile(file, content, compilationResult) } - this.watcher.on('change', async (f: string) => processArtifact()) - this.watcher.on('add', async (f: string) => processArtifact()) + } + if (!this.warnLog) { + // @ts-ignore + this.call('terminal', 'log', 'receiving compilation result from truffle') + this.warnLog = true + } + this.emit('compilationFinished', '', { sources: compilationResult.input }, 'soljson', compilationResult.output, compilationResult.solcVersion) + } + + listenOnTruffleCompilation () { + try { + this.watcher = chokidar.watch(this.buildPath, { depth: 3, ignorePermissionErrors: true, ignoreInitial: true }) + + this.watcher.on('change', async (f: string) => this.processArtifact()) + this.watcher.on('add', async (f: string) => this.processArtifact()) // process the artifact on activation - processArtifact() + this.processArtifact() } catch (e) { console.log(e) } @@ -129,4 +132,11 @@ export class TruffleClient extends PluginClient { } } } + + async sync () { + console.log('syncing from truffle') + this.processArtifact() + // @ts-ignore + this.call('terminal', 'log', 'synced with truffle') + } }