From 60fb6b14d4b339ed8670007b308549b67b79c36e Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 19 Apr 2018 13:29:25 -0400 Subject: [PATCH] downlaod import files --- lib/core/file.js | 26 ++++++++++++++++---------- test/contracts/simple_storage.sol | 1 - test/file.js | 8 +++++--- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/core/file.js b/lib/core/file.js index 34fa0fe1..4812e897 100644 --- a/lib/core/file.js +++ b/lib/core/file.js @@ -14,26 +14,32 @@ class File { } parseFileForImport(content, callback) { - if (this.filename.indexOf('.sol') < 0) { + const self = this; + if (self.filename.indexOf('.sol') < 0) { // Only supported in Solidity return callback(); } const regex = /import "([a-zA-Z0-9_\-.\\\/]+)";/g; let matches; const filesToDownload = []; - const pathWithoutFile = path.dirname(this.path); + const pathWithoutFile = path.dirname(self.path); while ((matches = regex.exec(content))) { - console.log('Need to download', matches[1]); filesToDownload.push({ - fileRelativePath: matches[1], - url: path.join(pathWithoutFile, matches[1]) + fileRelativePath: path.join(path.dirname(self.filename), matches[1]), + url: `${pathWithoutFile}/${matches[1]}` }); } - //async.each(filesToDownload, (file)) + + async.each(filesToDownload, ((fileObj, eachCb) => { + self.downloadFile(fileObj.fileRelativePath, fileObj.url, (_content) => { + eachCb(); + }); + }), callback); } downloadFile (filename, url, callback) { - // const self = this; + console.log('Downloading:', filename, 'form', url); + const self = this; async.waterfall([ function makeTheDir(next) { fs.mkdirp(path.dirname(filename), (err) => { @@ -56,12 +62,12 @@ class File { }, function readFile(next) { fs.readFile(filename, next); - }// , - /*function parseForImports(content, next) { + }, + function parseForImports(content, next) { self.parseFileForImport(content, (err) => { next(err, content); }); - }*/ + } ], (err, content) => { if (err) { console.error('Error while downloading the file', err); diff --git a/test/contracts/simple_storage.sol b/test/contracts/simple_storage.sol index 6497f05d..7a07a304 100644 --- a/test/contracts/simple_storage.sol +++ b/test/contracts/simple_storage.sol @@ -2,7 +2,6 @@ pragma solidity ^0.4.7; contract SimpleStorage { uint public storedData; import "./ownable.sol"; - import "../../contracts/token.sol"; function SimpleStorage(uint initialValue) { storedData = initialValue; diff --git a/test/file.js b/test/file.js index 52ceda98..265db4d9 100644 --- a/test/file.js +++ b/test/file.js @@ -4,11 +4,13 @@ const fs = require('fs-extra'); describe('embark.File', function () { describe('parseFileForImport', () => { - it('should find all the imports', function () { + it('should find all the imports', function (done) { const contract = fs.readFileSync('./test/contracts/simple_storage.sol').toString(); - const file = new File({filename: 'simple_storage.sol', + const file = new File({filename: '.embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol', path: 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/simple_storage.sol'}); - file.parseFileForImport(contract); + file.parseFileForImport(contract, () => { + done(); + }); }); }); });