forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryOutputFileSystem.js
More file actions
119 lines (110 loc) · 3.8 KB
/
MemoryOutputFileSystem.js
File metadata and controls
119 lines (110 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
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.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 = 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 doesn't exist " + _path));
delete current[path[i]];
return callback();
};
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 = 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(!isFile(current[path[i]]))
return callback(new Error("File doesn't exist " + _path));
delete current[path[i]];
return 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;
};