Skip to content

Commit

Permalink
Show a helpful message when .all-contributorsrc doesn't exist. (#23)
Browse files Browse the repository at this point in the history
* Show a helpful message when .all-contributorsrc doesn't exist.

* Adding unit tests.

* Better test descriptions.

* Just code improvements.
  • Loading branch information
fadc80 authored and jfmengels committed Oct 17, 2016
1 parent bae31e1 commit 190813c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
15 changes: 4 additions & 11 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/* eslint-disable no-console */
'use strict';

var fs = require('fs');
var path = require('path');
var yargs = require('yargs');
var inquirer = require('inquirer');
Expand Down Expand Up @@ -31,11 +30,10 @@ var argv = yargs
.default('config', defaultRCFile)
.config('config', function (configPath) {
try {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
return util.configFile.readConfig(configPath);
} catch (error) {
if (configPath !== defaultRCFile) {
console.error(error.message);
process.exit(1);
onError(error);
}
}
})
Expand Down Expand Up @@ -80,17 +78,12 @@ function addContribution(argv, cb) {

function onError(error) {
if (error) {
return console.error(error);
console.error(error.message);
process.exit(1);
}
}

function promptForCommand(argv, cb) {
try {
fs.statSync(argv.config);
} catch (error) { // No config file --> first time using the command
return cb('init');
}

var questions = [{
type: 'list',
name: 'command',
Expand Down
16 changes: 14 additions & 2 deletions lib/util/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ var fs = require('fs');
var _ = require('lodash/fp');

function readConfig(configPath) {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
try {
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) {
return fs.writeFile(configPath, JSON.stringify(content, null, 2) + '\n', 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});
return writeConfig(configPath, content, cb);
}
Expand Down
18 changes: 18 additions & 0 deletions lib/util/config-file.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 190813c

Please sign in to comment.