@@ -19,16 +19,39 @@ const readFile = fs.promises.readFile;
1919const writeFile = fs . promises . writeFile ;
2020const mkdir = fs . promises . mkdir ;
2121
22- const CONFIG_DIR = path . join ( os . homedir ( ) , '.config' , 'ccstatusline' ) ;
23- const SETTINGS_PATH = process . env . CCSTATUSLINE_CONFIG ?? path . join ( CONFIG_DIR , 'settings.json' ) ;
24- const SETTINGS_BACKUP_PATH = path . join ( CONFIG_DIR , 'settings.bak' ) ;
22+ export function getSettingsConfiguration ( ) {
23+ const projectConfig = path . join ( process . cwd ( ) , '.claude' , 'ccstatusline.json' ) ;
24+
25+ if ( fs . existsSync ( projectConfig ) ) {
26+ return {
27+ configDir : path . dirname ( projectConfig ) ,
28+ path : projectConfig ,
29+ relativePath : path . relative ( process . cwd ( ) , projectConfig ) ,
30+ type : 'project'
31+ } ;
32+ }
33+
34+ const userConfigDir = path . join ( os . homedir ( ) , '.config' , 'ccstatusline' ) ;
35+ const userConfig = path . join ( userConfigDir , 'settings.json' ) ;
36+
37+ // Fallback to global config
38+ return {
39+ configDir : userConfigDir ,
40+ path : userConfig ,
41+ relativePath : '~/' + path . relative ( os . homedir ( ) , userConfig ) ,
42+ type : 'global'
43+ } ;
44+ }
2545
2646async function backupBadSettings ( ) : Promise < void > {
2747 try {
28- if ( fs . existsSync ( SETTINGS_PATH ) ) {
29- const content = await readFile ( SETTINGS_PATH , 'utf-8' ) ;
30- await writeFile ( SETTINGS_BACKUP_PATH , content , 'utf-8' ) ;
31- console . error ( `Bad settings backed up to ${ SETTINGS_BACKUP_PATH } ` ) ;
48+ const { path : settingsPath } = getSettingsConfiguration ( ) ;
49+ const settingsBackupPath = settingsPath . replace ( '.json' , '.json.bak' ) ;
50+
51+ if ( fs . existsSync ( settingsPath ) ) {
52+ const content = await readFile ( settingsPath , 'utf-8' ) ;
53+ await writeFile ( settingsBackupPath , content , 'utf-8' ) ;
54+ console . error ( `Bad settings backed up to ${ settingsBackupPath } ` ) ;
3255 }
3356 } catch ( error ) {
3457 console . error ( 'Failed to backup bad settings:' , error ) ;
@@ -37,15 +60,10 @@ async function backupBadSettings(): Promise<void> {
3760
3861async function writeDefaultSettings ( ) : Promise < Settings > {
3962 const defaults = SettingsSchema . parse ( { } ) ;
40- const settingsWithVersion = {
41- ...defaults ,
42- version : CURRENT_VERSION
43- } ;
4463
4564 try {
46- await mkdir ( CONFIG_DIR , { recursive : true } ) ;
47- await writeFile ( SETTINGS_PATH , JSON . stringify ( settingsWithVersion , null , 2 ) , 'utf-8' ) ;
48- console . error ( `Default settings written to ${ SETTINGS_PATH } ` ) ;
65+ const { path : settingsPath } = await saveSettings ( defaults ) ;
66+ console . error ( `Default settings written to ${ settingsPath } ` ) ;
4967 } catch ( error ) {
5068 console . error ( 'Failed to write default settings:' , error ) ;
5169 }
@@ -55,11 +73,13 @@ async function writeDefaultSettings(): Promise<Settings> {
5573
5674export async function loadSettings ( ) : Promise < Settings > {
5775 try {
76+ const { path : settingsPath } = getSettingsConfiguration ( ) ;
77+
5878 // Check if settings file exists
59- if ( ! fs . existsSync ( SETTINGS_PATH ) )
79+ if ( ! fs . existsSync ( settingsPath ) )
6080 return await writeDefaultSettings ( ) ;
6181
62- const content = await readFile ( SETTINGS_PATH , 'utf-8' ) ;
82+ const content = await readFile ( settingsPath , 'utf-8' ) ;
6383 let rawData : unknown ;
6484
6585 try {
@@ -84,16 +104,17 @@ export async function loadSettings(): Promise<Settings> {
84104
85105 // Migrate v1 to current version and save the migrated settings back to disk
86106 rawData = migrateConfig ( rawData , CURRENT_VERSION ) ;
87- await writeFile ( SETTINGS_PATH , JSON . stringify ( rawData , null , 2 ) , 'utf-8' ) ;
107+ await writeFile ( settingsPath , JSON . stringify ( rawData , null , 2 ) , 'utf-8' ) ;
88108 } else if ( needsMigration ( rawData , CURRENT_VERSION ) ) {
89109 // Handle migrations for versioned configs (v2+) and save the migrated settings back to disk
90110 rawData = migrateConfig ( rawData , CURRENT_VERSION ) ;
91- await writeFile ( SETTINGS_PATH , JSON . stringify ( rawData , null , 2 ) , 'utf-8' ) ;
111+ await writeFile ( settingsPath , JSON . stringify ( rawData , null , 2 ) , 'utf-8' ) ;
92112 }
93113
94114 // At this point, data should be in current format with version field
95115 // Parse with main schema which will apply all defaults
96116 const result = SettingsSchema . safeParse ( rawData ) ;
117+
97118 if ( ! result . success ) {
98119 console . error ( 'Failed to parse settings:' , result . error ) ;
99120 await backupBadSettings ( ) ;
@@ -109,9 +130,11 @@ export async function loadSettings(): Promise<Settings> {
109130 }
110131}
111132
112- export async function saveSettings ( settings : Settings ) : Promise < void > {
133+ export async function saveSettings ( settings : Settings ) {
134+ const { path, configDir } = getSettingsConfiguration ( ) ;
135+
113136 // Ensure config directory exists
114- await mkdir ( CONFIG_DIR , { recursive : true } ) ;
137+ await mkdir ( configDir , { recursive : true } ) ;
115138
116139 // Always include version when saving
117140 const settingsWithVersion = {
@@ -120,5 +143,10 @@ export async function saveSettings(settings: Settings): Promise<void> {
120143 } ;
121144
122145 // Write settings using Node.js-compatible API
123- await writeFile ( SETTINGS_PATH , JSON . stringify ( settingsWithVersion , null , 2 ) , 'utf-8' ) ;
146+ await writeFile ( path , JSON . stringify ( settingsWithVersion , null , 2 ) , 'utf-8' ) ;
147+
148+ return {
149+ settings : settingsWithVersion ,
150+ path
151+ } ;
124152}
0 commit comments