1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const assert = require ('assert');
|
18 | const program = require ('commander');
|
19 |
|
20 | const { BO, computed} = require ('base-object');
|
21 |
|
22 | const { env } = require ('./environment');
|
23 | const {version} = require ('../package.json');
|
24 | const ClusterApplication = require ('./cluster');
|
25 | const Application = require ('./application');
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | module.exports = BO.extend ({
|
33 | version,
|
34 |
|
35 |
|
36 | _app: null,
|
37 |
|
38 |
|
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 |
|
56 |
|
57 |
|
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 |
|
69 |
|
70 |
|
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 |
|
85 |
|
86 |
|
87 |
|
88 | createApplicationAndStart (appPath) {
|
89 | return this.createApplication (appPath).then (app => {
|
90 | return app.start ();
|
91 | });
|
92 | },
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
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 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | lookup (component) {
|
121 | assert (this._app, 'The application has not been created.');
|
122 | return this._app.lookup (component);
|
123 | },
|
124 |
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
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 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | mount (routerName) {
|
150 | assert (this._app, 'The application has not been created.');
|
151 | return this._app.mount (routerName);
|
152 | },
|
153 |
|
154 |
|
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 | });
|