-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: throw better error when failing to parse json
Fixes #266
- Loading branch information
Showing
2 changed files
with
49 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |