|
1 | 1 | #!/usr/bin/env node
|
2 | 2 | import commander from 'commander';
|
3 |
| -import { cosmiconfig, cosmiconfigSync } from 'cosmiconfig'; |
4 | 3 |
|
| 4 | +import * as packgeJSON from '../../package.json'; |
5 | 5 | import { Delimiter } from '../lib/generators/code-snippet-generators';
|
6 | 6 | import { convertToSingleFile } from '../lib/converters/single-file.converter';
|
7 | 7 | import { convertToMultipleFiles } from '../lib/converters/multiple-files.converter';
|
| 8 | +import { DEFAULT_OPTIONS } from '../lib/options/default-options'; |
| 9 | +import { getOptions, MultiFileConvertionOptions, SingleFileConvertionOptions } from '../lib/options/convertion-options'; |
8 | 10 |
|
9 |
| -import * as packgeJSON from '../../package.json'; |
10 |
| - |
11 |
| -export interface ConvertionOptions { |
12 |
| - delimiter: Delimiter; |
13 |
| - typeName: string; |
14 |
| - prefix: string; |
15 |
| - fileName: string; |
16 |
| - interfaceName: string; |
17 |
| - srcFiles: string[]; |
18 |
| - outputDirectory: string; |
19 |
| - modelOutputPath: string; |
20 |
| - modelFileName: string; |
21 |
| -} |
22 |
| - |
23 |
| -const DEFAULTS = { |
24 |
| - fileName: 'my-icons', |
25 |
| - delimiter: Delimiter.SNAKE, |
26 |
| - interfaceName: 'MyIcon', |
27 |
| - outputDirectory: './dist', |
28 |
| - prefix: 'myIcon', |
29 |
| - sourceFilesRegex: ['*.svg'], |
30 |
| - typeName: 'myIcons', |
31 |
| - optimizeForLazyLoading: false |
32 |
| -}; |
33 |
| - |
34 |
| -function collect(value, previous) { |
35 |
| - return previous.concat([value]); |
36 |
| -} |
37 |
| - |
38 |
| -const moduleName = 'svg-to-ts'; |
39 |
| -const explorerSync = cosmiconfigSync(moduleName); |
40 |
| - |
41 |
| -const searchedFor = explorerSync.search(); |
42 |
| -console.log('Searchedfor', searchedFor); |
| 11 | +const collect = (value, previous) => previous.concat([value]); |
43 | 12 |
|
44 | 13 | commander
|
45 | 14 | .version(packgeJSON.version)
|
46 |
| - .option('-t --typeName <string>', 'name of the generated enumeration type', DEFAULTS.typeName) |
47 |
| - .option('-f --fileName <string>', 'name of the generated file', DEFAULTS.fileName) |
| 15 | + .option('-t --typeName <string>', 'name of the generated enumeration type', DEFAULT_OPTIONS.typeName) |
| 16 | + .option('-f --fileName <string>', 'name of the generated file', DEFAULT_OPTIONS.fileName) |
48 | 17 | .option(
|
49 | 18 | '-d --delimiter <Delimiter>',
|
50 | 19 | `delimiter which is used to generate the types and name properties (${Object.values(Delimiter).join(',')})`,
|
51 |
| - DEFAULTS.delimiter |
| 20 | + DEFAULT_OPTIONS.delimiter |
52 | 21 | )
|
53 |
| - .option('-p --prefix <string>', 'prefix for the generated svg constants', DEFAULTS.prefix) |
54 |
| - .option('-i --interfaceName <string>', 'name for the generated interface', DEFAULTS.interfaceName) |
| 22 | + .option('-p --prefix <string>', 'prefix for the generated svg constants', DEFAULT_OPTIONS.prefix) |
| 23 | + .option('-i --interfaceName <string>', 'name for the generated interface', DEFAULT_OPTIONS.interfaceName) |
55 | 24 | .option('-s --srcFiles <value>', 'name of the source directory', collect, [])
|
56 |
| - .option('-o --outputDirectory <string>', 'name of the output directory', DEFAULTS.outputDirectory) |
57 |
| - .option('--optimizeForLazyLoading <boolean>', 'optimize the output for lazyloading', DEFAULTS.optimizeForLazyLoading) |
58 |
| - .option('--modelOutputPath <string>', 'Output path for the types file') |
59 |
| - .option('--modelFileName <string>', 'FileName of the model file') |
| 25 | + .option('-o --outputDirectory <string>', 'name of the output directory', DEFAULT_OPTIONS.outputDirectory) |
| 26 | + .option( |
| 27 | + '--optimizeForLazyLoading <boolean>', |
| 28 | + 'optimize the output for lazyloading', |
| 29 | + DEFAULT_OPTIONS.optimizeForLazyLoading |
| 30 | + ) |
| 31 | + .option( |
| 32 | + '--modelOutputPath <string>', |
| 33 | + 'Output path for the types file (only necessary when optimizeForLazyLoading option is enabled)', |
| 34 | + DEFAULT_OPTIONS.modelOutputPath |
| 35 | + ) |
| 36 | + .option( |
| 37 | + '--modelFileName <string>', |
| 38 | + 'FileName of the model file (only necessary when optimizeForLazyLoading option is enabled)', |
| 39 | + DEFAULT_OPTIONS.modelFileName |
| 40 | + ) |
| 41 | + .option( |
| 42 | + '--iconsFolderName <string>', |
| 43 | + 'Name of the folder the icons will be generated to (only necessary when optimizeForLazyLoading option is enabled)', |
| 44 | + DEFAULT_OPTIONS.iconsFolderName |
| 45 | + ) |
60 | 46 | .parse(process.argv);
|
61 | 47 |
|
62 |
| -const { |
63 |
| - delimiter, |
64 |
| - fileName, |
65 |
| - interfaceName, |
66 |
| - outputDirectory, |
67 |
| - prefix, |
68 |
| - typeName, |
69 |
| - modelFileName, |
70 |
| - modelOutputPath |
71 |
| -} = commander; |
72 |
| - |
73 |
| -// Because of commander adding default value to params |
74 |
| -// See: https://stackoverflow.com/questions/30238654/commander-js-collect-multiple-options-always-include-default |
75 |
| -let srcFiles = commander.srcFiles; |
76 |
| -if (srcFiles.length === 0) { |
77 |
| - srcFiles = DEFAULTS.sourceFilesRegex; |
78 |
| -} |
79 |
| -const optimizeForLazyLoading = commander.optimizeForLazyLoading; |
80 |
| - |
81 |
| -const convertionOptions = { |
82 |
| - delimiter, |
83 |
| - typeName, |
84 |
| - fileName, |
85 |
| - prefix, |
86 |
| - interfaceName, |
87 |
| - srcFiles, |
88 |
| - outputDirectory, |
89 |
| - modelOutputPath, |
90 |
| - modelFileName |
91 |
| -}; |
| 48 | +const convertionOptions = getOptions(); |
92 | 49 |
|
93 |
| -if (optimizeForLazyLoading) { |
94 |
| - convertToMultipleFiles(convertionOptions); |
| 50 | +if (convertionOptions.optimizeForLazyLoading) { |
| 51 | + convertToMultipleFiles(convertionOptions as MultiFileConvertionOptions); |
95 | 52 | } else {
|
96 |
| - convertToSingleFile(convertionOptions); |
| 53 | + convertToSingleFile(convertionOptions as SingleFileConvertionOptions); |
97 | 54 | }
|
0 commit comments