All files / src sets.ts

34.61% Statements 9/26
19.23% Branches 5/26
60% Functions 3/5
34.61% Lines 9/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 775x       5x               1x         1x 1x                                                       1x         1x                                     1x 1x          
import * as path from "path";
 
import type { ProjectConfig } from "./config";
import type { Datasource } from "./datasource";
import { CLI_FORMAT_BOLD } from "./tester/cliFormat";
 
export interface ProjectSetExecution {
  set: string;
  projectConfig: ProjectConfig;
  datasource: Datasource;
}
 
export async function getProjectSetExecutions(
  projectConfig: ProjectConfig,
  datasource: Datasource,
  selectedSet?: string,
): Promise<ProjectSetExecution[]> {
  Eif (!projectConfig.sets) {
    return [{ set: "", projectConfig, datasource }];
  }
 
  const availableSetKeys = await datasource.listSets();
 
  if (selectedSet && !availableSetKeys.includes(selectedSet)) {
    throw new Error(
      `Unknown set "${selectedSet}". Available sets: ${availableSetKeys.join(", ") || "none"}.`,
    );
  }
 
  const setKeys = selectedSet ? [selectedSet] : availableSetKeys;
 
  if (setKeys.length === 0) {
    throw new Error(`No sets found in ${projectConfig.setsDirectoryPath}.`);
  }
 
  return setKeys.map((set) => {
    const setDatasource = datasource.forSet(set);
 
    return {
      set,
      projectConfig: setDatasource.getConfig(),
      datasource: setDatasource,
    };
  });
}
 
export function assertProjectSetJsonSelection(
  projectConfig: ProjectConfig,
  selectedSet: string | undefined,
  json: boolean | undefined,
) {
  Iif (projectConfig.sets && json && !selectedSet) {
    throw new Error("Pass --set=<set> when using --json in a project with sets enabled.");
  }
}
 
export function getProjectSetRelativeFilePath(
  projectConfig: ProjectConfig,
  set: string,
  filePath: string,
) {
  const setDirectoryPath = path.join(projectConfig.setsDirectoryPath, set);
 
  if (filePath === setDirectoryPath || filePath.startsWith(`${setDirectoryPath}${path.sep}`)) {
    return filePath;
  }
 
  return path.join(setDirectoryPath, filePath);
}
 
export function printSetHeader(projectConfig: ProjectConfig, set: string, silent = false) {
  Iif (projectConfig.sets && !silent) {
    console.log("");
    console.log(CLI_FORMAT_BOLD, `Set "${set}"`);
  }
}