Skip to content

Commit

Permalink
feat(ALL): change freepack gen/valid options
Browse files Browse the repository at this point in the history
  • Loading branch information
Clunt committed May 28, 2018
1 parent 00f75af commit 2451d48
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 50 deletions.
2 changes: 2 additions & 0 deletions lib/VAR.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
20 changes: 3 additions & 17 deletions lib/freepack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down
35 changes: 35 additions & 0 deletions lib/generateOption.js
Original file line number Diff line number Diff line change
@@ -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;
};
89 changes: 56 additions & 33 deletions lib/validateOption.js
Original file line number Diff line number Diff line change
@@ -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'));
Expand Down

0 comments on commit 2451d48

Please sign in to comment.