1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
8 |
|
9 | var _util = require('util');
|
10 |
|
11 | var _sbEventKit = require('sb-event-kit');
|
12 |
|
13 | var _vorpal = require('vorpal');
|
14 |
|
15 | var _vorpal2 = _interopRequireDefault(_vorpal);
|
16 |
|
17 | var _chalk = require('chalk');
|
18 |
|
19 | var _chalk2 = _interopRequireDefault(_chalk);
|
20 |
|
21 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
22 |
|
23 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
24 |
|
25 | var CLI_DELIMITER = _chalk2.default.yellow('♥ motion ♥') + ' ' + _chalk2.default.red('❯') + _chalk2.default.yellow('❯') + _chalk2.default.green('❯');
|
26 | var WELCOME_MESSAGE = _chalk2.default.red('♥ ♥ ♥ ♥ ♥') + '\t' + _chalk2.default.yellow('Welcome to Motion') + '\t' + _chalk2.default.red('♥ ♥ ♥ ♥ ♥');
|
27 | var BYE_MESSAGE = _chalk2.default.red('♥ ♥ ♥ ♥ ♥') + '\t' + _chalk2.default.yellow('Bye from Motion') + '\t\t' + _chalk2.default.red('♥ ♥ ♥ ♥ ♥');
|
28 |
|
29 | var CLI = function () {
|
30 | function CLI() {
|
31 | _classCallCheck(this, CLI);
|
32 |
|
33 | this.active = false;
|
34 | this.instance = (0, _vorpal2.default)();
|
35 | this.subscriptions = new _sbEventKit.CompositeDisposable();
|
36 | }
|
37 |
|
38 | _createClass(CLI, [{
|
39 | key: 'activate',
|
40 | value: function activate() {
|
41 | if (process.versions.electron) {
|
42 |
|
43 | return;
|
44 | }
|
45 | if (this.active) {
|
46 | this.instance.show();
|
47 | return;
|
48 | }
|
49 |
|
50 | this.active = true;
|
51 | this.instance.delimiter(CLI_DELIMITER);
|
52 | this.instance.show();
|
53 | this.instance.log(WELCOME_MESSAGE);
|
54 | }
|
55 | }, {
|
56 | key: 'deactivate',
|
57 | value: function deactivate() {
|
58 | this.instance.hide();
|
59 | }
|
60 | }, {
|
61 | key: 'addCommand',
|
62 | value: function addCommand(name, helpText, callback) {
|
63 | var _this = this;
|
64 |
|
65 | this.instance.command(name, helpText).action(function (args, keepRunning) {
|
66 | var result = callback.call(_this, args);
|
67 | if (result && result.constructor.name === 'Promise') {
|
68 | result.then(keepRunning, keepRunning);
|
69 | } else keepRunning();
|
70 | });
|
71 | }
|
72 | }, {
|
73 | key: 'replaceCommand',
|
74 | value: function replaceCommand(name, helpText, callback) {
|
75 | var command = this.instance.find(name);
|
76 | if (command) {
|
77 | command.remove();
|
78 | }
|
79 | this.addCommand(name, helpText, callback);
|
80 | }
|
81 | }, {
|
82 | key: 'log',
|
83 | value: function log() {
|
84 | var contents = [];
|
85 | for (var i = 0; i < arguments.length; ++i) {
|
86 | var value = arguments[i];
|
87 | if (typeof value === 'string') {
|
88 | contents.push(value);
|
89 | } else if (value && value.constructor.name.endsWith('Error')) {
|
90 | contents.push('[' + value.constructor.name + ': ' + value.message + ' ' + value.stack.split('\n')[1].trim() + ']');
|
91 | } else {
|
92 | contents.push((0, _util.inspect)(value));
|
93 | }
|
94 | }
|
95 | if (this.active) {
|
96 | this.instance.log(contents.join(' '));
|
97 | } else {
|
98 | console.log(contents.join(' '));
|
99 | }
|
100 | }
|
101 | }, {
|
102 | key: 'dispose',
|
103 | value: function dispose() {
|
104 | if (this.active) {
|
105 | this.instance.log(BYE_MESSAGE);
|
106 | this.instance.hide();
|
107 | this.active = false;
|
108 | }
|
109 | this.subscriptions.dispose();
|
110 | }
|
111 | }]);
|
112 |
|
113 | return CLI;
|
114 | }();
|
115 |
|
116 | exports.default = CLI;
|
117 |
|
\ | No newline at end of file |