Move findPackRoot to ql.ts (#3268)

This commit is contained in:
Charis Kyriakou
2024-01-23 13:29:46 +00:00
committed by GitHub
parent e834d32a29
commit e612764c59
3 changed files with 29 additions and 27 deletions

View File

@@ -1,4 +1,4 @@
import { join } from "path";
import { dirname, join, parse } from "path";
import { pathExists } from "fs-extra";
export const QLPACK_FILENAMES = ["qlpack.yml", "codeql-pack.yml"];
@@ -27,3 +27,28 @@ export async function getQlPackFilePath(
return undefined;
}
/**
* Recursively find the directory containing qlpack.yml or codeql-pack.yml. If
* no such directory is found, the directory containing the query file is returned.
* @param queryFile The query file to start from.
* @returns The path to the pack root.
*/
export async function findPackRoot(queryFile: string): Promise<string> {
let dir = dirname(queryFile);
while (!(await getQlPackFilePath(dir))) {
dir = dirname(dir);
if (isFileSystemRoot(dir)) {
// there is no qlpack.yml or codeql-pack.yml in this directory or any parent directory.
// just use the query file's directory as the pack root.
return dirname(queryFile);
}
}
return dir;
}
function isFileSystemRoot(dir: string): boolean {
const pathObj = parse(dir);
return pathObj.root === dir && pathObj.base === "";
}

View File

@@ -1,6 +1,6 @@
import type { CancellationToken } from "vscode";
import { Uri, window } from "vscode";
import { relative, join, sep, dirname, parse, basename } from "path";
import { relative, join, sep, basename } from "path";
import { dump, load } from "js-yaml";
import { copy, writeFile, readFile, mkdirp } from "fs-extra";
import type { DirectoryResult } from "tmp-promise";
@@ -262,26 +262,6 @@ async function copyExistingQueryPack(
await fixPackFile(queryPackDir, packRelativePath);
}
export async function findPackRoot(queryFile: string): Promise<string> {
// recursively find the directory containing qlpack.yml or codeql-pack.yml
let dir = dirname(queryFile);
while (!(await getQlPackFilePath(dir))) {
dir = dirname(dir);
if (isFileSystemRoot(dir)) {
// there is no qlpack.yml or codeql-pack.yml in this directory or any parent directory.
// just use the query file's directory as the pack root.
return dirname(queryFile);
}
}
return dir;
}
function isFileSystemRoot(dir: string): boolean {
const pathObj = parse(dir);
return pathObj.root === dir && pathObj.base === "";
}
interface RemoteQueryTempDir {
remoteQueryDir: DirectoryResult;
queryPackDir: string;

View File

@@ -42,11 +42,7 @@ import type {
LoadResultsOptions,
VariantAnalysisResultsManager,
} from "./variant-analysis-results-manager";
import {
findPackRoot,
getQueryName,
prepareRemoteQueryRun,
} from "./run-remote-query";
import { getQueryName, prepareRemoteQueryRun } from "./run-remote-query";
import {
mapVariantAnalysis,
mapVariantAnalysisRepositoryTask,
@@ -94,6 +90,7 @@ import { handleRequestError } from "./custom-errors";
import { createMultiSelectionCommand } from "../common/vscode/selection-commands";
import { askForLanguage } from "../codeql-cli/query-language";
import type { QlPackDetails } from "./ql-pack-details";
import { findPackRoot } from "../common/ql";
const maxRetryCount = 3;