forked from iann0036/cloud9-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileSystemProvider.js
203 lines (203 loc) · 8.32 KB
/
fileSystemProvider.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var vscode = require("vscode");
var text_encoding_1 = require("text-encoding");
var File = /** @class */ (function () {
function File(name) {
this.type = vscode.FileType.File;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
}
return File;
}());
exports.File = File;
var Directory = /** @class */ (function () {
function Directory(name) {
this.type = vscode.FileType.Directory;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
this.entries = new Map();
}
return Directory;
}());
exports.Directory = Directory;
var Cloud9FileSystemProvider = /** @class */ (function () {
function Cloud9FileSystemProvider(fileManager, eventEmitter) {
this.fileManager = fileManager;
this.eventEmitter = eventEmitter;
this.root = new Directory('');
this.environmentConnections = {};
// --- manage file events
this._emitter = new vscode.EventEmitter();
this._bufferedEvents = [];
this.onDidChangeFile = this._emitter.event;
//this.createDirectory(vscode.Uri.parse(`cloud9:/123/`));
}
// --- manage file metadata
Cloud9FileSystemProvider.prototype._getEnvConnection = function (id) {
var _this = this;
return new Promise(function (resolve, reject) {
if (id in _this.environmentConnections) {
if (_this.environmentConnections[id].status == "connected") {
resolve(id);
return;
}
}
else {
_this.environmentConnections[id] = {
'status': 'connecting'
};
_this.eventEmitter.emit("request_connect", {
id: id
});
}
_this.eventEmitter.once('websocket_init_complete', function () {
_this.environmentConnections[id] = {
'status': 'connected'
};
resolve(id);
});
});
};
Cloud9FileSystemProvider.prototype._c9stattovsstat = function (stat) {
var entry;
if (stat['mime'] == "inode/directory") {
entry = new Directory(stat.name);
}
else {
entry = new File(stat.name);
}
entry.size = stat.size;
entry.ctime = stat.ctime;
entry.mtime = stat.mtime;
return entry;
};
Cloud9FileSystemProvider.prototype.stat = function (uri) {
var _this = this;
return new Promise(function (resolve, reject) {
var splituri = uri.path.split("/");
var environmentId = splituri[1];
_this._getEnvConnection(environmentId).then(function () {
_this.fileManager.stat(splituri.slice(2).join('/')).then(function (stats) {
resolve(_this._c9stattovsstat(stats));
}).catch(function (err) {
reject(err);
});
});
});
//reject(vscode.FileSystemError.FileNotFound());
};
Cloud9FileSystemProvider.prototype.readDirectory = function (uri) {
var _this = this;
return new Promise(function (resolve, reject) {
var splituri = uri.path.split("/");
var environmentId = splituri[1];
_this._getEnvConnection(environmentId).then(function () {
_this.fileManager.listdir(splituri.slice(2).join('/')).then(function (stats) {
var converted_stats = [];
stats.forEach(function (stat) {
var converted_stat = [stat['name'], vscode.FileType.File];
if (stat['mime'] == "inode/directory") {
converted_stat[1] = vscode.FileType.Directory;
}
converted_stats.push(converted_stat);
});
resolve(converted_stats);
});
});
});
};
// --- manage file contents
Cloud9FileSystemProvider.prototype.readFile = function (uri) {
var _this = this;
return new Promise(function (resolve, reject) {
var splituri = uri.path.split("/");
var environmentId = splituri[1];
_this._getEnvConnection(environmentId).then(function () {
_this.fileManager.downloadFile("/" + splituri.slice(2).join('/'), null, null).then(function (body) {
var uint8 = new text_encoding_1.TextEncoder().encode(body);
resolve(uint8);
});
});
});
};
Cloud9FileSystemProvider.prototype.writeFile = function (uri, content, options) {
var _this = this;
return new Promise(function (resolve, reject) {
var splituri = uri.path.split("/");
var environmentId = splituri[1];
_this._getEnvConnection(environmentId).then(function () {
if (options.create) {
_this.fileManager.uploadRemoteFile("/" + splituri.slice(2).join('/'), content.toString()).then(function () {
resolve();
_this._fireSoon({ type: vscode.FileChangeType.Changed, uri: uri });
return;
});
}
else if (options.overwrite) {
_this.fileManager.uploadExistingFile("/" + splituri.slice(2).join('/'), content.toString()).then(function () {
resolve();
_this._fireSoon({ type: vscode.FileChangeType.Changed, uri: uri });
return;
});
}
});
});
};
// --- manage files/folders
Cloud9FileSystemProvider.prototype.rename = function (oldUri, newUri, options) {
var oldsplituri = oldUri.path.split("/");
var newsplituri = newUri.path.split("/");
this.eventEmitter.emit("send_ch4_message", ["rename", "/" + newsplituri.slice(2).join('/'), { "from": "/" + oldsplituri.slice(2).join('/') }, { "$": 92 }]);
this._fireSoon({ type: vscode.FileChangeType.Deleted, uri: oldUri }, { type: vscode.FileChangeType.Created, uri: newUri });
};
Cloud9FileSystemProvider.prototype.delete = function (uri) {
var _this = this;
return new Promise(function (resolve, reject) {
var splituri = uri.path.split("/");
var environmentId = splituri[1];
_this._getEnvConnection(environmentId).then(function () {
_this.fileManager.deleteRemoteFile("/" + splituri.slice(2).join('/')).then(function () {
resolve();
_this._fireSoon({ type: vscode.FileChangeType.Changed, uri: uri }, { uri: uri, type: vscode.FileChangeType.Deleted });
return;
});
});
});
};
Cloud9FileSystemProvider.prototype.createDirectory = function (uri) {
var _this = this;
return new Promise(function (resolve, reject) {
var splituri = uri.path.split("/");
_this.eventEmitter.emit("send_ch4_message", ["mkdir", "/" + splituri.slice(2).join('/'), {}, { "$": 93 }]);
setTimeout(function () {
_this._fireSoon({ type: vscode.FileChangeType.Changed, uri: uri }, { type: vscode.FileChangeType.Created, uri: uri });
resolve();
}, 200);
});
};
Cloud9FileSystemProvider.prototype.watch = function (resource, opts) {
// ignore, fires for all changes...
return new vscode.Disposable(function () { });
};
Cloud9FileSystemProvider.prototype._fireSoon = function () {
var _a;
var _this = this;
var events = [];
for (var _i = 0; _i < arguments.length; _i++) {
events[_i] = arguments[_i];
}
(_a = this._bufferedEvents).push.apply(_a, events);
clearTimeout(this._fireSoonHandle);
this._fireSoonHandle = setTimeout(function () {
_this._emitter.fire(_this._bufferedEvents);
_this._bufferedEvents.length = 0;
}, 5);
};
return Cloud9FileSystemProvider;
}());
exports.Cloud9FileSystemProvider = Cloud9FileSystemProvider;