From e555e8549385851e78aa06b03ec4803a3bab5e82 Mon Sep 17 00:00:00 2001 From: Kevin Yeh Date: Sat, 5 May 2018 22:48:15 -0400 Subject: [PATCH 1/3] Add custom --config option --- lib/_options.js | 6 ++++++ lib/src/Program.js | 6 +++++- lib/src/_utils.js | 11 ++++++++--- test/_utils.spec.js | 9 +++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) 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..2989d18a 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,13 @@ 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) { - return getFileTypes() - .reduce((carry, filename) => carry || requireConfig(path + '/' + filename), false) || {}; +function getConfigFromFile(path, customFilename = null) { + if (customFilename) { + return requireConfig(Path.join(path, customFilename)) || {}; + } else { + return getFileTypes() + .reduce((carry, filename) => carry || requireConfig(Path.join(path, filename)), false) || {}; + } } /** diff --git a/test/_utils.spec.js b/test/_utils.spec.js index 9c65b914..65b373cc 100644 --- a/test/_utils.spec.js +++ b/test/_utils.spec.js @@ -142,18 +142,27 @@ 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'); }); afterEach(() => { fs.unlinkSync(filename); + fs.unlinkSync(customFilename); }); }); From c6b664bb31523be190a32b3708041ef3c1417969 Mon Sep 17 00:00:00 2001 From: Kevin Yeh Date: Wed, 9 May 2018 17:16:17 -0400 Subject: [PATCH 2/3] throw an error if custom config file does not exist --- lib/src/_utils.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/src/_utils.js b/lib/src/_utils.js index 2989d18a..b5605964 100644 --- a/lib/src/_utils.js +++ b/lib/src/_utils.js @@ -194,11 +194,16 @@ function requireConfig(filepath) { */ function getConfigFromFile(path, customFilename = null) { if (customFilename) { - return requireConfig(Path.join(path, customFilename)) || {}; - } else { - return getFileTypes() - .reduce((carry, filename) => carry || requireConfig(Path.join(path, filename)), false) || {}; + const config = requireConfig(Path.join(path, customFilename)); + if (!config) { + console.error(chalk.red(`Could not find custom config file: ${customFilename}`)); + process.exit(1); + } + return config; } + + return getFileTypes() + .reduce((carry, filename) => carry || requireConfig(Path.join(path, filename)), false) || {}; } /** From 5c19fc71362daf2735fb048312e90ab7a74dfc72 Mon Sep 17 00:00:00 2001 From: Kevin Yeh Date: Wed, 9 May 2018 17:21:58 -0400 Subject: [PATCH 3/3] Add a test --- lib/src/_utils.js | 3 +-- test/_utils.spec.js | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/src/_utils.js b/lib/src/_utils.js index b5605964..47d95570 100644 --- a/lib/src/_utils.js +++ b/lib/src/_utils.js @@ -196,8 +196,7 @@ function getConfigFromFile(path, customFilename = null) { if (customFilename) { const config = requireConfig(Path.join(path, customFilename)); if (!config) { - console.error(chalk.red(`Could not find custom config file: ${customFilename}`)); - process.exit(1); + throw chalk.red(`Could not find custom config file: ${customFilename}`); } return config; } diff --git a/test/_utils.spec.js b/test/_utils.spec.js index 65b373cc..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'; @@ -160,6 +161,13 @@ describe('_utils.js', () => { 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);