PageRenderTime 25ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/enhanced-resolve/node_modules/memory-fs/lib/MemoryFileSystem.js

https://gitlab.com/ricky1477/twitter-clone
JavaScript | 225 lines | 216 code | 5 blank | 4 comment | 5 complexity | 6502f8ab6df8a559d4d347198eec425a MD5 | raw file
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var normalize = require("./normalize");
  6. function MemoryFileSystem(data) {
  7. this.data = data || {};
  8. }
  9. module.exports = MemoryFileSystem;
  10. function isDir(item) {
  11. if(typeof item !== "object") return false;
  12. return item[""] === true;
  13. }
  14. function isFile(item) {
  15. if(typeof item !== "object") return false;
  16. return !item[""];
  17. }
  18. function pathToArray(path) {
  19. path = normalize(path);
  20. var nix = /^\//.test(path);
  21. if(!nix) {
  22. if(!/^[A-Za-z]:/.test(path)) throw new Error("Invalid path '" + path + "'");
  23. path = path.replace(/[\\\/]+/g, "\\"); // multi slashs
  24. path = path.split(/[\\\/]/);
  25. path[0] = path[0].toUpperCase();
  26. } else {
  27. path = path.replace(/\/+/g, "/"); // multi slashs
  28. path = path.substr(1).split("/");
  29. }
  30. if(!path[path.length-1]) path.pop();
  31. return path;
  32. }
  33. function trueFn() { return true; }
  34. function falseFn() { return false; }
  35. MemoryFileSystem.prototype.statSync = function(_path) {
  36. var path = pathToArray(_path);
  37. var current = this.data;
  38. for(var i = 0; i < path.length - 1; i++) {
  39. if(!isDir(current[path[i]]))
  40. throw new Error("Path doesn't exist '" + _path + "'");
  41. current = current[path[i]];
  42. }
  43. if(_path === "/" || isDir(current[path[i]])) {
  44. return {
  45. isFile: falseFn,
  46. isDirectory: trueFn,
  47. isBlockDevice: falseFn,
  48. isCharacterDevice: falseFn,
  49. isSymbolicLink: falseFn,
  50. isFIFO: falseFn,
  51. isSocket: falseFn
  52. };
  53. } else if(isFile(current[path[i]])) {
  54. return {
  55. isFile: trueFn,
  56. isDirectory: falseFn,
  57. isBlockDevice: falseFn,
  58. isCharacterDevice: falseFn,
  59. isSymbolicLink: falseFn,
  60. isFIFO: falseFn,
  61. isSocket: falseFn
  62. };
  63. } else
  64. throw new Error("Path doesn't exist '" + _path + "'");
  65. };
  66. MemoryFileSystem.prototype.readFileSync = function(_path, encoding) {
  67. var path = pathToArray(_path);
  68. var current = this.data;
  69. for(var i = 0; i < path.length - 1; i++) {
  70. if(!isDir(current[path[i]]))
  71. throw new Error("Path doesn't exist '" + _path + "'");
  72. current = current[path[i]];
  73. }
  74. if(!isFile(current[path[i]])) {
  75. if(isDir(current[path[i]]))
  76. throw new Error("Cannot readFile on directory '" + _path + "'");
  77. else
  78. throw new Error("Path doesn't exist '" + _path + "'");
  79. }
  80. current = current[path[i]];
  81. return encoding ? current.toString(encoding) : current;
  82. };
  83. MemoryFileSystem.prototype.readdirSync = function(_path) {
  84. if(_path === "/") return Object.keys(this.data).filter(Boolean);
  85. var path = pathToArray(_path);
  86. var current = this.data;
  87. for(var i = 0; i < path.length - 1; i++) {
  88. if(!isDir(current[path[i]]))
  89. throw new Error("Path doesn't exist '" + _path + "'");
  90. current = current[path[i]];
  91. }
  92. if(!isDir(current[path[i]])) {
  93. if(isFile(current[path[i]]))
  94. throw new Error("Cannot readdir on file '" + _path + "'");
  95. else
  96. throw new Error("Path doesn't exist '" + _path + "'");
  97. }
  98. return Object.keys(current[path[i]]).filter(Boolean);
  99. };
  100. MemoryFileSystem.prototype.mkdirpSync = function(_path) {
  101. var path = pathToArray(_path);
  102. if(path.length === 0) return;
  103. var current = this.data;
  104. for(var i = 0; i < path.length; i++) {
  105. if(isFile(current[path[i]]))
  106. throw new Error("Path is a file '" + _path + "'");
  107. else if(!isDir(current[path[i]]))
  108. current[path[i]] = {"":true};
  109. current = current[path[i]];
  110. }
  111. return;
  112. };
  113. MemoryFileSystem.prototype.mkdirSync = function(_path) {
  114. var path = pathToArray(_path);
  115. if(path.length === 0) return;
  116. var current = this.data;
  117. for(var i = 0; i < path.length - 1; i++) {
  118. if(!isDir(current[path[i]]))
  119. throw new Error("Path doesn't exist '" + _path + "'");
  120. current = current[path[i]];
  121. }
  122. if(isDir(current[path[i]]))
  123. throw new new Error("Directory already exist '" + _path + "'");
  124. else if(isFile(current[path[i]]))
  125. throw new Error("Cannot mkdir on file '" + _path + "'");
  126. current[path[i]] = {"":true};
  127. return;
  128. };
  129. MemoryFileSystem.prototype._remove = function(_path, name, testFn) {
  130. var path = pathToArray(_path);
  131. if(path.length === 0) throw new Error("Path cannot be removed '" + _path + "'");
  132. var current = this.data;
  133. for(var i = 0; i < path.length - 1; i++) {
  134. if(!isDir(current[path[i]]))
  135. throw new Error("Path doesn't exist '" + _path + "'");
  136. current = current[path[i]];
  137. }
  138. if(!testFn(current[path[i]]))
  139. throw new Error("'" + name + "' doesn't exist '" + _path + "'");
  140. delete current[path[i]];
  141. return;
  142. };
  143. MemoryFileSystem.prototype.rmdirSync = function(_path) {
  144. return this._remove(_path, "Directory", isDir);
  145. };
  146. MemoryFileSystem.prototype.unlinkSync = function(_path) {
  147. return this._remove(_path, "File", isFile);
  148. };
  149. MemoryFileSystem.prototype.readlinkSync = function(_path) {
  150. throw new Error("Path is not a link '" + _path + "'");
  151. };
  152. MemoryFileSystem.prototype.writeFileSync = function(_path, content, encoding) {
  153. if(!content && !encoding) throw new Error("No content");
  154. var path = pathToArray(_path);
  155. if(path.length === 0) throw new Error("Path is not a file '" + _path + "'");
  156. var current = this.data;
  157. for(var i = 0; i < path.length - 1; i++) {
  158. if(!isDir(current[path[i]]))
  159. throw new Error("Path doesn't exist '" + _path + "'");
  160. current = current[path[i]];
  161. }
  162. if(isDir(current[path[i]]))
  163. throw new Error("Cannot writeFile on directory '" + _path + "'");
  164. current[path[i]] = encoding || typeof content === "string" ? new Buffer(content, encoding) : content;
  165. return;
  166. };
  167. MemoryFileSystem.prototype.join = require("./join");
  168. MemoryFileSystem.prototype.normalize = normalize;
  169. // async functions
  170. ["stat", "readdir", "mkdirp", "mkdir", "rmdir", "unlink", "readlink"].forEach(function(fn) {
  171. MemoryFileSystem.prototype[fn] = function(path, callback) {
  172. try {
  173. var result = this[fn + "Sync"](path);
  174. } catch(e) {
  175. return callback(e);
  176. }
  177. return callback(null, result);
  178. };
  179. });
  180. MemoryFileSystem.prototype.readFile = function(path, optArg, callback) {
  181. if(!callback) {
  182. callback = optArg;
  183. optArg = undefined;
  184. }
  185. try {
  186. var result = this.readFileSync(path, optArg);
  187. } catch(e) {
  188. return callback(e);
  189. }
  190. return callback(null, result);
  191. };
  192. MemoryFileSystem.prototype.writeFile = function (path, content, encoding, callback) {
  193. if(!callback) {
  194. callback = encoding;
  195. encoding = undefined;
  196. }
  197. try {
  198. this.writeFileSync(path, content, encoding);
  199. } catch(e) {
  200. return callback(e);
  201. }
  202. return callback();
  203. };