From: alexlamsl Date: Sat, 26 Mar 2016 17:41:38 +0000 (+0800) Subject: process JSON config options that contain regex's X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=02b06975904ee09d4e2f46a0a3c4c1923e03a9c0;p=html-minifier.git process JSON config options that contain regex's --- diff --git a/cli.js b/cli.js index 95c68f8..121c814 100755 --- a/cli.js +++ b/cli.js @@ -142,20 +142,13 @@ cli.main(function(args, options) { try { jsonArray = JSON.parse(value); if (regexArray) { - jsonArray = jsonArray.map(function (regexString) { - return stringToRegExp(regexString); - }); + jsonArray = jsonArray.map(stringToRegExp); } } catch (e) { cli.fatal('Could not parse JSON value \'' + value + '\''); } - if (jsonArray instanceof Array) { - return jsonArray; - } - else { - return [value]; - } + return Array.isArray(jsonArray) ? jsonArray : [value]; } } @@ -235,29 +228,40 @@ cli.main(function(args, options) { } if (options['config-file']) { - var fileOptions; - var fileOptionsPath = path.resolve(options['config-file']); - try { - fileOptions = fs.readFileSync(fileOptionsPath, { encoding: 'utf8' }); - } - catch (e) { - cli.fatal('The specified config file doesn’t exist or is unreadable:\n' + fileOptionsPath); - } + var configPath = path.resolve(options['config-file']); try { - fileOptions = JSON.parse(fileOptions); + var configData; + try { + configData = fs.readFileSync(configPath, { encoding: 'utf8' }); + } + catch (e) { + cli.fatal('The specified config file doesn’t exist or is unreadable:\n' + configPath); + } + configData = JSON.parse(configData); + mainOptionKeys.forEach(function(key) { + var value = configData[key]; + if (value !== undefined) { + switch (mainOptions[key][1]) { + case 'json-regex': + minifyOptions[key] = value.map(stringToRegExp); + break; + case 'string-regex': + minifyOptions[key] = stringToRegExp(value); + break; + default: + minifyOptions[key] = value; + } + } + }); } catch (je) { try { - fileOptions = require(fileOptionsPath); + minifyOptions = require(configPath); } catch (ne) { - cli.fatal('Cannot read the specified config file. \nAs JSON: ' + je.message + '\nAs module: ' + ne.message); + cli.fatal('Cannot read the specified config file.\nAs JSON: ' + je.message + '\nAs module: ' + ne.message); } } - - if (fileOptions && typeof fileOptions === 'object') { - minifyOptions = fileOptions; - } } mainOptionKeys.forEach(function(key) { var paramKey = changeCase.paramCase(key);