Skip to content

Commit

Permalink
feat(ls): show stopped ghost instances as well as running ones
Browse files Browse the repository at this point in the history
closes TryGhost#178
- remove register/deregister from ghost start/stop
- change system config file structure and pull url/port/process manager data from individual
instance configs rather than a global list
  • Loading branch information
acburdine committed Mar 20, 2017
1 parent 833d8d0 commit 17ad89e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
38 changes: 35 additions & 3 deletions lib/commands/ls.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
'use strict';
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const each = require('lodash/each');
const Config = require('../utils/config');

module.exports.execute = function execute() {
let systemConfig = Config.load('system');
let rows = [];
let instances = systemConfig.get('instances', {});
let save = false;

each(systemConfig.get('instances', {}), (instance, name) => {
rows.push([name, instance.url, instance.port, instance.mode, instance.process, instance.cwd]);
each(instances, (dir, name) => {
if (typeof dir !== 'string') {
dir = dir.cwd;
instances[name] = dir;
save = true;
}

if (!fs.existsSync(path.join(dir, '.ghost-cli'))) {
// install has been removed, so we want to remove it from the list
delete instances[name];
save = true;
return;
}

let installConfig = Config.load(path.join(dir, '.ghost-cli'));
let environment = installConfig.get('running', false);

if (!environment) {
rows.push([name, dir, chalk.red('stopped'), chalk.red('n/a'), chalk.red('n/a'), chalk.red('n/a')]);
return;
}

let instanceConfig = Config.load(path.join(dir, `config.${environment}.json`));
rows.push([name, dir, `${chalk.green('running')} (${environment})`, instanceConfig.get('url'), instanceConfig.get('server.port'), instanceConfig.get('process')]);
});

this.ui.table(['Name', 'Url', 'Port', 'Environment', 'Process Manager', 'Location'], rows, {
if (save) {
// There have been instances removed, re-save the instance list
systemConfig.save();
}

this.ui.table(['Name', 'Location', 'Status', 'Url', 'Port', 'Process Manager'], rows, {
style: {head: ['cyan']}
});
};
7 changes: 7 additions & 0 deletions lib/commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const chalk = require('chalk');
const Listr = require('listr');

const Config = require('../utils/config');
const errors = require('../errors');
const setupChecks = require('./doctor/checks/setup');
const startCommand = require('./start');
Expand Down Expand Up @@ -84,6 +85,12 @@ module.exports.execute = function execute(options) {
default: true
});
}).then((answer) => {
// Add config to system blog list
let systemConfig = Config.load('system');
let instances = systemConfig.get('instances', {});
instances[context.config.get('pname')] = process.cwd();
systemConfig.save();

if (context.start || answer.start) {
return startCommand.execute.call(this);
}
Expand Down
15 changes: 0 additions & 15 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,6 @@ const errors = require('../errors');
const startupChecks = require('./doctor/checks/startup');
const checkValidInstall = require('../utils/check-valid-install');

function register(config, environment) {
let systemConfig = Config.load('system');
let instances = systemConfig.get('instances', {});

instances[config.get('pname')] = {
mode: environment,
cwd: process.cwd(),
url: config.get('url'),
port: config.get('server.port'),
process: config.get('process')
};
systemConfig.set('instances', instances).save();
}

module.exports.execute = function execute(options) {
checkValidInstall('start');

Expand Down Expand Up @@ -83,7 +69,6 @@ module.exports.execute = function execute(options) {
});
}).then(() => {
let start = () => Promise.resolve(this.service.process.start(process.cwd(), this.environment)).then(() => {
register(config, this.environment);
cliConfig.set('running', this.environment).save();
return Promise.resolve();
});
Expand Down
14 changes: 3 additions & 11 deletions lib/commands/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,14 @@ function stopAll() {
// Unlike lodash, bluebird doesn't support iterating over objects,
// so we have to iterate over the keys and then get the url later
return Promise.each(Object.keys(instances), (pname) => {
let instance = instances[pname];
process.chdir(instance.cwd);
return this.ui.run(execute.call(this, {quiet: true}), `Stopping Ghost: ${instance.url}`);
let instanceDir = instances[pname];
process.chdir(instanceDir);
return this.ui.run(execute.call(this, {quiet: true}), `Stopping Ghost: ${pname}`);
}).then(() => {
process.chdir(cwd);
});
}

function deregister(config) {
let systemConfig = Config.load('system');
let instances = systemConfig.get('instances', {});
delete instances[config.get('pname')];
systemConfig.set('instances', instances).save();
}

function execute(options) {
options = options || {};

Expand All @@ -46,7 +39,6 @@ function execute(options) {
let config = Config.load(cliConfig.get('running'));
this.service.setConfig(config);
let stop = () => Promise.resolve(this.service.process.stop(process.cwd())).then(() => {
deregister(config);
cliConfig.set('running', null).save();
return Promise.resolve();
});
Expand Down

0 comments on commit 17ad89e

Please sign in to comment.