From 109152730fe0a829a2474cc8b98f7302a4ee1ddc Mon Sep 17 00:00:00 2001 From: bakaoh Date: Tue, 2 Oct 2018 17:32:54 +0700 Subject: [PATCH] Add remapping when parse file to support embark-solc --- lib/core/config.js | 2 +- lib/core/file.js | 17 +++++++++++++++++ test/config.js | 4 ++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/core/config.js b/lib/core/config.js index 869f5fb75..514fa29e0 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -438,7 +438,7 @@ Config.prototype.loadPluginContractFiles = function() { contractsPlugins.forEach(function(plugin) { plugin.contractsFiles.forEach(function(file) { var filename = file.replace('./',''); - self.contractsFiles.push(new File({filename: filename, type: File.types.custom, path: filename, resolver: function(callback) { + self.contractsFiles.push(new File({filename: filename, pluginPath: plugin.pluginPath, type: File.types.custom, path: filename, resolver: function(callback) { callback(plugin.loadPluginFile(file)); }})); }); diff --git a/lib/core/file.js b/lib/core/file.js index 689bd0bb9..d4aced3d9 100644 --- a/lib/core/file.js +++ b/lib/core/file.js @@ -12,7 +12,9 @@ class File { this.path = options.path; this.basedir = options.basedir; this.resolver = options.resolver; + this.pluginPath = options.pluginPath ? options.pluginPath : ''; this.downloadedImports = false; + this.importRemappings = []; // mapping downloaded imports to local file } parseFileForImport(content, isHttpContract, callback) { @@ -36,6 +38,21 @@ class File { fileRelativePath: path.join(path.dirname(self.filename), matches[1]), url: `${pathWithoutFile}/${matches[1]}` }; + + var target = matches[1]; + if (httpFileObj) { + target = httpFileObj.filePath; + } else if (fs.existsSync(path.join(path.dirname(self.filename), matches[1]))) { + target = path.join(path.dirname(self.filename), matches[1]); + } else if (fs.existsSync(path.join("node_modules", matches[1]))) { + target = path.join("node_modules", matches[1]); + } + + self.importRemappings.push({ + prefix: matches[1], + target: fs.dappPath(target) + }); + if (httpFileObj) { // Replace http import by filePath import in content newContent = newContent.replace(matches[1], httpFileObj.filePath); diff --git a/test/config.js b/test/config.js index 7d9c83ad9..2872769ec 100644 --- a/test/config.js +++ b/test/config.js @@ -77,6 +77,8 @@ describe('embark.Config', function () { "filename": ".embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol", "type": "http", "path": "https://raw.githubusercontent.com/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol", + "pluginPath": '', + "importRemappings": [], "basedir": "", "resolver": undefined, "downloadedImports": false @@ -85,6 +87,8 @@ describe('embark.Config', function () { "filename": ".embark/contracts/status-im/contracts/master/contracts/identity/ERC725.sol", "type": "http", "path": "https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol", + "pluginPath": '', + "importRemappings": [], "basedir": "", "resolver": undefined, "downloadedImports": false