file-manager.js 5.7 KB

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