From 9faf644c31dbb6f4695b77985717d6d5feb3ceb0 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 18 Apr 2018 12:09:42 -0400 Subject: [PATCH] add loading in file.js but includes a patch to fix --- lib/core/config.js | 44 +++---------------------- lib/core/file.js | 62 +++++++++++++++++++++++++++++++---- lib/core/fs.js | 5 +++ lib/modules/solidity/index.js | 7 ++++ test/config.js | 26 +++++++++++---- 5 files changed, 92 insertions(+), 52 deletions(-) diff --git a/lib/core/config.js b/lib/core/config.js index 6180f26d0..c675c1e87 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -3,7 +3,6 @@ const File = require('./file.js'); const Plugins = require('./plugins.js'); const utils = require('../utils/utils.js'); const path = require('path'); -const request = require('request'); var Config = function(options) { this.env = options.env; @@ -180,39 +179,18 @@ Config.prototype.getExternalContractUrl = function (contract) { return url; }; -Config.prototype.loadContractOnTheWeb = function (directory, contract) { - return new Promise((resolve, reject) => { - fs.mkdirp(directory, (err) => { - if (err) { - return reject(new Error(err)); - } - const url = this.getExternalContractUrl(contract); - const localFile = utils.joinPath(process.cwd(), directory, path.basename(url)); - request(url) - .on('response', function (response) { - if (response.statusCode !== 200) { - return reject(new Error('Getting file returned code ' + response.statusCode)); - } - resolve(localFile); - }) - .on('error', function (err) { - reject(new Error(err)); - }) - .pipe(fs.createWriteStream(localFile)); - }); - }); -}; - -Config.prototype.loadExternalContractsFiles = async function() { +Config.prototype.loadExternalContractsFiles = 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')) { - downloadPromises.push(this.loadContractOnTheWeb('.embark/contracts', contract)); + const url = this.getExternalContractUrl(contract); + // TODO put directory name somewhere else + const localFile = utils.joinPath(process.cwd(), '.embark/contracts', path.basename(url)); + this.contractsFiles.push(new File({filename: localFile, type: "http", basedir: '', path: url})); } 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))) { @@ -221,18 +199,6 @@ Config.prototype.loadExternalContractsFiles = async 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/lib/core/file.js b/lib/core/file.js index 98e49c0c5..09498fc92 100644 --- a/lib/core/file.js +++ b/lib/core/file.js @@ -1,8 +1,11 @@ -let fs = require('./fs.js'); +const async = require('async'); +const fs = require('./fs.js'); +const path = require('path'); +const request = require('request'); class File { - constructor(options) { + constructor (options) { this.filename = options.filename; this.type = options.type; this.path = options.path; @@ -10,13 +13,53 @@ class File { this.resolver = options.resolver; } - content(callback) { - if (this.type === 'embark_internal') { + downloadFile (callback) { + const self = this; + async.waterfall([ + function makeTheDir(next) { + fs.mkdirp(path.dirname(self.filename), (err) => { + if (err) { + return next(err); + } + next(); + }); + }, + function downloadTheFile(next) { + request(self.path) + .on('response', function (response) { + if (response.statusCode !== 200) { + next('Getting file returned code ' + response.statusCode); + } + }) + .on('error', next) + .pipe(fs.createWriteStream(self.filename)) + .on('finish', () => { + self.path = self.filename; + self.type = File.types.dapp_file; + next(); + }); + }, + function readFile(next) { + fs.readFile(self.path, next); + } + ], (err, content) => { + if (err) { + console.error('Error while downloading the file', err); + return callback(''); + } + callback(content.toString()); + }); + } + + content (callback) { + if (this.type === File.types.embark_internal) { return callback(fs.readFileSync(fs.embarkPath(this.path)).toString()); - } else if (this.type === 'dapp_file') { + } else if (this.type === File.types.dapp_file) { return callback(fs.readFileSync(this.path).toString()); - } else if (this.type === 'custom') { + } else if (this.type === File.types.custom) { return this.resolver(callback); + } else if (this.type === File.types.http) { + this.downloadFile(callback); } else { throw new Error("unknown file: " + this.filename); } @@ -24,4 +67,11 @@ class File { } +File.types = { + embark_internal: 'embark_internal', + dapp_file: 'dapp_file', + custom: 'custom', + http: 'http' +}; + module.exports = File; diff --git a/lib/core/fs.js b/lib/core/fs.js index 28fc4f378..d196d99c8 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -23,6 +23,10 @@ function writeFileSync() { return fs.writeFileSync.apply(fs.writeFileSync, arguments); } +function readFile() { + return fs.readFile.apply(fs.readFile, arguments); +} + function readFileSync() { return fs.readFileSync.apply(fs.readFileSync, arguments); } @@ -66,6 +70,7 @@ module.exports = { mkdirpSync: mkdirpSync, mkdirp, copySync: copySync, + readFile, readFileSync: readFileSync, appendFileSync: appendFileSync, writeFileSync: writeFileSync, diff --git a/lib/modules/solidity/index.js b/lib/modules/solidity/index.js index a4f5c035d..2048b27d6 100644 --- a/lib/modules/solidity/index.js +++ b/lib/modules/solidity/index.js @@ -1,5 +1,6 @@ let async = require('../../utils/async_extend.js'); let SolcW = require('./solcW.js'); +const path = require('path'); class Solidity { @@ -25,8 +26,14 @@ class Solidity { let match = new RegExp("^" + directory); filename = filename.replace(match, ''); } + // TODO remove this and fix it + filename = path.basename(filename); file.content(function(fileContent) { + if (!fileContent) { + self.logger.error('Error while loading the content of ' + filename); + return fileCb(); + } input[filename] = {content: fileContent.replace(/\r\n/g, '\n')}; fileCb(); }); diff --git a/test/config.js b/test/config.js index e50ca4a19..28ea70972 100644 --- a/test/config.js +++ b/test/config.js @@ -141,7 +141,7 @@ describe('embark.Config', function () { }); describe('#loadExternalContractsFiles', function () { - it('should create the right list of files and download', async function () { + it('should create the right list of files and download', function () { config.contractsFiles = []; config.contractsConfig.contracts = [ { @@ -151,12 +151,24 @@ describe('embark.Config', function () { 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') - ]); + const expected = [ + { + "filename": path.normalize("C:/dev/embark/.embark/contracts/simple_storage.sol"), + "type": "http", + "path": "https://raw.githubusercontent.com/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol", + "basedir": "", + "resolver": undefined + }, + { + "filename": path.normalize("C:/dev/embark/.embark/contracts/ERC725.sol"), + "type": "http", + "path": "https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol", + "basedir": "", + "resolver": undefined + } + ]; + config.loadExternalContractsFiles(); + assert.deepEqual(config.contractsFiles, expected); }); }); });