Moved to memory-fs module #98

This commit is contained in:
Tobias Koppers 2014-07-01 22:56:32 +02:00
parent d80cdcefac
commit 651fbfea24
2 changed files with 2 additions and 107 deletions

View File

@ -2,110 +2,4 @@
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[""];
}
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();
var current = this.data;
for(var i = 0; i < path.length; i++) {
if(isFile(current[path[i]]))
return callback(new Error("Path is a file " + _path));
else if(!isDir(current[path[i]]))
current[path[i]] = {"":true};
current = current[path[i]];
}
return callback();
};
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();
var current = this.data;
for(var i = 0; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
return callback(new Error("Path doesn't exists " + _path));
current = current[path[i]];
}
if(isDir(current[path[i]]))
return callback(new Error("Directory already exist " + _path));
else if(isFile(current[path[i]]))
return callback(new Error("Cannot mkdir on file " + _path));
current[path[i]] = {"":true};
return callback();
};
MemoryOutputFileSystem.prototype._remove = function(_path, name, testFn, 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));
var current = this.data;
for(var i = 0; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
return callback(new Error("Path doesn't exists " + _path));
current = current[path[i]];
}
if(!testFn(current[path[i]]))
return callback(new Error(name + " doesn't exist " + _path));
delete current[path[i]];
return callback();
};
MemoryOutputFileSystem.prototype.rmdir = function(_path, callback) {
return this._remove(_path, "Directory", isDir, callback);
};
MemoryOutputFileSystem.prototype.unlink = function(_path, callback) {
return this._remove(_path, "File", isFile, callback);
};
MemoryOutputFileSystem.prototype.writeFile = function(_path, content, callback) {
if(!content) return callback(new Error("No content"));
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));
var current = this.data;
for(var i = 0; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
return callback(new Error("Path doesn't exists " + _path));
current = current[path[i]];
}
if(isDir(current[path[i]]))
return callback(new Error("Cannot writeFile on directory " + _path));
current[path[i]] = content;
return callback();
};
MemoryOutputFileSystem.prototype.join = function(a, b) {
if(a[a.length-1] === "/") return a + b;
if(a[a.length-1] === "\\") return a + b;
return a + "/" + b;
};
module.exports = require("memory-fs");

View File

@ -10,6 +10,7 @@
"uglify-js": "2.4.x",
"async": "0.9.x",
"enhanced-resolve": "0.7.x",
"memory-fs": "~0.1.0",
"clone": "0.1.x",
"webpack-core": "0.4.x",
"node-libs-browser": "0.3.x",