Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add custom --config option #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ module.exports = {
short: '-q',
name: 'quiet',
description: 'Run command without console logs.'
},
{
short: '-c',
name: 'config',
valueType: '<string>',
description: 'Specify a custom config filename'
}
],
releaseOptions: [
Expand Down
6 changes: 5 additions & 1 deletion lib/src/Program.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand Down
13 changes: 11 additions & 2 deletions lib/src/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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) || {};
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/_utils.spec.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
});
});

Expand Down