1
1
import { access , readFile , writeFile } from 'node:fs/promises' ;
2
2
import { URL } from 'node:url' ;
3
3
import { parse , stringify } from 'yaml' ;
4
- import type { CodeStyleSetupOptions } from '@code-style/code-style/config-types' ;
4
+ import type {
5
+ CodeStyleSetupOptions ,
6
+ CodeStyleRCFile ,
7
+ } from '@code-style/code-style/config-types' ;
8
+ import { version } from './utils.js' ;
5
9
6
10
export const default_config_path = '.codestyleinitrc.yaml' ;
7
11
@@ -12,15 +16,15 @@ export interface LoadRCOptions {
12
16
export async function load_rc ( {
13
17
config_path = default_config_path ,
14
18
throw_no_config = false ,
15
- } : LoadRCOptions = { } ) : Promise < Partial < CodeStyleSetupOptions > > {
19
+ } : LoadRCOptions = { } ) : Promise < Partial < CodeStyleRCFile > > {
16
20
const found = await access ( config_path )
17
21
. then ( ( ) => true )
18
22
. catch ( ( ) => false ) ;
19
23
if ( found ) {
20
24
return readFile ( config_path )
21
25
. then ( ( buf ) => buf . toString ( ) )
22
- . then < Partial < CodeStyleSetupOptions > > (
23
- ( str ) => ( parse ( str ) ?? { } ) as Partial < CodeStyleSetupOptions > ,
26
+ . then < Partial < CodeStyleRCFile > > (
27
+ ( str ) => ( parse ( str ) ?? { } ) as Partial < CodeStyleRCFile > ,
24
28
)
25
29
. catch ( ( err : unknown ) => {
26
30
if ( throw_no_config ) throw err ;
@@ -50,7 +54,7 @@ export async function save_rc(
50
54
. then < string > ( ( pkg ) => pkg . version as string ) ;
51
55
52
56
const header = `# yaml-language-server: $schema=https://github.com/dudeofawesome/code-style/releases/download/v${ package_version } /codestylerc.schema.json` ;
53
- const contents = `${ header } \n\n${ stringify ( config ) } ` ;
57
+ const contents = `${ header } \n\n${ stringify ( { version , ... config } ) } ` ;
54
58
55
59
await writeFile ( config_path , contents ) ;
56
60
}
0 commit comments