embark-area-51/lib/core/file.js

111 lines
3.0 KiB
JavaScript
Raw Normal View History

const async = require('async');
const fs = require('./fs.js');
const path = require('path');
const request = require('request');
class File {
constructor (options) {
this.filename = options.filename;
this.type = options.type;
this.path = options.path;
2018-02-24 01:36:11 +00:00
this.basedir = options.basedir;
this.resolver = options.resolver;
}
2018-04-18 20:32:51 +00:00
parseFileForImport(content, callback) {
2018-04-19 17:29:25 +00:00
const self = this;
if (self.filename.indexOf('.sol') < 0) {
2018-04-18 20:32:51 +00:00
// Only supported in Solidity
return callback();
}
const regex = /import "([a-zA-Z0-9_\-.\\\/]+)";/g;
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-04-18 20:32:51 +00:00
while ((matches = regex.exec(content))) {
2018-04-19 14:05:11 +00:00
filesToDownload.push({
2018-04-19 17:29:25 +00:00
fileRelativePath: path.join(path.dirname(self.filename), matches[1]),
url: `${pathWithoutFile}/${matches[1]}`
2018-04-19 14:05:11 +00:00
});
2018-04-18 20:32:51 +00:00
}
2018-04-19 17:29:25 +00:00
async.each(filesToDownload, ((fileObj, eachCb) => {
self.downloadFile(fileObj.fileRelativePath, fileObj.url, (_content) => {
eachCb();
});
}), callback);
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
console.log('Downloading:', filename, 'form', url);
const self = this;
async.waterfall([
function makeTheDir(next) {
2018-04-19 14:05:11 +00:00
fs.mkdirp(path.dirname(filename), (err) => {
if (err) {
return next(err);
}
next();
});
},
function downloadTheFile(next) {
2018-04-19 14:05:11 +00:00
request(url)
.on('response', function (response) {
if (response.statusCode !== 200) {
next('Getting file returned code ' + response.statusCode);
}
})
.on('error', next)
2018-04-19 14:05:11 +00:00
.pipe(fs.createWriteStream(filename))
.on('finish', next);
},
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-04-18 20:32:51 +00:00
self.parseFileForImport(content, (err) => {
next(err, content);
});
2018-04-19 17:29:25 +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) {
return callback(fs.readFileSync(fs.embarkPath(this.path)).toString());
} else if (this.type === File.types.dapp_file) {
return callback(fs.readFileSync(this.path).toString());
} else if (this.type === File.types.custom) {
return this.resolver(callback);
} else if (this.type === File.types.http) {
2018-04-19 14:05:11 +00:00
this.downloadFile(this.filename, this.path, (content) => {
if (!content) {
return callback(content);
}
this.path = this.filename;
this.type = File.types.dapp_file;
callback(content);
});
} else {
throw new Error("unknown file: " + this.filename);
}
}
}
File.types = {
embark_internal: 'embark_internal',
dapp_file: 'dapp_file',
custom: 'custom',
http: 'http'
};
module.exports = File;