Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Use synchronous version of readFile
Browse files Browse the repository at this point in the history
  • Loading branch information
eggplantzzz committed Jun 25, 2019
1 parent 0087ffb commit a3c1e0f
Showing 1 changed file with 36 additions and 41 deletions.
77 changes: 36 additions & 41 deletions packages/truffle-core/lib/console.js
Expand Up @@ -50,28 +50,27 @@ class Console extends EventEmitter {
// passed down to commands.
this.options.repl = this.repl;

this.provision()
.then(abstractions => {
this.repl.start({
prompt: "truffle(" + this.options.network + ")> ",
context: {
web3: this.web3
},
interpreter: this.interpret.bind(this),
done: callback
});

this.resetContractsInConsoleContext(abstractions);
})
.catch(error => {
this.options.logger.log(
"Unexpected error: Cannot provision contracts while instantiating the console."
);
this.options.logger.log(error.stack || error.message || error);
try {
const abstractions = this.provision();
this.repl.start({
prompt: "truffle(" + this.options.network + ")> ",
context: {
web3: this.web3
},
interpreter: this.interpret.bind(this),
done: callback
});

this.resetContractsInConsoleContext(abstractions);
} catch (error) {
this.options.logger.log(
"Unexpected error: Cannot provision contracts while instantiating the console."
);
this.options.logger.log(error.stack || error.message || error);
}
}

async provision() {
provision() {
let files;
try {
files = fse.readdirSync(this.options.contracts_build_directory);
Expand All @@ -82,26 +81,21 @@ class Console extends EventEmitter {
// doesn't exist" 99.9% of the time.
}

let promises = [];
let jsonBlobs = [];
files = files || [];

files.forEach(file => {
promises.push(
fse
.readFile(
path.join(this.options.contracts_build_directory, file),
"utf8"
)
.then(body => JSON.parse(body))
.catch(error => {
throw new Error(
`Error parsing or reading ${file}: ${error.message}`
);
})
);
try {
const body = fse.readFileSync(
path.join(this.options.contracts_build_directory, file),
"utf8"
);
jsonBlobs.push(JSON.parse(body));
} catch (error) {
throw new Error(`Error parsing or reading ${file}: ${error.message}`);
}
});

const jsonBlobs = await Promise.all(promises);
const abstractions = jsonBlobs.map(json => {
const abstraction = contract(json);
provision(abstraction, this.options);
Expand Down Expand Up @@ -140,13 +134,14 @@ class Console extends EventEmitter {
}

// Reprovision after each command as it may change contracts.
this.provision()
.then(() => callback())
.catch(error => {
// Don't pass abstractions to the callback if they're there or else
// they'll get printed in the repl.
callback(error);
});
try {
this.provision();
callback();
} catch (error) {
// Don't pass abstractions to the callback if they're there or else
// they'll get printed in the repl.
callback(error);
}
});
}

Expand Down

0 comments on commit a3c1e0f

Please sign in to comment.