UNPKG

3.55 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = undefined;
7
8var _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; }; }();
9
10var _util = require('util');
11
12var _vorpal = require('vorpal');
13
14var _vorpal2 = _interopRequireDefault(_vorpal);
15
16var _chalk = require('chalk');
17
18var _chalk2 = _interopRequireDefault(_chalk);
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
24var CLI_DELIMITER = '' + _chalk2.default.yellow('❯');
25var BYE_MESSAGE = '' + _chalk2.default.yellow('Bye');
26
27var CLI = function () {
28 function CLI() {
29 _classCallCheck(this, CLI);
30
31 this.active = false;
32 this.instance = (0, _vorpal2.default)();
33 }
34
35 _createClass(CLI, [{
36 key: 'activate',
37 value: function activate(projectName) {
38 if (process.versions.electron) {
39 return;
40 }
41 if (this.active) {
42 this.instance.show();
43 return;
44 }
45
46 this.active = true;
47 this.instance.delimiter(' ' + _chalk2.default.green(projectName) + ' ' + CLI_DELIMITER);
48 this.instance.show();
49 }
50 }, {
51 key: 'deactivate',
52 value: function deactivate() {
53 this.instance.hide();
54 }
55 }, {
56 key: 'addCommand',
57 value: function addCommand(name, helpText, callback) {
58 var _this = this;
59
60 this.instance.command(name, helpText).action(function (args, keepRunning) {
61 var result = callback.call(_this, args);
62 if (result && result.constructor.name === 'Promise') {
63 result.then(keepRunning, keepRunning);
64 } else keepRunning();
65 });
66 }
67 }, {
68 key: 'replaceCommand',
69 value: function replaceCommand(name, helpText, callback) {
70 var command = this.instance.find(name);
71 if (command) {
72 command.remove();
73 }
74 this.addCommand(name, helpText, callback);
75 }
76 }, {
77 key: 'log',
78 value: function log() {
79 var contents = [];
80
81 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
82 args[_key] = arguments[_key];
83 }
84
85 for (var i = 0; i < args.length; ++i) {
86 var value = args[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.hide();
106 this.active = false;
107 this.log(BYE_MESSAGE);
108 }
109 }
110 }]);
111
112 return CLI;
113}();
114
115exports.default = CLI;
\No newline at end of file