(folder: string, allowOutsideWorkspace = false)
| 3 | import { hasPackageMapFile, hasPubspec } from "../utils/fs"; |
| 4 | |
| 5 | export function locateBestProjectRoot(folder: string, allowOutsideWorkspace = false): string | undefined { |
| 6 | // TODO(dantup): The term "Project" is a bit ambiguous, and some places that call here really want "pub roots" (eg. the package, or if it's |
| 7 | // part of a Pub Workspace, the pub workspace root), and some want the packages (eg. the things that have a pubspec.yaml and a name). |
| 8 | // Perhaps consider a GLOSSARY.md and define terms to use consistently (and match don't use "Project" at all?). |
| 9 | |
| 10 | // TODO(dantup): Review places where allowOutsideWorkspace is effectively false, because opening sub-folders |
| 11 | // of Pub Workspaces is probably not uncommon. |
| 12 | if (!folder) |
| 13 | return undefined; |
| 14 | |
| 15 | if (!allowOutsideWorkspace && (!isWithinWorkspace(folder) && workspace.workspaceFolders?.length)) { |
| 16 | return undefined; |
| 17 | } |
| 18 | |
| 19 | let dir = folder; |
| 20 | while (dir !== path.dirname(dir)) { |
| 21 | if (hasPubspec(dir) || hasPackageMapFile(dir)) |
| 22 | return dir; |
| 23 | dir = path.dirname(dir); |
| 24 | } |
| 25 | |
| 26 | return undefined; |
| 27 | } |
| 28 | |
| 29 | export function isWithinWorkspace(file: string) { |
| 30 | return !!workspace.getWorkspaceFolder(Uri.file(file)); |
no test coverage detected