All files / src/utils prepareConfig.ts

100% Statements 49/49
100% Branches 15/15
100% Functions 5/5
100% Lines 49/49

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 506x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 15x 15x 15x 15x 9x 9x 9x 9x 9x 9x 9x 9x 4x 4x 4x 4x 9x 5x 9x 9x 2x 2x 2x 2x 2x 2x 9x 4x 4x 4x 4x 4x 4x 4x 4x 4x  
import assert = require('assert');
 
export interface PluginConfig {
  name: string;
  indexFile?: null | string;
}
 
export function prepareConfig(args: unknown): PluginConfig[] {
  const configs: PluginConfig[] = [];
 
  for (const config of (Array.isArray(args) ? args : [args]) as unknown[]) {
    if (typeof config == 'string') {
      configs.push({ name: config });
    } else if (typeof config == 'object' && config) {
      const { name, indexFile, ...unknownOptions } = config as PluginConfig;
 
      assert(typeof name == 'string', '{ name } expected to be a string');
      assert(name, '{ name } is empty');
 
      if (indexFile != null) {
        assert(
          typeof indexFile == 'string',
          '{ indexFile } expected to be a string',
        );
        assert(indexFile, '{ indexFile } is empty');
      }
 
      if (indexFile != null && indexFile !== name) {
        const [nameID] = name.split('/');
        const [indexFileID] = indexFile.split('/');
 
        assert(
          nameID === indexFileID,
          `index file '${indexFile}' must belong to '${name}' package`,
        );
      }
 
      const unknownOptionsKeys = Object.keys(unknownOptions);
      assert(
        !unknownOptionsKeys.length,
        `contains unknown keys { ${unknownOptionsKeys.join(', ')} }`,
      );
 
      configs.push({ name, indexFile });
    }
  }
 
  return configs;
}