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 Plugins = require('./plugins.js');
|
||||||
const utils = require('../utils/utils.js');
|
const utils = require('../utils/utils.js');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const request = require('request');
|
|
||||||
|
|
||||||
var Config = function(options) {
|
var Config = function(options) {
|
||||||
this.env = options.env;
|
this.env = options.env;
|
||||||
|
@ -180,39 +179,18 @@ Config.prototype.getExternalContractUrl = function (contract) {
|
||||||
return url;
|
return url;
|
||||||
};
|
};
|
||||||
|
|
||||||
Config.prototype.loadContractOnTheWeb = function (directory, contract) {
|
Config.prototype.loadExternalContractsFiles = function() {
|
||||||
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() {
|
|
||||||
let contracts = this.contractsConfig.contracts;
|
let contracts = this.contractsConfig.contracts;
|
||||||
let downloadPromises = [];
|
|
||||||
for (let contractName in contracts) {
|
for (let contractName in contracts) {
|
||||||
let contract = contracts[contractName];
|
let contract = contracts[contractName];
|
||||||
if (!contract.file) {
|
if (!contract.file) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (contract.file.startsWith('http') || contract.file.startsWith('git')) {
|
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)) {
|
} else if (fs.existsSync(contract.file)) {
|
||||||
this.contractsFiles.push(new File({filename: contract.file, type: "dapp_file", basedir: '', path: 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))) {
|
} 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);
|
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() {
|
Config.prototype.loadStorageConfigFile = function() {
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
let fs = require('./fs.js');
|
const async = require('async');
|
||||||
|
const fs = require('./fs.js');
|
||||||
|
const path = require('path');
|
||||||
|
const request = require('request');
|
||||||
|
|
||||||
class File {
|
class File {
|
||||||
|
|
||||||
|
@ -10,13 +13,53 @@ class File {
|
||||||
this.resolver = options.resolver;
|
this.resolver = options.resolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
content (callback) {
|
||||||
if (this.type === 'embark_internal') {
|
if (this.type === File.types.embark_internal) {
|
||||||
return callback(fs.readFileSync(fs.embarkPath(this.path)).toString());
|
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());
|
return callback(fs.readFileSync(this.path).toString());
|
||||||
} else if (this.type === 'custom') {
|
} else if (this.type === File.types.custom) {
|
||||||
return this.resolver(callback);
|
return this.resolver(callback);
|
||||||
|
} else if (this.type === File.types.http) {
|
||||||
|
this.downloadFile(callback);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("unknown file: " + this.filename);
|
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;
|
module.exports = File;
|
||||||
|
|
|
@ -23,6 +23,10 @@ function writeFileSync() {
|
||||||
return fs.writeFileSync.apply(fs.writeFileSync, arguments);
|
return fs.writeFileSync.apply(fs.writeFileSync, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function readFile() {
|
||||||
|
return fs.readFile.apply(fs.readFile, arguments);
|
||||||
|
}
|
||||||
|
|
||||||
function readFileSync() {
|
function readFileSync() {
|
||||||
return fs.readFileSync.apply(fs.readFileSync, arguments);
|
return fs.readFileSync.apply(fs.readFileSync, arguments);
|
||||||
}
|
}
|
||||||
|
@ -66,6 +70,7 @@ module.exports = {
|
||||||
mkdirpSync: mkdirpSync,
|
mkdirpSync: mkdirpSync,
|
||||||
mkdirp,
|
mkdirp,
|
||||||
copySync: copySync,
|
copySync: copySync,
|
||||||
|
readFile,
|
||||||
readFileSync: readFileSync,
|
readFileSync: readFileSync,
|
||||||
appendFileSync: appendFileSync,
|
appendFileSync: appendFileSync,
|
||||||
writeFileSync: writeFileSync,
|
writeFileSync: writeFileSync,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
let async = require('../../utils/async_extend.js');
|
let async = require('../../utils/async_extend.js');
|
||||||
let SolcW = require('./solcW.js');
|
let SolcW = require('./solcW.js');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
class Solidity {
|
class Solidity {
|
||||||
|
|
||||||
|
@ -25,8 +26,14 @@ class Solidity {
|
||||||
let match = new RegExp("^" + directory);
|
let match = new RegExp("^" + directory);
|
||||||
filename = filename.replace(match, '');
|
filename = filename.replace(match, '');
|
||||||
}
|
}
|
||||||
|
// TODO remove this and fix it
|
||||||
|
filename = path.basename(filename);
|
||||||
|
|
||||||
file.content(function(fileContent) {
|
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')};
|
input[filename] = {content: fileContent.replace(/\r\n/g, '\n')};
|
||||||
fileCb();
|
fileCb();
|
||||||
});
|
});
|
||||||
|
|
|
@ -141,7 +141,7 @@ describe('embark.Config', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#loadExternalContractsFiles', 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.contractsFiles = [];
|
||||||
config.contractsConfig.contracts = [
|
config.contractsConfig.contracts = [
|
||||||
{
|
{
|
||||||
|
@ -151,12 +151,24 @@ describe('embark.Config', function () {
|
||||||
file: 'github.com/status-im/contracts/contracts/identity/ERC725.sol'
|
file: 'github.com/status-im/contracts/contracts/identity/ERC725.sol'
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
await config.loadExternalContractsFiles();
|
const expected = [
|
||||||
assert.deepEqual(config.contractsFiles,
|
{
|
||||||
[
|
"filename": path.normalize("C:/dev/embark/.embark/contracts/simple_storage.sol"),
|
||||||
path.normalize('C:/dev/embark/.embark/contracts/simple_storage.sol'),
|
"type": "http",
|
||||||
path.normalize('C:/dev/embark/.embark/contracts/ERC725.sol')
|
"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