Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(configuration): .versionrc.js files are now supported
- Updates the configuration retrieval to support Javascript (`.js`) configurations. - Javascript configurations MUST export a configuration object _or_ a function (returning a configuration object) as the default export. closes #371
- Loading branch information
1 parent
fe37e73
commit 1cb1068
Showing
3 changed files
with
91 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const path = require('path') | ||
const findUp = require('find-up') | ||
const { readFileSync } = require('fs') | ||
|
||
const CONFIGURATION_FILES = [ | ||
'.versionrc', | ||
'.versionrc.json', | ||
'.versionrc.js' | ||
] | ||
|
||
module.exports.getConfiguration = function () { | ||
let config = {} | ||
const configPath = findUp.sync(CONFIGURATION_FILES) | ||
if (!configPath) { | ||
return config | ||
} | ||
if (path.extname(configPath) === '.js') { | ||
const jsConfiguration = require(configPath) | ||
if (typeof jsConfiguration === 'function') { | ||
config = jsConfiguration() | ||
} else { | ||
config = jsConfiguration | ||
} | ||
} else { | ||
config = JSON.parse(readFileSync(configPath)) | ||
} | ||
|
||
/** | ||
* @todo we could eventually have deeper validation of the configuration (using `ajv`) and | ||
* provide a more helpful error. | ||
*/ | ||
if (typeof config !== 'object') { | ||
throw Error( | ||
'[standard-version] Invalid default export found in configuration file.\n' + | ||
'You must provide an object or function.' | ||
) | ||
} | ||
|
||
return config | ||
} |
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