file-manager.js 6.1 KB

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