Skip to content

Commit

Permalink
fix: throw better error when failing to parse json
Browse files Browse the repository at this point in the history
Fixes #266
  • Loading branch information
marionebl committed Aug 2, 2016
1 parent d0c80a8 commit 26f2369
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"inquirer": "1.1.2",
"lodash": "4.14.1",
"minimist": "1.2.0",
"path-exists": "2.1.0",
"shelljs": "0.5.3",
"strip-json-comments": "2.0.1"
},
Expand Down
80 changes: 48 additions & 32 deletions src/configLoader/getContent.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
import fs from 'fs';
import path from 'path';
import {sync as existsSync} from 'path-exists';
import stripJSONComments from 'strip-json-comments';

import {getNormalizedConfig} from '../configLoader';

export default getContent;
export default getConfigContent;

/**
* Read the content of a configuration file
* - if not js or json: strip any comments
* - if js or json: require it
* @param {String} configPath - full path to configuration file
* @return {Object}
*/
function readConfigContent(configPath) {
const parsedPath = path.parse(configPath)
const isRcFile = parsedPath.ext !== '.js' && parsedPath.ext !== '.json';
const jsonString = fs.readFileSync(configPath, 'utf-8');
const parse = isRcFile ?
(contents) => JSON.parse(stripJSONComments(contents)) :
(contents) => JSON.parse(contents);

try {
const parsed = parse(jsonString);

Object.defineProperty(parsed, 'configPath', {
value: configPath
});

return parsed;
} catch (error) {
error.message = [
`Parsing JSON at ${configPath} for commitizen config failed:`,
error.mesasge
].join('\n');

throw error;
}
}

/**
* Get content of the configuration file
* @param {String} config - partial path to configuration file
* @param {String} configPath - partial path to configuration file
* @param {String} directory - directory path which will be joined with config argument
* @return {Object}
*/
function getContent(config, directory) {
if (!config) {
return;
function getConfigContent(configPath, baseDirectory) {
if (!configPath) {
return;
}

var configPath = path.resolve(directory, config);
var ext;
var content;

config = path.basename(config);

if (fs.existsSync(configPath)) {
ext = path.extname(configPath);

if (ext === '.js' || ext === '.json') {
content = JSON.parse(fs.readFileSync(configPath, 'utf8'));
} else {
content = JSON.parse(
stripJSONComments(
fs.readFileSync(configPath, 'utf8')
)
);
}

// Adding property via Object.defineProperty makes it
// non-enumerable and avoids warning for unsupported rules
Object.defineProperty(content, 'configPath', {
value: configPath
});
const resolvedPath = path.resolve(baseDirectory, configPath);
const configBasename = path.basename(resolvedPath);

if (!existsSync(resolvedPath)) {
return getNormalizedConfig(resolvedPath);
}
return getNormalizedConfig(config, content);

};

const content = readConfigContent(resolvedPath);
return getNormalizedConfig(configBasename, content);
};

0 comments on commit 26f2369

Please sign in to comment.