file-manager.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. var modified = false;
  94. if (self.contents[fullFilename]) {
  95. try {
  96. var stat = fs.statSync.apply(this, [fullFilename]);
  97. if (stat.mtime.getTime() === self.contents[fullFilename].mtime.getTime()) {
  98. fulfill({ contents: self.contents[fullFilename].data, filename: fullFilename});
  99. }
  100. else {
  101. modified = true;
  102. }
  103. }
  104. catch (e) {
  105. modified = true;
  106. }
  107. }
  108. if (modified || !self.contents[fullFilename]) {
  109. var readFileArgs = [fullFilename];
  110. if (!options.rawBuffer) {
  111. readFileArgs.push('utf-8');
  112. }
  113. if (options.syncImport) {
  114. try {
  115. var data = fs.readFileSync.apply(this, readFileArgs);
  116. var stat = fs.statSync.apply(this, [fullFilename]);
  117. self.contents[fullFilename] = { data: data, mtime: stat.mtime };
  118. fulfill({ contents: data, filename: fullFilename});
  119. }
  120. catch (e) {
  121. filenamesTried.push(isNodeModule ? npmPrefix + fullFilename : fullFilename);
  122. return tryPrefix(j + 1);
  123. }
  124. }
  125. else {
  126. readFileArgs.push(function(e, data) {
  127. if (e) {
  128. filenamesTried.push(isNodeModule ? npmPrefix + fullFilename : fullFilename);
  129. return tryPrefix(j + 1);
  130. }
  131. var stat = fs.statSync.apply(this, [fullFilename]);
  132. self.contents[fullFilename] = { data: data, mtime: stat.mtime };
  133. fulfill({ contents: data, filename: fullFilename});
  134. });
  135. fs.readFile.apply(this, readFileArgs);
  136. }
  137. }
  138. }
  139. else {
  140. tryPathIndex(i + 1);
  141. }
  142. })(0);
  143. } else {
  144. reject({ type: 'File', message: '\'' + filename + '\' wasn\'t found. Tried - ' + filenamesTried.join(',') });
  145. }
  146. }(0));
  147. }
  148. };
  149. FileManager.prototype.loadFileSync = function(filename, currentDirectory, options, environment) {
  150. options.syncImport = true;
  151. return this.loadFile(filename, currentDirectory, options, environment);
  152. };
  153. module.exports = FileManager;