11import { extname } from 'node:path' ;
2+ import { existsSync } from 'node:fs' ;
23import { readFile , writeFile } from 'node:fs/promises' ;
34import type { CSpellSettings } from 'cspell' ;
45import { searchForConfig } from 'cspell-lib' ;
6+ import findDefaultConfigPath from 'application-config-path' ;
57// eslint-disable-next-line import/no-relative-packages
68import { previousState } from './shared' ;
79
810let configPath : string | undefined ;
911
10- export const findConfig = async ( config ?: string ) => {
12+ export const writeToSettings = async ( settings : CSpellSettings | { cspell : CSpellSettings } ) => {
13+ await writeFile ( configPath ! , JSON . stringify ( settings , null , 4 ) ) ;
14+ } ;
15+
16+ export const findOrCreateConfig = async ( config ?: string ) => {
1117 // Try to locate a config file in the current working directory, or with the `config` option if it was provided.
1218 const configSource = config ?? ( await searchForConfig ( process . cwd ( ) ) ) ?. __importRef ?. filename ;
1319
1420 // If no config file was found, use/create a config file in the user's configuration directory (platform dependent).
15- const path = configSource ?? ( await import ( 'application-config-path' ) ) . default ( 'cspell.json' ) ;
21+ const path = configSource ?? findDefaultConfigPath ( 'cspell.json' ) ;
1622
1723 // Only JSON files are supported to prevent more dependencies for yml parsing. If the config file is not a JSON
1824 // file, it can still be used, but it won't be updated with new ignored words.
1925 if ( extname ( path ) === '.json' ) {
2026 configPath = path ;
2127 }
2228
29+ // If configSource is undefined, check if default config path exists. If it doesn't, create it.
30+ if ( ! configSource && ! existsSync ( path ) ) {
31+ await writeToSettings ( { } ) ;
32+ }
33+
2334 return path ;
2435} ;
2536
@@ -37,10 +48,6 @@ export const getSettings = async (): Promise<CSpellSettings | { cspell: CSpellSe
3748 }
3849} ;
3950
40- export const writeToSettings = async ( settings : CSpellSettings | { cspell : CSpellSettings } ) => {
41- await writeFile ( configPath ! , JSON . stringify ( settings , null , 4 ) ) ;
42- } ;
43-
4451export const addIgnoreWordToSettings = async ( word : string ) => {
4552 const settings = await getSettings ( ) ;
4653 if ( ! settings ) {
0 commit comments