All files config-loader.ts

88.89% Statements 16/18
83.33% Branches 10/12
50% Functions 1/2
88.89% Lines 16/18
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 731x 1x                                               1x 8x 4x 4x     4x 2x       2x               2x         2x             2x 1x           1x 1x   1x            
import * as TsConfigLoader from "./tsconfig-loader";
import * as path from "path";
 
export interface ExplicitParams {
  baseUrl: string,
  paths: { [key: string]: Array<string> }
}
 
interface ConfigLoaderParams {
  tsConfigLoader?: (params: TsConfigLoader.TsConfigLoaderParams) => TsConfigLoader.TsConfigLoaderResult
  explicitParams: ExplicitParams | undefined,
  cwd: string,
}
 
export interface ConfigLoaderSuccessResult {
  resultType: "success",
  absoluteBaseUrl: string,
  paths: { [key: string]: Array<string> }
}
 
export interface ConfigLoaderFailResult {
  resultType: "failed",
  message: string
}
 
export function configLoader({
  tsConfigLoader = TsConfigLoader.tsConfigLoader,
  explicitParams,
  cwd
}: ConfigLoaderParams): ConfigLoaderSuccessResult | ConfigLoaderFailResult {
 
  if (explicitParams) {
    const absoluteBaseUrl = path.isAbsolute(explicitParams.baseUrl)
      ? explicitParams.baseUrl
      : path.join(cwd, explicitParams.baseUrl);
 
    return {
      resultType: "success",
      absoluteBaseUrl,
      paths: explicitParams.paths
    };
  }
 
  // Load tsconfig and create path matching function
  const loadResult = tsConfigLoader({
    cwd,
    getEnv: (key: string) => process.env[key],
  });
 
  Iif (!loadResult.tsConfigPath) {
    return {
      resultType: "failed",
      message: "Couldn't find tsconfig.json"
    };
  }
 
  if (!loadResult.baseUrl) {
    return {
      resultType: "failed",
      message: "Missing baseUrl in compilerOptions"
    };
  }
 
  const tsConfigDir = path.dirname(loadResult.tsConfigPath);
  const absoluteBaseUrl = path.join(tsConfigDir, loadResult.baseUrl);
 
  return {
    resultType: "success",
    absoluteBaseUrl,
    paths: loadResult.paths || {}
  };
}