1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | const fs = require('fs-extra');
|
32 | const fg = require('fast-glob');
|
33 | const path = require('path');
|
34 | const Package = require('./package.js');
|
35 | const consola = require('consola');
|
36 | const logger = consola.withTag('Packages');
|
37 |
|
38 | const relative = filename => filename.replace(process.cwd(), '');
|
39 |
|
40 | const readOrDefault = filename => fs.existsSync(filename)
|
41 | ? fs.readJsonSync(filename)
|
42 | : [];
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | class Packages {
|
48 |
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 | constructor(core, options = {}) {
|
55 | this.core = core;
|
56 | this.packages = [];
|
57 | this.hotReloading = {};
|
58 | this.options = {
|
59 | manifestFile: null,
|
60 | discoveredFile: null,
|
61 | ...options
|
62 | };
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 | init() {
|
69 | this.core.on('osjs/application:socket:message', (ws, ...params) => {
|
70 | this.handleMessage(ws, params);
|
71 | });
|
72 |
|
73 | return this.load();
|
74 | }
|
75 |
|
76 | |
77 |
|
78 |
|
79 | load() {
|
80 | return this.createLoader()
|
81 | .then(packages => {
|
82 | this.packages = this.packages.concat(packages);
|
83 |
|
84 | return true;
|
85 | });
|
86 | }
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 | createLoader() {
|
93 | let result = [];
|
94 | const {discoveredFile, manifestFile} = this.options;
|
95 | const discovered = readOrDefault(discoveredFile);
|
96 | const manifest = readOrDefault(manifestFile);
|
97 | const sources = discovered.map(d => path.join(d, 'metadata.json'));
|
98 |
|
99 | logger.info('Using package discovery file', relative(discoveredFile));
|
100 | logger.info('Using package manifest file', relative(manifestFile));
|
101 |
|
102 | const stream = fg.stream(sources, {
|
103 | extension: false,
|
104 | brace: false,
|
105 | deep: 1,
|
106 | case: false
|
107 | });
|
108 |
|
109 | stream.on('error', error => logger.error(error));
|
110 | stream.on('data', filename => {
|
111 | result.push(this.loadPackage(filename, manifest));
|
112 | });
|
113 |
|
114 | return new Promise((resolve, reject) => {
|
115 | stream.once('end', () => {
|
116 | Promise.all(result)
|
117 | .then(result => result.filter(iter => !!iter.handler))
|
118 | .then(resolve)
|
119 | .catch(reject);
|
120 | });
|
121 | });
|
122 | }
|
123 |
|
124 | |
125 |
|
126 |
|
127 |
|
128 | onPackageChanged(pkg) {
|
129 | clearTimeout(this.hotReloading[pkg.metadata.name]);
|
130 |
|
131 | this.hotReloading[pkg.metadata.name] = setTimeout(() => {
|
132 | logger.debug('Sending reload signal for', pkg.metadata.name);
|
133 | this.core.broadcast('osjs/packages:package:changed', [pkg.metadata.name]);
|
134 | }, 500);
|
135 | }
|
136 |
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | loadPackage(filename, manifest) {
|
144 | const done = (pkg, error) => {
|
145 | if (error) {
|
146 | logger.warn(error);
|
147 | }
|
148 |
|
149 | return Promise.resolve(pkg);
|
150 | };
|
151 |
|
152 | return fs.readJson(filename)
|
153 | .then(metadata => {
|
154 | const pkg = new Package(this.core, {
|
155 | filename,
|
156 | metadata
|
157 | });
|
158 |
|
159 | return this.initializePackage(pkg, manifest, done);
|
160 | });
|
161 | }
|
162 |
|
163 | |
164 |
|
165 |
|
166 |
|
167 | initializePackage(pkg, manifest, done) {
|
168 | if (pkg.validate(manifest)) {
|
169 | logger.info(`Loading ${relative(pkg.script)}`);
|
170 |
|
171 | try {
|
172 | if (this.core.configuration.development) {
|
173 | pkg.watch(() => {
|
174 | this.onPackageChanged(pkg);
|
175 | });
|
176 | }
|
177 |
|
178 | return pkg.init()
|
179 | .then(() => done(pkg))
|
180 | .catch(e => done(pkg, e));
|
181 | } catch (e) {
|
182 | return done(pkg, e);
|
183 | }
|
184 | }
|
185 |
|
186 | return done(pkg);
|
187 | }
|
188 |
|
189 | |
190 |
|
191 |
|
192 | start() {
|
193 | this.packages.forEach(pkg => pkg.start());
|
194 | }
|
195 |
|
196 | |
197 |
|
198 |
|
199 | async destroy() {
|
200 | await Promise.all(this.packages.map(pkg => pkg.destroy()));
|
201 |
|
202 | this.packages = [];
|
203 | }
|
204 |
|
205 | |
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | handleMessage(ws, params) {
|
212 | const {pid, name, args} = params[0];
|
213 | const found = this.packages.findIndex(({metadata}) => metadata.name === name);
|
214 |
|
215 | if (found !== -1) {
|
216 | const {handler} = this.packages[found];
|
217 | if (handler && typeof handler.onmessage === 'function') {
|
218 | const respond = (...respondParams) => ws.send(JSON.stringify({
|
219 | name: 'osjs/application:socket:message',
|
220 | params: [{
|
221 | pid,
|
222 | args: respondParams
|
223 | }]
|
224 | }));
|
225 |
|
226 | handler.onmessage(ws, respond, args);
|
227 | }
|
228 | }
|
229 | }
|
230 | }
|
231 |
|
232 | module.exports = Packages;
|