refactor config into its own module

This commit is contained in:
Iuri Matias 2016-08-21 23:40:05 -04:00
parent 95615d14a7
commit b9c93fe97d
10 changed files with 141 additions and 55 deletions

View File

@ -5,5 +5,6 @@
"js/app.js": ["embark.js", "app/js/**"],
"index.html": "app/index.html"
},
"buildDir": "dist/",
"config": "config/"
}

View File

@ -5,5 +5,6 @@
"js/app.js": ["embark.js", "app/js/**"],
"index.html": "app/index.html"
},
"buildDir": "dist/",
"config": "config/"
}

View File

@ -48,6 +48,9 @@ Cmd.prototype.build = function() {
.command('build [environment]')
.description('deploy and build dapp at dist/ (default: development)')
.action(function(env, options) {
self.Embark.initConfig(env || 'development', {
embarkConfig: 'embark.json'
});
self.Embark.build(env || 'development');
});
};
@ -58,6 +61,9 @@ Cmd.prototype.run = function() {
.command('run [environment]')
.description('run dapp (default: development)')
.action(function(env, options) {
self.Embark.initConfig(env || 'development', {
embarkConfig: 'embark.json'
});
self.Embark.run(env || 'development');
});
};
@ -69,6 +75,9 @@ Cmd.prototype.blockchain = function() {
.option('-c, --client [client]', 'Use a specific ethereum client or simulator (supported: geth, parity, ethersim, testrpc')
.description('run blockchain server (default: development)')
.action(function(env ,options) {
self.Embark.initConfig(env || 'development', {
embarkConfig: 'embark.json'
});
self.Embark.blockchain(env || 'development', options.client || 'geth');
});
};

View File

@ -11,8 +11,8 @@ Compiler.prototype.compile_solidity = function(contractFiles) {
for (var i = 0; i < contractFiles.length; i++){
// TODO: this depends on the config
var filename = contractFiles[i].replace('app/contracts/','');
input[filename] = fs.readFileSync(contractFiles[i]).toString();
var filename = contractFiles[i].filename.replace('app/contracts/','');
input[filename] = contractFiles[i].content.toString();
}
var output = solc.compile({sources: input}, 1);

67
lib/config.js Normal file
View File

@ -0,0 +1,67 @@
var fs = require('fs');
var grunt = require('grunt');
var Config = function(env) {
this.env = env;
this.blockchainConfig = {};
this.contractsConfig = {};
this.pipelineConfig = {};
this.assetFiles = {};
this.contractsFiles = [];
this.configDir = 'config/';
};
Config.prototype.loadConfigFiles = function(options) {
this.embarkConfig = JSON.parse(fs.readFileSync(options.embarkConfig));
this.loadPipelineConfigFile();
this.loadBlockchainConfigFile();
this.loadContractsConfigFile();
};
Config.prototype.loadBlockchainConfigFile = function() {
var defaultBlockchainConfig = JSON.parse(fs.readFileSync(this.configDir + this.env + "/blockchain.json"))[this.env];
this.blockchainConfig = defaultBlockchainConfig;
};
Config.prototype.loadContractsConfigFile = function() {
var defaultContractsConfig = JSON.parse(fs.readFileSync(this.configDir + "contracts.json"))['default'];
//var envContractsConfig = JSON.parse(fs.readFileSync(this.configDir + this.env + "/contracts.json"))[this.env];
//merge.recursive(defaultContractsConfig, envContractsConfig);
this.contractsConfig = defaultContractsConfig;
};
Config.prototype.loadPipelineConfigFile = function() {
var contracts = this.embarkConfig.contracts;
this.contractsFiles = this.loadFiles(contracts);
var assets = this.embarkConfig.app;
for(var targetFile in assets) {
this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]);
}
this.buildDir = this.embarkConfig.buildDir;
this.configDir = this.embarkConfig.config;
};
Config.prototype.loadFiles = function(files) {
var originalFiles = grunt.file.expand({nonull: true}, files);
var readFiles = [];
originalFiles.filter(function(file) {
return file.indexOf('.') >= 0;
}).filter(function(file) {
if (file === 'embark.js') {
readFiles.push({filename: 'bluebird.js', content: fs.readFileSync("../js/bluebird.js").toString()});
readFiles.push({filename: 'web3.js', content: fs.readFileSync("../js/web3.js").toString()});
readFiles.push({filename: 'embark.js', content: fs.readFileSync("../js/embark.js").toString()});
} else {
readFiles.push({filename: file, content: fs.readFileSync(file).toString()});
}
});
return readFiles;
};
module.exports = Config;

View File

