UNPKG

4.39 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2018 One Hill Technologies, LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17const assert = require ('assert');
18const program = require ('commander');
19
20const { BO, computed} = require ('base-object');
21
22const { env } = require ('./environment');
23const {version} = require ('../package.json');
24const ClusterApplication = require ('./cluster');
25const Application = require ('./application');
26
27/**
28 * @class Framework
29 *
30 * Wrapper class for the Blueprint framework that hosts the application.
31 */
32module.exports = BO.extend ({
33 version,
34
35 /// The application installed in the framework.
36 _app: null,
37
38 /// The execution environment for the framework.
39 env,
40
41 app: computed ({
42 get () { return this._app; }
43 }),
44
45 hasApplication: computed ({
46 get () { return !!this._app; }
47 }),
48
49 init () {
50 this._super.call (this, ...arguments);
51 this._parseCommandLineOptions ();
52 },
53
54 /**
55 * Parse the command-line options.
56 *
57 * @private
58 */
59 _parseCommandLineOptions () {
60 program
61 .option ('--cluster [workers]', 'Run cluster mode with optional number of workers', parseInt)
62 .parse (process.argv);
63
64 this.cluster = program.cluster;
65 },
66
67 /**
68 * Create an application in the framework.
69 *
70 * @param appPath
71 */
72 createApplication (appPath) {
73 assert (!this._app, 'The framework already has an application.');
74
75 if (this.cluster)
76 this._app = new ClusterApplication ({appPath, cluster: this.cluster});
77 else
78 this._app = new Application ({appPath});
79
80 return this._app.configure ();
81 },
82
83 /**
84 * Create an application in the framework as start it.
85 *
86 * @param appPath
87 */
88 createApplicationAndStart (appPath) {
89 return this.createApplication (appPath).then (app => {
90 return app.start ();
91 });
92 },
93
94 /**
95 * Destroy the application.
96 *
97 * @returns {Promise<any>}
98 */
99 destroyApplication () {
100 if (!this._app)
101 return Promise.resolve ();
102
103 return this._app.destroy ().then (() => {
104 this._app = null;
105 });
106 },
107
108 /**
109 * Lookup a loaded component.
110 *
111 * The name of the component must have the format <type:name>.
112 *
113 * Ex.
114 *
115 * lookup ('controller:main')
116 * lookup ('config:app')
117 *
118 * @param component Name of the component.
119 */
120 lookup (component) {
121 assert (this._app, 'The application has not been created.');
122 return this._app.lookup (component);
123 },
124
125 /**
126 * Load an asset from the main application.
127 *
128 * @param filename
129 * @param opts
130 * @param callback
131 * @returns {*}
132 */
133 asset (filename, opts, callback) {
134 assert (this._app, 'The application has not been created.');
135 return this._app.asset (filename, opts, callback);
136 },
137
138 assetSync (filename, opts) {
139 assert (this._app, 'The application has not been created.');
140 return this._app.assetSync (filename, opts);
141 },
142
143 /**
144 * Mount a router. The returned router is an Express router that can be
145 * bound to any path in the router specification via the `use` property.
146 *
147 * @param routerName
148 */
149 mount (routerName) {
150 assert (this._app, 'The application has not been created.');
151 return this._app.mount (routerName);
152 },
153
154 // Events
155
156 on () {
157 assert (this._app, 'The application has not been created.');
158 return this._app.on (...arguments);
159 },
160
161 once () {
162 assert (this._app, 'The application has not been created.');
163 return this._app.once (...arguments);
164 },
165
166 emit () {
167 assert (this._app, 'The application has not been created.');
168 return this._app.emit (...arguments);
169 },
170
171 getListeners (ev) {
172 assert (this._app, 'The application has not been created.');
173 return this._app.getListeners (ev);
174 },
175
176 hasListeners (ev) {
177 assert (this._app, 'The application has not been created.');
178 return this._app.hasListeners (ev);
179 }
180});