18
18
*/
19
19
const { AdvancedBase } = require ( "@heyputer/puter-js-common" ) ;
20
20
const { Context } = require ( './util/context' ) ;
21
+ const BaseService = require ( "./services/BaseService" ) ;
22
+ const useapi = require ( 'useapi' ) ;
21
23
22
24
class Kernel extends AdvancedBase {
23
25
constructor ( ) {
24
26
super ( ) ;
25
27
26
28
this . modules = [ ] ;
29
+ this . useapi = useapi ( ) ;
30
+
31
+ this . useapi . withuse ( ( ) => {
32
+ def ( 'Module' , AdvancedBase ) ;
33
+ def ( 'Service' , BaseService ) ;
34
+ } ) ;
27
35
}
28
36
29
37
add_module ( module ) {
@@ -48,7 +56,8 @@ class Kernel extends AdvancedBase {
48
56
const runtimeEnv = new RuntimeEnvironment ( {
49
57
logger : bootLogger ,
50
58
} ) ;
51
- runtimeEnv . init ( ) ;
59
+ const environment = runtimeEnv . init ( ) ;
60
+ this . environment = environment ;
52
61
53
62
// polyfills
54
63
require ( './polyfill/to-string-higher-radix' ) ;
@@ -89,6 +98,8 @@ class Kernel extends AdvancedBase {
89
98
// app.set('services', services);
90
99
91
100
const root_context = Context . create ( {
101
+ environment : this . environment ,
102
+ useapi : this . useapi ,
92
103
services,
93
104
config,
94
105
logger : this . bootLogger ,
@@ -108,10 +119,14 @@ class Kernel extends AdvancedBase {
108
119
async _install_modules ( ) {
109
120
const { services } = this ;
110
121
122
+ // Internal modules
111
123
for ( const module of this . modules ) {
112
124
await module . install ( Context . get ( ) ) ;
113
125
}
114
126
127
+ // External modules
128
+ await this . install_extern_mods_ ( ) ;
129
+
115
130
try {
116
131
await services . init ( ) ;
117
132
} catch ( e ) {
@@ -173,6 +188,34 @@ class Kernel extends AdvancedBase {
173
188
await services . emit ( 'boot.activation' ) ;
174
189
await services . emit ( 'boot.ready' ) ;
175
190
}
191
+
192
+ async install_extern_mods_ ( ) {
193
+ const path_ = require ( 'path' ) ;
194
+ const fs = require ( 'fs' ) ;
195
+
196
+ const mod_paths = this . environment . mod_paths ;
197
+ for ( const mods_dirpath of mod_paths ) {
198
+ const mod_dirnames = fs . readdirSync ( mods_dirpath ) ;
199
+ for ( const mod_dirname of mod_dirnames ) {
200
+ const mod_path = path_ . join ( mods_dirpath , mod_dirname ) ;
201
+ if ( ! fs . lstatSync ( mod_path ) . isDirectory ( ) ) {
202
+ continue ;
203
+ }
204
+
205
+ const mod_class = this . useapi . withuse ( ( ) => require ( mod_path ) ) ;
206
+ const mod = new mod_class ( ) ;
207
+ if ( ! mod ) {
208
+ continue ;
209
+ }
210
+
211
+ if ( mod . install ) {
212
+ this . useapi . awithuse ( async ( ) => {
213
+ await mod . install ( Context . get ( ) ) ;
214
+ } ) ;
215
+ }
216
+ }
217
+ }
218
+ }
176
219
}
177
220
178
221
module . exports = { Kernel } ;
0 commit comments