generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve path detection for extension (#1235)
- Loading branch information
1 parent
6dda791
commit c18aade
Showing
3 changed files
with
53 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ dist | |
dist-ssr | ||
*.local | ||
*.vsix | ||
**/*.map | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
|
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,41 @@ | ||
import * as vscode from "vscode" | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
export const getProjectOrWorkspaceRoot = async (): Promise<string> => { | ||
const workspaceFolders = vscode.workspace.workspaceFolders | ||
if (!workspaceFolders) { | ||
vscode.window.showErrorMessage("FTL extension requires an open folder or workspace to work correctly.") | ||
return "" | ||
} | ||
|
||
// Check each folder for the 'ftl-project.toml' file | ||
for (const folder of workspaceFolders) { | ||
const workspaceRootPath = folder.uri.fsPath | ||
const ftlProjectPath = await findFileInWorkspace(workspaceRootPath, 'ftl-project.toml') | ||
if (ftlProjectPath) { | ||
return ftlProjectPath.replace('ftl-project.toml', '') | ||
} | ||
} | ||
|
||
return workspaceFolders[0].uri.fsPath | ||
} | ||
|
||
export const findFileInWorkspace = async (rootPath: string, fileName: string): Promise<string | null> => { | ||
try { | ||
const filesAndFolders = await fs.promises.readdir(rootPath, { withFileTypes: true }) | ||
|
||
for (const dirent of filesAndFolders) { | ||
const fullPath = path.join(rootPath, dirent.name) | ||
if (dirent.isDirectory()) { | ||
const result = await findFileInWorkspace(fullPath, fileName) | ||
if (result) { return result } | ||
} else if (dirent.isFile() && dirent.name === fileName) { | ||
return fullPath | ||
} | ||
} | ||
} catch (error) { | ||
console.error('Failed to read directory:', rootPath) | ||
} | ||
return null | ||
} |
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