All files / lib exhaust.ts

92.59% Statements 25/27
66.66% Branches 4/6
100% Functions 3/3
92.3% Lines 24/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 691x         1x 1x 1x 1x   1x           1x 1x         1x 2x         1x 2x   1x           1x               1x         4x   4x   4x 1x     4x 2x     2x   1x 1x      
import {ERRORS} from '@grnsft/if-core/utils';
 
/**
 * @todo This is temporary solution, will be refactored to support dynamic plugins.
 */
import {ExportCSV} from '../builtins/export-csv';
import {ExportCSVRaw} from '../builtins/export-csv-raw';
import {ExportLog} from '../builtins/export-log';
import {ExportYaml} from '../builtins/export-yaml';
 
import {STRINGS} from '../config';
 
import {ExhaustPluginInterface} from '../types/exhaust-plugin-interface';
import {Context} from '../types/manifest';
import {Options} from '../types/process-args';
 
const {InvalidExhaustPluginError} = ERRORS;
const {INVALID_EXHAUST_PLUGIN, PREPARING_OUTPUT_DATA} = STRINGS;
 
/**
 * Initialize exhaust plugins based on the provided config
 */
const initializeExhaustPlugins = (plugins: string[]) =>
  plugins.map(initializeExhaustPlugin);
 
/**
 * Factory method for exhaust plugins.
 */
const initializeExhaustPlugin = (name: string): ExhaustPluginInterface => {
  switch (name) {
    case 'yaml':
      return ExportYaml();
    case 'csv':
      return ExportCSV();
    case 'csv-raw':
      return ExportCSVRaw();
    default:
      throw new InvalidExhaustPluginError(INVALID_EXHAUST_PLUGIN(name));
  }
};
 
/**
 * Output manager - Exhaust.
 * Grabs output plugins from context, executes every.
 */
export const exhaust = async (
  tree: any,
  context: Context,
  outputOptions: Options
) => {
  console.debug(PREPARING_OUTPUT_DATA);
 
  const outputPlugins = context.initialize.outputs;
 
  if (outputOptions.stdout) {
    ExportLog().execute(tree, context);
  }
 
  if (!outputPlugins) {
    return;
  }
 
  const exhaustPlugins = initializeExhaustPlugins(outputPlugins);
 
  for await (const plugin of exhaustPlugins) {
    await plugin.execute(tree, context, outputOptions.outputPath);
  }
};