Skip to content

Commit

Permalink
🐛 🎨 make the config utility a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine committed Sep 26, 2016
1 parent 0d9efe2 commit 9c337f9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/commands/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = BaseCommand.extend({

var Config = require('../../utils/config'),
configFile = 'config.' + options.environment + '.json',
config = new Config(configFile);
config = Config.load(configFile);

if (key && !value) {
// getter
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = BaseCommand.extend({
);
}).then(function afterMove() {
var symlinkSync = require('symlink-or-copy').sync,
cliConfig = new Config('.ghost-cli');
cliConfig = Config.load('.ghost-cli');

symlinkSync(installPath, path.join(process.cwd(), 'current'));

Expand Down
4 changes: 2 additions & 2 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module.exports = BaseCommand.extend({
options = options || {};
environment = (options.production || !options.development) ? 'production' : 'development';

config = new Config('config.' + environment + '.json');
cliConfig = new Config('.ghost-cli');
config = Config.load('config.' + environment + '.json');
cliConfig = Config.load('.ghost-cli');

if (cliConfig.has('running')) {
return Promise.reject('Ghost is already running.');
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ module.exports = BaseCommand.extend({

var Promise = require('bluebird'),
Config = require('../utils/config'),
cliConfig = new Config('.ghost-cli'),
cliConfig = Config.load('.ghost-cli'),
resolveProcessManager = require('../process').resolve,
config, pm;

if (!cliConfig.has('running')) {
return Promise.reject('Ghost instance is not currently running.');
}

config = new Config('config.' + cliConfig.get('running') + '.json');
config = Config.load('config.' + cliConfig.get('running') + '.json');
pm = resolveProcessManager(config);

return this.ui.run(
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = BaseCommand.extend({
path = require('path'),
Config = require('../utils/config'),
findVersion = require('../utils/version'),
config = new Config('.ghost-cli'),
config = Config.load('.ghost-cli'),
self = this,
installPath;

Expand Down
11 changes: 11 additions & 0 deletions lib/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var isPlainObject = require('lodash/isPlainObject'),
_set = require('lodash/set'),
_has = require('lodash/has'),
fs = require('fs-extra'),
singletonCache = {},

Config;

Expand Down Expand Up @@ -56,3 +57,13 @@ Config.exists = function exists(filename) {
};

module.exports = Config;
module.exports.load = function load(filename) {
if (singletonCache[filename] && singletonCache[filename] instanceof Config) {
return singletonCache[filename];
}

var configInstance = new Config(filename);
singletonCache[filename] = configInstance;

return configInstance;
};
28 changes: 28 additions & 0 deletions test/unit/utils/config-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,32 @@ describe('Unit: Config', function () {
fs.removeSync('config-test.json');
});
});

describe('load()', function () {
it('returns an instance of Config', function () {
fs.writeJsonSync('config-test.json', {test: 'a'});
var result = Config.load('config-test.json');
expect(result).to.be.an.instanceof(Config);
expect(result.get('test')).to.equal('a');
fs.removeSync('config-test.json');
});

it('returns a singleton instance of Config', function () {
fs.writeJsonSync('config-test.json', {test: 'a'});
var result = Config.load('config-test.json'),
result2;

expect(result).to.be.an.instanceof(Config);
expect(result.get('test')).to.equal('a');

result2 = Config.load('config-test.json');
expect(result2).to.be.an.instanceof(Config);
expect(result2.get('test')).to.equal('a');

result2.set('test', 'b');
expect(result2.get('test')).to.equal('b');
expect(result.get('test')).to.equal('b');
fs.removeSync('config-test.json');
});
});
});

0 comments on commit 9c337f9

Please sign in to comment.