| 1 | import * as TsConfigLoader2 from "./tsconfig-loader";
|
|---|
| 2 | import * as path from "path";
|
|---|
| 3 | import { options } from "./options";
|
|---|
| 4 |
|
|---|
| 5 | export interface ExplicitParams {
|
|---|
| 6 | baseUrl: string;
|
|---|
| 7 | paths: { [key: string]: Array<string> };
|
|---|
| 8 | mainFields?: Array<string>;
|
|---|
| 9 | addMatchAll?: boolean;
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | export type TsConfigLoader = (
|
|---|
| 13 | params: TsConfigLoader2.TsConfigLoaderParams
|
|---|
| 14 | ) => TsConfigLoader2.TsConfigLoaderResult;
|
|---|
| 15 |
|
|---|
| 16 | export interface ConfigLoaderParams {
|
|---|
| 17 | cwd: string;
|
|---|
| 18 | explicitParams?: ExplicitParams;
|
|---|
| 19 | tsConfigLoader?: TsConfigLoader;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | export interface ConfigLoaderSuccessResult {
|
|---|
| 23 | resultType: "success";
|
|---|
| 24 | configFileAbsolutePath: string;
|
|---|
| 25 | baseUrl: string;
|
|---|
| 26 | absoluteBaseUrl: string;
|
|---|
| 27 | paths: { [key: string]: Array<string> };
|
|---|
| 28 | mainFields?: Array<string>;
|
|---|
| 29 | addMatchAll?: boolean;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | export interface ConfigLoaderFailResult {
|
|---|
| 33 | resultType: "failed";
|
|---|
| 34 | message: string;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | export type ConfigLoaderResult =
|
|---|
| 38 | | ConfigLoaderSuccessResult
|
|---|
| 39 | | ConfigLoaderFailResult;
|
|---|
| 40 |
|
|---|
| 41 | export function loadConfig(cwd: string = options.cwd): ConfigLoaderResult {
|
|---|
| 42 | return configLoader({ cwd: cwd });
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | export function configLoader({
|
|---|
| 46 | cwd,
|
|---|
| 47 | explicitParams,
|
|---|
| 48 | tsConfigLoader = TsConfigLoader2.tsConfigLoader,
|
|---|
| 49 | }: ConfigLoaderParams): ConfigLoaderResult {
|
|---|
| 50 | if (explicitParams) {
|
|---|
| 51 | // tslint:disable-next-line:no-shadowed-variable
|
|---|
| 52 | const absoluteBaseUrl = path.isAbsolute(explicitParams.baseUrl)
|
|---|
| 53 | ? explicitParams.baseUrl
|
|---|
| 54 | : path.join(cwd, explicitParams.baseUrl);
|
|---|
| 55 |
|
|---|
| 56 | return {
|
|---|
| 57 | resultType: "success",
|
|---|
| 58 | configFileAbsolutePath: "",
|
|---|
| 59 | baseUrl: explicitParams.baseUrl,
|
|---|
| 60 | absoluteBaseUrl,
|
|---|
| 61 | paths: explicitParams.paths,
|
|---|
| 62 | mainFields: explicitParams.mainFields,
|
|---|
| 63 | addMatchAll: explicitParams.addMatchAll,
|
|---|
| 64 | };
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | // Load tsconfig and create path matching function
|
|---|
| 68 | const loadResult = tsConfigLoader({
|
|---|
| 69 | cwd,
|
|---|
| 70 | getEnv: (key: string) => process.env[key],
|
|---|
| 71 | });
|
|---|
| 72 |
|
|---|
| 73 | if (!loadResult.tsConfigPath) {
|
|---|
| 74 | return {
|
|---|
| 75 | resultType: "failed",
|
|---|
| 76 | message: "Couldn't find tsconfig.json",
|
|---|
| 77 | };
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | if (!loadResult.baseUrl) {
|
|---|
| 81 | return {
|
|---|
| 82 | resultType: "failed",
|
|---|
| 83 | message: "Missing baseUrl in compilerOptions",
|
|---|
| 84 | };
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | const tsConfigDir = path.dirname(loadResult.tsConfigPath);
|
|---|
| 88 | const absoluteBaseUrl = path.join(tsConfigDir, loadResult.baseUrl);
|
|---|
| 89 |
|
|---|
| 90 | return {
|
|---|
| 91 | resultType: "success",
|
|---|
| 92 | configFileAbsolutePath: loadResult.tsConfigPath,
|
|---|
| 93 | baseUrl: loadResult.baseUrl,
|
|---|
| 94 | absoluteBaseUrl,
|
|---|
| 95 | paths: loadResult.paths || {},
|
|---|
| 96 | };
|
|---|
| 97 | }
|
|---|