File tree 2 files changed +50
-0
lines changed
2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -228,6 +228,9 @@ const install = async ({ services, app }) => {
228
228
229
229
const { DriverService } = require ( "./services/drivers/DriverService" ) ;
230
230
services . registerService ( 'driver' , DriverService ) ;
231
+
232
+ const { ScriptService } = require ( './services/ScriptService' ) ;
233
+ services . registerService ( 'script' , ScriptService ) ;
231
234
}
232
235
233
236
const install_legacy = async ( { services } ) => {
Original file line number Diff line number Diff line change
1
+ const BaseService = require ( "./BaseService" ) ;
2
+
3
+ class BackendScript {
4
+ constructor ( name , fn ) {
5
+ this . name = name ;
6
+ this . fn = fn ;
7
+ }
8
+
9
+ async run ( ctx , args ) {
10
+ return await this . fn ( ctx , args ) ;
11
+ }
12
+
13
+ }
14
+
15
+ class ScriptService extends BaseService {
16
+ _construct ( ) {
17
+ this . scripts = [ ] ;
18
+ }
19
+
20
+ async _init ( ) {
21
+ const svc_commands = this . services . get ( 'commands' ) ;
22
+ svc_commands . registerCommands ( 'script' , [
23
+ {
24
+ id : 'run' ,
25
+ description : 'run a script' ,
26
+ handler : async ( args , ctx ) => {
27
+ const script_name = args . shift ( ) ;
28
+ const script = this . scripts . find ( s => s . name === script_name ) ;
29
+ if ( ! script ) {
30
+ ctx . error ( `script not found: ${ script_name } ` ) ;
31
+ return ;
32
+ }
33
+ await script . run ( ctx , args ) ;
34
+ }
35
+ }
36
+ ] ) ;
37
+ }
38
+
39
+ register ( name , fn ) {
40
+ this . scripts . push ( new BackendScript ( name , fn ) ) ;
41
+ }
42
+ }
43
+
44
+ module . exports = {
45
+ ScriptService,
46
+ BackendScript,
47
+ } ;
You can’t perform that action at this time.
0 commit comments