Merge pull request #242 from toadkicker/make_cli_faster

this is a quick attempt at speeding up the init of CLI…
This commit is contained in:
Iuri Matias 2017-03-11 21:18:20 -05:00 committed by GitHub
commit 1534bf6207
3 changed files with 51 additions and 59 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env node
var Embark = require('..');
Embark.process(process.argv);
var Cmd = require('../lib/cmd');
var cli = new Cmd();
cli.process(process.argv);

View File

@ -1,9 +1,9 @@
var program = require('commander');
var colors = require('colors');
var shelljs = require('shelljs');
var Embark = require('../lib/index')();
var Cmd = function(Embark) {
this.Embark = Embark;
var Cmd = function() {
program.version(Embark.version);
};
@ -27,7 +27,6 @@ Cmd.prototype.process = function(args) {
};
Cmd.prototype.newApp = function() {
var self = this;
program
.command('new [name]')
.description('new application')
@ -39,32 +38,29 @@ Cmd.prototype.newApp = function() {
console.log("e.g embark new --help for more information".green);
process.exit(9);
}
self.Embark.generateTemplate('boilerplate', './', name);
Embark.generateTemplate('boilerplate', './', name);
});
};
Cmd.prototype.demo = function() {
var self = this;
program
.command('demo')
.description('create a working dapp with a SimpleStorage contract')
.action(function() {
self.Embark.generateTemplate('demo', './', 'embark_demo');
Embark.generateTemplate('demo', './', 'embark_demo');
});
};
Cmd.prototype.build = function() {
var self = this;
program
.command('build [environment]')
.description('deploy and build dapp at dist/ (default: development)')
.action(function(env, options) {
self.Embark.build({env: env || 'development'});
Embark.build({env: env || 'development'});
});
};
Cmd.prototype.run = function() {
var self = this;
program
.command('run [environment]')
.option('-p, --port [port]', 'port to run the dev webserver (default: 8000)')
@ -74,7 +70,7 @@ Cmd.prototype.run = function() {
.option('--no-color', 'no colors in case it\'s needed for compatbility purposes')
.description('run dapp (default: development)')
.action(function(env, options) {
self.Embark.run({
Embark.run({
env: env || 'development',
serverPort: options.port,
serverHost: options.host,
@ -85,22 +81,20 @@ Cmd.prototype.run = function() {
};
Cmd.prototype.blockchain = function() {
var self = this;
program
.command('blockchain [environment]')
.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', {
Embark.initConfig(env || 'development', {
embarkConfig: 'embark.json',
interceptLogs: false
});
self.Embark.blockchain(env || 'development', options.client || 'geth');
Embark.blockchain(env || 'development', options.client || 'geth');
});
};
Cmd.prototype.simulator = function() {
var self = this;
program
.command('simulator [environment]')
.description('run a fast ethereum rpc simulator')
@ -108,11 +102,11 @@ Cmd.prototype.simulator = function() {
.option('-p, --port [port]', 'port to run the rpc simulator (default: 8000)')
.option('-h, --host [host]', 'host to run the rpc simulator (default: localhost)')
.action(function(env, options) {
self.Embark.initConfig(env || 'development', {
Embark.initConfig(env || 'development', {
embarkConfig: 'embark.json',
interceptLogs: false
});
self.Embark.simulator({port: options.port, host: options.host});
Embark.simulator({port: options.port, host: options.host});
});
};
@ -126,16 +120,15 @@ Cmd.prototype.test = function() {
};
Cmd.prototype.upload = function() {
var self = this;
program
.command('upload [platform] [environment]')
.description('upload your dapp to a decentralized storage. possible options: ipfs, swarm (e.g embark upload swarm)')
.action(function(platform, env, options) {
// TODO: get env in cmd line as well
self.Embark.initConfig(env || 'development', {
Embark.initConfig(env || 'development', {
embarkConfig: 'embark.json', interceptLogs: false
});
self.Embark.upload(platform);
Embark.upload(platform);
});
};

View File

@ -6,58 +6,46 @@ var colors = require('colors');
var Engine = require('./core/engine.js');
var Blockchain = require('./cmds/blockchain/blockchain.js');
var Simulator = require('./cmds/simulator.js');
var TemplateGenerator = require('./cmds/template_generator.js');
var Test = require('./core/test.js');
var Logger = require('./core/logger.js');
var Config = require('./core/config.js');
var Events = require('./core/events.js');
var Dashboard = require('./dashboard/dashboard.js');
var IPFS = require('./upload/ipfs.js');
var Swarm = require('./upload/swarm.js');
var Cmd = require('./cmd.js');
var version = require('../package.json').version;
var Embark = {
var Embark = function () {
function initConfig (env, options) {
var Events = require('./core/events.js');
var Logger = require('./core/logger.js');
var Config = require('./core/config.js');
version: version,
process: function(args) {
var cmd = new Cmd(Embark);
cmd.process(args);
},
initConfig: function(env, options) {
this.events = new Events();
this.logger = new Logger({logLevel: 'debug'});
this.config = new Config({env: env, logger: this.logger, events: this.events});
this.config.loadConfigFiles(options);
this.plugins = this.config.plugins;
},
}
blockchain: function(env, client) {
var blockchain = Blockchain(this.config.blockchainConfig, client, env);
blockchain.run();
},
function blockchain (env, client) {
return require('./cmds/blockchain/blockchain.js')(this.config.blockchainConfig, client, env).run();
}
simulator: function(options) {
function simulator (options) {
var Simulator = require('./cmds/simulator.js');
var simulator = new Simulator({blockchainConfig: this.config.blockchainConfig});
simulator.run(options);
},
}
generateTemplate: function(templateName, destinationFolder, name) {
function generateTemplate (templateName, destinationFolder, name) {
var TemplateGenerator = require('./cmds/template_generator.js');
var templateGenerator = new TemplateGenerator(templateName);
templateGenerator.generate(destinationFolder, name);
},
}
function run (options) {
var Dashboard = require('./dashboard/dashboard.js');
run: function(options) {
var self = this;
var env = options.env;
var engine = new Engine({
@ -139,10 +127,9 @@ var Embark = {
engine.events.emit('firstDeploymentDone');
}
});
},
}
build: function(options) {
var self = this;
function build (options) {
var engine = new Engine({
env: options.env,
@ -177,14 +164,14 @@ var Embark = {
// needed due to child processes
process.exit();
});
},
}
initTests: function(options) {
function initTests (options) {
return new Test(options);
},
}
// TODO: should deploy if it hasn't already
upload: function(platform) {
function upload (platform) {
if (platform === 'ipfs') {
var ipfs = new IPFS({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig});
ipfs.deploy();
@ -197,6 +184,18 @@ var Embark = {
}
}
return {
version: version,
initConfig: initConfig,
blockchain: blockchain,
simulator: simulator,
generateTemplate: generateTemplate,
run: run,
build: build,
initTests: initTests,
upload: upload
};
};
module.exports = Embark;