-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): read config from package.json, rc file and args
- Loading branch information
Showing
8 changed files
with
218 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,54 @@ | ||
#!/usr/bin/env node | ||
import commander from 'commander'; | ||
import { cosmiconfig, cosmiconfigSync } from 'cosmiconfig'; | ||
|
||
import * as packgeJSON from '../../package.json'; | ||
import { Delimiter } from '../lib/generators/code-snippet-generators'; | ||
import { convertToSingleFile } from '../lib/converters/single-file.converter'; | ||
import { convertToMultipleFiles } from '../lib/converters/multiple-files.converter'; | ||
import { DEFAULT_OPTIONS } from '../lib/options/default-options'; | ||
import { getOptions, MultiFileConvertionOptions, SingleFileConvertionOptions } from '../lib/options/convertion-options'; | ||
|
||
import * as packgeJSON from '../../package.json'; | ||
|
||
export interface ConvertionOptions { | ||
delimiter: Delimiter; | ||
typeName: string; | ||
prefix: string; | ||
fileName: string; | ||
interfaceName: string; | ||
srcFiles: string[]; | ||
outputDirectory: string; | ||
modelOutputPath: string; | ||
modelFileName: string; | ||
} | ||
|
||
const DEFAULTS = { | ||
fileName: 'my-icons', | ||
delimiter: Delimiter.SNAKE, | ||
interfaceName: 'MyIcon', | ||
outputDirectory: './dist', | ||
prefix: 'myIcon', | ||
sourceFilesRegex: ['*.svg'], | ||
typeName: 'myIcons', | ||
optimizeForLazyLoading: false | ||
}; | ||
|
||
function collect(value, previous) { | ||
return previous.concat([value]); | ||
} | ||
|
||
const moduleName = 'svg-to-ts'; | ||
const explorerSync = cosmiconfigSync(moduleName); | ||
|
||
const searchedFor = explorerSync.search(); | ||
console.log('Searchedfor', searchedFor); | ||
const collect = (value, previous) => previous.concat([value]); | ||
|
||
commander | ||
.version(packgeJSON.version) | ||
.option('-t --typeName <string>', 'name of the generated enumeration type', DEFAULTS.typeName) | ||
.option('-f --fileName <string>', 'name of the generated file', DEFAULTS.fileName) | ||
.option('-t --typeName <string>', 'name of the generated enumeration type', DEFAULT_OPTIONS.typeName) | ||
.option('-f --fileName <string>', 'name of the generated file', DEFAULT_OPTIONS.fileName) | ||
.option( | ||
'-d --delimiter <Delimiter>', | ||
`delimiter which is used to generate the types and name properties (${Object.values(Delimiter).join(',')})`, | ||
DEFAULTS.delimiter | ||
DEFAULT_OPTIONS.delimiter | ||
) | ||
.option('-p --prefix <string>', 'prefix for the generated svg constants', DEFAULTS.prefix) | ||
.option('-i --interfaceName <string>', 'name for the generated interface', DEFAULTS.interfaceName) | ||
.option('-p --prefix <string>', 'prefix for the generated svg constants', DEFAULT_OPTIONS.prefix) | ||
.option('-i --interfaceName <string>', 'name for the generated interface', DEFAULT_OPTIONS.interfaceName) | ||
.option('-s --srcFiles <value>', 'name of the source directory', collect, []) | ||
.option('-o --outputDirectory <string>', 'name of the output directory', DEFAULTS.outputDirectory) | ||
.option('--optimizeForLazyLoading <boolean>', 'optimize the output for lazyloading', DEFAULTS.optimizeForLazyLoading) | ||
.option('--modelOutputPath <string>', 'Output path for the types file') | ||
.option('--modelFileName <string>', 'FileName of the model file') | ||
.option('-o --outputDirectory <string>', 'name of the output directory', DEFAULT_OPTIONS.outputDirectory) | ||
.option( | ||
'--optimizeForLazyLoading <boolean>', | ||
'optimize the output for lazyloading', | ||
DEFAULT_OPTIONS.optimizeForLazyLoading | ||
) | ||
.option( | ||
'--modelOutputPath <string>', | ||
'Output path for the types file (only necessary when optimizeForLazyLoading option is enabled)', | ||
DEFAULT_OPTIONS.modelOutputPath | ||
) | ||
.option( | ||
'--modelFileName <string>', | ||
'FileName of the model file (only necessary when optimizeForLazyLoading option is enabled)', | ||
DEFAULT_OPTIONS.modelFileName | ||
) | ||
.option( | ||
'--iconsFolderName <string>', | ||
'Name of the folder the icons will be generated to (only necessary when optimizeForLazyLoading option is enabled)', | ||
DEFAULT_OPTIONS.iconsFolderName | ||
) | ||
.parse(process.argv); | ||
|
||
const { | ||
delimiter, | ||
fileName, | ||
interfaceName, | ||
outputDirectory, | ||
prefix, | ||
typeName, | ||
modelFileName, | ||
modelOutputPath | ||
} = commander; | ||
|
||
// Because of commander adding default value to params | ||
// See: https://stackoverflow.com/questions/30238654/commander-js-collect-multiple-options-always-include-default | ||
let srcFiles = commander.srcFiles; | ||
if (srcFiles.length === 0) { | ||
srcFiles = DEFAULTS.sourceFilesRegex; | ||
} | ||
const optimizeForLazyLoading = commander.optimizeForLazyLoading; | ||
|
||
const convertionOptions = { | ||
delimiter, | ||
typeName, | ||
fileName, | ||
prefix, | ||
interfaceName, | ||
srcFiles, | ||
outputDirectory, | ||
modelOutputPath, | ||
modelFileName | ||
}; | ||
const convertionOptions = getOptions(); | ||
|
||
if (optimizeForLazyLoading) { | ||
convertToMultipleFiles(convertionOptions); | ||
if (convertionOptions.optimizeForLazyLoading) { | ||
convertToMultipleFiles(convertionOptions as MultiFileConvertionOptions); | ||
} else { | ||
convertToSingleFile(convertionOptions); | ||
convertToSingleFile(convertionOptions as SingleFileConvertionOptions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import commander from 'commander'; | ||
import { MultiFileConvertionOptions, SingleFileConvertionOptions } from './convertion-options'; | ||
import { DEFAULT_OPTIONS } from './default-options'; | ||
|
||
export const collectArgumentOptions = (): SingleFileConvertionOptions | MultiFileConvertionOptions => { | ||
const { | ||
delimiter, | ||
fileName, | ||
interfaceName, | ||
outputDirectory, | ||
prefix, | ||
typeName, | ||
modelFileName, | ||
modelOutputPath, | ||
iconsFolderName, | ||
optimizeForLazyLoading | ||
} = commander; | ||
|
||
// Because of commander adding default value to params | ||
// See: https://stackoverflow.com/questions/30238654/commander-js-collect-multiple-options-always-include-default | ||
let srcFiles = commander.srcFiles; | ||
if (srcFiles.length === 0) { | ||
srcFiles = DEFAULT_OPTIONS.srcFiles; | ||
} | ||
|
||
return { | ||
delimiter, | ||
fileName, | ||
interfaceName, | ||
srcFiles, | ||
outputDirectory, | ||
prefix, | ||
typeName, | ||
modelFileName, | ||
modelOutputPath, | ||
iconsFolderName, | ||
optimizeForLazyLoading | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { cosmiconfigSync } from 'cosmiconfig'; | ||
|
||
import * as packgeJSON from '../../../package.json'; | ||
import { info } from '../helpers/log-helper'; | ||
|
||
import { MultiFileConvertionOptions, SingleFileConvertionOptions } from './convertion-options'; | ||
import { DEFAULT_OPTIONS } from './default-options'; | ||
|
||
export const collectConfigurationOptions = (): SingleFileConvertionOptions | MultiFileConvertionOptions | null => { | ||
const explorerSync = cosmiconfigSync(packgeJSON.name); | ||
const cosmiConfigResult = explorerSync.search(); | ||
cosmiConfigResult ? info(`Configuration found under: ${cosmiConfigResult.filepath}`) : info('No config found'); | ||
return cosmiConfigResult ? mergeWithDefaults(cosmiConfigResult.config) : null; | ||
}; | ||
|
||
const mergeWithDefaults = ( | ||
options: MultiFileConvertionOptions | SingleFileConvertionOptions | ||
): MultiFileConvertionOptions | SingleFileConvertionOptions => { | ||
const configOptions = { ...options }; | ||
if (!configOptions.typeName) { | ||
configOptions.typeName = DEFAULT_OPTIONS.typeName; | ||
info(`No typeName provided, "${DEFAULT_OPTIONS.typeName}" will be used`); | ||
} | ||
|
||
if (!configOptions.interfaceName) { | ||
configOptions.interfaceName = DEFAULT_OPTIONS.interfaceName; | ||
info(`No interfaceName provided, "${DEFAULT_OPTIONS.interfaceName}" will be used`); | ||
} | ||
|
||
if (!configOptions.prefix) { | ||
configOptions.prefix = DEFAULT_OPTIONS.prefix; | ||
info(`No prefix provided, "${DEFAULT_OPTIONS.prefix}" will be used`); | ||
} | ||
|
||
if (!configOptions.delimiter) { | ||
configOptions.delimiter = DEFAULT_OPTIONS.delimiter; | ||
info(`No delimiter provided, "${DEFAULT_OPTIONS.delimiter}" will be used`); | ||
} | ||
|
||
if (!configOptions.outputDirectory) { | ||
configOptions.outputDirectory = DEFAULT_OPTIONS.outputDirectory; | ||
info(`No outputDirectory provided, "${DEFAULT_OPTIONS.outputDirectory}" will be used`); | ||
} | ||
|
||
if (!configOptions.srcFiles) { | ||
configOptions.srcFiles = DEFAULT_OPTIONS.srcFiles; | ||
info(`No srcFiles provided, "${DEFAULT_OPTIONS.srcFiles}" will be used`); | ||
} | ||
|
||
if (configOptions.optimizeForLazyLoading) { | ||
if (!(configOptions as MultiFileConvertionOptions).modelOutputPath) { | ||
(configOptions as MultiFileConvertionOptions).modelOutputPath = DEFAULT_OPTIONS.modelOutputPath; | ||
info(`No modelOutputPath provided, "${DEFAULT_OPTIONS.modelOutputPath}" will be used`); | ||
} | ||
|
||
if (!(configOptions as MultiFileConvertionOptions).modelFileName) { | ||
(configOptions as MultiFileConvertionOptions).modelFileName = DEFAULT_OPTIONS.modelFileName; | ||
info(`No modelFileName provided, "${DEFAULT_OPTIONS.modelFileName}" will be used`); | ||
} | ||
|
||
if (!(configOptions as MultiFileConvertionOptions).iconsFolderName) { | ||
(configOptions as MultiFileConvertionOptions).iconsFolderName = DEFAULT_OPTIONS.iconsFolderName; | ||
info(`No iconsFolderName provided, "${DEFAULT_OPTIONS.iconsFolderName}" will be used`); | ||
} | ||
|
||
return configOptions as MultiFileConvertionOptions; | ||
} else { | ||
if (!(configOptions as SingleFileConvertionOptions).fileName) { | ||
(configOptions as SingleFileConvertionOptions).fileName = DEFAULT_OPTIONS.modelFileName; | ||
info(`No fileName provided, "${DEFAULT_OPTIONS.modelFileName}" will be used`); | ||
} | ||
return configOptions as SingleFileConvertionOptions; | ||
} | ||
}; |
Oops, something went wrong.