embark/cmd/cmd.js

412 lines
16 KiB
JavaScript
Raw Normal View History

2017-10-14 10:06:09 +00:00
const program = require('commander');
2018-08-08 22:07:10 +00:00
const EmbarkController = require('./cmd_controller.js');
2018-08-08 19:05:22 +00:00
const i18n = require('../lib/core/i18n/i18n.js');
2018-08-17 17:37:15 +00:00
const utils = require('../lib/utils/utils.js');
2018-08-08 22:07:10 +00:00
let embark = new EmbarkController;
2016-08-18 00:29:41 +00:00
2018-08-17 17:37:15 +00:00
// set PWD to process.cwd() since Windows doesn't have a value for PWD
if (!process.env.PWD) {
process.env.PWD = process.cwd();
}
// set the anchor for embark's fs.dappPath()
if (!process.env.DAPP_PATH) {
process.env.DAPP_PATH = process.env.PWD;
}
// set the anchor for embark's fs.embarkPath()
if (!process.env.EMBARK_PATH) {
process.env.EMBARK_PATH = utils.joinPath(__dirname, '..');
}
2018-08-19 00:36:50 +00:00
// NOTE: setting NODE_PATH at runtime won't effect lookup behavior in the
// current process, but will take effect in child processes; this enables
// lookup of *global* embark's own node_modules from within dapp scripts (such
// as an ejected webpack.config.js), making embark's dependencies trasitive
// dependencies of a dapp without the dapp explicitly specifying embark as a
// dependency in the dapp's package.json
2018-09-07 16:37:18 +00:00
process.env.NODE_PATH = utils.joinPath(process.env.EMBARK_PATH, 'node_modules') +
(process.env.NODE_PATH ? require('path').delimiter : '') +
(process.env.NODE_PATH || '');
2018-08-17 17:37:15 +00:00
function checkDeps() {
const path = require('path');
try {
const dappPackage = require(path.join(process.cwd(), 'package.json'));
require(path.join(process.cwd(), 'embark.json')); // Make sure we are in a Dapp
require('check-dependencies')(dappPackage, (state) => {
if (state.status) {
require('colors');
console.error('\nMissing dependencies. Please run npm install'.red);
process.exit();
}
return true;
});
} catch (_e) {
// We are not in a Dapp
return true;
}
}
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();
2018-07-23 08:34:21 +00:00
this.console();
2017-03-30 11:12:39 +00:00
this.blockchain();
this.simulator();
this.test();
2018-01-11 14:22:58 +00:00
this.reset();
2018-08-16 23:02:54 +00:00
this.ejectWebpack();
this.graph();
2017-03-30 11:12:39 +00:00
this.upload();
2017-12-19 15:20:05 +00:00
this.versionCmd();
2018-08-14 20:48:40 +00:00
this.helpCmd();
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
2018-09-07 16:37:18 +00:00
let validateName = function(value) {
2017-03-30 11:12:39 +00:00
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) {
2018-05-08 21:49:46 +00:00
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]')
2018-05-08 21:49:46 +00:00
.description(__('New Application'))
.option('--simple', __('create a barebones project meant only for contract development'))
2018-05-09 22:46:38 +00:00
.option('--locale [locale]', __('language to use (default: en)'))
2018-09-18 15:24:40 +00:00
.option('--template [url]', __('download template from a GitHub repository'))
2018-09-07 16:37:18 +00:00
.action(function(name, options) {
2018-05-09 22:46:38 +00:00
i18n.setOrDetectLocale(options.locale);
2017-03-30 11:12:39 +00:00
if (name === undefined) {
const promptly = require('promptly');
2018-05-08 21:49:46 +00:00
return promptly.prompt(__("Name your app (default is %s):", 'embarkDapp'), {
2017-06-27 19:45:30 +00:00
default: "embarkDApp",
2017-03-30 11:12:39 +00:00
validator: validateName
2018-09-07 16:37:18 +00:00
}, function(err, inputvalue) {
2017-03-30 11:12:39 +00:00
if (err) {
2018-05-08 21:49:46 +00:00
console.error(__('Invalid name') + ':', err.message);
2017-03-30 11:12:39 +00:00
// 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
2018-03-29 23:42:47 +00:00
if (options.simple) {
embark.generateTemplate('simple', './', inputvalue);
} else {
2018-07-06 08:38:09 +00:00
embark.generateTemplate('boilerplate', './', inputvalue, options.template);
2018-03-29 23:42:47 +00:00
}
2017-03-30 11:12:39 +00:00
}
});
2018-09-07 16:37:18 +00:00
}
if (options.simple) {
embark.generateTemplate('simple', './', name);
2017-03-30 11:12:39 +00:00
} else {
2018-09-07 16:37:18 +00:00
embark.generateTemplate('boilerplate', './', name, options.template);
2017-03-30 11:12:39 +00:00
}
});
}
demo() {
program
.command('demo')
2018-05-09 22:46:38 +00:00
.option('--locale [locale]', __('language to use (default: en)'))
2018-05-08 21:49:46 +00:00
.description(__('create a working dapp with a SimpleStorage contract'))
2018-09-07 16:37:18 +00:00
.action(function(options) {
2018-05-09 22:46:38 +00:00
i18n.setOrDetectLocale(options.locale);
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]')
.option('--contracts', 'only compile contracts into Embark wrappers')
2018-05-08 21:49:46 +00:00
.option('--logfile [logfile]', __('filename to output logs (default: none)'))
2018-09-05 13:12:26 +00:00
.option('-c, --client [client]', __('Use a specific ethereum client (supported: %s)', 'geth'))
2018-05-08 21:49:46 +00:00
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
2018-05-09 22:46:38 +00:00
.option('--locale [locale]', __('language to use (default: en)'))
.option('--pipeline [pipeline]', __('webpack config to use (default: production)'))
2018-05-08 21:49:46 +00:00
.description(__('deploy and build dapp at ') + 'dist/ (default: development)')
2018-09-07 16:37:18 +00:00
.action(function(env, _options) {
checkDeps();
2018-05-10 19:36:24 +00:00
i18n.setOrDetectLocale(_options.locale);
_options.env = env || 'development';
_options.logFile = _options.logfile; // fix casing
_options.logLevel = _options.loglevel; // fix casing
_options.onlyCompile = _options.contracts;
2018-05-22 19:46:02 +00:00
_options.client = _options.client || 'geth';
_options.webpackConfigName = _options.pipeline || 'production';
embark.build(_options);
2017-03-30 11:12:39 +00:00
});
}
run() {
program
.command('run [environment]')
2018-05-08 21:49:46 +00:00
.option('-p, --port [port]', __('port to run the dev webserver (default: %s)', '8000'))
2018-09-05 13:12:26 +00:00
.option('-c, --client [client]', __('Use a specific ethereum client (supported: %s)', 'geth'))
2018-05-08 21:49:46 +00:00
.option('-b, --host [host]', __('host to run the dev webserver (default: %s)', 'localhost'))
.option('--noserver', __('disable the development webserver'))
.option('--nodashboard', __('simple mode, disables the dashboard'))
2018-09-17 23:50:47 +00:00
.option('--nobrowser', __('prevent the development webserver from automatically opening a web browser'))
2018-05-08 21:49:46 +00:00
.option('--no-color', __('no colors in case it\'s needed for compatbility purposes'))
.option('--logfile [logfile]', __('filename to output logs (default: %s)', 'none'))
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
.option('--locale [locale]', __('language to use (default: en)'))
.option('--pipeline [pipeline]', __('webpack config to use (default: development)'))
2018-05-08 21:49:46 +00:00
.description(__('run dapp (default: %s)', 'development'))
2018-09-07 16:37:18 +00:00
.action(function(env, options) {
checkDeps();
i18n.setOrDetectLocale(options.locale);
const nullify = (v) => (!v || typeof v !== 'string') ? null : v;
2017-03-30 13:16:46 +00:00
embark.run({
2017-03-30 11:12:39 +00:00
env: env || 'development',
serverPort: nullify(options.port),
serverHost: nullify(options.host),
2018-07-23 08:34:21 +00:00
client: options.client || 'geth',
locale: options.locale,
runWebserver: options.noserver == null ? null : !options.noserver,
2018-07-23 08:34:21 +00:00
useDashboard: !options.nodashboard,
logFile: options.logfile,
logLevel: options.loglevel,
2018-09-17 22:46:48 +00:00
webpackConfigName: options.pipeline || 'development',
openBrowser: options.nobrowser == null ? null : !options.nobrowser,
2018-07-23 08:34:21 +00:00
});
});
}
console() {
program
.command('console [environment]')
2018-09-05 13:12:26 +00:00
.option('-c, --client [client]', __('Use a specific ethereum client (supported: %s)', 'geth'))
2018-07-23 08:34:21 +00:00
.option('--logfile [logfile]', __('filename to output logs (default: %s)', 'none'))
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
.option('--locale [locale]', __('language to use (default: en)'))
.option('--pipeline [pipeline]', __('webpack config to use (default: development)'))
2018-07-23 12:36:15 +00:00
.description(__('Start the Embark console'))
2018-09-07 16:37:18 +00:00
.action(function(env, options) {
checkDeps();
2018-07-23 08:34:21 +00:00
i18n.setOrDetectLocale(options.locale);
embark.console({
env: env || 'development',
client: options.client || 'geth',
locale: options.locale,
logFile: options.logfile,
logLevel: options.loglevel,
webpackConfigName: options.pipeline || 'development'
2017-03-30 11:12:39 +00:00
});
});
}
blockchain() {
program
.command('blockchain [environment]')
2018-09-05 13:12:26 +00:00
.option('-c, --client [client]', __('Use a specific ethereum client (supported: %s)', 'geth'))
2018-05-09 22:46:38 +00:00
.option('--locale [locale]', __('language to use (default: en)'))
2018-05-08 21:49:46 +00:00
.description(__('run blockchain server (default: %s)', 'development'))
2018-09-07 16:37:18 +00:00
.action(function(env, options) {
checkDeps();
2018-05-09 22:46:38 +00:00
i18n.setOrDetectLocale(options.locale);
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]')
2018-05-08 21:49:46 +00:00
.description(__('run a fast ethereum rpc simulator'))
.option('--testrpc', __('use testrpc as the rpc simulator [%s]', 'default'))
.option('-p, --port [port]', __('port to run the rpc simulator (default: %s)', '8545'))
.option('-h, --host [host]', __('host to run the rpc simulator (default: %s)', 'localhost'))
.option('-a, --accounts [numAccounts]', __('number of accounts (default: %s)', '10'))
.option('-e, --defaultBalanceEther [balance]', __('Amount of ether to assign each test account (default: %s)', '100'))
.option('-l, --gasLimit [gasLimit]', __('custom gas limit (default: %s)', '8000000'))
2018-05-09 22:46:38 +00:00
.option('--locale [locale]', __('language to use (default: en)'))
2017-12-27 22:48:33 +00:00
2018-09-07 16:37:18 +00:00
.action(function(env, options) {
checkDeps();
2018-05-09 22:46:38 +00:00
i18n.setOrDetectLocale(options.locale);
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-12-27 22:48:33 +00:00
embark.simulator({
port: options.port,
host: options.host,
numAccounts: options.numAccounts,
defaultBalance: options.balance,
gasLimit: options.gasLimit
});
2017-03-30 11:12:39 +00:00
});
}
test() {
program
.command('test [file]')
.option('-n , --node <node>', __('Node to connect to. Valid values are ["vm", "embark", "<custom node endpoint>"]: \n') +
' vm - ' + __('Starts an Ethereum simulator (ganache) and runs the tests using the simulator') + '\n' +
' embark - ' + __('Uses the node associated with an already running embark process') + '\n' +
' ' + __('<custom node endpoint> - Connects to a running node available at the end point and uses it to run the tests'))
2018-09-18 17:55:37 +00:00
.option('-d , --gasDetails', __('When set, will print the gas cost for each contract deployment'))
2018-09-13 18:30:15 +00:00
.option('-c , --coverage', __('When set, will generate the coverage after the tests'))
2018-05-09 22:46:38 +00:00
.option('--locale [locale]', __('language to use (default: en)'))
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'warn')
2018-05-08 21:49:46 +00:00
.description(__('run tests'))
2018-09-07 16:37:18 +00:00
.action(function(file, options) {
const node = options.node || 'vm';
const urlRegexExp = /^(vm|embark|((ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?))$/i;
if (!urlRegexExp.test(node)) {
console.error(`invalid --node option: must be 'vm', 'embark' or a valid URL\n`.red);
options.outputHelp();
process.exit(1);
}
options.node = node;
checkDeps();
2018-05-09 22:46:38 +00:00
i18n.setOrDetectLocale(options.locale);
2018-09-13 18:30:15 +00:00
embark.runTests({file, loglevel: options.loglevel, gasDetails: options.gasDetails,
node: options.node, coverage: options.coverage});
2017-03-30 11:12:39 +00:00
});
}
upload() {
program
.command('upload [environment]')
.option('--ens [ensDomain]', __('ENS domain to associate to'))
2018-05-08 21:49:46 +00:00
.option('--logfile [logfile]', __('filename to output logs (default: %s)', 'none'))
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
2018-05-09 22:46:38 +00:00
.option('--locale [locale]', __('language to use (default: en)'))
2018-09-05 13:12:26 +00:00
.option('-c, --client [client]', __('Use a specific ethereum client (supported: %s)', 'geth'))
.option('--pipeline [pipeline]', __('webpack config to use (default: production)'))
.description(__('Upload your dapp to a decentralized storage') + '.')
2018-09-07 16:37:18 +00:00
.action(function(env, _options) {
checkDeps();
2018-05-10 19:36:24 +00:00
i18n.setOrDetectLocale(_options.locale);
2018-06-19 18:07:14 +00:00
if (env === "ipfs" || env === "swarm") {
console.warn(("did you mean " + "embark upload".bold + " ?").underline);
console.warn("In embark 3.1 forwards, the correct command is embark upload <environment> and the provider is configured in config/storage.js");
}
_options.env = env || 'development';
_options.ensDomain = _options.ens;
_options.logFile = _options.logfile; // fix casing
_options.logLevel = _options.loglevel; // fix casing
2018-05-22 19:46:58 +00:00
_options.client = _options.client || 'geth';
_options.webpackConfigName = _options.pipeline || 'production';
embark.upload(_options);
2017-03-30 11:12:39 +00:00
});
}
graph() {
program
.command('graph [environment]')
2018-05-08 21:49:46 +00:00
.option('--skip-undeployed', __('Graph will not include undeployed contracts'))
.option('--skip-functions', __('Graph will not include functions'))
.option('--skip-events', __('Graph will not include events'))
2018-05-09 22:46:38 +00:00
.option('--locale [locale]', __('language to use (default: en)'))
2018-05-08 21:49:46 +00:00
.description(__('generates documentation based on the smart contracts configured'))
2018-09-07 16:37:18 +00:00
.action(function(env, options) {
checkDeps();
2018-05-09 22:46:38 +00:00
i18n.setOrDetectLocale(options.locale);
embark.graph({
env: env || 'development',
logFile: options.logfile,
skipUndeployed: options.skipUndeployed,
skipFunctions: options.skipFunctions,
skipEvents: options.skipEvents
});
});
}
2018-01-11 14:22:58 +00:00
reset() {
program
.command('reset')
2018-05-09 22:46:38 +00:00
.option('--locale [locale]', __('language to use (default: en)'))
2018-05-08 21:49:46 +00:00
.description(__('resets embarks state on this dapp including clearing cache'))
2018-09-07 16:37:18 +00:00
.action(function(options) {
2018-05-09 22:46:38 +00:00
i18n.setOrDetectLocale(options.locale);
2018-01-11 14:22:58 +00:00
embark.initConfig('development', {
embarkConfig: 'embark.json', interceptLogs: false
});
embark.reset();
});
}
2018-08-16 23:02:54 +00:00
ejectWebpack() {
program
.command('eject-webpack')
.description(__('copy the default webpack config into your dapp for customization'))
2018-09-07 16:37:18 +00:00
.action(function() {
embark.initConfig('development', {
embarkConfig: 'embark.json',
interceptLogs: false
});
2018-08-16 23:02:54 +00:00
embark.ejectWebpack();
});
}
2017-12-19 15:20:05 +00:00
versionCmd() {
program
2018-09-07 16:37:18 +00:00
.command('version')
.description(__('output the version number'))
.action(function() {
console.log(embark.version);
process.exit(0);
});
2017-12-19 15:20:05 +00:00
}
2018-08-14 20:48:40 +00:00
helpCmd() {
program
2018-09-07 16:37:18 +00:00
.command('help')
.description(__('output usage information and help information'))
.action(function() {
console.log("Documentation can be found at: ".green + "https://embark.status.im/docs/".underline.green);
console.log("");
console.log("Have an issue? submit it here: ".green + "https://github.com/embark-framework/embark/issues/new".underline.green);
console.log("or chat with us directly at: ".green + "https://gitter.im/embark-framework/Lobby".underline.green);
program.help();
process.exit(0);
});
2018-08-14 20:48:40 +00:00
}
2017-03-30 11:12:39 +00:00
otherCommands() {
program
2018-09-07 16:37:18 +00:00
.action(function(cmd) {
2018-05-08 21:49:46 +00:00
console.log((__('unknown command') + ' "%s"').red, cmd);
2018-08-08 19:05:22 +00:00
let utils = require('../lib/utils/utils.js');
2017-12-19 20:48:56 +00:00
let dictionary = ['new', 'demo', 'build', 'run', 'blockchain', 'simulator', 'test', 'upload', 'version'];
2017-12-19 19:07:48 +00:00
let suggestion = utils.proposeAlternative(cmd, dictionary);
2017-12-19 18:29:48 +00:00
if (suggestion) {
2018-05-08 21:49:46 +00:00
console.log((__('did you mean') + ' "%s"?').green, suggestion);
2017-12-19 18:29:48 +00:00
}
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;