Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit 0b1b6ac

Browse files
committed
Initial commit
0 parents  commit 0b1b6ac

File tree

6 files changed

+591
-0
lines changed

6 files changed

+591
-0
lines changed

.gitattributes

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
*.sln merge=union
7+
*.csproj merge=union
8+
*.vbproj merge=union
9+
*.fsproj merge=union
10+
*.dbproj merge=union
11+
12+
# Standard to msysgit
13+
*.doc diff=astextplain
14+
*.DOC diff=astextplain
15+
*.docx diff=astextplain
16+
*.DOCX diff=astextplain
17+
*.dot diff=astextplain
18+
*.DOT diff=astextplain
19+
*.pdf diff=astextplain
20+
*.PDF diff=astextplain
21+
*.rtf diff=astextplain
22+
*.RTF diff=astextplain

.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# Compiled binary addons (http://nodejs.org/api/addons.html)
20+
build/Release
21+
22+
# Dependency directory
23+
# Deployed apps should consider commenting this line out:
24+
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
25+
node_modules
26+
27+
# =========================
28+
# Operating System Files
29+
# =========================
30+
31+
# OSX
32+
# =========================
33+
34+
.DS_Store
35+
.AppleDouble
36+
.LSOverride
37+
38+
# Icon must ends with two \r.
39+
Icon
40+
41+
# Thumbnails
42+
._*
43+
44+
# Files that might appear on external disk
45+
.Spotlight-V100
46+
.Trashes
47+
48+
# Windows
49+
# =========================
50+
51+
# Windows image file caches
52+
Thumbs.db
53+
ehthumbs.db
54+
55+
# Folder config file
56+
Desktop.ini
57+
58+
# Recycle Bin used on file shares
59+
$RECYCLE.BIN/
60+
61+
# Windows Installer files
62+
*.cab
63+
*.msi
64+
*.msm
65+
*.msp

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# memory-fs
2+
3+
A simple in-memory filesystem. Holds data in a javascript object.
4+
5+
``` javascript
6+
var MemoryFileSystem = require("memory-fs");
7+
var fs = new MemoryFileSystem(); // Optionally pass a javascript object
8+
9+
fs.mkdirpSync("/a/test/dir");
10+
fs.writeFileSync("/a/test/dir/file.txt", "Hello World");
11+
fs.readFileSync("/a/test/dir/file.txt"); // returns Buffer("Hello World")
12+
13+
// Async variantes too
14+
fs.unlink("/a/test/dir/file.txt", function(err) {
15+
// ...
16+
});
17+
18+
fs.readdirSync("/a/test"); // returns ["dir"]
19+
fs.statSync("/a/test/dir").isDirectory(); // returns true
20+
fs.rmdirSync("/a/test/dir");
21+
22+
fs.mkdirpSync("C:\\use\\windows\\style\\paths");
23+
```
24+
25+
## License
26+
27+
Copyright (c) 2012-2014 Tobias Koppers
28+
29+
MIT (http://www.opensource.org/licenses/mit-license.php)

lib/MemoryFileSystem.js

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
};

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "memory-fs",
3+
"version": "0.1.0",
4+
"description": "A simple in-memory filesystem. Holds data in a javascript object.",
5+
"main": "lib/MemoryFileSystem.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "mocha -R spec",
11+
"cover": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/webpack/memory-fs.git"
16+
},
17+
"keywords": [
18+
"fs",
19+
"memory"
20+
],
21+
"author": "Tobias Koppers @sokra",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/webpack/memory-fs/issues"
25+
},
26+
"homepage": "https://github.com/webpack/memory-fs",
27+
"devDependencies": {
28+
"istanbul": "^0.2.13",
29+
"mocha": "^1.20.1",
30+
"should": "^4.0.4"
31+
}
32+
}

0 commit comments

Comments
 (0)