tsconfig-loader.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import * as path from "path";
  2. import * as fs from "fs";
  3. // tslint:disable:no-require-imports
  4. import JSON5 = require("json5");
  5. import StripBom = require("strip-bom");
  6. // tslint:enable:no-require-imports
  7. /**
  8. * Typing for the parts of tsconfig that we care about
  9. */
  10. export interface Tsconfig {
  11. extends?: string;
  12. compilerOptions?: {
  13. baseUrl?: string;
  14. paths?: { [key: string]: Array<string> };
  15. strict?: boolean;
  16. };
  17. }
  18. export interface TsConfigLoaderResult {
  19. tsConfigPath: string | undefined;
  20. baseUrl: string | undefined;
  21. paths: { [key: string]: Array<string> } | undefined;
  22. }
  23. export interface TsConfigLoaderParams {
  24. getEnv: (key: string) => string | undefined;
  25. cwd: string;
  26. loadSync?(
  27. cwd: string,
  28. filename?: string,
  29. baseUrl?: string
  30. ): TsConfigLoaderResult;
  31. }
  32. export function tsConfigLoader({
  33. getEnv,
  34. cwd,
  35. loadSync = loadSyncDefault,
  36. }: TsConfigLoaderParams): TsConfigLoaderResult {
  37. const TS_NODE_PROJECT = getEnv("TS_NODE_PROJECT");
  38. const TS_NODE_BASEURL = getEnv("TS_NODE_BASEURL");
  39. // tsconfig.loadSync handles if TS_NODE_PROJECT is a file or directory
  40. // and also overrides baseURL if TS_NODE_BASEURL is available.
  41. const loadResult = loadSync(cwd, TS_NODE_PROJECT, TS_NODE_BASEURL);
  42. return loadResult;
  43. }
  44. function loadSyncDefault(
  45. cwd: string,
  46. filename?: string,
  47. baseUrl?: string
  48. ): TsConfigLoaderResult {
  49. // Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename
  50. const configPath = resolveConfigPath(cwd, filename);
  51. if (!configPath) {
  52. return {
  53. tsConfigPath: undefined,
  54. baseUrl: undefined,
  55. paths: undefined,
  56. };
  57. }
  58. const config = loadTsconfig(configPath);
  59. return {
  60. tsConfigPath: configPath,
  61. baseUrl:
  62. baseUrl ||
  63. (config && config.compilerOptions && config.compilerOptions.baseUrl),
  64. paths: config && config.compilerOptions && config.compilerOptions.paths,
  65. };
  66. }
  67. function resolveConfigPath(cwd: string, filename?: string): string | undefined {
  68. if (filename) {
  69. const absolutePath = fs.lstatSync(filename).isDirectory()
  70. ? path.resolve(filename, "./tsconfig.json")
  71. : path.resolve(cwd, filename);
  72. return absolutePath;
  73. }
  74. if (fs.statSync(cwd).isFile()) {
  75. return path.resolve(cwd);
  76. }
  77. const configAbsolutePath = walkForTsConfig(cwd);
  78. return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
  79. }
  80. export function walkForTsConfig(
  81. directory: string,
  82. existsSync: (path: string) => boolean = fs.existsSync
  83. ): string | undefined {
  84. const configPath = path.join(directory, "./tsconfig.json");
  85. if (existsSync(configPath)) {
  86. return configPath;
  87. }
  88. const parentDirectory = path.join(directory, "../");
  89. // If we reached the top
  90. if (directory === parentDirectory) {
  91. return undefined;
  92. }
  93. return walkForTsConfig(parentDirectory, existsSync);
  94. }
  95. export function loadTsconfig(
  96. configFilePath: string,
  97. existsSync: (path: string) => boolean = fs.existsSync,
  98. readFileSync: (filename: string) => string = (filename: string) =>
  99. fs.readFileSync(filename, "utf8")
  100. ): Tsconfig | undefined {
  101. if (!existsSync(configFilePath)) {
  102. return undefined;
  103. }
  104. const configString = readFileSync(configFilePath);
  105. const cleanedJson = StripBom(configString);
  106. const config: Tsconfig = JSON5.parse(cleanedJson);
  107. let extendedConfig = config.extends;
  108. if (extendedConfig) {
  109. if (
  110. typeof extendedConfig === "string" &&
  111. extendedConfig.indexOf(".json") === -1
  112. ) {
  113. extendedConfig += ".json";
  114. }
  115. const currentDir = path.dirname(configFilePath);
  116. let extendedConfigPath = path.join(currentDir, extendedConfig);
  117. if (
  118. extendedConfig.indexOf("/") !== -1 &&
  119. extendedConfig.indexOf(".") !== -1 &&
  120. !existsSync(extendedConfigPath)
  121. ) {
  122. extendedConfigPath = path.join(
  123. currentDir,
  124. "node_modules",
  125. extendedConfig
  126. );
  127. }
  128. const base =
  129. loadTsconfig(extendedConfigPath, existsSync, readFileSync) || {};
  130. // baseUrl should be interpreted as relative to the base tsconfig,
  131. // but we need to update it so it is relative to the original tsconfig being loaded
  132. if (base.compilerOptions && base.compilerOptions.baseUrl) {
  133. const extendsDir = path.dirname(extendedConfig);
  134. base.compilerOptions.baseUrl = path.join(
  135. extendsDir,
  136. base.compilerOptions.baseUrl
  137. );
  138. }
  139. return {
  140. ...base,
  141. ...config,
  142. compilerOptions: {
  143. ...base.compilerOptions,
  144. ...config.compilerOptions,
  145. },
  146. };
  147. }
  148. return config;
  149. }