From 190813c61e98cf5c91a09c77501840c2be2ab712 Mon Sep 17 00:00:00 2001 From: fadc80 Date: Mon, 17 Oct 2016 16:11:33 -0300 Subject: [PATCH] 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. --- cli.js | 15 ++++----------- lib/util/config-file.js | 16 ++++++++++++++-- lib/util/config-file.test.js | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 lib/util/config-file.test.js diff --git a/cli.js b/cli.js index 5ef587ba..6be248b6 100755 --- a/cli.js +++ b/cli.js @@ -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'); @@ -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); } } }) @@ -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', diff --git a/lib/util/config-file.js b/lib/util/config-file.js index cc343501..5078b11f 100644 --- a/lib/util/config-file.js +++ b/lib/util/config-file.js @@ -4,7 +4,14 @@ 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) { @@ -12,7 +19,12 @@ function writeConfig(configPath, content, 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); } diff --git a/lib/util/config-file.test.js b/lib/util/config-file.test.js new file mode 100644 index 00000000..24291d32 --- /dev/null +++ b/lib/util/config-file.test.js @@ -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(); + }); +});