1
- import { cosmiconfig } from 'cosmiconfig ' ;
1
+ import { loadConfig as loadC12Config , watchConfig } from 'c12 ' ;
2
2
import { ArgumentsCamelCase } from 'yargs' ;
3
3
4
4
import { SharedOptions } from '../options' ;
@@ -107,12 +107,6 @@ export const getConfigFromArgv = (argv: ArgumentsCamelCase<SharedOptions>) => {
107
107
} ;
108
108
} ;
109
109
110
- function removeUndefined ( obj : any ) {
111
- return Object . fromEntries (
112
- Object . entries ( obj ) . filter ( ( [ _ , value ] ) => value !== undefined ) ,
113
- ) ;
114
- }
115
-
116
110
/**
117
111
* Validates custom commands configuration
118
112
* @param config The configuration object
@@ -138,32 +132,53 @@ function validateCustomCommands(config: Config): void {
138
132
} ) ;
139
133
}
140
134
/**
141
- * Load configuration using cosmiconfig
135
+ * Load configuration using c12
142
136
* @returns Merged configuration with default values
143
137
*/
144
138
export async function loadConfig (
145
139
cliOptions : Partial < Config > = { } ,
146
140
) : Promise < Config > {
147
- // Initialize cosmiconfig
148
- const explorer = cosmiconfig ( 'mycoder' , {
149
- searchStrategy : 'global' ,
141
+ // Load configuration using c12
142
+ const { config } = await loadC12Config ( {
143
+ name : 'mycoder' ,
144
+ defaults : defaultConfig ,
145
+ overrides : cliOptions ,
146
+ // Optionally enable .env support
147
+ // dotenv: true,
150
148
} ) ;
151
149
152
- // Search for configuration file
153
- const result = await explorer . search ( ) ;
154
-
155
- // Merge configurations with precedence: default < file < cli
156
- const fileConfig = result ?. config || { } ;
150
+ // Convert to Config type and validate custom commands
151
+ const typedConfig = config as unknown as Config ;
152
+ validateCustomCommands ( typedConfig ) ;
157
153
158
- // Return merged configuration
159
- const mergedConfig = {
160
- ...defaultConfig ,
161
- ...removeUndefined ( fileConfig ) ,
162
- ...removeUndefined ( cliOptions ) ,
163
- } ;
154
+ return typedConfig ;
155
+ }
164
156
165
- // Validate custom commands if present
166
- validateCustomCommands ( mergedConfig ) ;
157
+ /**
158
+ * Watch configuration for changes
159
+ * @param cliOptions CLI options to override configuration
160
+ * @param onUpdate Callback when configuration is updated
161
+ */
162
+ export async function watchConfigForChanges (
163
+ cliOptions : Partial < Config > = { } ,
164
+ onUpdate ?: ( config : Config ) => void ,
165
+ ) {
166
+ const { config, watchingFiles, unwatch } = await watchConfig ( {
167
+ name : 'mycoder' ,
168
+ defaults : defaultConfig ,
169
+ overrides : cliOptions ,
170
+ onUpdate : ( { newConfig } ) => {
171
+ const typedConfig = newConfig as unknown as Config ;
172
+ validateCustomCommands ( typedConfig ) ;
173
+ if ( onUpdate ) {
174
+ onUpdate ( typedConfig ) ;
175
+ }
176
+ } ,
177
+ } ) ;
167
178
168
- return mergedConfig ;
179
+ return {
180
+ config : config as unknown as Config ,
181
+ watchingFiles,
182
+ unwatch,
183
+ } ;
169
184
}
0 commit comments