Skip to content

Commit 74f4685

Browse files
author
deepsweet
committed
coa: repair --disable and --enable opts
1 parent c0f8cf0 commit 74f4685

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

lib/svgo/coa.js

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ module.exports = require('coa').Cmd()
123123

124124
}
125125

126+
// --disable
127+
if (opts.disable) {
128+
config = changePluginsState(opts.disable, false, config);
129+
}
130+
131+
// --enable
132+
if (opts.enable) {
133+
config = changePluginsState(opts.enable, true, config);
134+
}
135+
126136
// --pretty
127137
if (opts.pretty) {
128138

@@ -139,7 +149,7 @@ module.exports = require('coa').Cmd()
139149
return;
140150
}
141151

142-
// --inpput
152+
// --input
143153
if (input) {
144154

145155
// STDIN
@@ -249,6 +259,81 @@ function printProfitInfo(inBytes, outBytes) {
249259

250260
}
251261

262+
/**
263+
* Change plugins state by names array.
264+
*
265+
* @param {Array} names plugins names
266+
* @param {Boolean} state active state
267+
* @param {Object} config original config
268+
* @return {Object} changed config
269+
*/
270+
function changePluginsState(names, state, config) {
271+
272+
// extend config
273+
if (config && config.plugins) {
274+
275+
names.forEach(function(name) {
276+
277+
var matched,
278+
key;
279+
280+
config.plugins.forEach(function(plugin) {
281+
282+
// get plugin name
283+
if (typeof plugin === 'object') {
284+
key = Object.keys(plugin)[0];
285+
} else {
286+
key = plugin;
287+
}
288+
289+
// if there are such plugin name
290+
if (key === name) {
291+
// do not replace plugin's params with true
292+
if (typeof plugin[key] !== 'object' || !state) {
293+
plugin[key] = state;
294+
}
295+
296+
// mark it as matched
297+
matched = true;
298+
}
299+
300+
});
301+
302+
// if not matched and current config is not full
303+
if (!matched && !config.full) {
304+
305+
var obj = {};
306+
307+
obj[name] = state;
308+
309+
// push new plugin Object
310+
config.plugins.push(obj);
311+
312+
matched = true;
313+
314+
}
315+
316+
});
317+
318+
// just push
319+
} else {
320+
321+
config = { plugins: [] };
322+
323+
names.forEach(function(name) {
324+
var obj = {};
325+
326+
obj[name] = state;
327+
328+
config.plugins.push(obj);
329+
});
330+
331+
}
332+
333+
return config;
334+
335+
}
336+
252337
function optimizeFolder(path, config) {
253338

254339
var svgo = new SVGO(config);

0 commit comments

Comments
 (0)