support in-memory windows-like filesystems, fixes #78

This commit is contained in:
Tobias Koppers 2013-05-12 22:33:17 +02:00
parent b720229360
commit 9959b05319
2 changed files with 54 additions and 33 deletions

View File

@ -18,14 +18,26 @@ function isFile(item) {
return !item[""];
}
MemoryOutputFileSystem.prototype.mkdirp = function(path, callback) {
if(path == "/") return callback();
path = path.split("/");
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
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 = 1; i < path.length; i++) {
for(var i = 0; i < path.length; i++) {
if(isFile(current[path[i]]))
return callback(new Error("Path is a file " + path.join("/")));
return callback(new Error("Path is a file " + _path));
else if(!isDir(current[path[i]]))
current[path[i]] = {"":true};
current = current[path[i]];
@ -33,66 +45,75 @@ MemoryOutputFileSystem.prototype.mkdirp = function(path, callback) {
return callback();
};
MemoryOutputFileSystem.prototype.mkdir = function(path, callback) {
path = path.split("/");
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
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 = 1; i < path.length - 1; i++) {
for(var i = 0; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
return callback(new Error("Path doesn't exists " + path.join("/")));
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.join("/")));
return callback(new Error("Directory already exist " + _path));
else if(isFile(current[path[i]]))
return callback(new Error("Cannot mkdir on file " + path.join("/")));
return callback(new Error("Cannot mkdir on file " + _path));
current[path[i]] = {"":true};
return callback();
};
MemoryOutputFileSystem.prototype.rmdir = function(path, callback) {
path = path.split("/");
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
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));
var current = this.data;
for(var i = 1; i < path.length - 1; i++) {
for(var i = 0; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
return callback(new Error("Path doesn't exists " + path.join("/")));
return callback(new Error("Path doesn't exists " + _path));
current = current[path[i]];
}
if(!isDir(current[path[i]]))
return callback(new Error("Directory doesn't exist " + path.join("/")));
return callback(new Error("Directory doesn't exist " + _path));
delete current[path[i]];
return callback();
};
MemoryOutputFileSystem.prototype.unlink = function(path, callback) {
path = path.split("/");
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
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));
var current = this.data;
for(var i = 1; i < path.length - 1; i++) {
for(var i = 0; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
return callback(new Error("Path doesn't exists " + path.join("/")));
return callback(new Error("Path doesn't exists " + _path));
current = current[path[i]];
}
if(!isFile(current[path[i]]))
return callback(new Error("File doesn't exist " + path.join("/")));
return callback(new Error("File doesn't exist " + _path));
delete current[path[i]];
return callback();
};
MemoryOutputFileSystem.prototype.writeFile = function(path, content, callback) {
MemoryOutputFileSystem.prototype.writeFile = function(_path, content, callback) {
if(!content) return callback(new Error("No content"));
path = path.split("/");
if(path[0] != "") return callback(new Error("Invalid path " + path.join("/")));
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 = 1; i < path.length - 1; i++) {
for(var i = 0; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
return callback(new Error("Path doesn't exists " + path.join("/")));
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.join("/")));
return callback(new Error("Cannot writeFile on directory " + _path));
current[path[i]] = content;
return callback();
};
MemoryOutputFileSystem.prototype.join = function(a, b) {
if(a == "/") return "/" + b;
if(a[a.length-1] == "/") return a + b;
if(a[a.length-1] == "\\") return a + b;
return a + "/" + b;
};

View File

@ -1,6 +1,6 @@
{
"name": "webpack",
"version": "0.10.0-beta7",
"version": "0.10.0-beta8",
"author": "Tobias Koppers @sokra",
"description": "Packs CommonJs/AMD/Labeled Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jade, coffee, css, less, ... and your custom stuff.",
"dependencies": {