Skip to content

Commit

Permalink
fix: use lookpath package for finding ftl (#1666)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman authored Jun 5, 2024
1 parent 6fa96d7 commit 06b1316
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
16 changes: 14 additions & 2 deletions extensions/vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"lookpath": "^1.2.2",
"semver": "^7.6.0",
"vscode-languageclient": "^9.0.1"
}
Expand Down
34 changes: 7 additions & 27 deletions extensions/vscode/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path'
import { exec, execSync } from 'child_process'
import { promisify } from 'util'
import semver from 'semver'
import { lookpath } from "lookpath"

const execAsync = promisify(exec)

Expand All @@ -17,36 +18,15 @@ export const getProjectOrWorkspaceRoot = async (): Promise<string> => {
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
}

export const resolveFtlPath = (workspaceRoot: string, config: vscode.WorkspaceConfiguration): string => {
export const resolveFtlPath = async (workspaceRoot: string, config: vscode.WorkspaceConfiguration): Promise<string> => {
const ftlPath = config.get<string>("executablePath")
if (!ftlPath || ftlPath.trim() === '') {
try {
// Use `which` for Unix-based systems, `where` for Windows
const command = process.platform === 'win32' ? 'where ftl' : 'which ftl'
return execSync(command).toString().trim()
} catch (error) {
vscode.window.showErrorMessage('Error: ftl binary not found in PATH.')
throw new Error('ftl binary not found in PATH')
const path = await lookpath('ftl')
if (path) {
return path
}
vscode.window.showErrorMessage("FTL executable not found in PATH.")
throw new Error("FTL executable not found in PATH.")
}

return path.isAbsolute(ftlPath) ? ftlPath : path.join(workspaceRoot || '', ftlPath)
Expand Down
2 changes: 1 addition & 1 deletion extensions/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async function startClient(context: ExtensionContext) {
const ftlConfig = vscode.workspace.getConfiguration("ftl")

const workspaceRootPath = await getProjectOrWorkspaceRoot()
const resolvedFtlPath = resolveFtlPath(workspaceRootPath, ftlConfig)
const resolvedFtlPath = await resolveFtlPath(workspaceRootPath, ftlConfig)

outputChannel.appendLine(`VSCode workspace root path: ${workspaceRootPath}`)
outputChannel.appendLine(`FTL path: ${resolvedFtlPath}`)
Expand Down

0 comments on commit 06b1316

Please sign in to comment.