|
|
|
@ -102,10 +102,14 @@ class FileManager extends Plugin { |
|
|
|
|
|
|
|
|
|
/** The current opened file */ |
|
|
|
|
file () { |
|
|
|
|
try { |
|
|
|
|
const file = this.currentFile() |
|
|
|
|
|
|
|
|
|
if (!file) throw createError({ code: 'ENOENT', message: 'No file selected' }) |
|
|
|
|
return file |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -114,6 +118,7 @@ class FileManager extends Plugin { |
|
|
|
|
* @returns {boolean} true if the path exists |
|
|
|
|
*/ |
|
|
|
|
exists (path) { |
|
|
|
|
try { |
|
|
|
|
path = this.limitPluginScope(path) |
|
|
|
|
const provider = this.fileProviderOf(path) |
|
|
|
|
const result = provider.exists(path, (err, result) => { |
|
|
|
@ -122,6 +127,9 @@ class FileManager extends Plugin { |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return result |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -154,10 +162,14 @@ class FileManager extends Plugin { |
|
|
|
|
* @returns {void} |
|
|
|
|
*/ |
|
|
|
|
async open (path) { |
|
|
|
|
try { |
|
|
|
|
path = this.limitPluginScope(path) |
|
|
|
|
await this._handleExists(path, `Cannot open file ${path}`) |
|
|
|
|
await this._handleIsFile(path, `Cannot open file ${path}`) |
|
|
|
|
return this.openFile(path) |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -167,6 +179,7 @@ class FileManager extends Plugin { |
|
|
|
|
* @returns {void} |
|
|
|
|
*/ |
|
|
|
|
async writeFile (path, data) { |
|
|
|
|
try { |
|
|
|
|
path = this.limitPluginScope(path) |
|
|
|
|
if (await this.exists(path)) { |
|
|
|
|
await this._handleIsFile(path, `Cannot write file ${path}`) |
|
|
|
@ -176,6 +189,9 @@ class FileManager extends Plugin { |
|
|
|
|
this.emit('fileAdded', path) |
|
|
|
|
return ret |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -184,10 +200,14 @@ class FileManager extends Plugin { |
|
|
|
|
* @returns {string} content of the file |
|
|
|
|
*/ |
|
|
|
|
async readFile (path) { |
|
|
|
|
try { |
|
|
|
|
path = this.limitPluginScope(path) |
|
|
|
|
await this._handleExists(path, `Cannot read file ${path}`) |
|
|
|
|
await this._handleIsFile(path, `Cannot read file ${path}`) |
|
|
|
|
return this.getFileContent(path) |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -197,6 +217,7 @@ class FileManager extends Plugin { |
|
|
|
|
* @returns {void} |
|
|
|
|
*/ |
|
|
|
|
async copyFile (src, dest) { |
|
|
|
|
try { |
|
|
|
|
src = this.limitPluginScope(src) |
|
|
|
|
dest = this.limitPluginScope(dest) |
|
|
|
|
await this._handleExists(src, `Cannot copy from ${src}`) |
|
|
|
@ -205,6 +226,9 @@ class FileManager extends Plugin { |
|
|
|
|
const content = await this.readFile(src) |
|
|
|
|
|
|
|
|
|
await this.writeFile(dest, content) |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -214,6 +238,7 @@ class FileManager extends Plugin { |
|
|
|
|
* @returns {void} |
|
|
|
|
*/ |
|
|
|
|
async rename (oldPath, newPath) { |
|
|
|
|
try { |
|
|
|
|
oldPath = this.limitPluginScope(oldPath) |
|
|
|
|
newPath = this.limitPluginScope(newPath) |
|
|
|
|
await this._handleExists(oldPath, `Cannot rename ${oldPath}`) |
|
|
|
@ -234,6 +259,9 @@ class FileManager extends Plugin { |
|
|
|
|
} |
|
|
|
|
return provider.rename(oldPath, newPath, true) |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -242,13 +270,17 @@ class FileManager extends Plugin { |
|
|
|
|
* @returns {void} |
|
|
|
|
*/ |
|
|
|
|
async mkdir (path) { |
|
|
|
|
try { |
|
|
|
|
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) |
|
|
|
|
return provider.createDir(path) |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -257,6 +289,7 @@ class FileManager extends Plugin { |
|
|
|
|
* @returns {string[]} list of the file/directory name in this directory |
|
|
|
|
*/ |
|
|
|
|
async readdir (path) { |
|
|
|
|
try { |
|
|
|
|
path = this.limitPluginScope(path) |
|
|
|
|
await this._handleExists(path) |
|
|
|
|
await this._handleIsDir(path) |
|
|
|
@ -269,6 +302,9 @@ class FileManager extends Plugin { |
|
|
|
|
resolve(filesProvider) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -277,11 +313,15 @@ class FileManager extends Plugin { |
|
|
|
|
* @returns {void} |
|
|
|
|
*/ |
|
|
|
|
async remove (path) { |
|
|
|
|
try { |
|
|
|
|
path = this.limitPluginScope(path) |
|
|
|
|
await this._handleExists(path, `Cannot remove file or directory ${path}`) |
|
|
|
|
const provider = this.fileProviderOf(path) |
|
|
|
|
|
|
|
|
|
return await provider.remove(path) |
|
|
|
|
} catch (e) { |
|
|
|
|
throw new Error(e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
init () { |
|
|
|
|