UNPKG

5.01 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
8
9var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
10
11var _renderer = require('./renderer');
12
13var _renderer2 = _interopRequireDefault(_renderer);
14
15var _configReader = require('./config-reader');
16
17var _configReader2 = _interopRequireDefault(_configReader);
18
19var _prodBuilder = require('./builder/prod-builder');
20
21var _prodBuilder2 = _interopRequireDefault(_prodBuilder);
22
23var _devBuilder = require('./builder/dev-builder');
24
25var _devBuilder2 = _interopRequireDefault(_devBuilder);
26
27var _middlewareComposer = require('./middleware-composer');
28
29var _middlewareComposer2 = _interopRequireDefault(_middlewareComposer);
30
31var _logger = require('./utils/logger');
32
33var _logger2 = _interopRequireDefault(_logger);
34
35var _events = require('events');
36
37var _events2 = _interopRequireDefault(_events);
38
39function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
41class LavasCore extends _events2.default {
42 constructor(cwd = process.cwd()) {
43 super();
44 this.cwd = cwd;
45 }
46
47 /**
48 * invoked before build & runAfterBuild, do something different in each senario
49 *
50 * @param {string} env NODE_ENV
51 * @param {boolean} isInBuild is in build process
52 * @param {Object} options options
53 * @param {string} options.config custom config file absolute path
54 */
55 init(env, isInBuild, options = {}) {
56 var _this = this;
57
58 return (0, _asyncToGenerator3.default)(function* () {
59 _this.env = env;
60 _this.isProd = _this.env === 'production';
61 _this.configReader = new _configReader2.default(_this.cwd, _this.env, options.config);
62
63 if (!process.env.NODE_ENV) {
64 process.env.NODE_ENV = env;
65 }
66
67 /**
68 * in a build process, we need to read config by scan a directory,
69 * but for online server after build, we just read config.json directly
70 */
71 if (isInBuild) {
72 // scan directory
73 _this.config = yield _this.configReader.read();
74 } else {
75 // read config from config.json
76 _this.config = yield _this.configReader.readConfigFile();
77 }
78
79 _this.middlewareComposer = new _middlewareComposer2.default(_this);
80 _this.renderer = new _renderer2.default(_this);
81 _this.builder = _this.isProd ? new _prodBuilder2.default(_this) : new _devBuilder2.default(_this);
82
83 // expose Koa & express middleware factory function
84 _this.koaMiddleware = _this.middlewareComposer.koa.bind(_this.middlewareComposer);
85 _this.expressMiddleware = _this.middlewareComposer.express.bind(_this.middlewareComposer);
86
87 // expose render function
88 _this.render = _this.renderer.render.bind(_this.renderer);
89
90 if (!_this.isProd) {
91 // register rebuild listener
92 _this.on('start-rebuild', (0, _asyncToGenerator3.default)(function* () {
93 // read config again
94 let newConfig = yield _this.configReader.read();
95
96 // init builder again
97 _this.builder.init(newConfig);
98
99 // clean middlewares
100 _this.middlewareComposer.reset(newConfig);
101
102 // notify the server that it needs to restart
103 _this.emit('rebuild');
104 }));
105 }
106 })();
107 }
108
109 /**
110 * build in dev & prod mode
111 *
112 */
113 build() {
114 var _this2 = this;
115
116 return (0, _asyncToGenerator3.default)(function* () {
117 try {
118 yield _this2.builder.build();
119 } catch (e) {
120 console.error(e);
121 _logger2.default.error('build', `${_this2.env} build failed.`);
122 }
123 })();
124 }
125
126 /**
127 * must run after build in prod mode
128 *
129 */
130 runAfterBuild() {
131 var _this3 = this;
132
133 return (0, _asyncToGenerator3.default)(function* () {
134 // create with bundle & manifest
135 yield _this3.renderer.createWithBundle();
136 })();
137 }
138
139 /**
140 * close builder in development mode
141 *
142 */
143 close() {
144 var _this4 = this;
145
146 return (0, _asyncToGenerator3.default)(function* () {
147 yield _this4.builder.close();
148 console.log('[Lavas] lavas closed.');
149 })();
150 }
151
152 /**
153 * add flag to req which will be ignored by lavas middlewares
154 *
155 * @param {Request} req req
156 */
157 ignore(req) {
158 req.lavasIgnoreFlag = true;
159 }
160}
161exports.default = LavasCore; /**
162 * @file index.js
163 * @author lavas
164 */
165
166module.exports = exports['default'];
\No newline at end of file