embark-area-51/lib/cmd.js

188 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-10-14 10:06:09 +00:00
const program = require('commander');
const promptly = require('promptly');
2017-12-19 18:29:48 +00:00
const propose = require('propose');
2017-03-30 13:16:46 +00:00
const Embark = require('../lib/index');
let embark = new Embark;
2016-08-18 00:29:41 +00:00
2017-03-30 11:12:39 +00:00
class Cmd {
constructor() {
2017-03-30 13:16:46 +00:00
program.version(embark.version);
}
2016-08-18 00:29:41 +00:00
2017-03-30 11:12:39 +00:00
process(args) {
this.newApp();
this.demo();
this.build();
this.run();
this.blockchain();
this.simulator();
this.test();
this.upload();
2017-12-19 15:20:05 +00:00
this.versionCmd();
2017-03-30 11:12:39 +00:00
this.otherCommands();
//If no arguments are passed display help by default
if (!process.argv.slice(2).length) {
program.help();
}
2017-03-30 11:12:39 +00:00
program.parse(args);
}
2017-10-14 10:06:09 +00:00
newApp() {
2017-03-30 11:12:39 +00:00
let validateName = function (value) {
try {
2017-10-14 10:06:09 +00:00
if (value.match(/^[a-zA-Z\s-]+$/)) return value;
2017-03-30 11:12:39 +00:00
} catch (e) {
throw new Error('Name must be only letters, spaces, or dashes');
2017-03-22 05:13:58 +00:00
}
2017-03-30 11:12:39 +00:00
};
program
.command('new [name]')
.description('new application')
.action(function (name) {
if (name === undefined) {
2017-06-27 19:45:30 +00:00
return promptly.prompt("Name your app (default is embarkDApp):", {
default: "embarkDApp",
2017-03-30 11:12:39 +00:00
validator: validateName
}, function (err, inputvalue) {
if (err) {
console.error('Invalid name:', err.message);
// Manually call retry
// The passed error has a retry method to easily prompt again.
err.retry();
} else {
//slightly different assignment of name since it comes from child prompt
2017-03-30 13:16:46 +00:00
embark.generateTemplate('boilerplate', './', inputvalue);
2017-03-30 11:12:39 +00:00
}
});
} else {
2017-03-30 13:16:46 +00:00
embark.generateTemplate('boilerplate', './', name);
2017-03-30 11:12:39 +00:00
}
});
}
demo() {
program
.command('demo')
.description('create a working dapp with a SimpleStorage contract')
.action(function () {
2017-03-30 13:16:46 +00:00
embark.generateTemplate('demo', './', 'embark_demo');
2017-03-30 11:12:39 +00:00
});
}
build() {
program
.command('build [environment]')
.description('deploy and build dapp at dist/ (default: development)')
2017-10-14 10:06:09 +00:00
.action(function (env, _options) {
2017-03-30 13:16:46 +00:00
embark.build({env: env || 'development'});
2017-03-30 11:12:39 +00:00
});
}
run() {
program
.command('run [environment]')
.option('-p, --port [port]', 'port to run the dev webserver (default: 8000)')
.option('-b, --host [host]', 'host to run the dev webserver (default: localhost)')
.option('--noserver', 'disable the development webserver')
.option('--nodashboard', 'simple mode, disables the dashboard')
.option('--no-color', 'no colors in case it\'s needed for compatbility purposes')
.description('run dapp (default: development)')
.action(function (env, options) {
2017-03-30 13:16:46 +00:00
embark.run({
2017-03-30 11:12:39 +00:00
env: env || 'development',
serverPort: options.port,
serverHost: options.host,
runWebserver: !options.noserver,
useDashboard: !options.nodashboard
});
});
}
blockchain() {
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) {
2017-03-30 13:16:46 +00:00
embark.initConfig(env || 'development', {
2017-03-30 11:12:39 +00:00
embarkConfig: 'embark.json',
interceptLogs: false
});
2017-03-30 13:16:46 +00:00
embark.blockchain(env || 'development', options.client || 'geth');
2017-03-30 11:12:39 +00:00
});
}
simulator() {
program
.command('simulator [environment]')
.description('run a fast ethereum rpc simulator')
.option('--testrpc', 'use testrpc as the rpc simulator [default]')
.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) {
2017-03-30 13:16:46 +00:00
embark.initConfig(env || 'development', {
2017-03-30 11:12:39 +00:00
embarkConfig: 'embark.json',
interceptLogs: false
});
2017-03-30 13:16:46 +00:00
embark.simulator({port: options.port, host: options.host});
2017-03-30 11:12:39 +00:00
});
}
test() {
program
.command('test [file]')
2017-03-30 11:12:39 +00:00
.description('run tests')
.action(function (file) {
embark.initConfig('development', {
embarkConfig: 'embark.json', interceptLogs: false
});
embark.runTests(file);
2017-03-30 11:12:39 +00:00
});
}
upload() {
program
.command('upload [platform] [environment]')
.description('upload your dapp to a decentralized storage. possible options: ipfs, swarm (e.g embark upload swarm)')
2017-10-14 10:06:09 +00:00
.action(function (platform, env, _options) {
2017-03-30 11:12:39 +00:00
// TODO: get env in cmd line as well
2017-03-30 13:16:46 +00:00
embark.initConfig(env || 'development', {
2017-03-30 11:12:39 +00:00
embarkConfig: 'embark.json', interceptLogs: false
});
2017-03-30 13:16:46 +00:00
embark.upload(platform);
2017-03-30 11:12:39 +00:00
});
}
2017-12-19 15:20:05 +00:00
versionCmd() {
program
.command('version')
.description('output the version number')
.action(function () {
console.log(embark.version);
process.exit(0);
});
}
2017-03-30 11:12:39 +00:00
otherCommands() {
program
2017-12-19 18:29:48 +00:00
.action(function (cmd) {
console.log('unknown command "%s"'.red, cmd);
let dictionary = ['new', 'demo', 'build', 'run', 'blockchain', 'simulator', 'test', 'upload', 'version' ];
let suggestion = propose(cmd, dictionary, {threshold: 0.3});
if (suggestion) {
console.log('did you mean "%s"?'.green, suggestion);
}
2017-03-30 11:12:39 +00:00
console.log("type embark --help to see the available commands");
process.exit(0);
});
}
2017-03-22 05:13:58 +00:00
2017-03-30 11:12:39 +00:00
}
2016-08-18 00:29:41 +00:00
module.exports = Cmd;