tsconfig.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var fs = require("fs");
  4. var path = require("path");
  5. var stripBom = require("strip-bom");
  6. var stripComments = require("strip-json-comments");
  7. var CONFIG_FILENAME = 'tsconfig.json';
  8. function resolve(cwd, filename) {
  9. if (!filename) {
  10. return find(cwd);
  11. }
  12. var fullPath = path.resolve(cwd, filename);
  13. return stat(fullPath)
  14. .then(function (stats) {
  15. if (isFile(stats)) {
  16. return fullPath;
  17. }
  18. if (isDirectory(stats)) {
  19. var configFile_1 = path.join(fullPath, CONFIG_FILENAME);
  20. return stat(configFile_1)
  21. .then(function (stats) {
  22. if (isFile(stats)) {
  23. return configFile_1;
  24. }
  25. throw new TypeError("Cannot find a " + CONFIG_FILENAME + " file at the specified directory: " + filename);
  26. });
  27. }
  28. throw new TypeError("The specified path does not exist: " + filename);
  29. });
  30. }
  31. exports.resolve = resolve;
  32. function resolveSync(cwd, filename) {
  33. if (!filename) {
  34. return findSync(cwd);
  35. }
  36. var fullPath = path.resolve(cwd, filename);
  37. var stats = statSync(fullPath);
  38. if (isFile(stats)) {
  39. return fullPath;
  40. }
  41. if (isDirectory(stats)) {
  42. var configFile = path.join(fullPath, CONFIG_FILENAME);
  43. var stats_1 = statSync(configFile);
  44. if (isFile(stats_1)) {
  45. return configFile;
  46. }
  47. throw new TypeError("Cannot find a " + CONFIG_FILENAME + " file at the specified directory: " + filename);
  48. }
  49. throw new TypeError("The specified path does not exist: " + filename);
  50. }
  51. exports.resolveSync = resolveSync;
  52. function find(dir) {
  53. var configFile = path.resolve(dir, CONFIG_FILENAME);
  54. return stat(configFile)
  55. .then(function (stats) {
  56. if (isFile(stats)) {
  57. return configFile;
  58. }
  59. var parentDir = path.dirname(dir);
  60. if (dir === parentDir) {
  61. return;
  62. }
  63. return find(parentDir);
  64. });
  65. }
  66. exports.find = find;
  67. function findSync(dir) {
  68. var configFile = path.resolve(dir, CONFIG_FILENAME);
  69. var stats = statSync(configFile);
  70. if (isFile(stats)) {
  71. return configFile;
  72. }
  73. var parentDir = path.dirname(dir);
  74. if (dir === parentDir) {
  75. return;
  76. }
  77. return findSync(parentDir);
  78. }
  79. exports.findSync = findSync;
  80. function load(cwd, filename) {
  81. return resolve(cwd, filename)
  82. .then(function (path) {
  83. if (path == null) {
  84. return Promise.resolve({
  85. config: {
  86. files: [],
  87. compilerOptions: {}
  88. }
  89. });
  90. }
  91. return readFile(path).then(function (config) { return ({ path: path, config: config }); });
  92. });
  93. }
  94. exports.load = load;
  95. function loadSync(cwd, filename) {
  96. var path = resolveSync(cwd, filename);
  97. if (path == null) {
  98. return {
  99. config: {
  100. files: [],
  101. compilerOptions: {}
  102. }
  103. };
  104. }
  105. var config = readFileSync(path);
  106. return { path: path, config: config };
  107. }
  108. exports.loadSync = loadSync;
  109. function readFile(filename) {
  110. return new Promise(function (resolve, reject) {
  111. fs.readFile(filename, 'utf8', function (err, contents) {
  112. if (err) {
  113. return reject(err);
  114. }
  115. try {
  116. return resolve(parse(contents, filename));
  117. }
  118. catch (err) {
  119. return reject(err);
  120. }
  121. });
  122. });
  123. }
  124. exports.readFile = readFile;
  125. function readFileSync(filename) {
  126. var contents = fs.readFileSync(filename, 'utf8');
  127. return parse(contents, filename);
  128. }
  129. exports.readFileSync = readFileSync;
  130. function parse(contents, filename) {
  131. var data = stripComments(stripBom(contents));
  132. if (/^\s*$/.test(data)) {
  133. return {};
  134. }
  135. return JSON.parse(data);
  136. }
  137. exports.parse = parse;
  138. function stat(filename) {
  139. return new Promise(function (resolve, reject) {
  140. fs.stat(filename, function (err, stats) {
  141. return err ? resolve(undefined) : resolve(stats);
  142. });
  143. });
  144. }
  145. function statSync(filename) {
  146. try {
  147. return fs.statSync(filename);
  148. }
  149. catch (e) {
  150. return;
  151. }
  152. }
  153. function isFile(stats) {
  154. return stats ? stats.isFile() || stats.isFIFO() : false;
  155. }
  156. function isDirectory(stats) {
  157. return stats ? stats.isDirectory() : false;
  158. }
  159. //# sourceMappingURL=tsconfig.js.map