add loading in file.js but includes a patch to fix
This commit is contained in:
parent
ce34d40fe7
commit
9faf644c31
|
@ -3,7 +3,6 @@ const File = require('./file.js');
|
|||
const Plugins = require('./plugins.js');
|
||||
const utils = require('../utils/utils.js');
|
||||
const path = require('path');
|
||||
const request = require('request');
|
||||
|
||||
var Config = function(options) {
|
||||
this.env = options.env;
|
||||
|
@ -180,39 +179,18 @@ Config.prototype.getExternalContractUrl = function (contract) {
|
|||
return url;
|
||||
};
|
||||
|
||||
Config.prototype.loadContractOnTheWeb = function (directory, contract) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.mkdirp(directory, (err) => {
|
||||
if (err) {
|
||||
return reject(new Error(err));
|
||||
}
|
||||
const url = this.getExternalContractUrl(contract);
|
||||
const localFile = utils.joinPath(process.cwd(), directory, path.basename(url));
|
||||
request(url)
|
||||
.on('response', function (response) {
|
||||
if (response.statusCode !== 200) {
|
||||
return reject(new Error('Getting file returned code ' + response.statusCode));
|
||||
}
|
||||
resolve(localFile);
|
||||
})
|
||||
.on('error', function (err) {
|
||||
reject(new Error(err));
|
||||
})
|
||||
.pipe(fs.createWriteStream(localFile));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Config.prototype.loadExternalContractsFiles = async function() {
|
||||
Config.prototype.loadExternalContractsFiles = function() {
|
||||
let contracts = this.contractsConfig.contracts;
|
||||
let downloadPromises = [];
|
||||
for (let contractName in contracts) {
|
||||
let contract = contracts[contractName];
|
||||
if (!contract.file) {
|
||||
continue;
|
||||
}
|
||||
if (contract.file.startsWith('http') || contract.file.startsWith('git')) {
|
||||
downloadPromises.push(this.loadContractOnTheWeb('.embark/contracts', contract));
|
||||
const url = this.getExternalContractUrl(contract);
|
||||
// TODO put directory name somewhere else
|
||||
const localFile = utils.joinPath(process.cwd(), '.embark/contracts', path.basename(url));
|
||||
this.contractsFiles.push(new File({filename: localFile, type: "http", basedir: '', path: url}));
|
||||
} else if (fs.existsSync(contract.file)) {
|
||||
this.contractsFiles.push(new File({filename: contract.file, type: "dapp_file", basedir: '', path: contract.file}));
|
||||
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {
|
||||
|
@ -221,18 +199,6 @@ Config.prototype.loadExternalContractsFiles = async function() {
|
|||
this.logger.error("contract file not found: " + contract.file);
|
||||
}
|
||||
}
|
||||
|
||||
if (downloadPromises.length) {
|
||||
try {
|
||||
const downloadedFiles = await Promise.all(downloadPromises);
|
||||
this.contractsFiles = this.contractsFiles.concat(downloadedFiles);
|
||||
} catch (err) {
|
||||
// TODO ad url?
|
||||
this.logger.error('Error while downloading one of the contracts.' +
|
||||
'Is the url correct? Refer to out contracts download guide');
|
||||
this.logger.error(err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Config.prototype.loadStorageConfigFile = function() {
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
let fs = require('./fs.js');
|
||||
const async = require('async');
|
||||
const fs = require('./fs.js');
|
||||
const path = require('path');
|
||||
const request = require('request');
|
||||
|
||||
class File {
|
||||
|
||||
constructor(options) {
|
||||
constructor (options) {
|
||||
this.filename = options.filename;
|
||||
this.type = options.type;
|
||||
this.path = options.path;
|
||||
|
@ -10,13 +13,53 @@ class File {
|
|||
this.resolver = options.resolver;
|
||||
}
|
||||
|
||||
content(callback) {
|
||||
if (this.type === 'embark_internal') {
|
||||
downloadFile (callback) {
|
||||
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);
|
||||
}
|
||||
], (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 === 'dapp_file') {
|
||||
} else if (this.type === File.types.dapp_file) {
|
||||
return callback(fs.readFileSync(this.path).toString());
|
||||
} else if (this.type === 'custom') {
|
||||
} else if (this.type === File.types.custom) {
|
||||
return this.resolver(callback);
|
||||
} else if (this.type === File.types.http) {
|
||||
this.downloadFile(callback);
|
||||
} else {
|
||||
throw new Error("unknown file: " + this.filename);
|
||||
}
|
||||
|
@ -24,4 +67,11 @@ class File {
|
|||
|
||||
}
|
||||
|
||||
File.types = {
|
||||
embark_internal: 'embark_internal',
|
||||
dapp_file: 'dapp_file',
|
||||
custom: 'custom',
|
||||
http: 'http'
|
||||
};
|
||||
|
||||
module.exports = File;
|
||||
|
|
|
@ -23,6 +23,10 @@ function writeFileSync() {
|
|||
return fs.writeFileSync.apply(fs.writeFileSync, arguments);
|
||||
}
|
||||
|
||||
function readFile() {
|
||||
return fs.readFile.apply(fs.readFile, arguments);
|
||||
}
|
||||
|
||||
function readFileSync() {
|
||||
return fs.readFileSync.apply(fs.readFileSync, arguments);
|
||||
}
|
||||
|
@ -66,6 +70,7 @@ module.exports = {
|
|||
mkdirpSync: mkdirpSync,
|
||||
mkdirp,
|
||||
copySync: copySync,
|
||||
readFile,
|
||||
readFileSync: readFileSync,
|
||||
appendFileSync: appendFileSync,
|
||||
writeFileSync: writeFileSync,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
let async = require('../../utils/async_extend.js');
|
||||
let SolcW = require('./solcW.js');
|
||||
const path = require('path');
|
||||
|
||||
class Solidity {
|
||||
|
||||
|
@ -25,8 +26,14 @@ class Solidity {
|
|||
let match = new RegExp("^" + directory);
|
||||
filename = filename.replace(match, '');
|
||||
}
|
||||
// TODO remove this and fix it
|
||||
filename = path.basename(filename);
|
||||
|
||||
file.content(function(fileContent) {
|
||||
if (!fileContent) {
|
||||
self.logger.error('Error while loading the content of ' + filename);
|
||||
return fileCb();
|
||||
}
|
||||
input[filename] = {content: fileContent.replace(/\r\n/g, '\n')};
|
||||
fileCb();
|
||||
});
|
||||
|
|
|
@ -141,7 +141,7 @@ describe('embark.Config', function () {
|
|||
});
|
||||
|
||||
describe('#loadExternalContractsFiles', function () {
|
||||
it('should create the right list of files and download', async function () {
|
||||
it('should create the right list of files and download', function () {
|
||||
config.contractsFiles = [];
|
||||
config.contractsConfig.contracts = [
|
||||
{
|
||||
|
@ -151,12 +151,24 @@ describe('embark.Config', function () {
|
|||
file: 'github.com/status-im/contracts/contracts/identity/ERC725.sol'
|
||||
}
|
||||
];
|
||||
await config.loadExternalContractsFiles();
|
||||
assert.deepEqual(config.contractsFiles,
|
||||
[
|
||||
path.normalize('C:/dev/embark/.embark/contracts/simple_storage.sol'),
|
||||
path.normalize('C:/dev/embark/.embark/contracts/ERC725.sol')
|
||||
]);
|
||||
const expected = [
|
||||
{
|
||||
"filename": path.normalize("C:/dev/embark/.embark/contracts/simple_storage.sol"),
|
||||
"type": "http",
|
||||
"path": "https://raw.githubusercontent.com/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol",
|
||||
"basedir": "",
|
||||
"resolver": undefined
|
||||
},
|
||||
{
|
||||
"filename": path.normalize("C:/dev/embark/.embark/contracts/ERC725.sol"),
|
||||
"type": "http",
|
||||
"path": "https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol",
|
||||
"basedir": "",
|
||||
"resolver": undefined
|
||||
}
|
||||
];
|
||||
config.loadExternalContractsFiles();
|
||||
assert.deepEqual(config.contractsFiles, expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue