Skip to content

Commit 22c804c

Browse files
authored
fix(instance): clean up config usage around application (#254)
1 parent b6ae853 commit 22c804c

File tree

12 files changed

+44
-42
lines changed

12 files changed

+44
-42
lines changed

extensions/nginx/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class NginxExtension extends cli.Extension {
3030
}
3131

3232
this.instance = this.system.getInstance();
33-
this.instance.loadConfig();
3433

3534
cmd.addStage('nginx', this.setupNginx.bind(this));
3635
cmd.addStage('ssl', this.setupSSL.bind(this));
@@ -219,7 +218,6 @@ class NginxExtension extends cli.Extension {
219218

220219
uninstall() {
221220
this.instance = this.system.getInstance();
222-
this.instance.loadConfig();
223221

224222
if (!this.instance.cliConfig.get('extension.nginx', false)) {
225223
return;

extensions/systemd/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class SystemdExtension extends cli.Extension {
1414

1515
setup(cmd, argv) {
1616
let instance = this.system.getInstance();
17-
instance.loadConfig();
1817

1918
if (!argv.local && instance.config.get('process') === 'systemd') {
2019
cmd.addStage('systemd', this._setup.bind(this));

lib/commands/config/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class ConfigCommand extends Command {
1414
super(ui, system);
1515

1616
let instance = this.system.getInstance();
17-
instance.loadConfig(true);
1817
this.config = instance.config;
1918
}
2019

lib/commands/log.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LogCommand extends Command {
1919
// relative to that
2020
process.chdir(instance.dir);
2121

22-
instance.loadRunningConfig(true);
22+
instance.loadRunningEnvironment(true);
2323

2424
// Check if logging file transport is set in config
2525
if (!includes(instance.config.get('logging.transports', []), 'file')) {

lib/commands/restart.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class RestartCommand extends Command {
1111
return Promise.reject(new Error('Ghost instance is not currently running.'));
1212
}
1313

14-
instance.loadRunningConfig(true, true);
14+
instance.loadRunningEnvironment(true, true);
1515

1616
return this.runCommand(StopCommand, argv).then(() => this.runCommand(StartCommand, argv));
1717
}

lib/commands/run.js

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class RunCommand extends Command {
1313
}
1414

1515
let instance = this.system.getInstance();
16-
instance.loadConfig(true);
1716

1817
process.env.paths__contentPath = path.join(process.cwd(), 'content');
1918

lib/commands/setup.js

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class SetupCommand extends Command {
6666
title: 'Setting up instance',
6767
task: () => {
6868
let instance = this.system.getInstance();
69-
instance.loadConfig();
7069
instance.name = argv.pname || url.parse(instance.config.get('url')).hostname.replace(/\./g, '-');
7170
this.system.addInstance(instance);
7271
}

lib/commands/start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class StartCommand extends Command {
2727
return Promise.reject(new Error('Ghost is already running.'));
2828
}
2929

30-
instance.loadConfig();
30+
instance.checkEnvironment();
3131

3232
return this.ui.listr(startupChecks.concat({
3333
title: 'Running database migrations',

lib/commands/stop.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class StopCommand extends Command {
4141
return Promise.reject(new errors.SystemError('No running Ghost instance found here.'));
4242
}
4343

44-
instance.loadRunningConfig();
44+
instance.loadRunningEnvironment(true);
4545

4646
let stop = () => Promise.resolve(instance.process.stop(process.cwd())).then(() => {
4747
instance.running = null;

lib/commands/uninstall.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ class UninstallCommand extends Command {
3131
return Promise.resolve();
3232
}
3333

34-
instance.loadRunningConfig(true, true);
34+
instance.loadRunningEnvironment(true, true);
3535

3636
// If the instance is currently running we need to make
3737
// sure it gets stopped
3838
return this.runCommand(StopCommand);
3939
}).then(() => {
4040
this.system.setEnvironment(!fs.existsSync('config.production.json'));
41-
instance.loadConfig(true);
4241

4342
return this.ui.run(this.system.hook('uninstall'), 'Removing related configuration');
4443
}).then(() => this.ui.run(() => {

lib/commands/update.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ class UpdateCommand extends Command {
3535
context.installPath = path.join(process.cwd(), 'versions', context.version);
3636
}
3737

38-
instance.running ? instance.loadRunningConfig(true, true) : instance.loadConfig();
38+
if (instance.running) {
39+
instance.loadRunningEnvironment(true, true);
40+
}
41+
42+
instance.checkEnvironment();
3943

4044
// TODO: add meaningful update checks after this task
4145
let tasks = [{

lib/instance.js

+34-29
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ class Instance {
3030
return false;
3131
}
3232

33-
// Ordinarily we'd use this.loadRunningConfig(), but stack overflow
34-
// (the error not the programming bible) happens if we do that.
35-
let env = this.cliConfig.get('running');
36-
this.config = new Config(path.join(this.dir, `config.${env}.json`));
37-
this.loadProcess();
33+
this.loadRunningEnvironment();
3834

3935
if (!this.process.isRunning(this.dir)) {
4036
this.cliConfig.set('running', null).save();
@@ -48,47 +44,56 @@ class Instance {
4844
return true;
4945
}
5046

47+
get config() {
48+
let currentEnv = this.system.environment;
49+
50+
if (!this._config || this._config.environment !== currentEnv) {
51+
this._config = new Config(path.join(this.dir, `config.${this.system.environment}.json`));
52+
this._config.environment = this.system.environment;
53+
}
54+
55+
return this._config;
56+
}
57+
58+
get process() {
59+
let name = this.config.get('process', 'local');
60+
61+
if (!this._process || name !== this._process.name) {
62+
let manager = this.system.getProcessManager(name);
63+
this._process = new manager.Class(this.ui, this.system, this);
64+
this._process.name = name;
65+
}
66+
67+
return this._process;
68+
}
69+
5170
constructor(ui, system, dir) {
5271
this.ui = ui;
5372
this.system = system;
5473
this.dir = dir;
5574
}
5675

57-
loadConfig(skipEnvCheck) {
58-
// If we are starting in production mode but a development config exists and a production config doesn't,
59-
// we want to start in development mode anyways.
76+
checkEnvironment() {
6077
if (
61-
!skipEnvCheck && this.system.production &&
78+
this.system.production &&
6279
Config.exists(path.join(this.dir, 'config.development.json')) &&
63-
!Config.exists(path.join(this.dir, 'config.production.json'))
80+
!Config.exists(path.join('config.production.json'))
6481
) {
6582
this.ui.log('Found a development config but not a production config, running in development mode instead', 'yellow');
6683
this.system.setEnvironment(true, true);
6784
}
68-
this.config = new Config(path.join(this.dir, `config.${this.system.environment}.json`));
69-
this.loadProcess();
7085
}
7186

72-
loadRunningConfig(setEnv, setNodeEnv) {
73-
if (!this.running) {
87+
loadRunningEnvironment(setEnv, setNodeEnv) {
88+
let env = this.cliConfig.get('running');
89+
90+
if (!env) {
7491
throw new Error('This instance is not running.');
7592
}
7693

77-
let env = this.cliConfig.get('running');
7894
if (setEnv) {
7995
this.system.setEnvironment(env === 'development', setNodeEnv);
8096
}
81-
82-
this.config = new Config(path.join(this.dir, `config.${env}.json`));
83-
this.loadProcess();
84-
return env;
85-
}
86-
87-
loadProcess() {
88-
let name = this.config.get('process', 'local');
89-
let manager = this.system.getProcessManager(name);
90-
this.process = new manager.Class(this.ui, this.system, this);
91-
this.processName = manager.name;
9297
}
9398

9499
summary() {
@@ -101,17 +106,17 @@ class Instance {
101106
};
102107
}
103108

104-
let env = this.loadRunningConfig();
109+
this.loadRunningEnvironment(true);
105110

106111
return {
107112
name: this.name,
108113
dir: this.dir.replace(os.homedir(), '~'),
109114
running: true,
110115
version: this.cliConfig.get('active-version'),
111-
mode: env,
116+
mode: this.system.environment,
112117
url: this.config.get('url'),
113118
port: this.config.get('server.port'),
114-
process: this.processName
119+
process: this._process.name
115120
};
116121
}
117122

0 commit comments

Comments
 (0)