| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | function MemoryOutputFileSystem(data) { | 
					
						
							|  |  |  | 	this.data = data || {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = MemoryOutputFileSystem; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isDir(item) { | 
					
						
							|  |  |  | 	if(typeof item != "object") return false; | 
					
						
							|  |  |  | 	return item[""] === true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isFile(item) { | 
					
						
							|  |  |  | 	if(typeof item == "string") return true; | 
					
						
							|  |  |  | 	if(typeof item != "object") return false; | 
					
						
							|  |  |  | 	return !item[""]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | function pathToArray(path) { | 
					
						
							|  |  |  | 	var nix = /^\//.test(path); | 
					
						
							|  |  |  | 	if(!nix) { | 
					
						
							|  |  |  | 		if(!/^[A-Za-z]:\\/.test(path)) return; | 
					
						
							|  |  |  | 		path = path.replace(/\\/g, "/"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	path = path.replace(/\/+/g, "/"); // multi slashs
 | 
					
						
							|  |  |  | 	path = (nix ? path.substr(1) : path).split("/"); | 
					
						
							|  |  |  | 	if(!path[path.length-1]) path.pop(); | 
					
						
							|  |  |  | 	return path; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MemoryOutputFileSystem.prototype.mkdirp = function(_path, callback) { | 
					
						
							|  |  |  | 	var path = pathToArray(_path); | 
					
						
							|  |  |  | 	if(!path) return callback(new Error("Invalid path " + _path)); | 
					
						
							|  |  |  | 	if(path.length == 0) return callback(); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	var current = this.data; | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 	for(var i = 0; i < path.length; i++) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		if(isFile(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 			return callback(new Error("Path is a file " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		else if(!isDir(current[path[i]])) | 
					
						
							|  |  |  | 			current[path[i]] = {"":true}; | 
					
						
							|  |  |  | 		current = current[path[i]]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return callback(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | MemoryOutputFileSystem.prototype.mkdir = function(_path, callback) { | 
					
						
							|  |  |  | 	var path = pathToArray(_path); | 
					
						
							|  |  |  | 	if(!path) return callback(new Error("Invalid path " + _path)); | 
					
						
							|  |  |  | 	if(path.length == 0) return callback(); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	var current = this.data; | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 	for(var i = 0; i < path.length - 1; i++) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		if(!isDir(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 			return callback(new Error("Path doesn't exists " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		current = current[path[i]]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(isDir(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 		return callback(new Error("Directory already exist " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	else if(isFile(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 		return callback(new Error("Cannot mkdir on file " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	current[path[i]] = {"":true}; | 
					
						
							|  |  |  | 	return callback(); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | MemoryOutputFileSystem.prototype.rmdir = function(_path, callback) { | 
					
						
							|  |  |  | 	var path = pathToArray(_path); | 
					
						
							|  |  |  | 	if(!path) return callback(new Error("Invalid path " + _path)); | 
					
						
							|  |  |  | 	if(path.length == 0) return callback(new Error("Path cannot be removed " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	var current = this.data; | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 	for(var i = 0; i < path.length - 1; i++) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		if(!isDir(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 			return callback(new Error("Path doesn't exists " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		current = current[path[i]]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(!isDir(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 		return callback(new Error("Directory doesn't exist " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	delete current[path[i]]; | 
					
						
							|  |  |  | 	return callback(); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | MemoryOutputFileSystem.prototype.unlink = function(_path, callback) { | 
					
						
							|  |  |  | 	var path = pathToArray(_path); | 
					
						
							|  |  |  | 	if(!path) return callback(new Error("Invalid path " + _path)); | 
					
						
							|  |  |  | 	if(path.length == 0) return callback(new Error("Path cannot be unlinked " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	var current = this.data; | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 	for(var i = 0; i < path.length - 1; i++) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		if(!isDir(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 			return callback(new Error("Path doesn't exists " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		current = current[path[i]]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(!isFile(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 		return callback(new Error("File doesn't exist " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	delete current[path[i]]; | 
					
						
							|  |  |  | 	return callback(); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | MemoryOutputFileSystem.prototype.writeFile = function(_path, content, callback) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	if(!content) return callback(new Error("No content")); | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 	var path = pathToArray(_path); | 
					
						
							|  |  |  | 	if(!path) return callback(new Error("Invalid path " + _path)); | 
					
						
							|  |  |  | 	if(path.length == 0) return callback(new Error("Path is not a file " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	var current = this.data; | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 	for(var i = 0; i < path.length - 1; i++) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		if(!isDir(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 			return callback(new Error("Path doesn't exists " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		current = current[path[i]]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(isDir(current[path[i]])) | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 		return callback(new Error("Cannot writeFile on directory " + _path)); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	current[path[i]] = content; | 
					
						
							|  |  |  | 	return callback(); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | MemoryOutputFileSystem.prototype.join = function(a, b) { | 
					
						
							| 
									
										
										
										
											2013-05-13 04:33:17 +08:00
										 |  |  | 	if(a[a.length-1] == "/") return a + b; | 
					
						
							|  |  |  | 	if(a[a.length-1] == "\\") return a + b; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	return a + "/" + b; | 
					
						
							|  |  |  | }; |