|
| 1 | +import { dirname } from "path"; |
| 2 | +import { containsPath, findCommonParentDir } from "../common/files"; |
| 3 | +import { findPackRoot } from "../common/ql"; |
| 4 | + |
| 5 | +/** |
| 6 | + * This function finds the root directory of the QL pack that contains the provided query files. |
| 7 | + * It handles several cases: |
| 8 | + * - If no query files are provided, it throws an error. |
| 9 | + * - If all query files are in the same QL pack, it returns the root directory of that pack. |
| 10 | + * - If the query files are in different QL packs, it throws an error. |
| 11 | + * - If some query files are in a QL pack and some aren't, it throws an error. |
| 12 | + * - If none of the query files are in a QL pack, it returns the common parent directory of the query files. However, |
| 13 | + * if there are more than one query files and they're not in the same workspace folder, it throws an error. |
| 14 | + * |
| 15 | + * @param queryFiles - An array of file paths for the query files. |
| 16 | + * @param workspaceFolders - An array of workspace folder paths. |
| 17 | + * @returns The root directory of the QL pack that contains the query files, or the common parent directory of the query files. |
| 18 | + */ |
| 19 | +export async function findVariantAnalysisQlPackRoot( |
| 20 | + queryFiles: string[], |
| 21 | + workspaceFolders: string[], |
| 22 | +): Promise<string> { |
| 23 | + if (queryFiles.length === 0) { |
| 24 | + throw Error("No query files provided"); |
| 25 | + } |
| 26 | + |
| 27 | + // Calculate the pack root for each query file |
| 28 | + const packRoots: Array<string | undefined> = []; |
| 29 | + for (const queryFile of queryFiles) { |
| 30 | + const packRoot = await findPackRoot(queryFile); |
| 31 | + packRoots.push(packRoot); |
| 32 | + } |
| 33 | + |
| 34 | + if (queryFiles.length === 1) { |
| 35 | + return packRoots[0] ?? dirname(queryFiles[0]); |
| 36 | + } |
| 37 | + |
| 38 | + const uniquePackRoots = Array.from(new Set(packRoots)); |
| 39 | + |
| 40 | + if (uniquePackRoots.length > 1) { |
| 41 | + if (uniquePackRoots.includes(undefined)) { |
| 42 | + throw Error("Some queries are in a pack and some aren't"); |
| 43 | + } else { |
| 44 | + throw Error("Some queries are in different packs"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (uniquePackRoots[0] === undefined) { |
| 49 | + return findQlPackRootForQueriesWithNoPack(queryFiles, workspaceFolders); |
| 50 | + } else { |
| 51 | + // All in the same pack, return that pack's root |
| 52 | + return uniquePackRoots[0]; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * For queries that are not in a pack, a potential pack root is the |
| 58 | + * common parent dir of all the queries. However, we only want to |
| 59 | + * return this if all the queries are in the same workspace folder. |
| 60 | + */ |
| 61 | +function findQlPackRootForQueriesWithNoPack( |
| 62 | + queryFiles: string[], |
| 63 | + workspaceFolders: string[], |
| 64 | +): string { |
| 65 | + const commonParentDir = findCommonParentDir(...queryFiles); |
| 66 | + |
| 67 | + // Check that all queries are in a workspace folder (the same one), |
| 68 | + // so that we don't return a pack root that's outside the workspace. |
| 69 | + // This is to avoid accessing files outside the workspace folders. |
| 70 | + for (const workspaceFolder of workspaceFolders) { |
| 71 | + if (containsPath(workspaceFolder, commonParentDir)) { |
| 72 | + return commonParentDir; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + throw Error( |
| 77 | + "All queries must be within the workspace and within the same workspace root", |
| 78 | + ); |
| 79 | +} |
0 commit comments