From 2451d48f5f1fe83ce3b26f016a981b7c475216ee Mon Sep 17 00:00:00 2001 From: Clunt Date: Mon, 28 May 2018 16:58:00 +0800 Subject: [PATCH] feat(ALL): change freepack gen/valid options --- lib/VAR.js | 2 + lib/freepack.js | 20 ++-------- lib/generateOption.js | 35 +++++++++++++++++ lib/validateOption.js | 89 +++++++++++++++++++++++++++---------------- 4 files changed, 96 insertions(+), 50 deletions(-) create mode 100644 lib/generateOption.js diff --git a/lib/VAR.js b/lib/VAR.js index be493bf..c6d56b8 100644 --- a/lib/VAR.js +++ b/lib/VAR.js @@ -14,3 +14,5 @@ const DIFF_TYPE = exports.DIFF_TYPE = {}; DIFF_TYPE.CREATE = 1; DIFF_TYPE.UPDATE = 0; DIFF_TYPE.DELETE = -1; + +exports.RULE_SYMBOL_TYPES = ['negation', 'relation', 'separation', 'file', 'regexp', 'match', 'alias', 'module'].sort(); diff --git a/lib/freepack.js b/lib/freepack.js index 78d2f91..374b6b7 100644 --- a/lib/freepack.js +++ b/lib/freepack.js @@ -4,27 +4,13 @@ const version = require('../package.json').version; const Packer = require('./Packer'); const VARIABLE = require('./VAR'); const validateOption = require('./validateOption'); +const generateOption = require('./generateOption'); -function getPacker(option) { - option = Object.assign({}, typeof option === 'object' ? option : {}); - - option.release = option.release || []; - option.alias = option.alias || {}; - option.module = option.module || {}; - option.symbol = Object.assign({ - negation: '!', - relation: ':', - separation: ',', - file: '$', - regexp: '~', - match: '-', - alias: '$', - module: '@', - }, option.symbol); +function getPacker(option) { validateOption(option); - return new Packer(option) + return new Packer(generateOption(option)); } diff --git a/lib/generateOption.js b/lib/generateOption.js new file mode 100644 index 0000000..b98b53a --- /dev/null +++ b/lib/generateOption.js @@ -0,0 +1,35 @@ +"use strict"; + + +exports = module.exports = option => { + option = Object.assign({ + context: undefined, + src: undefined, + diff: undefined, + output: undefined, + match: undefined, + dot: false, + backup: true, + strict: false, + }, option); + + option.ignore = option.ignore || []; + option.alias = option.alias || {}; + option.module = option.module || {}; + option.symbol = option.symbol || { + negation: '!', + relation: ':', + separation: ',', + file: '$', + regexp: '~', + match: '-', + alias: '$', + module: '@', + }; + + option.release = Array.isArray(option.release) + ? option.release.map(rule => typeof rule === 'function' ? rule(option) : rule) + : (option.release || []); + + return option; +}; diff --git a/lib/validateOption.js b/lib/validateOption.js index ff1f67f..87fa7b0 100644 --- a/lib/validateOption.js +++ b/lib/validateOption.js @@ -1,58 +1,81 @@ "use strict"; +const VARIABLE = require('./VAR'); + exports = module.exports = option => { const error = []; - if (!option.root) { - error.push(`required option.root`); + if (!option.context) { + error.push(`required option.context`); } - if (!option.diff && !option.git) { - error.push(`required option.diff or option.git`); + if (!option.diff) { + error.push(`required option.diff`); } - if (!option.diff && option.git && !Array.isArray(option.git)) { - error.push(`option.git like [ git_path, src_path, diff_tag ]`); + if (option.src && typeof option.src !== 'string') { + error.push(`option.src must be string`); } - if (typeof option.alias === 'object') { - Object.keys(option.alias).forEach(key => { - if (!(typeof option.alias[key] === 'string' && option.alias[key])) { - error.push(`option.alias ${key} must be exist string value`); - } - }); - } else { - error.push(`option.alias must be object`); + if (option.output && typeof option.output !== 'string') { + error.push(`option.output must be string`); } - if (typeof option.module !== 'object') { - error.push(`option.module must be object`); + if (option.match && Object.keys(VARIABLE.MATCH_MODE).map(mode => VARIABLE.MATCH_MODE[mode]).indexOf(option.match) < 0) { + error.push(`option.match invalid`); } - const symbolMap = []; - const symbolError = {}; - Object.keys(option.symbol).forEach(key => { - const symbol = option.symbol[key]; - if (typeof symbol !== 'string' || symbol.length !== 1) { - return error.push(`${key} symbol must be a character`); + if (option.alias) { + if (typeof option.alias === 'object' && !Array.isArray(option.alias)) { + Object.keys(option.alias).forEach(key => { + if (!(typeof option.alias[key] === 'string' && option.alias[key])) { + error.push(`option.alias ${key} must be exist string value`); + } + }); + } else { + error.push(`option.alias must be object`); } + } - if (key === 'file') { - return; + if (option.module) { + if (!(typeof option.module === 'object' && !Array.isArray(option.module))) { + error.push(`option.alias must be object`); } + } + - if (symbolMap[symbol]) { - symbolError[symbol] = symbolError[symbol] || [symbolMap[symbol]]; - symbolError[symbol].push(key) + if (option.symbol) { + if (typeof option.symbol !== 'object') { + error.push(`option.symbol must be object`); + } else if (JSON.stringify(Object.keys(option.symbol).sort()) !== JSON.stringify(VARIABLE.RULE_SYMBOL_TYPES)) { + error.push(`option.symbol invalid`); } else { - symbolMap[symbol] = key; - } - }); + const symbolMap = {}; + const symbolError = {}; + Object.keys(option.symbol).forEach(key => { + const symbol = option.symbol[key]; + if (typeof symbol !== 'string' || symbol.length !== 1) { + return error.push(`${key} symbol must be a character`); + } + + if (key === 'file') { + return; + } + + if (symbolMap[symbol]) { + symbolError[symbol] = symbolError[symbol] || [symbolMap[symbol]]; + symbolError[symbol].push(key) + } else { + symbolMap[symbol] = key; + } + }); - Object.keys(symbolError).forEach(symbol => { - error.push(`symbol ${symbol} repeat in ${symbolError[symbol].join(', ')}`); - }); + Object.keys(symbolError).forEach(symbol => { + error.push(`symbol ${symbol} repeat in ${symbolError[symbol].join(', ')}`); + }); + } + } if (error.length > 0) { throw new Error(`Invalid arguments:\n` + error.join('\n'));