file-manager.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var path_1 = tslib_1.__importDefault(require("path"));
  5. var fs_1 = tslib_1.__importDefault(require("./fs"));
  6. var abstract_file_manager_js_1 = tslib_1.__importDefault(require("../less/environment/abstract-file-manager.js"));
  7. var FileManager = function () { };
  8. FileManager.prototype = Object.assign(new abstract_file_manager_js_1.default(), {
  9. supports: function () {
  10. return true;
  11. },
  12. supportsSync: function () {
  13. return true;
  14. },
  15. loadFile: function (filename, currentDirectory, options, environment, callback) {
  16. var fullFilename;
  17. var isAbsoluteFilename = this.isPathAbsolute(filename);
  18. var filenamesTried = [];
  19. var self = this;
  20. var prefix = filename.slice(0, 1);
  21. var explicit = prefix === '.' || prefix === '/';
  22. var result = null;
  23. var isNodeModule = false;
  24. var npmPrefix = 'npm://';
  25. options = options || {};
  26. var paths = isAbsoluteFilename ? [''] : [currentDirectory];
  27. if (options.paths) {
  28. paths.push.apply(paths, options.paths);
  29. }
  30. if (!isAbsoluteFilename && paths.indexOf('.') === -1) {
  31. paths.push('.');
  32. }
  33. var prefixes = options.prefixes || [''];
  34. var fileParts = this.extractUrlParts(filename);
  35. if (options.syncImport) {
  36. getFileData(returnData, returnData);
  37. if (callback) {
  38. callback(result.error, result);
  39. }
  40. else {
  41. return result;
  42. }
  43. }
  44. else {
  45. // promise is guaranteed to be asyncronous
  46. // which helps as it allows the file handle
  47. // to be closed before it continues with the next file
  48. return new Promise(getFileData);
  49. }
  50. function returnData(data) {
  51. if (!data.filename) {
  52. result = { error: data };
  53. }
  54. else {
  55. result = data;
  56. }
  57. }
  58. function getFileData(fulfill, reject) {
  59. (function tryPathIndex(i) {
  60. function tryWithExtension() {
  61. var extFilename = options.ext ? self.tryAppendExtension(fullFilename, options.ext) : fullFilename;
  62. if (extFilename !== fullFilename && !explicit && paths[i] === '.') {
  63. try {
  64. fullFilename = require.resolve(extFilename);
  65. isNodeModule = true;
  66. }
  67. catch (e) {
  68. filenamesTried.push(npmPrefix + extFilename);
  69. fullFilename = extFilename;
  70. }
  71. }
  72. else {
  73. fullFilename = extFilename;
  74. }
  75. }
  76. if (i < paths.length) {
  77. (function tryPrefix(j) {
  78. if (j < prefixes.length) {
  79. isNodeModule = false;
  80. fullFilename = fileParts.rawPath + prefixes[j] + fileParts.filename;
  81. if (paths[i]) {
  82. fullFilename = path_1.default.join(paths[i], fullFilename);
  83. }
  84. if (!explicit && paths[i] === '.') {
  85. try {
  86. fullFilename = require.resolve(fullFilename);
  87. isNodeModule = true;
  88. }
  89. catch (e) {
  90. filenamesTried.push(npmPrefix + fullFilename);
  91. tryWithExtension();
  92. }
  93. }
  94. else {
  95. tryWithExtension();
  96. }
  97. var readFileArgs = [fullFilename];
  98. if (!options.rawBuffer) {
  99. readFileArgs.push('utf-8');
  100. }
  101. if (options.syncImport) {
  102. try {
  103. var data = fs_1.default.readFileSync.apply(this, readFileArgs);
  104. fulfill({ contents: data, filename: fullFilename });
  105. }
  106. catch (e) {
  107. filenamesTried.push(isNodeModule ? npmPrefix + fullFilename : fullFilename);
  108. return tryPrefix(j + 1);
  109. }
  110. }
  111. else {
  112. readFileArgs.push(function (e, data) {
  113. if (e) {
  114. filenamesTried.push(isNodeModule ? npmPrefix + fullFilename : fullFilename);
  115. return tryPrefix(j + 1);
  116. }
  117. fulfill({ contents: data, filename: fullFilename });
  118. });
  119. fs_1.default.readFile.apply(this, readFileArgs);
  120. }
  121. }
  122. else {
  123. tryPathIndex(i + 1);
  124. }
  125. })(0);
  126. }
  127. else {
  128. reject({ type: 'File', message: "'" + filename + "' wasn't found. Tried - " + filenamesTried.join(',') });
  129. }
  130. }(0));
  131. }
  132. },
  133. loadFileSync: function (filename, currentDirectory, options, environment) {
  134. options.syncImport = true;
  135. return this.loadFile(filename, currentDirectory, options, environment);
  136. }
  137. });
  138. exports.default = FileManager;
  139. //# sourceMappingURL=file-manager.js.map