All files prepareConfig.ts

100% Statements 17/17
90% Branches 9/10
100% Functions 2/2
100% Lines 17/17

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                    15x   15x 15x 1x     14x   14x 10x   9x 6x       3x     5x 2x 2x   2x               4x 4x           3x      
import { assert } from './asserts';
 
export interface PluginConfig {
  name: string;
  indexFile?: null | string;
}
 
export function prepareConfig(
  args: string | PluginConfig | Array<string | PluginConfig>,
): PluginConfig[] {
  const configs = Array.isArray(args) ? args : [args];
 
  return configs.map((options) => {
    if (typeof options === 'string') {
      return { name: options };
    }
 
    const { name, indexFile, ...unknownOptions } = options;
 
    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 '%s' must belong to '%s' package",
        indexFile,
        name,
      );
    }
 
    const unknownOptionsKeys = Object.keys(unknownOptions);
    assert(
      unknownOptionsKeys.length === 0,
      'contains unknown keys { %s }',
      unknownOptionsKeys.join(', '),
    );
 
    return options;
  });
}