All files / src/utils getModuleExports.ts

84.66% Statements 138/163
82.5% Branches 33/40
90.91% Functions 10/11
84.66% Lines 138/163

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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 1644x 4x 4x 4x 4x 4x 4x 4x 4x 16x 16x 16x 4x 4x 4x 4x 4x 12x 12x 12x 12x 12x 12x   12x 12x 12x 600x 600x 600x 596x 596x 596x 596x 4x 4x 4x 12x 12x 12x 12x 4x 12x   12x 12x 12x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 394x 394x 394x 394x 394x 4x 4x 4x 4x 4x 4x 4x 4x                   4x 171x 171x 171x 171x 171x 171x 171x 171x 171x 171x 171x 604x 604x 604x 41x 41x 40x 39x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 36x 9x 41x 604x 563x 372x 372x 372x 372x 372x 6x 6x 6x 6x 6x 6x 6x                           372x 372x 349x 372x 372x 372x 372x 372x 372x 368x 368x 368x 368x 368x 368x 368x   368x 368x  
import { parse, types } from '@babel/core';
import { readFileSync } from 'fs';
import { dirname, join, sep } from 'path';
import assert = require('assert');
import resolveModule = require('../../lib/internal/resolveModule');
 
export interface PluginConfigExports {
  source: string;
  internal: string;
  external: string;
}
 
function toRelativeSource(filePath: string): string {
  const pathChunks = filePath.split(sep);
 
  const relativeChunks = pathChunks.slice(
    pathChunks.lastIndexOf('node_modules') + 1,
  );
 
  return relativeChunks.join('/');
}
 
function resolveIndexPath(
  name: string,
  indexFile: null | undefined | string,
): string {
  if (!indexFile) {
    const indexFilePath = resolveModule(name);
 
    assert.ok(indexFilePath, `failed to find 'indexFile' of '${name}'.`);
 
    return indexFilePath;
  }
 
  const pkgJSON = resolveModule(`${name}/package.json`);
 
  assert.ok(pkgJSON, `failed to resolve 'package.json' for '${name}'.`);
 
  const pkgDir = dirname(pkgJSON);
 
  const indexFilePath = resolveModule(join(pkgDir, indexFile));
 
  assert.ok(
    indexFilePath,
    `failed to resolve 'indexFile' specified for '${name}'.`,
  );
 
  return indexFilePath;
}
 
function fulfillExports(
  exports: Map<string, PluginConfigExports>,
  filePath: string,
): void {
  const content = readFileSync(filePath, 'utf-8');
  const ast = parse(content, {
    babelrc: false,
    configFile: false,
    filename: filePath,
    sourceType: 'module',
  });
 
  assert.ok(types.isFile(ast), `failed to parse '${filePath}'.`);
 
  const fileDir = dirname(filePath);
  const imports = new Map<string, { imported: string; source: string }>();
 
  for (const node of ast.program.body) {
    if (types.isImportDeclaration(node)) {
      const sourcePath = resolveModule(node.source.value, fileDir);
 
      if (!sourcePath) continue;
 
      for (const specifier of node.specifiers) {
        imports.set(specifier.local.name, {
          source: toRelativeSource(sourcePath),
          imported: types.isImportNamespaceSpecifier(specifier)
            ? '*'
            : types.isImportDefaultSpecifier(specifier)
            ? 'default'
            : types.isIdentifier(specifier.imported)
            ? specifier.imported.name
            : specifier.imported.value,
        });
      }
    } else if (types.isExportNamedDeclaration(node)) {
      const nodeDeclaration = node.declaration;
 
      if (types.isVariableDeclaration(nodeDeclaration)) {
        for (const declaration of nodeDeclaration.declarations) {
          if (
            types.isIdentifier(declaration.id) &&
            types.isIdentifier(declaration.init)
          ) {
            const external = declaration.id.name;
            const internal = declaration.init.name;
            const imported = imports.get(internal);
 
            if (imported) {
              exports.set(external, {
                external,
                source: imported.source,
                internal: imported.imported,
              });
            }
          }
        }
      }
 
      const sourcePath = !node.source
        ? null
        : resolveModule(node.source.value, fileDir);
 
      for (const specifier of node.specifiers) {
        if (types.isExportSpecifier(specifier)) {
          const internal = specifier.local.name;
          const external = types.isIdentifier(specifier.exported)
            ? specifier.exported.name
            : specifier.exported.value;
 
          if (sourcePath) {
            exports.set(external, {
              internal,
              external,
              source: toRelativeSource(sourcePath),
            });
          } else {
            const imported = imports.get(internal);
 
            if (imported) {
              exports.set(external, {
                external,
                source: imported.source,
                internal: imported.imported,
              });
            }
          }
        }
      }
    } else if (types.isExportAllDeclaration(node)) {
      const sourcePath = resolveModule(node.source.value, fileDir);

      assert.ok(
        sourcePath,
        `failed to resolve '${node.source.value}' from '${fileDir}'`,
      );
 
      fulfillExports(exports, sourcePath);
    }
  }
}
 
export function getModuleExports(
  name: string,
  indexFile?: string,
): Map<string, PluginConfigExports> {
  const indexPath = resolveIndexPath(name, indexFile);
  const exports = new Map<string, PluginConfigExports>();
 
  fulfillExports(exports, indexPath);

  return exports;
}