|
2 | 2 |
|
3 | 3 | /**
|
4 | 4 | * Virtual file system
|
5 |
| - * Keeps track of created/copied/deleted files and folders |
| 5 | + * Keeps track of files and folders |
6 | 6 | */
|
7 | 7 |
|
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', |
14 | 21 | 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 | + } |
21 | 44 |
|
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 | + } |
24 | 53 |
|
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 = []; |
26 | 58 |
|
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 | + }); |
28 | 68 |
|
29 |
| -VFS.prototype.copyFolder = function(folder, destination) { |
| 69 | + return absPath.length === 1 ? absPath[0] + '\\' : absPath.join('\\'); |
| 70 | + } |
30 | 71 |
|
31 |
| -}; |
| 72 | + _resolvePath(path, type) { |
| 73 | + path = type !== 'drive' ? this._formatPath(path) : path; |
32 | 74 |
|
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 | + } |
35 | 81 |
|
| 82 | + return entry.path === path; |
| 83 | + })[0]; |
| 84 | + } |
36 | 85 |
|
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 | + } |
38 | 102 |
|
39 |
| -ScriptingFSO.prototype.CreateTextFile = function() { |
| 103 | + this._vfs.push(this._createFileObject(destination, resolvedFile.content)); |
| 104 | + } |
40 | 105 |
|
41 |
| -}; |
| 106 | + copyFolder(folder, destination) { |
42 | 107 |
|
43 |
| -// https://msdn.microsoft.com/en-us/library/thx0f315(v=vs.84).aspx |
44 |
| -ScriptingFSO.prototype.DeleteFile = function() { |
| 108 | + } |
45 | 109 |
|
46 |
| -}; |
| 110 | + createFolder(path) { |
47 | 111 |
|
48 |
| -// https://msdn.microsoft.com/en-us/library/ca0at0xh(v=vs.84).aspx |
49 |
| -ScriptingFSO.prototype.DeleteFolder = function() { |
| 112 | + } |
50 | 113 |
|
51 |
| -}; |
| 114 | + createTextFile() { |
52 | 115 |
|
53 |
| -// https://msdn.microsoft.com/en-us/library/t565x0f1(v=vs.84).aspx |
54 |
| -ScriptingFSO.prototype.DriveExists = function() { |
| 116 | + } |
55 | 117 |
|
56 |
| -}; |
| 118 | + deleteFile(path) { |
| 119 | + let resolvedFile = this._resolvePath(path); |
57 | 120 |
|
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 | + } |
60 | 124 |
|
61 |
| -}; |
| 125 | + this._vfs.splice(this._vfs.indexOf(resolvedFile), 1); |
| 126 | + } |
62 | 127 |
|
63 |
| -// https://msdn.microsoft.com/en-us/library/5xc78d8d(v=vs.84).aspx |
64 |
| -ScriptingFSO.prototype.FolderExists = function() { |
| 128 | + deleteFolder() { |
65 | 129 |
|
66 |
| -}; |
| 130 | + } |
67 | 131 |
|
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 | + } |
70 | 135 |
|
71 |
| -}; |
| 136 | + fileExists(path) { |
| 137 | + return this._resolvePath(path, 'file') ? -1 : 0; |
| 138 | + } |
72 | 139 |
|
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 | + } |
75 | 143 |
|
76 |
| -}; |
| 144 | + getAbsolutePathName() { |
77 | 145 |
|
78 |
| -// https://msdn.microsoft.com/en-us/library/1z6e0fk3(v=vs.84).aspx |
79 |
| -ScriptingFSO.prototype.GetDrive = function() { |
| 146 | + } |
80 | 147 |
|
81 |
| -}; |
| 148 | + getBaseName() { |
82 | 149 |
|
83 |
| -// https://msdn.microsoft.com/en-us/library/48e3yfdw(v=vs.84).aspx |
84 |
| -ScriptingFSO.prototype.GetDriveName = function() { |
| 150 | + } |
85 | 151 |
|
86 |
| -}; |
| 152 | + getDrive() { |
87 | 153 |
|
88 |
| -// https://msdn.microsoft.com/en-us/library/x0fxha2a(v=vs.84).aspx |
89 |
| -ScriptingFSO.prototype.GetExtensionName = function() { |
| 154 | + } |
90 | 155 |
|
91 |
| -}; |
| 156 | + getDriveName() { |
92 | 157 |
|
93 |
| -// https://msdn.microsoft.com/en-us/library/sheydkke(v=vs.84).aspx |
94 |
| -ScriptingFSO.prototype.GetFile = function() { |
| 158 | + } |
95 | 159 |
|
96 |
| -}; |
| 160 | + getExtensionName() { |
97 | 161 |
|
98 |
| -// https://msdn.microsoft.com/en-us/library/a99s8akf(v=vs.84).aspx |
99 |
| -ScriptingFSO.prototype.GetFileName = function() { |
| 162 | + } |
100 | 163 |
|
101 |
| -}; |
| 164 | + getFile(path) { |
| 165 | + let resolvedFile = this._resolvePath(path); |
102 | 166 |
|
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 | + } |
105 | 173 |
|
106 |
| -}; |
| 174 | + getFileName() { |
107 | 175 |
|
108 |
| -// https://msdn.microsoft.com/en-us/library/f1xtf7ta(v=vs.84).aspx |
109 |
| -ScriptingFSO.prototype.GetFolder = function() { |
| 176 | + } |
110 | 177 |
|
111 |
| -}; |
| 178 | + getFileVersion() { |
112 | 179 |
|
113 |
| -// https://msdn.microsoft.com/en-us/library/22dyy47c(v=vs.84).aspx |
114 |
| -ScriptingFSO.prototype.GetParentFolderName = function() { |
| 180 | + } |
115 | 181 |
|
116 |
| -}; |
| 182 | + getFolder() { |
117 | 183 |
|
118 |
| -// https://msdn.microsoft.com/en-us/library/a72y2t1c(v=vs.84).aspx |
119 |
| -ScriptingFSO.prototype.GetSpecialFolder = function() { |
| 184 | + } |
120 | 185 |
|
121 |
| -}; |
| 186 | + getParentFolderName() { |
122 | 187 |
|
123 |
| -// https://msdn.microsoft.com/en-us/library/y6hbz9es(v=vs.84).aspx |
124 |
| -ScriptingFSO.prototype.GetStandardStream = function() { |
| 188 | + } |
125 | 189 |
|
126 |
| -}; |
| 190 | + getSpecialFolder() { |
127 | 191 |
|
128 |
| -// https://msdn.microsoft.com/en-us/library/w0azsy9b(v=vs.84).aspx |
129 |
| -ScriptingFSO.prototype.GetTempName = function() { |
| 192 | + } |
130 | 193 |
|
131 |
| -}; |
| 194 | + getStandardStream() { |
132 | 195 |
|
133 |
| -// https://msdn.microsoft.com/en-us/library/2wcf3ba6(v=vs.84).aspx |
134 |
| -ScriptingFSO.prototype.MoveFile = function() { |
| 196 | + } |
135 | 197 |
|
136 |
| -}; |
| 198 | + getTempName() { |
137 | 199 |
|
138 |
| -// https://msdn.microsoft.com/en-us/library/465s5y8s(v=vs.84).aspx |
139 |
| -ScriptingFSO.prototype.MoveFolder = function() { |
| 200 | + } |
140 | 201 |
|
141 |
| -}; |
| 202 | + moveFile() { |
142 | 203 |
|
143 |
| -// https://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx |
144 |
| -ScriptingFSO.prototype.OpenTextFile = function() { |
| 204 | + } |
145 | 205 |
|
146 |
| -}; |
| 206 | + moveFolder() { |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + openTextFile() { |
| 211 | + |
| 212 | + } |
| 213 | +} |
147 | 214 |
|
148 | 215 | module.exports = VFS;
|
0 commit comments