mirror of
https://github.com/zhigang1992/replace-in-file.git
synced 2026-06-15 10:37:52 +08:00
* Bump dependencies * [feature] #38 Count number of matches * Update make-replacements.js * [feature] #42 Differentiate number of matches and number of replacements * [enhance] #56 Support for CWD parameter * Default config value * [enhance] #63 Add --quiet flag to supress console output in CLI * Update success-handler.js * Update readme and add change log
68 lines
1.3 KiB
JavaScript
68 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Defaults
|
|
*/
|
|
const defaults = {
|
|
ignore: [],
|
|
encoding: 'utf-8',
|
|
disableGlobs: false,
|
|
allowEmptyPaths: false,
|
|
countMatches: false,
|
|
isRegex: false,
|
|
verbose: false,
|
|
quiet: false,
|
|
dry: false,
|
|
glob: {},
|
|
cwd: null,
|
|
};
|
|
|
|
/**
|
|
* Parse config
|
|
*/
|
|
module.exports = function parseConfig(config) {
|
|
|
|
//Validate config
|
|
if (typeof config !== 'object' || config === null) {
|
|
throw new Error('Must specify configuration object');
|
|
}
|
|
|
|
//Fix glob
|
|
config.glob = config.glob || {};
|
|
|
|
//Extract data
|
|
const {files, from, to, ignore, encoding} = config;
|
|
|
|
//Validate values
|
|
if (typeof files === 'undefined') {
|
|
throw new Error('Must specify file or files');
|
|
}
|
|
if (typeof from === 'undefined') {
|
|
throw new Error('Must specify string or regex to replace');
|
|
}
|
|
if (typeof to === 'undefined') {
|
|
throw new Error('Must specify a replacement (can be blank string)');
|
|
}
|
|
|
|
//Ensure arrays
|
|
if (!Array.isArray(files)) {
|
|
config.files = [files];
|
|
}
|
|
if (!Array.isArray(ignore)) {
|
|
if (typeof ignore === 'undefined') {
|
|
config.ignore = [];
|
|
}
|
|
else {
|
|
config.ignore = [ignore];
|
|
}
|
|
}
|
|
|
|
//Use default encoding if invalid
|
|
if (typeof encoding !== 'string' || encoding === '') {
|
|
config.encoding = 'utf-8';
|
|
}
|
|
|
|
//Merge config with defaults
|
|
return Object.assign({}, defaults, config);
|
|
};
|