Skip to content

Commit

Permalink
feat(instance): add method for retrieving all available configs (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine authored Jun 3, 2020
1 parent 9640827 commit d654bf4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
24 changes: 24 additions & 0 deletions lib/instance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const os = require('os');
const fs = require('fs-extra');
const path = require('path');
const Config = require('./utils/config');

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

/**
* Gets a list of availble configurations for this instance
*
* @method getAvailableConfigs
* @return {Promise<{[s: string]: Config}>}
*/
async getAvailableConfigs() {
const configRegex = /^config\.([a-z]+)\.json$/;
const files = await fs.readdir(this.dir);

return files.reduce((configs, f) => {
const matches = f.match(configRegex);
if (!matches) {
return configs;
}

return {
...configs,
[matches[1]]: new Config(path.join(this.dir, f))
};
}, {});
}

/**
* Starts the Ghost instance
*
Expand Down
50 changes: 48 additions & 2 deletions test/unit/instance-spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const expect = require('chai').expect;
const {expect} = require('chai');
const sinon = require('sinon');
const createConfigStub = require('../utils/config-stub');
const {setupTestFolder} = require('../utils/test-folder');

const Instance = require('../../lib/instance');
const Config = require('../../lib/utils/config');
Expand Down Expand Up @@ -449,6 +449,52 @@ describe('Unit: Instance', function () {
});
});

it('getAvailableConfigs returns available configs', async function () {
const {dir, cleanup} = setupTestFolder({
files: [{
path: 'config.development.json',
content: {
env: 'development'
},
json: true
}, {
path: 'config.staging.json',
content: {
env: 'staging'
},
json: true
}, {
path: 'config.production.json',
content: {
env: 'production'
},
json: true
}, {
path: 'somefile.txt',
content: 'filecontents'
}]
});

try {
const instance = new Instance({}, {}, dir);
const results = await instance.getAvailableConfigs();

expect(results.development).to.exist;
expect(results.development).to.be.an.instanceof(Config);
expect(results.development.get('env')).to.equal('development');

expect(results.staging).to.exist;
expect(results.staging).to.be.an.instanceof(Config);
expect(results.staging.get('env')).to.equal('staging');

expect(results.production).to.exist;
expect(results.production).to.be.an.instanceof(Config);
expect(results.production.get('env')).to.equal('production');
} finally {
cleanup();
}
});

describe('start', function () {
it('skips enable functionality if enable param is false', async function () {
const process = {
Expand Down

0 comments on commit d654bf4

Please sign in to comment.