UNPKG

5.78 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 _sbFs = require('sb-fs');
6
7var _sbFs2 = _interopRequireDefault(_sbFs);
8
9var _sbCopy = require('sb-copy');
10
11var _sbCopy2 = _interopRequireDefault(_sbCopy);
12
13var _path = require('path');
14
15var _path2 = _interopRequireDefault(_path);
16
17var _sbConfigFile = require('sb-config-file');
18
19var _sbConfigFile2 = _interopRequireDefault(_sbConfigFile);
20
21var _sbEventKit = require('sb-event-kit');
22
23var _compilation = require('./compilation');
24
25var _compilation2 = _interopRequireDefault(_compilation);
26
27var _helpers = require('./helpers');
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31function _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"); }); }; }
32
33function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
34
35var PRIVATE_VAR = {};
36
37var Motion = function () {
38 function Motion(privateVar, projectPath, config, options) {
39 _classCallCheck(this, Motion);
40
41 if (privateVar !== PRIVATE_VAR) {
42 throw new Error('Invalid invocation of new Motion(). Use Motion.get() instead');
43 }
44
45 this.config = config;
46 this.options = options;
47 this.projectPath = projectPath;
48 this.compilation = new _compilation2.default(projectPath, config, options);
49 this.subscriptions = new _sbEventKit.CompositeDisposable();
50
51 this.subscriptions.add(this.compilation);
52 }
53
54 _createClass(Motion, [{
55 key: 'exists',
56 value: function () {
57 var _ref = _asyncToGenerator(function* () {
58 return _sbFs2.default.exists(_path2.default.join(this.projectPath, _helpers.CONFIG_FILE_NAME));
59 });
60
61 function exists() {
62 return _ref.apply(this, arguments);
63 }
64
65 return exists;
66 }()
67 }, {
68 key: 'watch',
69 value: function () {
70 var _ref2 = _asyncToGenerator(function* () {
71 if (!(yield this.exists())) {
72 throw new Error('Unable to run, directory is not a motion app');
73 }
74 try {
75 yield this.compilation.watch(this.options.useCache);
76 } catch (error) {
77 (yield this.compilation.getPundle(true)).context.report(error);
78 return false;
79 }
80 return true;
81 });
82
83 function watch() {
84 return _ref2.apply(this, arguments);
85 }
86
87 return watch;
88 }()
89 }, {
90 key: 'build',
91 value: function () {
92 var _ref3 = _asyncToGenerator(function* () {
93 if (!(yield this.exists())) {
94 throw new Error('Unable to run, directory is not a motion app');
95 }
96 try {
97 yield this.compilation.build(this.options.useCache);
98 } catch (error) {
99 (yield this.compilation.getPundle(false)).context.report(error);
100 }
101 });
102
103 function build() {
104 return _ref3.apply(this, arguments);
105 }
106
107 return build;
108 }()
109 }, {
110 key: 'init',
111 value: function () {
112 var _ref4 = _asyncToGenerator(function* () {
113 var overwrite = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
114
115 if (yield this.exists()) {
116 throw new Error('Directory is already a motion app');
117 }
118 yield _sbFs2.default.mkdirp(this.projectPath);
119 yield _sbFs2.default.mkdirp(this.config.outputDirectory);
120 yield (0, _sbCopy2.default)(_path2.default.normalize(_path2.default.join(__dirname, '..', 'template', 'bundle')), this.projectPath, {
121 overwrite: overwrite,
122 failIfExists: false
123 });
124 yield (0, _sbCopy2.default)(_path2.default.normalize(_path2.default.join(__dirname, '..', 'template', 'dist')), this.config.outputDirectory, {
125 overwrite: overwrite,
126 failIfExists: false
127 });
128 yield _sbFs2.default.writeFile(_path2.default.join(this.projectPath, _helpers.CONFIG_FILE_NAME), JSON.stringify(_helpers.CONFIG_FILE_DEFAULT, null, 2));
129 });
130
131 function init() {
132 return _ref4.apply(this, arguments);
133 }
134
135 return init;
136 }()
137 }, {
138 key: 'dispose',
139 value: function dispose() {
140 this.subscriptions.dispose();
141 }
142 }], [{
143 key: 'create',
144 value: function () {
145 var _ref5 = _asyncToGenerator(function* (projectRoot, options) {
146 var configFile = yield _sbConfigFile2.default.get(_path2.default.join(projectRoot, _helpers.CONFIG_FILE_NAME), _helpers.CONFIG_FILE_DEFAULT, _helpers.CONFIG_FILE_OPTIONS);
147 var config = yield configFile.get();
148
149 config.outputDirectory = _path2.default.resolve(projectRoot, config.outputDirectory);
150
151 return new Motion(PRIVATE_VAR, projectRoot, config, options);
152 });
153
154 function create(_x2, _x3) {
155 return _ref5.apply(this, arguments);
156 }
157
158 return create;
159 }()
160 }]);
161
162 return Motion;
163}();
164
165module.exports = Motion;
\No newline at end of file