All files / src/info index.ts

0% Statements 0/58
0% Branches 0/12
0% Functions 0/4
0% Lines 0/58

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                                                                                                                                                                                                                                                   
import { Dependencies } from "../dependencies";
import { getMatrixCombinations } from "../list/matrix";
import { Plugin } from "../cli";
import { getProjectSetExecutions, printSetHeader } from "../sets";
import { CLI_COLOR_CYAN, CLI_FORMAT_BOLD, colorize } from "../tester/cliFormat";
import { buildRuntimeDatafiles } from "../builder/buildRuntimeDatafiles";
 
async function showTargetInfo(deps: Dependencies, target: string | string[]) {
  const { projectConfig } = deps;
  const environments = Array.isArray(projectConfig.environments)
    ? projectConfig.environments
    : [false as const];
 
  for (const environment of environments) {
    const datafiles = await buildRuntimeDatafiles(deps, {
      environment,
      target,
      revision: "info",
    });
 
    for (const entry of datafiles) {
      const variables = Object.values(entry.datafile.features).reduce((count, feature) => {
        const schemas = feature.variablesSchema;
        return (
          count + (Array.isArray(schemas) ? schemas.length : Object.keys(schemas || {}).length)
        );
      }, 0);
 
      console.log("");
      console.log(CLI_FORMAT_BOLD, `Target "${entry.target}"`);
      console.log(`  ${colorize("Environment", CLI_COLOR_CYAN)}: ${environment}`);
      console.log(
        `  ${colorize("Features", CLI_COLOR_CYAN)}:    ${Object.keys(entry.datafile.features).length}`,
      );
      console.log(
        `  ${colorize("Segments", CLI_COLOR_CYAN)}:    ${Object.keys(entry.datafile.segments).length}`,
      );
      console.log(`  ${colorize("Variables", CLI_COLOR_CYAN)}:   ${variables}`);
      console.log(
        `  ${colorize("Datafile size", CLI_COLOR_CYAN)}: ${(JSON.stringify(entry.datafile).length / 1024).toFixed(2)} kB`,
      );
    }
  }
}
 
export async function showProjectInfo(deps: Dependencies) {
  const { datasource, options } = deps;
 
  if (options.target) {
    await showTargetInfo(deps, options.target);
    return;
  }
 
  console.log("");
  console.log(CLI_FORMAT_BOLD, "Project info");
  console.log("");
 
  const revision = await datasource.readRevision();
  console.log(`  ${colorize("Revision", CLI_COLOR_CYAN)}:         ${revision}`);
 
  console.log("");
 
  const attributes = await datasource.listAttributes();
  const segments = await datasource.listSegments();
  const features = await datasource.listFeatures();
  const groups = await datasource.listGroups();
  const targets = await datasource.listTargets();
 
  let variablesCount = 0;
  for (const featureKey of features) {
    const feature = await datasource.readFeature(featureKey);
 
    if (feature.variablesSchema) {
      variablesCount += Object.keys(feature.variablesSchema).length;
    }
  }
 
  console.log(`  ${colorize("Total attributes", CLI_COLOR_CYAN)}: ${attributes.length}`);
  console.log(`  ${colorize("Total segments", CLI_COLOR_CYAN)}:   ${segments.length}`);
  console.log(`  ${colorize("Total features", CLI_COLOR_CYAN)}:   ${features.length}`);
  console.log(`  ${colorize("Total variables", CLI_COLOR_CYAN)}:  ${variablesCount}`);
  console.log(`  ${colorize("Total groups", CLI_COLOR_CYAN)}:     ${groups.length}`);
  console.log(`  ${colorize("Total targets", CLI_COLOR_CYAN)}:    ${targets.length}`);
 
  console.log("");
 
  const tests = await datasource.listTests();
  console.log(`  ${colorize("Total test specs", CLI_COLOR_CYAN)}: ${tests.length}`);
 
  let assertionsCount = 0;
  for (const test of tests) {
    const testSpec = await datasource.readTest(test);
 
    for (const assertion of testSpec.assertions) {
      if (assertion.matrix) {
        const combinations = getMatrixCombinations(assertion.matrix);
        assertionsCount += combinations.length;
      } else {
        assertionsCount += 1;
      }
    }
  }
 
  console.log(`  ${colorize("Total assertions", CLI_COLOR_CYAN)}: ${assertionsCount}`);
}
 
export const infoPlugin: Plugin = {
  command: "info",
  handler: async function ({ rootDirectoryPath, projectConfig, datasource, parsed }) {
    const executions = await getProjectSetExecutions(projectConfig, datasource, parsed.set);
 
    for (const execution of executions) {
      printSetHeader(projectConfig, execution.set);
 
      await showProjectInfo({
        rootDirectoryPath,
        projectConfig: execution.projectConfig,
        datasource: execution.datasource,
        options: parsed,
      });
    }
  },
  examples: [
    {
      command: "info",
      description: "show various stats for the project",
    },
  ],
};