mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-10 05:56:29 +00:00
Show a helpful message when .all-contributorsrc doesn't exist. (#23)
* Show a helpful message when .all-contributorsrc doesn't exist. * Adding unit tests. * Better test descriptions. * Just code improvements.
This commit is contained in:
parent
bae31e1f47
commit
190813c61e
3 changed files with 36 additions and 13 deletions
15
cli.js
15
cli.js
|
@ -2,7 +2,6 @@
|
||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var fs = require('fs');
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var yargs = require('yargs');
|
var yargs = require('yargs');
|
||||||
var inquirer = require('inquirer');
|
var inquirer = require('inquirer');
|
||||||
|
@ -31,11 +30,10 @@ var argv = yargs
|
||||||
.default('config', defaultRCFile)
|
.default('config', defaultRCFile)
|
||||||
.config('config', function (configPath) {
|
.config('config', function (configPath) {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
return util.configFile.readConfig(configPath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (configPath !== defaultRCFile) {
|
if (configPath !== defaultRCFile) {
|
||||||
console.error(error.message);
|
onError(error);
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -80,17 +78,12 @@ function addContribution(argv, cb) {
|
||||||
|
|
||||||
function onError(error) {
|
function onError(error) {
|
||||||
if (error) {
|
if (error) {
|
||||||
return console.error(error);
|
console.error(error.message);
|
||||||
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function promptForCommand(argv, cb) {
|
function promptForCommand(argv, cb) {
|
||||||
try {
|
|
||||||
fs.statSync(argv.config);
|
|
||||||
} catch (error) { // No config file --> first time using the command
|
|
||||||
return cb('init');
|
|
||||||
}
|
|
||||||
|
|
||||||
var questions = [{
|
var questions = [{
|
||||||
type: 'list',
|
type: 'list',
|
||||||
name: 'command',
|
name: 'command',
|
||||||
|
|
|
@ -4,7 +4,14 @@ var fs = require('fs');
|
||||||
var _ = require('lodash/fp');
|
var _ = require('lodash/fp');
|
||||||
|
|
||||||
function readConfig(configPath) {
|
function readConfig(configPath) {
|
||||||
|
try {
|
||||||
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === 'ENOENT') {
|
||||||
|
throw new Error('Configuration file not found: ' + configPath);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeConfig(configPath, content, cb) {
|
function writeConfig(configPath, content, cb) {
|
||||||
|
@ -12,7 +19,12 @@ function writeConfig(configPath, content, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeContributors(configPath, contributors, cb) {
|
function writeContributors(configPath, contributors, cb) {
|
||||||
var config = readConfig(configPath);
|
var config;
|
||||||
|
try {
|
||||||
|
config = readConfig(configPath);
|
||||||
|
} catch (error) {
|
||||||
|
return cb(error);
|
||||||
|
}
|
||||||
var content = _.assign(config, {contributors: contributors});
|
var content = _.assign(config, {contributors: contributors});
|
||||||
return writeConfig(configPath, content, cb);
|
return writeConfig(configPath, content, cb);
|
||||||
}
|
}
|
||||||
|
|
18
lib/util/config-file.test.js
Normal file
18
lib/util/config-file.test.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import test from 'ava';
|
||||||
|
import configFile from './config-file.js';
|
||||||
|
|
||||||
|
const absentFile = './abc';
|
||||||
|
const expected = 'Configuration file not found: ' + absentFile;
|
||||||
|
|
||||||
|
test('Reading an absent configuration file throws a helpful error', t => {
|
||||||
|
t.throws(() => {
|
||||||
|
configFile.readConfig(absentFile);
|
||||||
|
}, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.cb('Writing contributors in an absent configuration file throws a helpful error', t => {
|
||||||
|
configFile.writeContributors(absentFile, [], error => {
|
||||||
|
t.is(error.message, expected);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue