diff --git a/lib/_options.js b/lib/_options.js index 04f997be..2ebd94b3 100644 --- a/lib/_options.js +++ b/lib/_options.js @@ -159,6 +159,12 @@ module.exports = { short: '-q', name: 'quiet', description: 'Run command without console logs.' + }, + { + short: '-c', + name: 'config', + valueType: '', + description: 'Specify a custom config filename' } ], releaseOptions: [ diff --git a/lib/src/Program.js b/lib/src/Program.js index 3ae4dd54..d1e0a784 100644 --- a/lib/src/Program.js +++ b/lib/src/Program.js @@ -16,7 +16,11 @@ class Program { .description(this.description) .parse(props.argv); - this.options = Object.assign({}, getConfigFromFile(props.cwd), this._getOptionsFromObject(this.program, this.defaults)); + this.options = Object.assign( + {}, + getConfigFromFile(props.cwd, program.config), + this._getOptionsFromObject(this.program, this.defaults) + ); } /** diff --git a/lib/src/_utils.js b/lib/src/_utils.js index 7153b240..47d95570 100644 --- a/lib/src/_utils.js +++ b/lib/src/_utils.js @@ -2,6 +2,7 @@ const chalk = require('chalk'); const fs = require('fs'); const ora = require('ora'); const YAML = require('json2yaml'); +const Path = require('path'); const { js_beautify: beautify } = require('js-beautify'); require('require-yaml'); @@ -191,9 +192,17 @@ function requireConfig(filepath) { * @param {string} path Path where to look for config files * @return {Object} The configuration from the first found file or empty object */ -function getConfigFromFile(path) { +function getConfigFromFile(path, customFilename = null) { + if (customFilename) { + const config = requireConfig(Path.join(path, customFilename)); + if (!config) { + throw chalk.red(`Could not find custom config file: ${customFilename}`); + } + return config; + } + return getFileTypes() - .reduce((carry, filename) => carry || requireConfig(path + '/' + filename), false) || {}; + .reduce((carry, filename) => carry || requireConfig(Path.join(path, filename)), false) || {}; } /** diff --git a/test/_utils.spec.js b/test/_utils.spec.js index 9c65b914..73f64e95 100644 --- a/test/_utils.spec.js +++ b/test/_utils.spec.js @@ -1,4 +1,5 @@ import { assert } from 'chai'; +import chalk from 'chalk'; import fs from 'fs'; import YAML from 'yamljs'; import * as utils from '../lib/src/_utils'; @@ -142,18 +143,34 @@ describe('_utils.js', () => { b: 2 }; + const customFilename = process.cwd() + '/test/.temp/.custom-grenrc'; + const customFileContent = { + c: 3, + d: 4 + }; + beforeEach(() => { fs.writeFileSync(filename, JSON.stringify(fileContent)); + fs.writeFileSync(customFilename, JSON.stringify(customFileContent)); }); it('Should always return an Object', () => { assert.isOk(typeof utils.getConfigFromFile(process.cwd() + '/test/.temp') === 'object', 'The type is an object'); assert.deepEqual(utils.getConfigFromFile(process.cwd() + '/test/.temp'), fileContent, 'Given the right path'); + assert.deepEqual(utils.getConfigFromFile(process.cwd() + '/test/.temp', '.custom-grenrc'), customFileContent, 'Given a custom path'); assert.deepEqual(utils.getConfigFromFile(process.cwd() + '/test'), {}, 'Given a path with no config file'); }); + it('Should throw on non-existent custom config file', () => { + assert.throws( + () => utils.getConfigFromFile(process.cwd() + '/test/.temp', '.non-existing-grenrc'), + chalk.red('Could not find custom config file: .non-existing-grenrc') + ); + }); + afterEach(() => { fs.unlinkSync(filename); + fs.unlinkSync(customFilename); }); });