UNPKG

5.9 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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
9var _sbEventKit = require('sb-event-kit');
10
11var _ora = require('ora');
12
13var _ora2 = _interopRequireDefault(_ora);
14
15var _chalk = require('chalk');
16
17var _chalk2 = _interopRequireDefault(_chalk);
18
19var _open = require('open');
20
21var _open2 = _interopRequireDefault(_open);
22
23var _lodash = require('lodash.uniq');
24
25var _lodash2 = _interopRequireDefault(_lodash);
26
27var _sbExec = require('sb-exec');
28
29var _cli2 = require('./cli');
30
31var _cli3 = _interopRequireDefault(_cli2);
32
33function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
35function _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) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }
36
37function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
38
39var SPINNER_GLUE = ' & ';
40
41var Main = function () {
42 function Main(config) {
43 _classCallCheck(this, Main);
44
45 this.cli = new _cli3.default();
46 this.active = false;
47 this.config = config;
48 this.emitter = new _sbEventKit.Emitter();
49 this.subscriptions = new _sbEventKit.CompositeDisposable();
50
51 this.subscriptions.add(this.emitter);
52 }
53
54 _createClass(Main, [{
55 key: 'activate',
56 value: function activate() {
57 var _this = this;
58
59 if (this.active) {
60 this.cli.activate();
61 return;
62 }
63
64 var serverAddress = 'http://localhost:' + this.config.get('webServerPort') + '/';
65
66 this.cli.activate();
67 this.cli.log(_chalk2.default.green('Server running at') + ' ' + serverAddress);
68 this.cli.log('' + _chalk2.default.yellow('Type ' + _chalk2.default.underline('help') + ' to get list of available commands'));
69 this.cli.addCommand('open', 'Open this app in Browser', function () {
70 (0, _open2.default)(serverAddress);
71 });
72 this.cli.addCommand('editor', 'Open this app in Atom', _asyncToGenerator(function* () {
73 yield (0, _sbExec.exec)('atom', [_this.config.getBundleDirectory()]);
74 }));
75 this.cli.addCommand('build', 'Build this app for production usage', _asyncToGenerator(function* () {
76 yield _this.emitter.emit('should-build');
77 _this.cli.log('Dist files built successfully in', _this.config.getPublicDirectory());
78 }));
79 this.cli.addCommand('reload', 'Rebuild the bundle clearing all cache', _asyncToGenerator(function* () {
80 try {
81 yield _this.emitter.emit('should-reload');
82 _this.cli.log('Bundle reloaded peacefully');
83 } catch (_) {
84 _this.cli.log('Unable to reload the bundle', _);
85 }
86 }));
87 this.cli.replaceCommand('exit', 'Exit motion daemon', function () {
88 _this.emitter.emit('should-dispose');
89 process.exit();
90 });
91 // TODO: Read manifest scripts and prompt to run them here
92 }
93 }, {
94 key: 'deactivate',
95 value: function deactivate() {
96 this.cli.deactivate();
97 }
98 }, {
99 key: 'log',
100 value: function log() {
101 var _cli;
102
103 (_cli = this.cli).log.apply(_cli, arguments);
104 }
105 }, {
106 key: 'addSpinner',
107 value: function addSpinner(text) {
108 var spinner = this.spinner;
109 if (spinner) {
110 spinner.texts.push(text);
111 spinner.instance.text = (0, _lodash2.default)(spinner.texts).join(SPINNER_GLUE);
112 } else {
113 var _instance = new _ora2.default({
114 text: text,
115 color: 'yellow'
116 });
117 this.spinner = {
118 texts: [text],
119 instance: _instance
120 };
121 _instance.start();
122 }
123 }
124 }, {
125 key: 'removeSpinner',
126 value: function removeSpinner(text) {
127 var spinner = this.spinner;
128 if (spinner) {
129 var index = spinner.texts.indexOf(text);
130 if (index !== -1) {
131 spinner.texts.splice(index, 1);
132 }
133 if (spinner.texts.length) {
134 spinner.instance.text = (0, _lodash2.default)(spinner.texts).join(SPINNER_GLUE);
135 } else {
136 this.removeAllSpinners();
137 }
138 }
139 }
140 }, {
141 key: 'removeAllSpinners',
142 value: function removeAllSpinners() {
143 var spinner = this.spinner;
144 if (spinner) {
145 spinner.instance.stop();
146 this.cli.instance.ui.refresh();
147 }
148 }
149 }, {
150 key: 'onShouldBuild',
151 value: function onShouldBuild(callback) {
152 return this.emitter.on('should-build', callback);
153 }
154 }, {
155 key: 'onShouldReload',
156 value: function onShouldReload(callback) {
157 return this.emitter.on('should-reload', callback);
158 }
159 }, {
160 key: 'onShouldDispose',
161 value: function onShouldDispose(callback) {
162 return this.emitter.on('should-dispose', callback);
163 }
164 }, {
165 key: 'dispose',
166 value: function dispose() {
167 this.cli.dispose();
168 this.subscriptions.dispose();
169 }
170 }]);
171
172 return Main;
173}();
174
175exports.default = Main;
176//# sourceMappingURL=index.js.map
\No newline at end of file