2018-04-18 16:09:42 +00:00
|
|
|
const async = require('async');
|
|
|
|
const fs = require('./fs.js');
|
|
|
|
const path = require('path');
|
|
|
|
const request = require('request');
|
2018-04-20 13:52:13 +00:00
|
|
|
const utils = require('../utils/utils');
|
2017-07-05 12:35:51 +00:00
|
|
|
|
|
|
|
class File {
|
|
|
|
|
2018-04-18 16:09:42 +00:00
|
|
|
constructor (options) {
|
2018-04-20 17:40:41 +00:00
|
|
|
this.filename = options.filename.replace(/\\/g, '/');
|
2017-07-05 12:35:51 +00:00
|
|
|
this.type = options.type;
|
|
|
|
this.path = options.path;
|
2018-02-24 01:36:11 +00:00
|
|
|
this.basedir = options.basedir;
|
2017-07-05 12:35:51 +00:00
|
|
|
this.resolver = options.resolver;
|
2018-09-14 15:50:24 +00:00
|
|
|
this.downloadedImports = false;
|
2017-07-05 12:35:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 13:52:13 +00:00
|
|
|
parseFileForImport(content, isHttpContract, callback) {
|
2018-04-20 14:03:03 +00:00
|
|
|
const self = this;
|
2018-04-20 13:52:13 +00:00
|
|
|
if (typeof isHttpContract === 'function') {
|
|
|
|
callback = isHttpContract;
|
|
|
|
isHttpContract = false;
|
|
|
|
}
|
2018-04-19 17:29:25 +00:00
|
|
|
if (self.filename.indexOf('.sol') < 0) {
|
2018-04-18 20:32:51 +00:00
|
|
|
// Only supported in Solidity
|
2018-04-20 15:39:17 +00:00
|
|
|
return callback(null, content);
|
2018-04-18 20:32:51 +00:00
|
|
|
}
|
2018-09-12 18:08:00 +00:00
|
|
|
const regex = /import ["']([-a-zA-Z0-9@:%_+.~#?&\/=]+)["'];/g;
|
2018-04-18 20:32:51 +00:00
|
|
|
let matches;
|
2018-04-19 14:05:11 +00:00
|
|
|
const filesToDownload = [];
|
2018-04-19 17:29:25 +00:00
|
|
|
const pathWithoutFile = path.dirname(self.path);
|
2018-09-12 18:08:00 +00:00
|
|
|
let newContent = content;
|
2018-04-18 20:32:51 +00:00
|
|
|
while ((matches = regex.exec(content))) {
|
2018-04-20 13:52:13 +00:00
|
|
|
const httpFileObj = utils.getExternalContractUrl(matches[1]);
|
|
|
|
const fileObj = {
|
2018-04-19 17:29:25 +00:00
|
|
|
fileRelativePath: path.join(path.dirname(self.filename), matches[1]),
|
|
|
|
url: `${pathWithoutFile}/${matches[1]}`
|
2018-04-20 13:52:13 +00:00
|
|
|
};
|
|
|
|
if (httpFileObj) {
|
2018-04-20 15:39:17 +00:00
|
|
|
// Replace http import by filePath import in content
|
2018-09-12 18:08:00 +00:00
|
|
|
newContent = newContent.replace(matches[1], httpFileObj.filePath);
|
2018-04-20 15:39:17 +00:00
|
|
|
|
2018-04-20 13:52:13 +00:00
|
|
|
fileObj.fileRelativePath = httpFileObj.filePath;
|
|
|
|
fileObj.url = httpFileObj.url;
|
|
|
|
} else if (!isHttpContract) {
|
|
|
|
// Just a normal import
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
filesToDownload.push(fileObj);
|
2018-04-18 20:32:51 +00:00
|
|
|
}
|
2018-04-19 17:29:25 +00:00
|
|
|
|
2018-04-20 15:39:17 +00:00
|
|
|
if (self.downloadedImports) {
|
|
|
|
// We already parsed this file
|
2018-09-12 18:08:00 +00:00
|
|
|
return callback(null, newContent);
|
2018-04-20 15:39:17 +00:00
|
|
|
}
|
2018-04-19 17:29:25 +00:00
|
|
|
async.each(filesToDownload, ((fileObj, eachCb) => {
|
|
|
|
self.downloadFile(fileObj.fileRelativePath, fileObj.url, (_content) => {
|
|
|
|
eachCb();
|
|
|
|
});
|
2018-04-20 15:39:17 +00:00
|
|
|
}), (err) => {
|
2018-09-14 15:50:24 +00:00
|
|
|
self.downloadedImports = true;
|
2018-09-12 18:08:00 +00:00
|
|
|
callback(err, newContent);
|
2018-04-20 15:39:17 +00:00
|
|
|
});
|
2018-04-18 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 14:05:11 +00:00
|
|
|
downloadFile (filename, url, callback) {
|
2018-04-19 17:29:25 +00:00
|
|
|
const self = this;
|
2018-04-18 16:09:42 +00:00
|
|
|
async.waterfall([
|
|
|
|
function makeTheDir(next) {
|
2018-04-19 14:05:11 +00:00
|
|
|
fs.mkdirp(path.dirname(filename), (err) => {
|
2018-04-18 16:09:42 +00:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function downloadTheFile(next) {
|
2018-06-13 18:37:15 +00:00
|
|
|
let alreadyCalledBack = false;
|
|
|
|
function doCallback(err) {
|
|
|
|
if (alreadyCalledBack) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
alreadyCalledBack = true;
|
|
|
|
next(err);
|
|
|
|
}
|
2018-04-19 14:05:11 +00:00
|
|
|
request(url)
|
2018-04-18 16:09:42 +00:00
|
|
|
.on('response', function (response) {
|
|
|
|
if (response.statusCode !== 200) {
|
2018-06-13 18:37:15 +00:00
|
|
|
doCallback('Getting file returned code ' + response.statusCode);
|
2018-04-18 16:09:42 +00:00
|
|
|
}
|
|
|
|
})
|
2018-06-13 18:37:15 +00:00
|
|
|
.on('error', doCallback)
|
2018-04-19 14:05:11 +00:00
|
|
|
.pipe(fs.createWriteStream(filename))
|
2018-06-13 18:37:15 +00:00
|
|
|
.on('finish', () => {
|
|
|
|
doCallback();
|
|
|
|
});
|
2018-04-18 16:09:42 +00:00
|
|
|
},
|
|
|
|
function readFile(next) {
|
2018-04-19 14:05:11 +00:00
|
|
|
fs.readFile(filename, next);
|
2018-04-19 17:29:25 +00:00
|
|
|
},
|
|
|
|
function parseForImports(content, next) {
|
2018-09-14 15:50:24 +00:00
|
|
|
self.parseFileForImport(content.toString(), true, (err) => {
|
2018-04-18 20:32:51 +00:00
|
|
|
next(err, content);
|
|
|
|
});
|
2018-04-19 17:29:25 +00:00
|
|
|
}
|
2018-04-18 16:09:42 +00:00
|
|
|
], (err, content) => {
|
|
|
|
if (err) {
|
2018-05-08 21:49:46 +00:00
|
|
|
console.error(__('Error while downloading the file'), err);
|
2018-04-18 16:09:42 +00:00
|
|
|
return callback('');
|
|
|
|
}
|
|
|
|
callback(content.toString());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
content (callback) {
|
2018-04-20 13:52:13 +00:00
|
|
|
let content;
|
2018-04-18 16:09:42 +00:00
|
|
|
if (this.type === File.types.embark_internal) {
|
2018-04-20 13:52:13 +00:00
|
|
|
content = fs.readFileSync(fs.embarkPath(this.path)).toString();
|
2018-04-18 16:09:42 +00:00
|
|
|
} else if (this.type === File.types.dapp_file) {
|
2018-04-20 13:52:13 +00:00
|
|
|
content = fs.readFileSync(this.path).toString();
|
2018-04-18 16:09:42 +00:00
|
|
|
} else if (this.type === File.types.custom) {
|
2018-04-20 13:52:13 +00:00
|
|
|
return this.resolver((theContent) => {
|
2018-04-20 15:39:17 +00:00
|
|
|
this.parseFileForImport(theContent, (err, newContent) => {
|
|
|
|
callback(newContent);
|
2018-04-20 14:03:03 +00:00
|
|
|
});
|
2018-04-20 13:52:13 +00:00
|
|
|
});
|
2018-04-18 16:09:42 +00:00
|
|
|
} else if (this.type === File.types.http) {
|
2018-04-20 13:52:13 +00:00
|
|
|
return this.downloadFile(this.filename, this.path, (content) => {
|
2018-04-19 14:05:11 +00:00
|
|
|
if (!content) {
|
|
|
|
return callback(content);
|
|
|
|
}
|
|
|
|
this.path = this.filename;
|
|
|
|
this.type = File.types.dapp_file;
|
|
|
|
callback(content);
|
|
|
|
});
|
2017-07-05 12:35:51 +00:00
|
|
|
} else {
|
|
|
|
throw new Error("unknown file: " + this.filename);
|
|
|
|
}
|
2018-04-20 15:39:17 +00:00
|
|
|
return this.parseFileForImport(content, (err, newContent) => {
|
|
|
|
callback(newContent);
|
2018-04-20 14:03:03 +00:00
|
|
|
});
|
2017-07-05 12:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:09:42 +00:00
|
|
|
File.types = {
|
|
|
|
embark_internal: 'embark_internal',
|
|
|
|
dapp_file: 'dapp_file',
|
|
|
|
custom: 'custom',
|
|
|
|
http: 'http'
|
|
|
|
};
|
|
|
|
|
2017-07-05 12:35:51 +00:00
|
|
|
module.exports = File;
|