Skip to content

Commit

Permalink
fix(config): only create config file when there is none
Browse files Browse the repository at this point in the history
* Show a friendly error when the file is malformed.
  • Loading branch information
tagoro9 committed Feb 24, 2017
1 parent 83d70af commit 57de6b2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
9 changes: 7 additions & 2 deletions lib/config/read-config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var _configFilePath = require('./config-file-path');

var _configFilePath2 = _interopRequireDefault(_configFilePath);

var _error = require('../error');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var createEmptyConfigFile = _ramda2['default'].curryN(3, _fs2['default'].writeFileSync)(_ramda2['default'].__, _ramda2['default'].__, 'utf8');
Expand All @@ -28,7 +30,10 @@ exports['default'] = function (defaults) {
try {
return readConfigFile(_configFilePath2['default']);
} catch (e) {
createEmptyConfigFile(_configFilePath2['default'], JSON.stringify(defaults));
return defaults;
if (e.code && e.code === 'ENOENT') {
createEmptyConfigFile(_configFilePath2['default'], JSON.stringify(defaults, null, 2));
return defaults;
}
return (0, _error.handleError)(new _error.ControlledError(_error.errors.config.malformedFile));
}
};
3 changes: 3 additions & 0 deletions lib/error/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Object.defineProperty(exports, "__esModule", {
});
/* eslint-disable max-len */
exports['default'] = {
config: {
malformedFile: 'It looks like your config file is not correct. If you have modified it, make sure it is a valid JSON'
},
git: {
branchAlreadyExists: 'It looks like you already have a branch for this issue.',
couldNotInitializeRepo: 'We have problems accessing the repo in \'${pathToRepo}\'. Make sure it exists.',
Expand Down
4 changes: 2 additions & 2 deletions lib/error/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.errors = exports.catchPromiseAndThrow = exports.handleError = exports.throwControlledError = undefined;
exports.errors = exports.catchPromiseAndThrow = exports.handleError = exports.throwControlledError = exports.ControlledError = undefined;

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

Expand Down Expand Up @@ -54,7 +54,7 @@ function _extendableBuiltin(cls) {
return ExtendableBuiltin;
}

var ControlledError = function (_extendableBuiltin2) {
var ControlledError = exports.ControlledError = function (_extendableBuiltin2) {
_inherits(ControlledError, _extendableBuiltin2);

function ControlledError(message) {
Expand Down
8 changes: 6 additions & 2 deletions src/config/read-config-file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import R from 'ramda';
import configFilePath from './config-file-path';
import { ControlledError, errors, handleError } from '../error';

const createEmptyConfigFile = R.curryN(3, fs.writeFileSync)(R.__, R.__, 'utf8');
const readConfigFile = R.curryN(2, R.compose(JSON.parse, fs.readFileSync))(R.__, 'utf8');
Expand All @@ -12,7 +13,10 @@ export default (defaults) => {
try {
return readConfigFile(configFilePath);
} catch (e) {
createEmptyConfigFile(configFilePath, JSON.stringify(defaults));
return defaults;
if (e.code && e.code === 'ENOENT') {
createEmptyConfigFile(configFilePath, JSON.stringify(defaults, null, 2));
return defaults;
}
return handleError(new ControlledError(errors.config.malformedFile));
}
};
3 changes: 3 additions & 0 deletions src/error/errors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-disable max-len */
export default {
config: {
malformedFile: 'It looks like your config file is not correct. If you have modified it, make sure it is a valid JSON'
},
git: {
branchAlreadyExists: 'It looks like you already have a branch for this issue.',
couldNotInitializeRepo: 'We have problems accessing the repo in \'${pathToRepo}\'. Make sure it exists.',
Expand Down
2 changes: 1 addition & 1 deletion src/error/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import errors from './errors';
import { debugCurried, error, errorCurried } from '../util';
import reporter from '../reporter';

class ControlledError extends Error {
export class ControlledError extends Error {
constructor(message, parameters = {}) {
super(R.compose(
R.reduce((msg, [k, v]) => R.replace(`\${${k}}`, v, msg), message),
Expand Down

0 comments on commit 57de6b2

Please sign in to comment.