@@ -5,42 +5,13 @@ import fs from 'fs';
55import yargs from 'yargs' ;
66import yaml from 'js-yaml' ;
77import path from 'path' ;
8+ import isPlainObject from 'lodash.isplainobject' ;
89
9- let { argv} = yargs
10- . usage ( 'Usage: $0 [files] [options]' )
11- . option ( '5' , {
12- alias : 'es5' ,
13- describe : 'Use babel presets es2015 and react' ,
14- boolean : true ,
15- default : false
16- } )
17- . option ( '0' , {
18- alias : 'stdout' ,
19- describe : 'Print output to stdout' ,
20- boolean : true ,
21- default : 'false'
22- } )
23- // svgo options
24- . option ( 'svgo' , {
25- describe : 'Path to YAML or JS or JSON config file for SVGO' ,
26- nargs : 1
27- } )
28- . array ( 'svgo.plugins' )
29- . option ( 'svgo.floatPrecision' , { number : true , default : 3 } )
30- . boolean ( 'svgo.multipass' )
31- . boolean ( 'svgo.full' )
32- . boolean ( 'svgo.js2svg.pretty' )
33- . option ( 'svgo.js2svg.useShortTags' , { default : true , boolean : true } )
34- . demand ( 1 )
35- . version ( require ( '../package.json' ) . version )
36- . help ( 'h' )
37- . alias ( 'h' , 'help' ) ;
38-
39- function makeFilename ( filename ) {
10+ export function makeFilename ( filename ) {
4011 return filename + '.react.js' ;
4112}
4213
43- function handlePath ( configFile ) {
14+ export function handlePath ( configFile ) {
4415 switch ( path . extname ( configFile ) ) {
4516 case '.yaml' :
4617 return yaml . safeLoad ( fs . readFileSync ( configFile ) ) ;
@@ -52,51 +23,48 @@ function handlePath(configFile) {
5223 }
5324}
5425
55- let svgoOpts ;
26+ export function getArgv ( ) {
27+ return yargs
28+ . usage ( 'Usage: $0 [files] [options]' )
29+ . option ( '5' , {
30+ alias : 'es5' ,
31+ describe : 'Use babel presets es2015 and react' ,
32+ boolean : true ,
33+ default : false
34+ } )
35+ . option ( '0' , {
36+ alias : 'stdout' ,
37+ describe : 'Print output to stdout' ,
38+ boolean : true ,
39+ default : 'false'
40+ } )
41+ // svgo options
42+ . option ( 'svgo' , {
43+ describe : 'Path to YAML or JS or JSON config file for SVGO'
44+ } )
45+ . demand ( 1 )
46+ . version ( require ( '../package.json' ) . version )
47+ . help ( 'h' )
48+ . alias ( 'h' , 'help' )
49+ . argv ;
50+ }
51+
52+ export function getSVGOOpts ( argv ) {
53+ let svgoOpts ;
5654
57- switch ( typeof argv . svgo ) {
58- case 'string' :
55+ if ( typeof argv . svgo === 'string' ) {
5956 svgoOpts = handlePath ( argv . svgo ) ;
60- break ;
61- case 'object' :
57+ } else if ( isPlainObject ( argv . svgo ) ) {
6258 svgoOpts = argv . svgo ;
63- if ( Array . isArray ( svgoOpts . plugins ) ) break ;
64-
65- // when there is only one element
66- if ( typeof svgoOpts . plugins === 'string' ) {
59+ if ( isPlainObject ( svgoOpts . plugins ) || typeof svgoOpts . plugins === 'string' ) {
6760 svgoOpts . plugins = [ svgoOpts . plugins ] ;
68- break ;
6961 }
70-
71- // when there are many and is an object
72- if ( svgoOpts . plugins ) {
73- svgoOpts . plugins = Object
74- . keys ( svgoOpts . plugins )
75- . map ( key => {
76- if ( svgoOpts . plugins [ key ] === true ) return key ;
77- return { [ key ] : svgoOpts . plugins [ key ] } ;
78- } ) ;
79- }
80- break ;
62+ }
63+ return svgoOpts ;
8164}
8265
83- argv . _ . map ( file => {
84- let source = fs . readFileSync ( file ) ;
85-
86- let query ;
87- try {
88- // serializable check
89- query = '?' + JSON . stringify ( {
90- es5 : argv . es5 ,
91- svgo : svgoOpts
92- } ) ;
93- } catch ( e ) {
94- /* eslint-disable no-console */
95- console . error ( 'The options passed are not serializable.' ) ;
96- /* eslint-enable */
97- process . exit ( 1 ) ;
98- }
99- let loaderContext = {
66+ export function getLoaderContext ( { argv, query, file} ) {
67+ return {
10068 query,
10169 cacheable ( ) { } ,
10270 addDependency ( ) { } ,
@@ -110,5 +78,33 @@ argv._.map(file => {
11078 } ;
11179 }
11280 } ;
113- loader . apply ( loaderContext , [ source ] ) ;
114- } ) ;
81+ }
82+
83+ export function run ( ) {
84+ const argv = getArgv ( ) ;
85+ const svgoOpts = getSVGOOpts ( argv ) ;
86+
87+ argv . _ . map ( file => {
88+ let source = fs . readFileSync ( file ) ;
89+
90+ let query ;
91+ try {
92+ // serializable check
93+ query = '?' + JSON . stringify ( {
94+ es5 : argv . es5 ,
95+ svgo : svgoOpts
96+ } ) ;
97+ } catch ( e ) {
98+ /* eslint-disable no-console */
99+ console . error ( 'The options passed are not serializable.' ) ;
100+ /* eslint-enable */
101+ process . exit ( 1 ) ;
102+ }
103+ loader . apply ( getLoaderContext ( { argv, query, file} ) , [ source ] ) ;
104+ } ) ;
105+ }
106+
107+ // for testability
108+ if ( require . main === module ) {
109+ run ( ) ;
110+ }
0 commit comments