Skip to content

Commit

Permalink
✨ add ability for services to register their own commands
Browse files Browse the repository at this point in the history
refs TryGhost#146
- add registerCommand utilities as well as "ghost service <name>"
command
  • Loading branch information
acburdine committed Mar 1, 2017
1 parent e2d2b5b commit b0ac8cd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/commands.js
Expand Up @@ -63,6 +63,15 @@ module.exports = {
description: 'start a managed ghost process'
},

service: {
description: 'run a service-defined command',

arguments: [
'command',
{name: 'args', optional: true, variadic: true}
]
},

setup: {
description: 'setup an installation of Ghost (after it is installed)',

Expand Down
5 changes: 5 additions & 0 deletions lib/commands/service.js
@@ -0,0 +1,5 @@
'use strict';

module.exports.execute = function execute(command, args) {
return this.service.callCommand(command, args);
};
4 changes: 4 additions & 0 deletions lib/services/base.js
Expand Up @@ -17,6 +17,10 @@ class BaseService {
this.serviceManager.registerHook(hook, fn, this.name);
}

command(name, fn) {
this.serviceManager.registerCommand(name, fn, this.name);
}

get config() {
return this.serviceManager.config;
}
Expand Down
24 changes: 24 additions & 0 deletions lib/services/index.js
Expand Up @@ -26,6 +26,7 @@ class ServiceManager {
this.ui = ui;

this.hooks = {};
this.commands = {};
this.services = {};
this.availableProcessManagers = {};
this.process = null;
Expand Down Expand Up @@ -61,6 +62,29 @@ class ServiceManager {
});
}

registerCommand(name, fn, serviceName) {
if (!this.services[serviceName]) {
throw new Error(`Service ${serviceName} does not exist`);
}

if (this.commands[name]) {
throw new Error(`Service command ${name} is already defined`);
}

this.commands[name] = [serviceName, fn];
}

callCommand(name, args) {
if (!this.commands[name]) {
throw new Error(`Command ${name} is not defined`);
}

let command = this.commands[name];
let service = this.services[command[0]];

return Promise.resolve(service[command[1]].apply(service, args));
}

setConfig(config, force) {
if (this.config && !force) {
// Config has already been set and we are not forcing a reload of the services,
Expand Down

0 comments on commit b0ac8cd

Please sign in to comment.