file-manager.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var path = require('path'),
  2. fs = require('./fs'),
  3. PromiseConstructor,
  4. AbstractFileManager = require("../less/environment/abstract-file-manager.js");
  5. try {
  6. PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise;
  7. } catch(e) {
  8. }
  9. var FileManager = function() {
  10. };
  11. FileManager.prototype = new AbstractFileManager();
  12. FileManager.prototype.supports = function(filename, currentDirectory, options, environment) {
  13. return true;
  14. };
  15. FileManager.prototype.supportsSync = function(filename, currentDirectory, options, environment) {
  16. return true;
  17. };
  18. FileManager.prototype.loadFile = function(filename, currentDirectory, options, environment, callback) {
  19. var fullFilename,
  20. data,
  21. isAbsoluteFilename = this.isPathAbsolute(filename),
  22. filenamesTried = [];
  23. options = options || {};
  24. if (options.syncImport || !PromiseConstructor) {
  25. data = this.loadFileSync(filename, currentDirectory, options, environment, 'utf-8');
  26. callback(data.error, data);
  27. return;
  28. }
  29. var paths = isAbsoluteFilename ? [""] : [currentDirectory];
  30. if (options.paths) { paths.push.apply(paths, options.paths); }
  31. if (!isAbsoluteFilename && paths.indexOf('.') === -1) { paths.push('.'); }
  32. // promise is guaranteed to be asyncronous
  33. // which helps as it allows the file handle
  34. // to be closed before it continues with the next file
  35. return new PromiseConstructor(function(fulfill, reject) {
  36. (function tryPathIndex(i) {
  37. if (i < paths.length) {
  38. fullFilename = filename;
  39. if (paths[i]) {
  40. fullFilename = path.join(paths[i], fullFilename);
  41. }
  42. fs.stat(fullFilename, function (err) {
  43. if (err) {
  44. filenamesTried.push(fullFilename);
  45. tryPathIndex(i + 1);
  46. } else {
  47. fs.readFile(fullFilename, 'utf-8', function(e, data) {
  48. if (e) { reject(e); return; }
  49. fulfill({ contents: data, filename: fullFilename});
  50. });
  51. }
  52. });
  53. } else {
  54. reject({ type: 'File', message: "'" + filename + "' wasn't found. Tried - " + filenamesTried.join(",") });
  55. }
  56. }(0));
  57. });
  58. };
  59. FileManager.prototype.loadFileSync = function(filename, currentDirectory, options, environment, encoding) {
  60. var fullFilename, paths, filenamesTried = [], isAbsoluteFilename = this.isPathAbsolute(filename) , data;
  61. options = options || {};
  62. paths = isAbsoluteFilename ? [""] : [currentDirectory];
  63. if (options.paths) {
  64. paths.push.apply(paths, options.paths);
  65. }
  66. if (!isAbsoluteFilename && paths.indexOf('.') === -1) {
  67. paths.push('.');
  68. }
  69. var err, result;
  70. for (var i = 0; i < paths.length; i++) {
  71. try {
  72. fullFilename = filename;
  73. if (paths[i]) {
  74. fullFilename = path.join(paths[i], fullFilename);
  75. }
  76. filenamesTried.push(fullFilename);
  77. fs.statSync(fullFilename);
  78. break;
  79. } catch (e) {
  80. fullFilename = null;
  81. }
  82. }
  83. if (!fullFilename) {
  84. err = { type: 'File', message: "'" + filename + "' wasn't found. Tried - " + filenamesTried.join(",") };
  85. result = { error: err };
  86. } else {
  87. data = fs.readFileSync(fullFilename, encoding);
  88. result = { contents: data, filename: fullFilename};
  89. }
  90. return result;
  91. };
  92. module.exports = FileManager;