11import { ExportType , FileNameCase , IConfig , StyleFormats } from '@/types' ;
2- import defaultConfig from './default.json' ;
2+ import {
3+ APP_ROOT ,
4+ DEFAULT_CONFIG ,
5+ EXPORT_TYPES ,
6+ STYLE_FORMATS ,
7+ } from '@/constants' ;
8+ import { Logger } from '@/core/Logger' ;
9+ import path from 'path' ;
310
411export type ConfigPrefixes = {
512 style : 'module' | 'styles' ;
@@ -13,8 +20,6 @@ export type ConfigExt = {
1320} ;
1421
1522export class Config implements IConfig {
16- config : IConfig ;
17-
1823 fileNameCase : FileNameCase ;
1924
2025 styles : StyleFormats ;
@@ -38,23 +43,63 @@ export class Config implements IConfig {
3843 exportType : ExportType ;
3944
4045 constructor ( config : IConfig ) {
41- this . config = Object . assign ( defaultConfig , config ) ;
46+ const cfg = Object . assign ( DEFAULT_CONFIG , config ) ;
4247
43- this . setInitialVariables ( ) ;
48+ this . setVariables ( cfg ) ;
4449 this . setFilesExtension ( ) ;
4550 this . setPrefixes ( ) ;
4651 }
4752
48- private setInitialVariables ( ) {
49- this . fileNameCase = this . config . fileNameCase ;
50- this . styles = this . config . styles ;
51- this . path = this . config . path ;
52- this . jsxExt = this . config . jsxExt ;
53- this . typescript = this . config . typescript ;
54- this . wrapFolder = this . config . wrapFolder ;
55- this . cssModules = this . config . cssModules ;
56- this . exportType = this . config . exportType ;
57- this . arrowFunction = this . config . arrowFunction ;
53+ private setVariables ( config ) {
54+ // Boolean
55+
56+ this . jsxExt = config . jsxExt ?? this . jsxExt ;
57+ this . typescript = config . typescript ?? this . typescript ;
58+ this . wrapFolder = config . wrapFolder ?? this . wrapFolder ;
59+ this . cssModules = config . cssModules ?? this . cssModules ;
60+ this . arrowFunction = config . arrowFunction ?? this . arrowFunction ;
61+
62+ // Enum
63+
64+ this . fileNameCase = config . fileNameCase ?? this . fileNameCase ;
65+
66+ if ( config . styles ) {
67+ const styles = config . styles . toLowerCase ( ) ;
68+ const formats = STYLE_FORMATS . map ( v => v . toLowerCase ( ) ) ;
69+ const isSupported = formats . includes ( styles ) ;
70+
71+ if ( isSupported ) {
72+ this . styles = config . styles ;
73+ } else {
74+ Logger . warn (
75+ chalk =>
76+ `Unsupported "styles" value - ${ styles } ${ chalk . white (
77+ `(using default value - ${ DEFAULT_CONFIG . styles } )`
78+ ) } `
79+ ) ;
80+ this . styles = DEFAULT_CONFIG . styles ;
81+ }
82+ }
83+
84+ if ( config . exportType ) {
85+ if ( EXPORT_TYPES . includes ( config . exportType ) ) {
86+ this . exportType = config . exportType ;
87+ } else {
88+ Logger . warn (
89+ chalk =>
90+ `Unsupported "exportType" value - ${
91+ config . exportType
92+ } ${ chalk . white (
93+ `(using default value - ${ DEFAULT_CONFIG . exportType } )`
94+ ) } `
95+ ) ;
96+ this . exportType = DEFAULT_CONFIG . exportType ;
97+ }
98+ }
99+
100+ // Other
101+
102+ this . path = path . resolve ( APP_ROOT , config . path ?? DEFAULT_CONFIG . path ) ;
58103 }
59104
60105 private setFilesExtension ( ) {
@@ -76,4 +121,10 @@ export class Config implements IConfig {
76121 test : 'test' ,
77122 } ;
78123 }
124+
125+ public update ( config : IConfig ) {
126+ this . setVariables ( config ) ;
127+ this . setFilesExtension ( ) ;
128+ this . setPrefixes ( ) ;
129+ }
79130}
0 commit comments