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