Skip to content

Commit 30550fc

Browse files
committed
feat(backend): add script service
The script service allows other services to register re-runnable tasks called "scripts". These can be invoked via "script:run" in the console.
1 parent 1416807 commit 30550fc

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/backend/src/CoreModule.js

+3
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ const install = async ({ services, app }) => {
228228

229229
const { DriverService } = require("./services/drivers/DriverService");
230230
services.registerService('driver', DriverService);
231+
232+
const { ScriptService } = require('./services/ScriptService');
233+
services.registerService('script', ScriptService);
231234
}
232235

233236
const install_legacy = async ({ services }) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
};

0 commit comments

Comments
 (0)