Skip to content

Commit afd1db5

Browse files
committed
Merge pull request #121 from zxqfox/feature/presets
presets functionality
2 parents f36add8 + 834e40e commit afd1db5

4 files changed

Lines changed: 56 additions & 23 deletions

File tree

lib/HtmlDiff.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ var diff = require('diff'),
55
* @class HtmlDiff
66
* @constructor
77
* @augments Diff
8-
* @param {Object} [options]
8+
* @param {Object|String} [options] options or preset name
9+
* @param {String} [options.preset]
910
* @param {String[]} [options.ignoreAttributes]
1011
* @param {String[]} [options.compareAttributesAsJSON]
1112
* @param {Boolean} [options.ignoreWhitespaces=true]

lib/cli.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var path = require('path'),
22
vow = require('vow'),
33
vfs = require('vow-fs'),
4+
colors = require('colors'),
45
HtmlDiffer = require('./index').HtmlDiffer,
56
diffLogger = require('./logger'),
67
defaults = require('./utils/defaults');
@@ -28,18 +29,37 @@ module.exports = require('coa').Cmd()
2829
.end()
2930
.opt()
3031
.name('bem')
31-
.title('Uses predefined options for BEM')
32+
.title('Uses predefined options for BEM (deprecated)')
3233
.long('bem')
3334
.flag()
35+
.act(function (opts) {
36+
console.error('Option ' + '--bem'.bold.red + ' is deprecated, use ' +
37+
'--preset=bem'.bold.green + ' option instead.');
38+
// support legacy
39+
opts.preset = 'bem';
40+
delete opts.bem;
41+
})
42+
.end()
43+
.opt()
44+
.name('preset')
45+
.title('Name of preset')
46+
.short('p').long('preset')
47+
.val(function (val) {
48+
if (!defaults.presets.hasOwnProperty(val)) {
49+
console.log(val.bold.red + ' is an invalid preset name. Available presets are: ' +
50+
Object.keys(defaults.presets).map(colors.green).map(colors.bold).join(', ') + '.');
51+
process.exit(1);
52+
}
53+
return val;
54+
})
3455
.end()
3556
.opt()
3657
.name('charsAroundDiff')
3758
.title('The number of characters around the diff (default: 40)')
3859
.long('chars-around-diff')
3960
.def(40)
4061
.val(function (val) {
41-
/*jshint es3:false */
42-
return parseInt(val);
62+
return parseInt(val, 10);
4363
})
4464
.end()
4565
.arg()
@@ -58,10 +78,10 @@ module.exports = require('coa').Cmd()
5878
vfs.read(path.resolve(args.path2), 'utf-8'),
5979
opts.config ? vfs.read(path.resolve(opts.config)) : undefined
6080
]).spread(function (html1, html2, config) {
61-
if (!opts.bem) {
62-
config = config ? JSON.parse(config) : {};
63-
} else {
64-
config = 'bem';
81+
config = config ? JSON.parse(config) : {};
82+
83+
if (opts.preset) {
84+
config.preset = opts.preset;
6585
}
6686

6787
var options = defaults(config),

lib/utils/defaults.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
var _ = require('lodash');
22

3+
var presets = {
4+
bem: require('../../presets/bem.json')
5+
};
6+
37
/**
48
* Sets options
5-
* @param {Object} [options]
9+
* @param {Object|String} [options] options or preset name
10+
* @param {String} [options.preset]
611
* @param {String[]} [options.ignoreAttributes]
712
* @param {String[]} [options.compareAttributesAsJSON]
813
* @param {Boolean} [options.ignoreWhitespaces=true]
914
* @param {Boolean} [options.ignoreComments=true]
10-
* @param {Boolean} [options.ignoreClosingTags=false]
15+
* @param {Boolean} [options.ignoreEndTags=false]
1116
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
1217
* returns {Object}
1318
*/
1419
module.exports = function (options) {
1520
if (typeof options === 'string') {
16-
if (options === 'bem') {
17-
options = {
18-
// ignore generated attributes
19-
ignoreAttributes: ['id', 'for', 'aria-labelledby', 'aria-describedby'],
20-
compareAttributesAsJSON: [
21-
'data-bem',
22-
{ name: 'onclick', isFunction: true },
23-
{ name: 'ondblclick', isFunction: true }
24-
]
25-
};
26-
} else {
27-
console.error(options.bold.red + ' is an invalid preset name. Use ' + 'bem'.bold.green + ' instead.');
28-
process.exit(1);
21+
options = { preset: options };
22+
}
23+
24+
if (options && options.preset) {
25+
var preset = String(options.preset);
26+
if (!presets.hasOwnProperty(preset)) {
27+
throw Error(preset + ' is an invalid preset name.');
2928
}
29+
30+
delete options.preset;
31+
options = _.defaults(options, presets[preset]);
3032
}
3133

3234
return _.defaults(options || {}, {
@@ -40,3 +42,5 @@ var _ = require('lodash');
4042
ignoreDuplicateAttributes: false
4143
});
4244
};
45+
46+
module.exports.presets = presets;

presets/bem.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"ignoreAttributes": ["id", "for", "aria-labelledby", "aria-describedby"],
3+
"compareAttributesAsJSON": [
4+
"data-bem",
5+
{ "name": "onclick", "isFunction": true },
6+
{ "name": "ondblclick", "isFunction": true }
7+
]
8+
}

0 commit comments

Comments
 (0)