Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
"sourceType": "module",
"ecmaVersion": 2017
},
"rules": {
"accessor-pairs": "error",
Expand Down
64 changes: 54 additions & 10 deletions lib/core/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var fs = require('./fs.js');
var File = require('./file.js');
var Plugins = require('./plugins.js');
var utils = require('../utils/utils.js');
var path = require('path');
const fs = require('./fs.js');
const File = require('./file.js');
const Plugins = require('./plugins.js');
const utils = require('../utils/utils.js');
const path = require('path');

const httpContractDir = '.embark/contracts/';

var Config = function(options) {
this.env = options.env;
Expand Down Expand Up @@ -142,17 +144,58 @@ Config.prototype.loadContractsConfigFile = function() {
this.contractsConfig = this._mergeConfig(configFilePath, configObject, this.env);
};

Config.prototype.getExternalContractUrl = function (contract) {
let url;
const RAW_URL = 'https://raw.githubusercontent.com/';
const MALFORMED_ERROR = 'Malformed Github URL for ';
if (contract.file.startsWith('https://github')) {
const match = contract.file.match(/https:\/\/github\.[a-z]+\/(.*)/);
if (!match) {
this.logger.error(MALFORMED_ERROR + contract.file);
return '';
}
url = `${RAW_URL}${match[1].replace('blob/', '')}`;
} else if (contract.file.startsWith('git')) {
// Match values
// [0] entire input
// [1] git://
// [2] user
// [3] repository
// [4] path
// [5] branch
const match = contract.file.match(
/(git:\/\/)?github\.[a-z]+\/([a-zA-Z0-9_\-.]+)\/([a-zA-Z0-9_\-7]+)\/([a-zA-Z0-9_\-\/.]+)#?([a-zA-Z0-1_\-.]*)?/
);
if (!match) {
this.logger.error(MALFORMED_ERROR + contract.file);
return '';
}
let branch = match[5];
if (!branch) {
branch = 'master';
}
url = `${RAW_URL}${match[2]}/${match[3]}/${branch}/${match[4]}`;
} else {
url = contract.file;
}
return url;
};

Config.prototype.loadExternalContractsFiles = function() {
let contracts = this.contractsConfig.contracts;
for (let contractName in contracts) {
let contract = contracts[contractName];
if (!contract.file) {
continue;
}
if (fs.existsSync(contract.file)) {
this.contractsFiles.push(new File({filename: contract.file, type: "dapp_file", basedir: '', path: contract.file}));
if (contract.file.startsWith('http') || contract.file.startsWith('git')) {
const url = this.getExternalContractUrl(contract);
const localFile = httpContractDir + path.basename(url);
this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: url}));
} else if (fs.existsSync(contract.file)) {
this.contractsFiles.push(new File({filename: contract.file, type: File.types.dapp_file, basedir: '', path: contract.file}));
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {
this.contractsFiles.push(new File({filename: path.join('./node_modules/', contract.file), type: "dapp_file", basedir: '', path: path.join('./node_modules/', contract.file)}));
this.contractsFiles.push(new File({filename: path.join('./node_modules/', contract.file), type: File.types.dapp_file, basedir: '', path: path.join('./node_modules/', contract.file)}));
} else {
this.logger.error("contract file not found: " + contract.file);
}
Expand Down Expand Up @@ -216,6 +259,7 @@ Config.prototype.loadEmbarkConfigFile = function() {
}).map((dir) => {
return dir.split("*.")[0];
});
this.contractDirectories.push(httpContractDir);

this.buildDir = this.embarkConfig.buildDir;
this.configDir = this.embarkConfig.config;
Expand Down Expand Up @@ -258,7 +302,7 @@ Config.prototype.loadFiles = function(files) {
return (file[0] === '$' || file.indexOf('.') >= 0);
}).filter(function(file) {
let basedir = findMatchingExpression(file, files);
readFiles.push(new File({filename: file, type: "dapp_file", basedir: basedir, path: file}));
readFiles.push(new File({filename: file, type: File.types.dapp_file, basedir: basedir, path: file}));
});

var filesFromPlugins = [];
Expand Down Expand Up @@ -291,7 +335,7 @@ Config.prototype.loadPluginContractFiles = function() {
contractsPlugins.forEach(function(plugin) {
plugin.contractsFiles.forEach(function(file) {
var filename = file.replace('./','');
self.contractsFiles.push(new File({filename: filename, type: 'custom', resolver: function(callback) {
self.contractsFiles.push(new File({filename: filename, type: File.types.custom, resolver: function(callback) {
callback(plugin.loadPluginFile(file));
}}));
});
Expand Down
62 changes: 56 additions & 6 deletions lib/core/file.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,77 @@
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;
this.basedir = options.basedir;
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);
}
}

}

File.types = {
embark_internal: 'embark_internal',
dapp_file: 'dapp_file',
custom: 'custom',
http: 'http'
};

module.exports = File;
17 changes: 16 additions & 1 deletion lib/core/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ function mkdirpSync() {
return fs.mkdirpSync.apply(fs.mkdirpSync, arguments);
}

function mkdirp() {
return fs.mkdirp.apply(fs.mkdirp, arguments);
}

function copySync() {
return fs.copySync.apply(fs.copySync, arguments);
}
Expand All @@ -19,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);
}
Expand Down Expand Up @@ -54,9 +62,15 @@ function dappPath() {
return utils.joinPath(utils.pwd(), ...arguments);
}

function createWriteStream() {
return fs.createWriteStream.apply(fs.createWriteStream, arguments);
}

module.exports = {
mkdirpSync: mkdirpSync,
mkdirp,
copySync: copySync,
readFile,
readFileSync: readFileSync,
appendFileSync: appendFileSync,
writeFileSync: writeFileSync,
Expand All @@ -65,5 +79,6 @@ module.exports = {
existsSync: existsSync,
removeSync: removeSync,
embarkPath: embarkPath,
dappPath: dappPath
dappPath: dappPath,
createWriteStream
};
4 changes: 4 additions & 0 deletions lib/modules/solidity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Solidity {
}

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();
});
Expand Down
19 changes: 11 additions & 8 deletions lib/tests/run_tests.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var utils = require('../utils/utils.js');
const utils = require('../utils/utils.js');

module.exports = {
run: function(filepath) {
var Mocha = require('mocha'),
fs = require('fs'),
const Mocha = require('mocha'),
fs = require('fs-extra'),
path = require('path');

var mocha = new Mocha();
const mocha = new Mocha();

if (filepath) {
if (filepath.substr(-1) === '/') {
Expand Down Expand Up @@ -60,11 +60,14 @@ module.exports = {
};

// Run the tests.
let runner = mocha.run(function(failures){
process.on('exit', function () {
process.exit(failures); // exit with non-zero status if there were failures
let runner = mocha.run(function(failures) {
// Clean contracts folder for next test run
fs.remove('.embark/contracts', (_err) => {
process.on('exit', function () {
process.exit(failures); // exit with non-zero status if there were failures
});
process.exit();
});
process.exit();
});

runner.on('suite', function() {
Expand Down
Loading