integrate downloadCOntract function in contract get function

This commit is contained in:
Jonathan Rainville 2018-04-18 09:07:39 -04:00
parent 2568ec1aec
commit ce34d40fe7
2 changed files with 37 additions and 4 deletions

View File

@ -187,7 +187,7 @@ Config.prototype.loadContractOnTheWeb = function (directory, contract) {
return reject(new Error(err));
}
const url = this.getExternalContractUrl(contract);
const localFile = utils.joinPath(process.cwd(), `${directory}/${path.basename(url)}`);
const localFile = utils.joinPath(process.cwd(), directory, path.basename(url));
request(url)
.on('response', function (response) {
if (response.statusCode !== 200) {
@ -203,15 +203,16 @@ Config.prototype.loadContractOnTheWeb = function (directory, contract) {
});
};
Config.prototype.loadExternalContractsFiles = function() {
Config.prototype.loadExternalContractsFiles = async function() {
let contracts = this.contractsConfig.contracts;
let downloadPromises = [];
for (let contractName in contracts) {
let contract = contracts[contractName];
if (!contract.file) {
continue;
}
if (contract.file.startsWith('http') || contract.file.startsWith('git')) {
this.loadContractOnTheWeb(contract);
downloadPromises.push(this.loadContractOnTheWeb('.embark/contracts', contract));
} else if (fs.existsSync(contract.file)) {
this.contractsFiles.push(new File({filename: contract.file, type: "dapp_file", basedir: '', path: contract.file}));
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {
@ -220,6 +221,18 @@ Config.prototype.loadExternalContractsFiles = function() {
this.logger.error("contract file not found: " + contract.file);
}
}
if (downloadPromises.length) {
try {
const downloadedFiles = await Promise.all(downloadPromises);
this.contractsFiles = this.contractsFiles.concat(downloadedFiles);
} catch (err) {
// TODO ad url?
this.logger.error('Error while downloading one of the contracts.' +
'Is the url correct? Refer to out contracts download guide');
this.logger.error(err);
}
}
};
Config.prototype.loadStorageConfigFile = function() {

View File

@ -130,7 +130,7 @@ describe('embark.Config', function () {
});
describe('#loadContractOnTheWeb', function () {
it('should get the right url for a https://github file', async function () {
it('should download the file correctly', async function () {
const filePath = await config.loadContractOnTheWeb(
'test_apps/test_app/.embark/contracts',
{file: 'https://github.com/embark-framework/embark/blob/master/test_app/app/contracts/simple_storage.sol'}
@ -139,4 +139,24 @@ describe('embark.Config', function () {
path.normalize('C:/dev/embark/test_apps/test_app/.embark/contracts/simple_storage.sol'));
});
});
describe('#loadExternalContractsFiles', function () {
it('should create the right list of files and download', async function () {
config.contractsFiles = [];
config.contractsConfig.contracts = [
{
file: 'https://github.com/embark-framework/embark/blob/master/test_app/app/contracts/simple_storage.sol'
},
{
file: 'github.com/status-im/contracts/contracts/identity/ERC725.sol'
}
];
await config.loadExternalContractsFiles();
assert.deepEqual(config.contractsFiles,
[
path.normalize('C:/dev/embark/.embark/contracts/simple_storage.sol'),
path.normalize('C:/dev/embark/.embark/contracts/ERC725.sol')
]);
});
});
});