Skip to content

Commit 26f2369

Browse files
committed
fix: throw better error when failing to parse json
Fixes #266
1 parent d0c80a8 commit 26f2369

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"inquirer": "1.1.2",
7878
"lodash": "4.14.1",
7979
"minimist": "1.2.0",
80+
"path-exists": "2.1.0",
8081
"shelljs": "0.5.3",
8182
"strip-json-comments": "2.0.1"
8283
},

src/configLoader/getContent.js

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,63 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import {sync as existsSync} from 'path-exists';
34
import stripJSONComments from 'strip-json-comments';
45

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

7-
export default getContent;
8+
export default getConfigContent;
9+
10+
/**
11+
* Read the content of a configuration file
12+
* - if not js or json: strip any comments
13+
* - if js or json: require it
14+
* @param {String} configPath - full path to configuration file
15+
* @return {Object}
16+
*/
17+
function readConfigContent(configPath) {
18+
const parsedPath = path.parse(configPath)
19+
const isRcFile = parsedPath.ext !== '.js' && parsedPath.ext !== '.json';
20+
const jsonString = fs.readFileSync(configPath, 'utf-8');
21+
const parse = isRcFile ?
22+
(contents) => JSON.parse(stripJSONComments(contents)) :
23+
(contents) => JSON.parse(contents);
24+
25+
try {
26+
const parsed = parse(jsonString);
27+
28+
Object.defineProperty(parsed, 'configPath', {
29+
value: configPath
30+
});
31+
32+
return parsed;
33+
} catch (error) {
34+
error.message = [
35+
`Parsing JSON at ${configPath} for commitizen config failed:`,
36+
error.mesasge
37+
].join('\n');
38+
39+
throw error;
40+
}
41+
}
842

943
/**
1044
* Get content of the configuration file
11-
* @param {String} config - partial path to configuration file
45+
* @param {String} configPath - partial path to configuration file
1246
* @param {String} directory - directory path which will be joined with config argument
1347
* @return {Object}
1448
*/
15-
function getContent(config, directory) {
16-
if (!config) {
17-
return;
49+
function getConfigContent(configPath, baseDirectory) {
50+
if (!configPath) {
51+
return;
1852
}
1953

20-
var configPath = path.resolve(directory, config);
21-
var ext;
22-
var content;
23-
24-
config = path.basename(config);
25-
26-
if (fs.existsSync(configPath)) {
27-
ext = path.extname(configPath);
28-
29-
if (ext === '.js' || ext === '.json') {
30-
content = JSON.parse(fs.readFileSync(configPath, 'utf8'));
31-
} else {
32-
content = JSON.parse(
33-
stripJSONComments(
34-
fs.readFileSync(configPath, 'utf8')
35-
)
36-
);
37-
}
38-
39-
// Adding property via Object.defineProperty makes it
40-
// non-enumerable and avoids warning for unsupported rules
41-
Object.defineProperty(content, 'configPath', {
42-
value: configPath
43-
});
54+
const resolvedPath = path.resolve(baseDirectory, configPath);
55+
const configBasename = path.basename(resolvedPath);
56+
57+
if (!existsSync(resolvedPath)) {
58+
return getNormalizedConfig(resolvedPath);
4459
}
45-
return getNormalizedConfig(config, content);
46-
47-
};
60+
61+
const content = readConfigContent(resolvedPath);
62+
return getNormalizedConfig(configBasename, content);
63+
};

0 commit comments

Comments
 (0)