diff --git a/lib/core/config.js b/lib/core/config.js index 1f65c2b37..6180f26d0 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -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() { diff --git a/test/config.js b/test/config.js index 5f5c78777..e50ca4a19 100644 --- a/test/config.js +++ b/test/config.js @@ -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') + ]); + }); + }); });