Skip to content

Commit 7ffc3df

Browse files
Clean up config.ts
1 parent f2015bc commit 7ffc3df

2 files changed

Lines changed: 16 additions & 27 deletions

File tree

src/cloud/cloudController.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export class CloudController {
2424
private hasConfig = false
2525
private workspaceRoot: vscode.Uri | null = null
2626
private refreshing = false
27-
private started = false
2827
private appNotFoundWarningShown = false
2928
private sessionListener: vscode.Disposable | null = null
3029

@@ -44,8 +43,7 @@ export class CloudController {
4443
showStatusBar() {
4544
this.statusBarItem.text = "$(cloud) FastAPI Cloud"
4645
this.statusBarItem.show()
47-
if (!this.started) {
48-
this.started = true
46+
if (!this.sessionListener) {
4947
this.sessionListener = vscode.authentication.onDidChangeSessions((e) => {
5048
if (e.provider.id === AUTH_PROVIDER_ID) this.refresh()
5149
})

src/cloud/config.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from "vscode"
2+
import { log } from "../utils/logger"
23
import 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
1516
That's why there's a ".gitignore" file in this folder.
1617
`
1718

19+
const CONFIG_DIR = ".fastapicloud"
20+
const CONFIG_FILE = "cloud.json"
21+
1822
export 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

Comments
 (0)