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');
|
2017-07-05 12:35:51 +00:00
|
|
|
|
|
|
|
class File {
|
|
|
|
|
2018-04-18 16:09:42 +00:00
|
|
|
constructor (options) {
|
2017-07-05 12:35:51 +00:00
|
|
|
this.filename = options.filename;
|
|
|
|
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-04-18 20:32:51 +00:00
|
|
|
parseFileForImport(content, callback) {
|
|
|
|
if (this.filename.indexOf('.sol') < 0) {
|
|
|
|
// Only supported in Solidity
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
const regex = /import "([a-zA-Z0-9_\-.\\\/]+)";/g;
|
|
|
|
let matches;
|
|
|
|
while ((matches = regex.exec(content))) {
|
|
|
|
console.log('Need to download', matches[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadFile (filename, callback) {
|
2018-04-18 16:09:42 +00:00
|
|
|
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);
|
2018-04-18 20:32:51 +00:00
|
|
|
},
|
|
|
|
function parseForImports(content, next) {
|
|
|
|
self.parseFileForImport(content, (err) => {
|
|
|
|
next(err, content);
|
|
|
|
});
|
2018-04-18 16:09:42 +00:00
|
|
|
}
|
|
|
|
], (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) {
|
2017-07-05 12:35:51 +00:00
|
|
|
return callback(fs.readFileSync(fs.embarkPath(this.path)).toString());
|
2018-04-18 16:09:42 +00:00
|
|
|
} else if (this.type === File.types.dapp_file) {
|
2017-07-05 12:35:51 +00:00
|
|
|
return callback(fs.readFileSync(this.path).toString());
|
2018-04-18 16:09:42 +00:00
|
|
|
} else if (this.type === File.types.custom) {
|
2017-07-05 12:35:51 +00:00
|
|
|
return this.resolver(callback);
|
2018-04-18 16:09:42 +00:00
|
|
|
} else if (this.type === File.types.http) {
|
|
|
|
this.downloadFile(callback);
|
2017-07-05 12:35:51 +00:00
|
|
|
} else {
|
|
|
|
throw new Error("unknown file: " + this.filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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;
|