All files / lib/internal prepareConfig.js

100% Statements 63/63
100% Branches 13/13
100% Functions 2/2
100% Lines 63/63

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 646x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 20x 20x 20x 20x 20x 20x 20x 6x 20x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 6x 6x 6x 6x 6x 6x 5x 14x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 20x 9x 9x 6x  
'use strict';
 
const assert = require('assert');
 
/**
 * @typedef {object} ModuleConfig
 * @property {string} name
 * @property {string} [indexFile]
 */
 
/**
 * @param {unknown} input
 * @returns {ModuleConfig[]}
 */
module.exports = function prepareConfig(input) {
  /** @type {ModuleConfig[]} */
  const configs = [];
  /** @type {unknown[]} */
  const inputs = Array.isArray(input) ? input : [input];
 
  for (const config of inputs) {
    if (typeof config == 'string') {
      configs.push({ name: config });
    } else if (typeof config == 'object' && config) {
      const {
        name,
        indexFile,
        ...unknownOptions
      } = /** @type {ModuleConfig} */ (config);
 
      assert.ok(typeof name == 'string', '{ name } expected to be a string');
      assert.ok(name, '{ name } is empty');
 
      if (indexFile != null) {
        assert.ok(
          typeof indexFile == 'string',
          '{ indexFile } expected to be a string',
        );
        assert.ok(indexFile, '{ indexFile } is empty');
      }
 
      if (indexFile != null && indexFile !== name) {
        const [nameID] = name.split('/');
        const [indexFileID] = indexFile.split('/');
 
        assert.ok(
          nameID === indexFileID,
          `index file '${indexFile}' must belong to '${name}' package`,
        );
      }
 
      const unknownOptionsKeys = Object.keys(unknownOptions);
      assert.ok(
        !unknownOptionsKeys.length,
        `contains unknown keys { ${unknownOptionsKeys.join(', ')} }`,
      );
 
      configs.push({ name, indexFile });
    }
  }
 
  return configs;
};