Skip to content
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

Show a helpful message when .all-contributorsrc doesn't exist. #23

Merged
merged 4 commits into from
Oct 17, 2016
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
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();
});
});