-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce WorkspaceContentService to handle workspace file loading
- Loading branch information
Showing
5 changed files
with
74 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ClientOrPluginProperties } from "../../kiotaInterop"; | ||
import WorkspaceContentService from "./workspaceContentService"; | ||
|
||
export interface WorkspaceContent { | ||
version: string; | ||
clients: Record<string, ClientOrPluginProperties>; | ||
plugins: Record<string, ClientOrPluginProperties>; | ||
} | ||
export { WorkspaceContentService }; |
43 changes: 43 additions & 0 deletions
43
vscode/microsoft-kiota/src/modules/workspace/workspaceContentService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
import { WorkspaceContent } from "."; | ||
import { KIOTA_WORKSPACE_FILE } from "../../constants"; | ||
import { getWorkspaceJsonPath } from '../../util'; | ||
|
||
class WorkspaceContentService { | ||
constructor() { } | ||
|
||
public async load(): Promise<WorkspaceContent | undefined> { | ||
const isWorkspacePresent = await this.isKiotaWorkspaceFilePresent(); | ||
if (!isWorkspacePresent) { | ||
return; | ||
} | ||
try { | ||
const workspaceJson = vscode.workspace.textDocuments.find(doc => doc.fileName.endsWith(KIOTA_WORKSPACE_FILE)); | ||
if (!workspaceJson) { | ||
throw new Error('Workspace file not found'); | ||
} | ||
const content = workspaceJson.getText(); | ||
return JSON.parse(content); | ||
} catch (error) { | ||
console.error('Error loading workspace.json:', error); | ||
} | ||
} | ||
|
||
async isKiotaWorkspaceFilePresent(): Promise<boolean> { | ||
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) { | ||
return false; | ||
} | ||
const workspaceFileDir = path.resolve(getWorkspaceJsonPath()); | ||
try { | ||
await fs.promises.access(workspaceFileDir); | ||
} catch (error) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
export default WorkspaceContentService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters