Skip to content

Commit 56551ae

Browse files
kyeahalexcanessa
authored andcommitted
Add custom --config option (#163)
* Add custom --config option * throw an error if custom config file does not exist * Add a test
1 parent 9ff8e2a commit 56551ae

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

lib/_options.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ module.exports = {
159159
short: '-q',
160160
name: 'quiet',
161161
description: 'Run command without console logs.'
162+
},
163+
{
164+
short: '-c',
165+
name: 'config',
166+
valueType: '<string>',
167+
description: 'Specify a custom config filename'
162168
}
163169
],
164170
releaseOptions: [

lib/src/Program.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ class Program {
1616
.description(this.description)
1717
.parse(props.argv);
1818

19-
this.options = Object.assign({}, getConfigFromFile(props.cwd), this._getOptionsFromObject(this.program, this.defaults));
19+
this.options = Object.assign(
20+
{},
21+
getConfigFromFile(props.cwd, program.config),
22+
this._getOptionsFromObject(this.program, this.defaults)
23+
);
2024
}
2125

2226
/**

lib/src/_utils.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const chalk = require('chalk');
22
const fs = require('fs');
33
const ora = require('ora');
44
const YAML = require('json2yaml');
5+
const Path = require('path');
56
const { js_beautify: beautify } = require('js-beautify');
67
require('require-yaml');
78

@@ -191,9 +192,17 @@ function requireConfig(filepath) {
191192
* @param {string} path Path where to look for config files
192193
* @return {Object} The configuration from the first found file or empty object
193194
*/
194-
function getConfigFromFile(path) {
195+
function getConfigFromFile(path, customFilename = null) {
196+
if (customFilename) {
197+
const config = requireConfig(Path.join(path, customFilename));
198+
if (!config) {
199+
throw chalk.red(`Could not find custom config file: ${customFilename}`);
200+
}
201+
return config;
202+
}
203+
195204
return getFileTypes()
196-
.reduce((carry, filename) => carry || requireConfig(path + '/' + filename), false) || {};
205+
.reduce((carry, filename) => carry || requireConfig(Path.join(path, filename)), false) || {};
197206
}
198207

199208
/**

test/_utils.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { assert } from 'chai';
2+
import chalk from 'chalk';
23
import fs from 'fs';
34
import YAML from 'yamljs';
45
import * as utils from '../lib/src/_utils';
@@ -142,18 +143,34 @@ describe('_utils.js', () => {
142143
b: 2
143144
};
144145

146+
const customFilename = process.cwd() + '/test/.temp/.custom-grenrc';
147+
const customFileContent = {
148+
c: 3,
149+
d: 4
150+
};
151+
145152
beforeEach(() => {
146153
fs.writeFileSync(filename, JSON.stringify(fileContent));
154+
fs.writeFileSync(customFilename, JSON.stringify(customFileContent));
147155
});
148156

149157
it('Should always return an Object', () => {
150158
assert.isOk(typeof utils.getConfigFromFile(process.cwd() + '/test/.temp') === 'object', 'The type is an object');
151159
assert.deepEqual(utils.getConfigFromFile(process.cwd() + '/test/.temp'), fileContent, 'Given the right path');
160+
assert.deepEqual(utils.getConfigFromFile(process.cwd() + '/test/.temp', '.custom-grenrc'), customFileContent, 'Given a custom path');
152161
assert.deepEqual(utils.getConfigFromFile(process.cwd() + '/test'), {}, 'Given a path with no config file');
153162
});
154163

164+
it('Should throw on non-existent custom config file', () => {
165+
assert.throws(
166+
() => utils.getConfigFromFile(process.cwd() + '/test/.temp', '.non-existing-grenrc'),
167+
chalk.red('Could not find custom config file: .non-existing-grenrc')
168+
);
169+
});
170+
155171
afterEach(() => {
156172
fs.unlinkSync(filename);
173+
fs.unlinkSync(customFilename);
157174
});
158175
});
159176

0 commit comments

Comments
 (0)