UNPKG

6.51 kBJavaScriptView Raw
1'use strict';
2
3var _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; }; }();
4
5var _path = require('path');
6
7var _path2 = _interopRequireDefault(_path);
8
9var _assert = require('assert');
10
11var _assert2 = _interopRequireDefault(_assert);
12
13var _sbEventKit = require('sb-event-kit');
14
15var _fs = require('./fs');
16
17var FS = _interopRequireWildcard(_fs);
18
19var _cli = require('./cli');
20
21var _cli2 = _interopRequireDefault(_cli);
22
23var _config = require('./config');
24
25var _config2 = _interopRequireDefault(_config);
26
27var _error = require('./error');
28
29var _helpers = require('./helpers');
30
31function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
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 Motion = function () {
40 function Motion(projectPath, config) {
41 var _this = this;
42
43 _classCallCheck(this, Motion);
44
45 (0, _assert2.default)(config instanceof _config2.default, 'Use Motion.create instead of constructor');
46
47 this.cli = new _cli2.default(config);
48 this.config = config;
49 this.watching = false;
50 this.projectPath = projectPath;
51 this.subscriptions = new _sbEventKit.CompositeDisposable();
52
53 this.subscriptions.add(this.cli);
54 this.cli.onShouldBuild(_asyncToGenerator(function* () {
55 yield _this.build(false);
56 }));
57 this.cli.onShouldDispose(function () {
58 _this.dispose();
59 });
60 }
61
62 _createClass(Motion, [{
63 key: 'exists',
64 value: function () {
65 var _ref2 = _asyncToGenerator(function* () {
66 return yield FS.exists(_path2.default.join(this.projectPath, '.motion.json'));
67 });
68
69 function exists() {
70 return _ref2.apply(this, arguments);
71 }
72
73 return exists;
74 }()
75 }, {
76 key: 'watch',
77 value: function () {
78 var _ref3 = _asyncToGenerator(function* () {
79 var _this2 = this;
80
81 var terminal = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
82
83 if (!(yield this.exists())) {
84 throw new _error.MotionError(_error.ERROR_CODE.NOT_MOTION_APP);
85 }
86
87 if (terminal) {
88 this.cli.activate();
89 }
90 var pundle = yield (0, _helpers.getPundleInstance)(this.cli, terminal, this.projectPath, true, this.config.config, function (error) {
91 _this2.cli.log(error);
92 });
93 yield pundle.activate();
94 var reloadHook = this.cli.onShouldReload(_asyncToGenerator(function* () {
95 pundle.pundle.clearCache();
96 yield pundle.pundle.compile();
97 }));
98 var disposable = new _sbEventKit.Disposable(function () {
99 _this2.subscriptions.remove(disposable);
100 pundle.dispose();
101 reloadHook.dispose();
102 });
103
104 this.subscriptions.add(disposable);
105 return disposable;
106 });
107
108 function watch(_x) {
109 return _ref3.apply(this, arguments);
110 }
111
112 return watch;
113 }()
114 }, {
115 key: 'build',
116 value: function () {
117 var _ref5 = _asyncToGenerator(function* () {
118 var terminal = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
119
120 if (!(yield this.exists())) {
121 throw new _error.MotionError(_error.ERROR_CODE.NOT_MOTION_APP);
122 }
123 var error = void 0;
124 var compilation = yield (0, _helpers.getPundleInstance)(this.cli, terminal, this.projectPath, false, this.config.config, function (givenError) {
125 error = givenError;
126 });
127 if (error) {
128 throw error;
129 }
130 yield compilation.compile();
131 yield FS.mkdir(_path2.default.join(this.config.getPublicDirectory(), '_'));
132 yield FS.writeFile(_path2.default.join(this.config.getPublicDirectory(), '_/bundle.js'), compilation.generate().contents);
133 });
134
135 function build(_x3) {
136 return _ref5.apply(this, arguments);
137 }
138
139 return build;
140 }()
141 }, {
142 key: 'init',
143 value: function () {
144 var _ref6 = _asyncToGenerator(function* () {
145 if (yield this.exists()) {
146 throw new _error.MotionError(_error.ERROR_CODE.ALREADY_MOTION_APP);
147 }
148 yield FS.mkdir(this.config.getBundleDirectory());
149 yield FS.mkdir(this.config.getPublicDirectory());
150 yield FS.copy(_path2.default.normalize(_path2.default.join(__dirname, '..', 'template', 'bundle')), this.config.getBundleDirectory());
151 yield FS.copy(_path2.default.normalize(_path2.default.join(__dirname, '..', 'template', 'public')), this.config.getPublicDirectory());
152 yield this.config.write();
153 });
154
155 function init() {
156 return _ref6.apply(this, arguments);
157 }
158
159 return init;
160 }()
161 }, {
162 key: 'dispose',
163 value: function dispose() {
164 this.subscriptions.dispose();
165 }
166 }], [{
167 key: 'create',
168 value: function () {
169 var _ref7 = _asyncToGenerator(function* (projectRoot) {
170 return new Motion(projectRoot, (yield _config2.default.create(projectRoot)));
171 });
172
173 function create(_x5) {
174 return _ref7.apply(this, arguments);
175 }
176
177 return create;
178 }()
179 }]);
180
181 return Motion;
182}();
183
184module.exports = Motion;
185//# sourceMappingURL=index.js.map
\No newline at end of file