forked from noir-lang/vscode-noir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
1 deletion.
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,41 @@ | ||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
/** | ||
* Given a program file path, walk up the file system until | ||
* finding the nearest a Nargo.toml in a directory that contains | ||
* the program. | ||
* | ||
* To reduce the odds of accidentally choosing the wrong Nargo package, | ||
* end the walk at the root of the current VS Code open files. | ||
*/ | ||
export default function findNearestPackageFolder(program: string): string { | ||
const workspaceFolders = vscode.workspace.workspaceFolders; | ||
if (!workspaceFolders) { | ||
throw new Error("No workspace is currently open in VS Code."); | ||
} | ||
|
||
const workspaceRoots = workspaceFolders.map(wf => wf.uri.fsPath); | ||
|
||
let currentFolder = path.dirname(program); | ||
|
||
try { | ||
while ( | ||
currentFolder !== path.dirname(currentFolder) && | ||
!workspaceRoots.includes(currentFolder) | ||
) { | ||
const maybeNargoProject = path.join(currentFolder, 'Nargo.toml'); | ||
|
||
if (fs.existsSync(maybeNargoProject)) { | ||
return currentFolder; | ||
} | ||
|
||
currentFolder = path.dirname(currentFolder); | ||
} | ||
} catch (error) { | ||
throw new Error("Could not find a Nargo package associated to this file."); | ||
} | ||
|
||
throw new Error("Could not find a Nargo package associated to this file."); | ||
} |