|
3 | 3 | import loader from './loader'; |
4 | 4 | import fs from 'fs'; |
5 | 5 | import yargs from 'yargs'; |
| 6 | +import yaml from 'js-yaml'; |
| 7 | +import path from 'path'; |
6 | 8 |
|
7 | | -let {argv} = yargs.boolean('es5').boolean('0'); |
| 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'); |
8 | 38 |
|
9 | 39 | function makeFilename(filename) { |
10 | 40 | return filename + '.react.js'; |
11 | 41 | } |
12 | 42 |
|
| 43 | +function handlePath(configFile) { |
| 44 | + switch (path.extname(configFile)) { |
| 45 | + case '.yaml': |
| 46 | + return yaml.safeLoad(fs.readFileSync(configFile)); |
| 47 | + case '.json': |
| 48 | + case '.js': |
| 49 | + return require(path.join(process.cwd(), configFile)); |
| 50 | + default: |
| 51 | + throw new Error('Unsupported config file format.'); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +let svgoOpts; |
| 56 | + |
| 57 | +switch (typeof argv.svgo) { |
| 58 | + case 'string': |
| 59 | + svgoOpts = handlePath(argv.svgo); |
| 60 | + break; |
| 61 | + case 'object': |
| 62 | + svgoOpts = argv.svgo; |
| 63 | + if (Array.isArray(svgoOpts.plugins)) break; |
| 64 | + |
| 65 | + // when there is only one element |
| 66 | + if (typeof svgoOpts.plugins === 'string') { |
| 67 | + svgoOpts.plugins = [svgoOpts.plugins]; |
| 68 | + break; |
| 69 | + } |
| 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; |
| 81 | +} |
| 82 | + |
13 | 83 | argv._.map(file => { |
14 | 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 | + console.error('The options passed are not serializable.'); |
| 95 | + process.exit(1); |
| 96 | + } |
15 | 97 | let loaderContext = { |
16 | | - query: `?es5=${argv.es5}`, |
| 98 | + query, |
17 | 99 | cacheable() {}, |
18 | 100 | addDependency() {}, |
19 | 101 | async() { |
|
0 commit comments