1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = undefined;
|
7 |
|
8 | 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; }; }();
|
9 |
|
10 | var _ora = require('ora');
|
11 |
|
12 | var _ora2 = _interopRequireDefault(_ora);
|
13 |
|
14 | var _open = require('open');
|
15 |
|
16 | var _open2 = _interopRequireDefault(_open);
|
17 |
|
18 | var _path = require('path');
|
19 |
|
20 | var _path2 = _interopRequireDefault(_path);
|
21 |
|
22 | var _chalk = require('chalk');
|
23 |
|
24 | var _chalk2 = _interopRequireDefault(_chalk);
|
25 |
|
26 | var _lodash = require('lodash.uniq');
|
27 |
|
28 | var _lodash2 = _interopRequireDefault(_lodash);
|
29 |
|
30 | var _stripAnsi = require('strip-ansi');
|
31 |
|
32 | var _stripAnsi2 = _interopRequireDefault(_stripAnsi);
|
33 |
|
34 | var _sbExec = require('sb-exec');
|
35 |
|
36 | var _sbEventKit = require('sb-event-kit');
|
37 |
|
38 | var _vorpal = require('./vorpal');
|
39 |
|
40 | var _vorpal2 = _interopRequireDefault(_vorpal);
|
41 |
|
42 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
43 |
|
44 | function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
|
45 |
|
46 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
47 |
|
48 | var SPINNER_GLUE = ' & ';
|
49 |
|
50 | var CLI = function () {
|
51 | function CLI(projectPath, config) {
|
52 | _classCallCheck(this, CLI);
|
53 |
|
54 | this.vorpal = new _vorpal2.default();
|
55 | this.active = false;
|
56 | this.config = config;
|
57 | this.emitter = new _sbEventKit.Emitter();
|
58 | this.projectPath = projectPath;
|
59 | this.subscriptions = new _sbEventKit.CompositeDisposable();
|
60 |
|
61 | this.subscriptions.add(this.emitter);
|
62 | }
|
63 |
|
64 | _createClass(CLI, [{
|
65 | key: 'activate',
|
66 | value: function activate() {
|
67 | var _this = this;
|
68 |
|
69 | var serverAddress = 'http://localhost:' + this.config.webServerPort + '/';
|
70 | var manifest = {};
|
71 | try {
|
72 | Object.assign({}, require(_path2.default.join(this.projectPath, 'package.json')));
|
73 | } catch (_) {}
|
74 |
|
75 | this.vorpal.activate(manifest.name || _path2.default.basename(this.projectPath));
|
76 | this.vorpal.log(_chalk2.default.green('Server running at') + ' ' + serverAddress);
|
77 | this.vorpal.log('' + _chalk2.default.yellow('Type ' + _chalk2.default.underline('help') + ' to get list of available commands'));
|
78 | this.vorpal.addCommand('open', 'Open this app in Browser', function () {
|
79 | (0, _open2.default)(serverAddress);
|
80 | });
|
81 | this.vorpal.addCommand('editor', 'Open this app in Atom', _asyncToGenerator(function* () {
|
82 | var defaultEditor = 'atom';
|
83 | var editor = process.env.EDITOR || defaultEditor;
|
84 |
|
85 | var editorName = _path2.default.basename(editor);
|
86 | if (editorName === 'nano' || editorName === 'vi' || editorName === 'vim') {
|
87 | editor = defaultEditor;
|
88 | }
|
89 |
|
90 | yield (0, _sbExec.exec)(editor, [_this.projectPath]);
|
91 | }));
|
92 | this.vorpal.addCommand('build', 'Build this app for production usage', _asyncToGenerator(function* () {
|
93 | yield _this.emitter.emit('should-build');
|
94 | _this.vorpal.log('Dist files built successfully in', _path2.default.relative(_this.projectPath, _this.config.outputDirectory));
|
95 | }));
|
96 | this.vorpal.replaceCommand('exit', 'Exit motion daemon', function () {
|
97 | _this.emitter.emit('should-dispose');
|
98 | process.exit();
|
99 | });
|
100 | this.active = true;
|
101 | }
|
102 | }, {
|
103 | key: 'deactivate',
|
104 | value: function deactivate() {
|
105 | this.vorpal.deactivate();
|
106 | }
|
107 | }, {
|
108 | key: 'log',
|
109 | value: function log(given) {
|
110 | var contents = given;
|
111 | if (this.active) {
|
112 | this.vorpal.log(contents);
|
113 | } else {
|
114 | if (!process.stdout.isTTY) {
|
115 | contents = (0, _stripAnsi2.default)(contents);
|
116 | }
|
117 | console.log(contents);
|
118 | }
|
119 | }
|
120 | }, {
|
121 | key: 'addSpinner',
|
122 | value: function addSpinner(text) {
|
123 | if (!this.active) {
|
124 | this.log(text);
|
125 | return;
|
126 | }
|
127 |
|
128 | var spinner = this.spinner;
|
129 | if (spinner) {
|
130 | spinner.texts.push(text);
|
131 | spinner.instance.text = (0, _lodash2.default)(spinner.texts).join(SPINNER_GLUE);
|
132 | } else {
|
133 | var _instance = new _ora2.default({
|
134 | text: text,
|
135 | color: 'yellow'
|
136 | });
|
137 | this.spinner = {
|
138 | texts: [text],
|
139 | instance: _instance
|
140 | };
|
141 | _instance.start();
|
142 | }
|
143 | }
|
144 | }, {
|
145 | key: 'removeSpinner',
|
146 | value: function removeSpinner(text) {
|
147 | if (!this.active) {
|
148 | return;
|
149 | }
|
150 |
|
151 | var spinner = this.spinner;
|
152 | if (spinner) {
|
153 | var index = spinner.texts.indexOf(text);
|
154 | if (index !== -1) {
|
155 | spinner.texts.splice(index, 1);
|
156 | }
|
157 | if (spinner.texts.length) {
|
158 | spinner.instance.text = (0, _lodash2.default)(spinner.texts).join(SPINNER_GLUE);
|
159 | } else {
|
160 | this.removeAllSpinners();
|
161 | }
|
162 | }
|
163 | }
|
164 | }, {
|
165 | key: 'removeAllSpinners',
|
166 | value: function removeAllSpinners() {
|
167 | if (!this.active) {
|
168 | return;
|
169 | }
|
170 | var spinner = this.spinner;
|
171 | if (spinner) {
|
172 | spinner.instance.stop();
|
173 | this.vorpal.instance.ui.refresh();
|
174 | }
|
175 | }
|
176 | }, {
|
177 | key: 'onShouldBuild',
|
178 | value: function onShouldBuild(callback) {
|
179 | return this.emitter.on('should-build', callback);
|
180 | }
|
181 | }, {
|
182 | key: 'onShouldDispose',
|
183 | value: function onShouldDispose(callback) {
|
184 | return this.emitter.on('should-dispose', callback);
|
185 | }
|
186 | }, {
|
187 | key: 'dispose',
|
188 | value: function dispose() {
|
189 | this.vorpal.dispose();
|
190 | this.subscriptions.dispose();
|
191 | }
|
192 | }]);
|
193 |
|
194 | return CLI;
|
195 | }();
|
196 |
|
197 | exports.default = CLI; |
\ | No newline at end of file |