Skip to content

Commit d654bf4

Browse files
authored
feat(instance): add method for retrieving all available configs (#1210)
1 parent 9640827 commit d654bf4

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

lib/instance.js

+24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const os = require('os');
2+
const fs = require('fs-extra');
23
const path = require('path');
34
const Config = require('./utils/config');
45

@@ -245,6 +246,29 @@ class Instance {
245246
this.system.setEnvironment(env === 'development', setNodeEnv);
246247
}
247248

249+
/**
250+
* Gets a list of availble configurations for this instance
251+
*
252+
* @method getAvailableConfigs
253+
* @return {Promise<{[s: string]: Config}>}
254+
*/
255+
async getAvailableConfigs() {
256+
const configRegex = /^config\.([a-z]+)\.json$/;
257+
const files = await fs.readdir(this.dir);
258+
259+
return files.reduce((configs, f) => {
260+
const matches = f.match(configRegex);
261+
if (!matches) {
262+
return configs;
263+
}
264+
265+
return {
266+
...configs,
267+
[matches[1]]: new Config(path.join(this.dir, f))
268+
};
269+
}, {});
270+
}
271+
248272
/**
249273
* Starts the Ghost instance
250274
*

test/unit/instance-spec.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
'use strict';
2-
const expect = require('chai').expect;
1+
const {expect} = require('chai');
32
const sinon = require('sinon');
43
const createConfigStub = require('../utils/config-stub');
4+
const {setupTestFolder} = require('../utils/test-folder');
55

66
const Instance = require('../../lib/instance');
77
const Config = require('../../lib/utils/config');
@@ -449,6 +449,52 @@ describe('Unit: Instance', function () {
449449
});
450450
});
451451

452+
it('getAvailableConfigs returns available configs', async function () {
453+
const {dir, cleanup} = setupTestFolder({
454+
files: [{
455+
path: 'config.development.json',
456+
content: {
457+
env: 'development'
458+
},
459+
json: true
460+
}, {
461+
path: 'config.staging.json',
462+
content: {
463+
env: 'staging'
464+
},
465+
json: true
466+
}, {
467+
path: 'config.production.json',
468+
content: {
469+
env: 'production'
470+
},
471+
json: true
472+
}, {
473+
path: 'somefile.txt',
474+
content: 'filecontents'
475+
}]
476+
});
477+
478+
try {
479+
const instance = new Instance({}, {}, dir);
480+
const results = await instance.getAvailableConfigs();
481+
482+
expect(results.development).to.exist;
483+
expect(results.development).to.be.an.instanceof(Config);
484+
expect(results.development.get('env')).to.equal('development');
485+
486+
expect(results.staging).to.exist;
487+
expect(results.staging).to.be.an.instanceof(Config);
488+
expect(results.staging.get('env')).to.equal('staging');
489+
490+
expect(results.production).to.exist;
491+
expect(results.production).to.be.an.instanceof(Config);
492+
expect(results.production.get('env')).to.equal('production');
493+
} finally {
494+
cleanup();
495+
}
496+
});
497+
452498
describe('start', function () {
453499
it('skips enable functionality if enable param is false', async function () {
454500
const process = {

0 commit comments

Comments
 (0)