11import * as vscode from "vscode"
2+ import { log } from "../utils/logger"
23import type { Config } from "./types"
34
45// README content aligned with fastapi-cloud-cli
@@ -15,9 +16,10 @@ No, you should not commit the ".fastapicloud" folder to your version control sys
1516That's why there's a ".gitignore" file in this folder.
1617`
1718
19+ const CONFIG_DIR = ".fastapicloud"
20+ const CONFIG_FILE = "cloud.json"
21+
1822export class ConfigService {
19- private static CONFIG_DIR = ".fastapicloud"
20- private static CONFIG_FILE = "cloud.json"
2123 private fileWatcher ?: vscode . FileSystemWatcher
2224
2325 private _onConfigStateChanged = new vscode . EventEmitter < Config | null > ( )
@@ -26,45 +28,36 @@ export class ConfigService {
2628 startWatching ( workspaceRoot : vscode . Uri ) {
2729 const pattern = new vscode . RelativePattern (
2830 workspaceRoot ,
29- `${ ConfigService . CONFIG_DIR } /${ ConfigService . CONFIG_FILE } ` ,
31+ `${ CONFIG_DIR } /${ CONFIG_FILE } ` ,
3032 )
3133 this . fileWatcher = vscode . workspace . createFileSystemWatcher ( pattern )
32- this . fileWatcher . onDidChange ( async ( ) => {
33- const config = await this . getConfig ( workspaceRoot )
34- this . _onConfigStateChanged . fire ( config )
35- } )
36- this . fileWatcher . onDidCreate ( async ( ) => {
34+ const fireConfig = async ( ) => {
3735 const config = await this . getConfig ( workspaceRoot )
3836 this . _onConfigStateChanged . fire ( config )
39- } )
37+ }
38+ this . fileWatcher . onDidChange ( fireConfig )
39+ this . fileWatcher . onDidCreate ( fireConfig )
4040 this . fileWatcher . onDidDelete ( ( ) => this . _onConfigStateChanged . fire ( null ) )
4141 }
4242
4343 async getConfig ( workspaceRoot : vscode . Uri ) : Promise < Config | null > {
4444 try {
45- const uri = vscode . Uri . joinPath (
46- workspaceRoot ,
47- ConfigService . CONFIG_DIR ,
48- ConfigService . CONFIG_FILE ,
49- )
45+ const uri = vscode . Uri . joinPath ( workspaceRoot , CONFIG_DIR , CONFIG_FILE )
5046 const data = await vscode . workspace . fs . readFile ( uri )
5147 return JSON . parse ( new TextDecoder ( ) . decode ( data ) )
5248 } catch ( err ) {
53- console . error ( "[FastAPI Cloud] Failed to read config:" , err )
49+ log ( ` Failed to read config: ${ err } ` )
5450 return null
5551 }
5652 }
5753
5854 async writeConfig ( workspaceRoot : vscode . Uri , config : Config ) {
5955 try {
60- const dirUri = vscode . Uri . joinPath (
61- workspaceRoot ,
62- ConfigService . CONFIG_DIR ,
63- )
56+ const dirUri = vscode . Uri . joinPath ( workspaceRoot , CONFIG_DIR )
6457 await vscode . workspace . fs . createDirectory ( dirUri )
6558
6659 // cloud.json
67- const configUri = vscode . Uri . joinPath ( dirUri , ConfigService . CONFIG_FILE )
60+ const configUri = vscode . Uri . joinPath ( dirUri , CONFIG_FILE )
6861 await vscode . workspace . fs . writeFile (
6962 configUri ,
7063 new TextEncoder ( ) . encode ( JSON . stringify ( config ) ) ,
@@ -90,15 +83,13 @@ export class ConfigService {
9083
9184 async deleteConfig ( workspaceRoot : vscode . Uri ) {
9285 try {
93- const dirUri = vscode . Uri . joinPath (
94- workspaceRoot ,
95- ConfigService . CONFIG_DIR ,
96- )
86+ const dirUri = vscode . Uri . joinPath ( workspaceRoot , CONFIG_DIR )
9787 await vscode . workspace . fs . delete ( dirUri , { recursive : true } )
9888 } catch {
9989 // No project linked
10090 }
10191 }
92+
10293 dispose ( ) {
10394 this . fileWatcher ?. dispose ( )
10495 this . _onConfigStateChanged . dispose ( )
0 commit comments