All files tsconfig-loader.ts

80% Statements 8/10
50% Branches 1/2
50% Functions 1/2
80% Lines 8/10
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 391x                           1x 3x 3x 6x     3x     3x 3x                            
import * as Tsconfig from "tsconfig";
 
export interface TsConfigLoaderResult {
  tsConfigPath: string | undefined,
  baseUrl: string | undefined,
  paths: { [key: string]: Array<string> } | undefined
}
 
export interface TsConfigLoaderParams {
  getEnv: (key: string) => string | undefined
  cwd: string,
  loadSync?(cwd: string, filename?: string): TsConfigLoaderResult;
}
 
export function tsConfigLoader({
  getEnv,
  cwd,
  loadSync = loadSyncDefault
}: TsConfigLoaderParams): TsConfigLoaderResult {
 
  const TS_NODE_PROJECT = getEnv("TS_NODE_PROJECT");
 
  // tsconfig.loadSync handles if TS_NODE_PROJECT is a file or directory
  const loadResult = loadSync(cwd, TS_NODE_PROJECT);
  return loadResult;
 
}
 
function loadSyncDefault(cwd: string, filename?: string): TsConfigLoaderResult {
  // Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename
  const loadResult = Tsconfig.loadSync(cwd, filename);
 
  return {
    tsConfigPath: loadResult.path,
    baseUrl: loadResult.config.compilerOptions.baseUrl,
    paths: loadResult.config.compilerOptions.paths
  };
}