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

Commit d5655c0

Browse files
committed
Adding VFS
1 parent 13c2c7a commit d5655c0

File tree

1 file changed

+154
-87
lines changed

1 file changed

+154
-87
lines changed

lib/util/VFS.js

Lines changed: 154 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,147 +2,214 @@
22

33
/**
44
* Virtual file system
5-
* Keeps track of created/copied/deleted files and folders
5+
* Keeps track of files and folders
66
*/
77

8-
var VFS = function() {
9-
// Structure filesystem as object
10-
this._vfs = [{
11-
type: 'drive',
12-
name: 'C:',
13-
subfolders: [{
8+
class VFS {
9+
constructor() {
10+
// Structure filesystem as object
11+
this._vfs = [{
12+
name: 'C',
13+
type: 'drive'
14+
}, {
15+
name: 'C:',
16+
path: 'C:\\',
17+
type: 'folder'
18+
}, {
19+
name: 'temp',
20+
path: 'C:\\temp',
1421
type: 'folder',
15-
name: 'temp'
16-
subfolders: [],
17-
files: []
18-
}],
19-
files: []
20-
}];
22+
}, {
23+
content: 'Hello world!',
24+
name: 'testfile.txt',
25+
path: 'C:\\temp\\testfile.txt',
26+
type: 'file'
27+
}];
28+
29+
// Relative paths will be appended by this
30+
this._relpath = 'C:\\temp';
31+
}
32+
33+
_printVFS() {
34+
console.log(JSON.stringify(this._vfs, null, 2));
35+
}
36+
37+
_createFolderObject(path) {
38+
return {
39+
name: path.split('\\').pop(),
40+
type: 'folder',
41+
path: path
42+
}
43+
}
2144

22-
this._pointer = null;
23-
};
45+
_createFileObject(path, content) {
46+
return {
47+
content,
48+
name: path.split('\\').pop(),
49+
path,
50+
type: 'file'
51+
}
52+
}
2453

25-
VFS.prototype.copyFile = function(file, destination) {
54+
_formatPath(path) {
55+
let curPathArr = !path.match(/^[A-Z]\:/) ? this._relpath.split('\\') : [];
56+
let pathArr = curPathArr.concat(path.split('\\').filter(el => el));
57+
let absPath = [];
2658

27-
};
59+
pathArr.forEach((el, index) => {
60+
if (el === '.') {
61+
// Do nothing
62+
} else if (el === '..') {
63+
absPath.pop();
64+
} else {
65+
absPath.push(el);
66+
}
67+
});
2868

29-
VFS.prototype.copyFolder = function(folder, destination) {
69+
return absPath.length === 1 ? absPath[0] + '\\' : absPath.join('\\');
70+
}
3071

31-
};
72+
_resolvePath(path, type) {
73+
path = type !== 'drive' ? this._formatPath(path) : path;
3274

33-
VFS.prototype.createFolder = function(path) {
34-
var pathArr = path.split('\\');
75+
return this._vfs.filter(entry => {
76+
if (type && entry.type !== type) {
77+
return false;
78+
} else if (type && type === 'drive') {
79+
return entry.name === path;
80+
}
3581

82+
return entry.path === path;
83+
})[0];
84+
}
3685

37-
}
86+
copyFile(file, destination) {
87+
let resolvedFile = this._resolvePath(file);
88+
let destArr = destination.split('\\');
89+
let filename = destArr.pop();
90+
91+
if (!resolvedFile) {
92+
throw TypeError('File not found');
93+
}
94+
95+
if (!this._resolvePath(destArr.join('\\'))) {
96+
throw TypeError('Path not found');
97+
}
98+
99+
if (this._resolvePath(destination)) {
100+
this.deleteFile(destination);
101+
}
38102

39-
ScriptingFSO.prototype.CreateTextFile = function() {
103+
this._vfs.push(this._createFileObject(destination, resolvedFile.content));
104+
}
40105

41-
};
106+
copyFolder(folder, destination) {
42107

43-
// https://msdn.microsoft.com/en-us/library/thx0f315(v=vs.84).aspx
44-
ScriptingFSO.prototype.DeleteFile = function() {
108+
}
45109

46-
};
110+
createFolder(path) {
47111

48-
// https://msdn.microsoft.com/en-us/library/ca0at0xh(v=vs.84).aspx
49-
ScriptingFSO.prototype.DeleteFolder = function() {
112+
}
50113

51-
};
114+
createTextFile() {
52115

53-
// https://msdn.microsoft.com/en-us/library/t565x0f1(v=vs.84).aspx
54-
ScriptingFSO.prototype.DriveExists = function() {
116+
}
55117

56-
};
118+
deleteFile(path) {
119+
let resolvedFile = this._resolvePath(path);
57120

58-
// https://msdn.microsoft.com/en-us/library/x23stk5t(v=vs.84).aspx
59-
ScriptingFSO.prototype.FileExists = function() {
121+
if (!resolvedFile) {
122+
throw new TypeError('File not found');
123+
}
60124

61-
};
125+
this._vfs.splice(this._vfs.indexOf(resolvedFile), 1);
126+
}
62127

63-
// https://msdn.microsoft.com/en-us/library/5xc78d8d(v=vs.84).aspx
64-
ScriptingFSO.prototype.FolderExists = function() {
128+
deleteFolder() {
65129

66-
};
130+
}
67131

68-
// https://msdn.microsoft.com/en-us/library/zx1xa64f(v=vs.84).aspx
69-
ScriptingFSO.prototype.GetAbsolutePathName = function() {
132+
driveExists(path) {
133+
return this._resolvePath(path, 'drive') ? -1 : 0;
134+
}
70135

71-
};
136+
fileExists(path) {
137+
return this._resolvePath(path, 'file') ? -1 : 0;
138+
}
72139

73-
// https://msdn.microsoft.com/en-us/library/xhxzwwe1(v=vs.84).aspx
74-
ScriptingFSO.prototype.GetBaseName = function() {
140+
folderExists(path) {
141+
return this._resolvePath(path, 'folder') ? -1 : path;
142+
}
75143

76-
};
144+
getAbsolutePathName() {
77145

78-
// https://msdn.microsoft.com/en-us/library/1z6e0fk3(v=vs.84).aspx
79-
ScriptingFSO.prototype.GetDrive = function() {
146+
}
80147

81-
};
148+
getBaseName() {
82149

83-
// https://msdn.microsoft.com/en-us/library/48e3yfdw(v=vs.84).aspx
84-
ScriptingFSO.prototype.GetDriveName = function() {
150+
}
85151

86-
};
152+
getDrive() {
87153

88-
// https://msdn.microsoft.com/en-us/library/x0fxha2a(v=vs.84).aspx
89-
ScriptingFSO.prototype.GetExtensionName = function() {
154+
}
90155

91-
};
156+
getDriveName() {
92157

93-
// https://msdn.microsoft.com/en-us/library/sheydkke(v=vs.84).aspx
94-
ScriptingFSO.prototype.GetFile = function() {
158+
}
95159

96-
};
160+
getExtensionName() {
97161

98-
// https://msdn.microsoft.com/en-us/library/a99s8akf(v=vs.84).aspx
99-
ScriptingFSO.prototype.GetFileName = function() {
162+
}
100163

101-
};
164+
getFile(path) {
165+
let resolvedFile = this._resolvePath(path);
102166

103-
// https://msdn.microsoft.com/en-us/library/b4e05k97(v=vs.84).aspx
104-
ScriptingFSO.prototype.GetFileVersion = function() {
167+
if (!resolvedFile) {
168+
throw new TypeError('File not found');
169+
} else {
170+
return resolvedFile;
171+
}
172+
}
105173

106-
};
174+
getFileName() {
107175

108-
// https://msdn.microsoft.com/en-us/library/f1xtf7ta(v=vs.84).aspx
109-
ScriptingFSO.prototype.GetFolder = function() {
176+
}
110177

111-
};
178+
getFileVersion() {
112179

113-
// https://msdn.microsoft.com/en-us/library/22dyy47c(v=vs.84).aspx
114-
ScriptingFSO.prototype.GetParentFolderName = function() {
180+
}
115181

116-
};
182+
getFolder() {
117183

118-
// https://msdn.microsoft.com/en-us/library/a72y2t1c(v=vs.84).aspx
119-
ScriptingFSO.prototype.GetSpecialFolder = function() {
184+
}
120185

121-
};
186+
getParentFolderName() {
122187

123-
// https://msdn.microsoft.com/en-us/library/y6hbz9es(v=vs.84).aspx
124-
ScriptingFSO.prototype.GetStandardStream = function() {
188+
}
125189

126-
};
190+
getSpecialFolder() {
127191

128-
// https://msdn.microsoft.com/en-us/library/w0azsy9b(v=vs.84).aspx
129-
ScriptingFSO.prototype.GetTempName = function() {
192+
}
130193

131-
};
194+
getStandardStream() {
132195

133-
// https://msdn.microsoft.com/en-us/library/2wcf3ba6(v=vs.84).aspx
134-
ScriptingFSO.prototype.MoveFile = function() {
196+
}
135197

136-
};
198+
getTempName() {
137199

138-
// https://msdn.microsoft.com/en-us/library/465s5y8s(v=vs.84).aspx
139-
ScriptingFSO.prototype.MoveFolder = function() {
200+
}
140201

141-
};
202+
moveFile() {
142203

143-
// https://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx
144-
ScriptingFSO.prototype.OpenTextFile = function() {
204+
}
145205

146-
};
206+
moveFolder() {
207+
208+
}
209+
210+
openTextFile() {
211+
212+
}
213+
}
147214

148215
module.exports = VFS;

0 commit comments

Comments
 (0)