-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfileManager.js
More file actions
615 lines (550 loc) · 18.9 KB
/
Copy pathfileManager.js
File metadata and controls
615 lines (550 loc) · 18.9 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
'use strict'
import yo from 'yo-yo'
import async from 'async'
import { Plugin } from '@remixproject/engine'
import * as packageJson from '../../../../../package.json'
const EventEmitter = require('events')
const globalRegistry = require('../../global/registry')
const toaster = require('../ui/tooltip')
const modalDialogCustom = require('../ui/modal-dialog-custom')
const helper = require('../../lib/helper.js')
/*
attach to files event (removed renamed)
trigger: currentFileChanged
*/
const profile = {
name: 'fileManager',
displayName: 'File manager',
description: 'Service - read/write to any files or folders, require giving permissions',
icon: 'assets/img/fileManager.webp',
permission: true,
version: packageJson.version,
methods: ['file', 'exists', 'open', 'writeFile', 'readFile', 'copyFile', 'rename', 'mkdir', 'readdir', 'remove', 'getCurrentFile', 'getFile', 'getFolder', 'setFile', 'switchFile'],
kind: 'file-system'
}
const errorMsg = {
ENOENT: 'No such file or directory',
EISDIR: 'Path is a directory',
ENOTDIR: 'Path is not on a directory',
EEXIST: 'File already exists',
EPERM: 'Permission denied'
}
const createError = (err) => {
return new Error(`${errorMsg[err.code]} ${err.message || ''}`)
}
class FileManager extends Plugin {
constructor (editor, appManager) {
super(profile)
this.mode = 'browser'
this.openedFiles = {} // list all opened files
this.events = new EventEmitter()
this.editor = editor
this._components = {}
this._components.registry = globalRegistry
this.appManager = appManager
this.init()
}
getOpenedFiles () {
return this.openedFiles
}
setMode (mode) {
this.mode = mode
}
limitPluginScope (path) {
return path.replace(/^\/browser\//, '').replace(/^browser\//, '') // forbids plugin to access the root filesystem
}
/**
* Emit error if path doesn't exist
* @param {string} path path of the file/directory
* @param {string} message message to display if path doesn't exist.
*/
async _handleExists (path, message) {
const exists = await this.exists(path)
if (!exists) {
throw createError({ code: 'ENOENT', message })
}
}
/**
* Emit error if path is not a file
* @param {string} path path of the file/directory
* @param {string} message message to display if path is not a file.
*/
async _handleIsFile (path, message) {
const isFile = await this.isFile(path)
if (!isFile) {
throw createError({ code: 'EISDIR', message })
}
}
/**
* Emit error if path is not a directory
* @param {string} path path of the file/directory
* @param {string} message message to display if path is not a directory.
*/
async _handleIsDir (path, message) {
const isDir = await this.isDirectory(path)
if (!isDir) {
throw createError({ code: 'ENOTDIR', message })
}
}
/** The current opened file */
file () {
const file = this.currentFile()
if (!file) throw createError({ code: 'ENOENT', message: 'No file selected' })
return file
}
/**
* Verify if the path exists (directory or file)
* @param {string} path path of the directory or file
* @returns {boolean} true if the path exists
*/
exists (path) {
path = this.limitPluginScope(path)
const provider = this.fileProviderOf(path)
const result = provider.exists(path, (err, result) => {
if (err) return false
return result
})
return result
}
/**
* Verify if the path provided is a file
* @param {string} path path of the directory or file
* @returns {boolean} true if path is a file.
*/
isFile (path) {
const provider = this.fileProviderOf(path)
const result = provider.isFile(path)
return result
}
/**
* Verify if the path provided is a directory
* @param {string} path path of the directory
* @returns {boolean} true if path is a directory.
*/
isDirectory (path) {
const provider = this.fileProviderOf(path)
const result = provider.isDirectory(path)
return result
}
/**
* Open the content of the file in the context (eg: Editor)
* @param {string} path path of the file
* @returns {void}
*/
async open (path) {
path = this.limitPluginScope(path)
await this._handleExists(path, `Cannot open file ${path}`)
await this._handleIsFile(path, `Cannot open file ${path}`)
return this.openFile(path)
}
/**
* Set the content of a specific file
* @param {string} path path of the file
* @param {string} data content to write on the file
* @returns {void}
*/
async writeFile (path, data) {
path = this.limitPluginScope(path)
if (await this.exists(path)) {
await this._handleIsFile(path, `Cannot write file ${path}`)
return await this.setFileContent(path, data)
} else {
const ret = await this.setFileContent(path, data)
this.emit('fileAdded', path)
return ret
}
}
/**
* Return the content of a specific file
* @param {string} path path of the file
* @returns {string} content of the file
*/
async readFile (path) {
path = this.limitPluginScope(path)
await this._handleExists(path, `Cannot read file ${path}`)
await this._handleIsFile(path, `Cannot read file ${path}`)
return this.getFileContent(path)
}
/**
* Upsert a file with the content of the source file
* @param {string} src path of the source file
* @param {string} dest path of the destrination file
* @returns {void}
*/
async copyFile (src, dest) {
src = this.limitPluginScope(src)
dest = this.limitPluginScope(dest)
await this._handleExists(src, `Cannot copy from ${src}`)
await this._handleIsFile(src, `Cannot copy from ${src}`)
await this._handleIsFile(dest, `Cannot paste content into ${dest}`)
const content = await this.readFile(src)
await this.writeFile(dest, content)
}
/**
* Change the path of a file/directory
* @param {string} oldPath current path of the file/directory
* @param {string} newPath new path of the file/directory
* @returns {void}
*/
async rename (oldPath, newPath) {
oldPath = this.limitPluginScope(oldPath)
newPath = this.limitPluginScope(newPath)
await this._handleExists(oldPath, `Cannot rename ${oldPath}`)
const isFile = await this.isFile(oldPath)
const newPathExists = await this.exists(newPath)
const provider = this.fileProviderOf(oldPath)
if (isFile) {
if (newPathExists) {
modalDialogCustom.alert('File already exists.')
return
}
return provider.rename(oldPath, newPath, false)
} else {
if (newPathExists) {
modalDialogCustom.alert('Folder already exists.')
return
}
return provider.rename(oldPath, newPath, true)
}
}
/**
* Create a directory
* @param {string} path path of the new directory
* @returns {void}
*/
async mkdir (path) {
path = this.limitPluginScope(path)
if (await this.exists(path)) {
throw createError({ code: 'EEXIST', message: `Cannot create directory ${path}` })
}
const provider = this.fileProviderOf(path)
provider.createDir(path)
}
/**
* Get the list of files in the directory
* @param {string} path path of the directory
* @returns {string[]} list of the file/directory name in this directory
*/
async readdir (path) {
path = this.limitPluginScope(path)
await this._handleExists(path)
await this._handleIsDir(path)
return new Promise((resolve, reject) => {
const provider = this.fileProviderOf(path)
provider.resolveDirectory(path, (error, filesProvider) => {
if (error) reject(error)
resolve(filesProvider)
})
})
}
/**
* Removes a file or directory recursively
* @param {string} path path of the directory/file to remove
* @returns {void}
*/
async remove (path) {
path = this.limitPluginScope(path)
await this._handleExists(path, `Cannot remove file or directory ${path}`)
const provider = this.fileProviderOf(path)
return await provider.remove(path)
}
init () {
this._deps = {
config: this._components.registry.get('config').api,
browserExplorer: this._components.registry.get('fileproviders/browser').api,
localhostExplorer: this._components.registry.get('fileproviders/localhost').api,
workspaceExplorer: this._components.registry.get('fileproviders/workspace').api,
filesProviders: this._components.registry.get('fileproviders').api
}
this._deps.browserExplorer.event.register('fileChanged', (path) => { this.fileChangedEvent(path) })
this._deps.browserExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this._deps.localhostExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this._deps.browserExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
this._deps.browserExplorer.event.register('fileAdded', (path) => { this.fileAddedEvent(path) })
this._deps.localhostExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
this._deps.localhostExplorer.event.register('errored', (event) => { this.removeTabsOf(this._deps.localhostExplorer) })
this._deps.localhostExplorer.event.register('closed', (event) => { this.removeTabsOf(this._deps.localhostExplorer) })
this._deps.workspaceExplorer.event.register('fileChanged', (path) => { this.fileChangedEvent(path) })
this._deps.workspaceExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this._deps.workspaceExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
this._deps.workspaceExplorer.event.register('fileAdded', (path) => { this.fileAddedEvent(path) })
this.getCurrentFile = this.file
this.getFile = this.readFile
this.getFolder = this.readdir
this.setFile = this.writeFile
this.switchFile = this.open
}
fileAddedEvent (path) {
this.emit('fileAdded', path)
}
fileChangedEvent (path) {
this.emit('currentFileChanged', path)
}
fileRenamedEvent (oldName, newName, isFolder) {
if (!isFolder) {
this._deps.config.set('currentFile', '')
this.editor.discard(oldName)
if (this.openedFiles[oldName]) {
delete this.openedFiles[oldName]
this.openedFiles[newName] = newName
}
this.openFile(newName)
} else {
for (var k in this.openedFiles) {
if (k.indexOf(oldName + '/') === 0) {
var newAbsolutePath = k.replace(oldName, newName)
this.openedFiles[newAbsolutePath] = newAbsolutePath
delete this.openedFiles[k]
if (this._deps.config.get('currentFile') === k) {
this._deps.config.set('currentFile', '')
}
}
}
}
// TODO: Only keep `this.emit` (issue#2210)
this.emit('fileRenamed', oldName, newName, isFolder)
this.events.emit('fileRenamed', oldName, newName, isFolder)
}
currentFileProvider () {
var path = this.currentPath()
if (path) {
return this.fileProviderOf(path)
}
return null
}
currentFile () {
return this._deps.config.get('currentFile')
}
closeFile (name) {
delete this.openedFiles[name]
if (!Object.keys(this.openedFiles).length) {
this._deps.config.set('currentFile', '')
// TODO: Only keep `this.emit` (issue#2210)
this.emit('noFileSelected')
this.events.emit('noFileSelected')
}
// TODO: Only keep `this.emit` (issue#2210)
this.emit('fileClosed', name)
this.events.emit('fileClosed', name)
}
currentPath () {
var currentFile = this._deps.config.get('currentFile')
return this.extractPathOf(currentFile)
}
extractPathOf (file) {
var reg = /(.*)(\/).*/
var path = reg.exec(file)
return path ? path[1] : '/'
}
getFileContent (path) {
const provider = this.fileProviderOf(path)
if (!provider) throw createError({ code: 'ENOENT', message: `${path} not available` })
// TODO: change provider to Promise
return new Promise((resolve, reject) => {
if (this.currentFile() === path) return resolve(this.editor.currentContent())
provider.get(path, (err, content) => {
if (err) reject(err)
resolve(content)
})
})
}
async setFileContent (path, content) {
if (this.currentRequest) {
const canCall = await this.askUserPermission('writeFile', '')
if (canCall) {
// inform the user about modification after permission is granted and even if permission was saved before
toaster(yo`
<div>
<i class="fas fa-exclamation-triangle text-danger mr-1"></i>
<span>
${this.currentRequest.from}
<span class="font-weight-bold text-warning">
is modifying
</span>${path}
</span>
</div>
`, '', { time: 3000 })
}
}
return await this._setFileInternal(path, content)
}
_setFileInternal (path, content) {
const provider = this.fileProviderOf(path)
if (!provider) throw createError({ code: 'ENOENT', message: `${path} not available` })
// TODO : Add permission
// TODO : Change Provider to Promise
return new Promise((resolve, reject) => {
provider.set(path, content, (error) => {
if (error) reject(error)
this.syncEditor(path)
this.emit('fileSaved', path)
resolve(true)
})
})
}
_saveAsCopy (path, content) {
const fileProvider = this.fileProviderOf(path)
if (fileProvider) {
helper.createNonClashingNameWithPrefix(path, fileProvider, '', (error, copyName) => {
if (error) {
copyName = path + '.' + this.currentRequest.from
}
this._setFileInternal(copyName, content)
this.openFile(copyName)
})
}
}
removeTabsOf (provider) {
for (var tab in this.openedFiles) {
if (this.fileProviderOf(tab).type === provider.type) {
this.fileRemovedEvent(tab)
}
}
}
fileRemovedEvent (path) {
if (path === this._deps.config.get('currentFile')) {
this._deps.config.set('currentFile', '')
}
this.editor.discard(path)
delete this.openedFiles[path]
// TODO: Only keep `this.emit` (issue#2210)
this.emit('fileRemoved', path)
this.events.emit('fileRemoved', path)
this.openFile()
}
unselectCurrentFile () {
this.saveCurrentFile()
this._deps.config.set('currentFile', '')
// TODO: Only keep `this.emit` (issue#2210)
this.emit('noFileSelected')
this.events.emit('noFileSelected')
}
openFile (file) {
const _openFile = (file) => {
this.saveCurrentFile()
const provider = this.fileProviderOf(file)
if (!provider) return console.error(`no provider for ${file}`)
file = provider.getPathFromUrl(file) || file // in case an external URL is given as input, we resolve it to the right internal path
this._deps.config.set('currentFile', file)
this.openedFiles[file] = file
provider.get(file, (error, content) => {
if (error) {
console.log(error)
} else {
if (provider.isReadOnly(file)) {
this.editor.openReadOnly(file, content)
} else {
this.editor.open(file, content)
}
// TODO: Only keep `this.emit` (issue#2210)
this.emit('currentFileChanged', file)
this.events.emit('currentFileChanged', file)
}
})
}
if (file) return _openFile(file)
else {
this.emit('noFileSelected')
this.events.emit('noFileSelected')
}
}
getProvider (name) {
return this._deps.filesProviders[name]
}
fileProviderOf (file) {
if (file.startsWith('localhost') || this.mode === 'localhost') {
return this._deps.filesProviders.localhost
}
if (file.startsWith('browser')) {
return this._deps.filesProviders.browser
}
const provider = this._deps.filesProviders.workspace
if (!provider.isReady()) throw createError({ code: 'ECONNRESET', message: 'No workspace has been opened.' })
return this._deps.filesProviders.workspace
}
// returns the list of directories inside path
dirList (path) {
const dirPaths = []
const collectList = (path) => {
return new Promise((resolve, reject) => {
this.readdir(path).then((ls) => {
const promises = Object.keys(ls).map((item, index) => {
if (ls[item].isDirectory && !dirPaths.includes(item)) {
dirPaths.push(item)
resolve(dirPaths)
}
return new Promise((resolve, reject) => { resolve() })
})
Promise.all(promises).then(() => { resolve(dirPaths) })
})
})
}
return collectList(path)
}
isRemixDActive () {
return this.appManager.isActive('remixd')
}
saveCurrentFile () {
var currentFile = this._deps.config.get('currentFile')
if (currentFile && this.editor.current()) {
var input = this.editor.get(currentFile)
if ((input !== null) && (input !== undefined)) {
var provider = this.fileProviderOf(currentFile)
if (provider) {
provider.set(currentFile, input)
this.emit('fileSaved', currentFile)
} else {
console.log('cannot save ' + currentFile + '. Does not belong to any explorer')
}
}
}
}
syncEditor (path) {
var currentFile = this._deps.config.get('currentFile')
if (path !== currentFile) return
var provider = this.fileProviderOf(currentFile)
if (provider) {
provider.get(currentFile, (error, content) => {
if (error) console.log(error)
this.editor.setText(content)
})
} else {
console.log('cannot save ' + currentFile + '. Does not belong to any explorer')
}
}
setBatchFiles (filesSet, fileProvider, override, callback) {
const self = this
if (!fileProvider) fileProvider = 'browser'
if (override === undefined) override = false
async.each(Object.keys(filesSet), (file, callback) => {
if (override) {
try {
self._deps.filesProviders[fileProvider].set(file, filesSet[file].content)
} catch (e) {
return callback(e.message || e)
}
self.syncEditor(fileProvider + file)
return callback()
}
helper.createNonClashingName(file, self._deps.filesProviders[fileProvider],
(error, name) => {
if (error) {
modalDialogCustom.alert('Unexpected error loading the file ' + error)
} else if (helper.checkSpecialChars(name)) {
modalDialogCustom.alert('Special characters are not allowed')
} else {
try {
self._deps.filesProviders[fileProvider].set(name, filesSet[file].content)
} catch (e) {
return callback(e.message || e)
}
self.syncEditor(fileProvider + name)
}
callback()
})
}, (error) => {
if (callback) callback(error)
})
}
}
module.exports = FileManager