-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMappedFileSystem.js
121 lines (106 loc) · 2.96 KB
/
MappedFileSystem.js
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
120
121
import DummyFsWatcher from "./DummyFsWatcher.js";
import MappedFileSystemEntry from "./Mapped/MappedFileSystemEntry.js";
/**
* @implements {import("@spyglassmc/core").ExternalFileSystem}
*/
export default class MappedFileSystem {
/** @type {MappedFileSystemEntry[]} */ fileSystems;
/**
* @param {MappedFileSystemEntry[]} fileSystems
*/
constructor(fileSystems = []) {
this.fileSystems = fileSystems;
}
/**
* @param {string} sourceBaseUri
* @param {import("@spyglassmc/core").ExternalFileSystem} fileSystem
* @param {string} targetBaseUri
* @return {this}
*/
mount(sourceBaseUri, fileSystem, targetBaseUri = 'file:///') {
this.fileSystems.push(new MappedFileSystemEntry(sourceBaseUri, targetBaseUri, fileSystem));
return this;
}
/**
* @param {string} sourceBaseUri
* @return {this}
*/
umount(sourceBaseUri) {
this.fileSystems = this.fileSystems.filter(entry => entry.sourceBaseUri !== sourceBaseUri);
return this;
}
/**
* @param location
* @return {[import("@spyglassmc/core").ExternalFileSystem, string]}
*/
findFileSystem(location) {
for (let entry of this.fileSystems) {
let mapped = entry.map(location);
if (mapped) {
return [entry.getFileSystem(), mapped];
}
}
throw new Error(`EACCES: ${location}`);
}
/**
* @inheritDoc
*/
async chmod(location, mode) {
let [fs, mappedLocation] = this.findFileSystem(location);
return await fs.chmod(mappedLocation, mode);
}
/**
* @inheritDoc
*/
async mkdir(location, _options) {
let [fs, mappedLocation] = this.findFileSystem(location);
return await fs.mkdir(mappedLocation);
}
/**
* @inheritDoc
*/
async readdir(location) {
let [fs, mappedLocation] = this.findFileSystem(location);
return await fs.readdir(mappedLocation);
}
/**
* @inheritDoc
*/
async readFile(location) {
let [fs, mappedLocation] = this.findFileSystem(location);
return await fs.readFile(mappedLocation);
}
/**
* @inheritDoc
*/
async showFile(_path) {
throw new Error('showFile not supported on browser');
}
/**
* @inheritDoc
*/
async stat(location) {
let [fs, mappedLocation] = this.findFileSystem(location);
return await fs.stat(mappedLocation);
}
/**
* @inheritDoc
*/
async unlink(location) {
let [fs, mappedLocation] = this.findFileSystem(location);
return await fs.unlink(mappedLocation);
}
/**
* @inheritDoc
*/
watch(_locations) {
return new DummyFsWatcher();
}
/**
* @inheritDoc
*/
async writeFile(location, data, options) {
let [fs, mappedLocation] = this.findFileSystem(location);
return await fs.writeFile(mappedLocation, data, options);
}
}