@ -1,27 +1,15 @@
var fs = require('fs');
var grunt = require('grunt');
var Compiler = require('./compiler.js');
var ContractsManager = function(configDir, contractFiles, env) {
this.contractFiles = grunt.file.expand(contractFiles);
this.configDir = configDir;
this.env = env;
var ContractsManager = function(options) {
this.contractFiles = options.contractFiles;
this.contractsConfig = options.contractsConfig;
this.contracts = {};
};
ContractsManager.prototype.init = function() {
this.contractsConfig = this.loadConfigFiles();
this.compiledContracts = this.compileContracts();
};
ContractsManager.prototype.loadConfigFiles = function() {
var defaultContractsConfig = JSON.parse(fs.readFileSync(this.configDir + "contracts.json"))['default'];
//var envContractsConfig = JSON.parse(fs.readFileSync(this.configDir + this.env + "/contracts.json"))[this.env];
//merge.recursive(defaultContractsConfig, envContractsConfig);
return defaultContractsConfig;
};
ContractsManager.prototype.compileContracts = function() {
var compiler = new Compiler();
return compiler.compile_solidity(this.contractFiles);

View File

@ -17,6 +17,8 @@ var Server = require('./server.js');
var Watch = require('./watch.js');
var Pipeline = require('./pipeline.js');
var Test = require('./test.js');
var Logger = require('./logger.js');
var Config = require('./config.js');
var Embark = {
@ -30,13 +32,17 @@ var Embark = {
templateGenerator.generate(destinationFolder, name);
},
initConfig: function(configDir, files, env) {
this.contractsManager = new ContractsManager(configDir, files, env);
this.contractsManager.init();
return this.contractsManager;
initConfig: function(env, options) {
this.config = new Config(env);
this.config.loadConfigFiles(options);
//this.contractsManager = new ContractsManager(configDir, files, env);
//this.contractsManager.init();
//return this.contractsManager;
},
run: function(env) {
var self = this;
async.waterfall([
function deployAndGenerateABI(callback) {
Embark.deploy(function(abi) {
@ -44,7 +50,11 @@ var Embark = {
});
},
function buildPipeline(abi, callback) {
var pipeline = new Pipeline({});
var pipeline = new Pipeline({
buildDir: self.config.buildDir,
contractsFiles: self.config.contractsFiles,
assetFiles: self.config.assetFiles
});
pipeline.build(abi);
callback();
},
@ -85,12 +95,14 @@ var Embark = {
},
deploy: function(done) {
var self = this;
async.waterfall([
function loadConfig(callback) {
var contractsManager = Embark.initConfig('config/', 'app/contracts/**/*.sol', 'development');
callback(null, contractsManager);
},
function buildContracts(contractsManager, callback) {
function buildContracts(callback) {
var contractsManager = new ContractsManager({
contractFiles: self.config.contractsFiles,
contractsConfig: self.config.contractsConfig
});
contractsManager.init();
contractsManager.build();
callback(null, contractsManager);
},

24
lib/logger.js Normal file
View File

@ -0,0 +1,24 @@
var colors = require('colors');
var Logger = {
logLevel: 'info',
info: function(txt) {
console.log(txt.blue);
},
log: function(txt) {
console.log(txt);
},
warn: function(txt) {
console.log(txt.yellow);
},
error: function(txt) {
console.log(txt.red);
}
};
module.exports = Logger;

View File

@ -3,36 +3,28 @@ var grunt = require('grunt');
var mkdirp = require('mkdirp');
var Pipeline = function(options) {
this.options = options;
this.buildDir = options.buildDir;
this.contractsFiles = options.contractsFiles;
this.assetFiles = options.assetFiles;
};
Pipeline.prototype.build = function(abi) {
var embarkConfig = JSON.parse(fs.readFileSync("embark.json"));
for(var targetFile in this.assetFiles) {
var appConfig = embarkConfig.app;
for(var targetFile in appConfig) {
var originalFiles = grunt.file.expand({nonull: true}, appConfig[targetFile]);
console.log(originalFiles);
// remove duplicates
var content = originalFiles.filter(function(file) {
return file.indexOf('.') >= 0;
}).map(function(file) {
console.log("reading " + file);
if (file === 'embark.js') {
return fs.readFileSync("../js/bluebird.js") + fs.readFileSync("../js/web3.js") + fs.readFileSync("../js/embark.js") + "\n" + abi;
var content = this.assetFiles[targetFile].map(function(file) {
console.log("reading " + file.filename);
if (file.filename === 'embark.js') {
return file.content + "\n" + abi;
} else {
return fs.readFileSync(file);
return file.content;
}
}).join("\n");
var dir = targetFile.split('/').slice(0, -1).join('/');
console.log("creating dir " + "dist/" + dir);
mkdirp.sync("dist/" + dir);
console.log("creating dir " + this.buildDir + dir);
mkdirp.sync(this.buildDir + dir);
//console.log(content);
fs.writeFileSync("dist/" + targetFile, content);
fs.writeFileSync(this.buildDir + targetFile, content);
}
};

View File

@ -25,14 +25,6 @@ var Test = function(options) {
}
};
//Test.prototype.deployAll = function(cb) {
// var web3 = this.web3;
// Embark.deployContracts('development', this.contractFiles, "/tmp/abi.js", "chains.json", false, false, function(abi) {
// eval(abi);
// cb();
// });
//};
Test.prototype.deployContract = function(className, args, cb) {
var self = this;
this.web3 = new Web3();