-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The script service allows other services to register re-runnable tasks called "scripts". These can be invoked via "script:run" in the console.
- Loading branch information
1 parent
1416807
commit 30550fc
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const BaseService = require("./BaseService"); | ||
|
||
class BackendScript { | ||
constructor (name, fn) { | ||
this.name = name; | ||
this.fn = fn; | ||
} | ||
|
||
async run (ctx, args) { | ||
return await this.fn(ctx, args); | ||
} | ||
|
||
} | ||
|
||
class ScriptService extends BaseService { | ||
_construct () { | ||
this.scripts = []; | ||
} | ||
|
||
async _init () { | ||
const svc_commands = this.services.get('commands'); | ||
svc_commands.registerCommands('script', [ | ||
{ | ||
id: 'run', | ||
description: 'run a script', | ||
handler: async (args, ctx) => { | ||
const script_name = args.shift(); | ||
const script = this.scripts.find(s => s.name === script_name); | ||
if ( ! script ) { | ||
ctx.error(`script not found: ${script_name}`); | ||
return; | ||
} | ||
await script.run(ctx, args); | ||
} | ||
} | ||
]); | ||
} | ||
|
||
register (name, fn) { | ||
this.scripts.push(new BackendScript(name, fn)); | ||
} | ||
} | ||
|
||
module.exports = { | ||
ScriptService, | ||
BackendScript, | ||
}; |