Implemented unlink and changed implementation of error handler

pull/4/head
ioedeveloper 5 years ago
parent 9c830a11e5
commit de2a467c9d
  1. 58
      src/app/files/fileManager.js
  2. 18
      src/app/files/fileProvider.js

@ -26,6 +26,16 @@ const profile = {
methods: ['file', 'exists', 'open', 'writeFile', 'readFile', 'copyFile', 'unlink', 'rename', 'readdir', 'rmdir'], methods: ['file', 'exists', 'open', 'writeFile', 'readFile', 'copyFile', 'unlink', 'rename', 'readdir', 'rmdir'],
kind: 'file-system' 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 || ''}`)
}
// File System profile // File System profile
// - methods: ['getFolder', 'getCurrentFile', 'getFile', 'setFile', 'switchFile'] // - methods: ['getFolder', 'getCurrentFile', 'getFile', 'setFile', 'switchFile']
@ -49,7 +59,7 @@ class FileManager extends Plugin {
*/ */
_handleExists (path, message) { _handleExists (path, message) {
if (!this.exists(path)) { if (!this.exists(path)) {
this._handleError({ code: 'ENOENT', message }) throw createError({ code: 'ENOENT', message })
} }
} }
@ -60,7 +70,7 @@ class FileManager extends Plugin {
*/ */
_handleIsFile (path, message) { _handleIsFile (path, message) {
if (!this.isFile(path)) { if (!this.isFile(path)) {
this._handleError({ code: 'EISDIR', message }) throw createError({ code: 'EISDIR', message })
} }
} }
@ -71,45 +81,16 @@ class FileManager extends Plugin {
*/ */
_handleIsDir (path, message) { _handleIsDir (path, message) {
if (this.isFile(path)) { if (this.isFile(path)) {
throw new Error({ code: 'ENOTDIR', message }) throw createError({ code: 'ENOTDIR', message })
}
}
/**
* Emits error based on error code
* @param {object} error error { code, message }
*/
_handleError (error) {
const message = error.message ? `: ${error.message}` : ''
if (error.code === 'ENOENT') {
throw new Error('No such file or directory' + message)
}
if (error.code === 'EISDIR') {
throw new Error('Path is a directory' + message)
} }
if (error.code === 'ENOTDIR') {
throw new Error('Path is not on a directory' + message)
}
if (error.code === 'EEXIST') {
throw new Error('File already exists' + message)
}
if (error.code === 'EPERM' || error.code === 'EACCESS') {
throw new Error('Permission denied' + message)
}
return error
} }
/** The current opened file */ /** The current opened file */
file () { file () {
const file = this.currentFile() const file = this.currentFile()
if (!file) this._handleError({ code: 'ENOENT', message: 'No file selected' })
if (!file) throw createError({ code: 'ENOENT', message: 'No file selected' })
return file return file
} }
@ -194,6 +175,9 @@ class FileManager extends Plugin {
unlink (path) { unlink (path) {
this._handleExists(path, `Cannot remove file ${path}`) this._handleExists(path, `Cannot remove file ${path}`)
this._handleIsDir(path, `Cannot remove file ${path}`) this._handleIsDir(path, `Cannot remove file ${path}`)
const provider = this.fileProviderOf(path)
provider.removeFile(path)
} }
/** /**
@ -216,7 +200,7 @@ class FileManager extends Plugin {
*/ */
mkdir (path) { mkdir (path) {
if (this.exists(path)) { if (this.exists(path)) {
this._handleError({ code: 'EEXIST', message: `Cannot create directory ${path}` }) throw createError({ code: 'EEXIST', message: `Cannot create directory ${path}` })
} }
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
@ -347,7 +331,7 @@ class FileManager extends Plugin {
getFile (path) { getFile (path) {
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
if (!provider) this._handleError({ code: 'ENOENT', message: `${path} not available` }) if (!provider) throw createError({ code: 'ENOENT', message: `${path} not available` })
// TODO: change provider to Promise // TODO: change provider to Promise
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.currentFile() === path) return resolve(this.editor.currentContent()) if (this.currentFile() === path) return resolve(this.editor.currentContent())
@ -382,7 +366,7 @@ class FileManager extends Plugin {
_setFileInternal (path, content) { _setFileInternal (path, content) {
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
if (!provider) this._handleError({ code: 'ENOENT', message: `${path} not available` }) if (!provider) throw createError({ code: 'ENOENT', message: `${path} not available` })
// TODO : Add permission // TODO : Add permission
// TODO : Change Provider to Promise // TODO : Change Provider to Promise
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

@ -139,10 +139,12 @@ class FileProvider {
} }
isDirectory (path) { isDirectory (path) {
path = this.removePrefix(path)
return window.remixFileSystem.statSync(path).isDirectory() return window.remixFileSystem.statSync(path).isDirectory()
} }
isFile (path) { isFile (path) {
path = this.removePrefix(path)
return window.remixFileSystem.statSync(path).isFile() return window.remixFileSystem.statSync(path).isFile()
} }
@ -156,9 +158,7 @@ class FileProvider {
const stat = window.remixFileSystem.statSync(path) const stat = window.remixFileSystem.statSync(path)
try { try {
if (!stat.isDirectory()) { if (!stat.isDirectory()) {
window.remixFileSystem.unlinkSync(path, console.log) return this.removeFile(path)
this.event.trigger('fileRemoved', [this._normalizePath(path)])
return true
} else { } else {
const items = window.remixFileSystem.readdirSync(path) const items = window.remixFileSystem.readdirSync(path)
if (items.length !== 0) { if (items.length !== 0) {
@ -167,8 +167,7 @@ class FileProvider {
if (window.remixFileSystem.statSync(curPath).isDirectory()) { // delete folder if (window.remixFileSystem.statSync(curPath).isDirectory()) { // delete folder
this.remove(curPath) this.remove(curPath)
} else { // delete file } else { // delete file
window.remixFileSystem.unlinkSync(curPath, console.log) this.removeFile(curPath)
this.event.trigger('fileRemoved', [this._normalizePath(path)])
} }
}) })
if (window.remixFileSystem.readdirSync(path).length === 0) window.remixFileSystem.rmdirSync(path, console.log) if (window.remixFileSystem.readdirSync(path).length === 0) window.remixFileSystem.rmdirSync(path, console.log)
@ -185,6 +184,15 @@ class FileProvider {
return true return true
} }
removeFile (path) {
path = this.removePrefix(path)
if (window.remixFileSystem.existsSync(path) && !window.remixFileSystem.statSync(path).isDirectory()) {
window.remixFileSystem.unlinkSync(path, console.log)
this.event.trigger('fileRemoved', [this._normalizePath(path)])
return true
} else return false
}
rename (oldPath, newPath, isFolder) { rename (oldPath, newPath, isFolder) {
var unprefixedoldPath = this.removePrefix(oldPath) var unprefixedoldPath = this.removePrefix(oldPath)
var unprefixednewPath = this.removePrefix(newPath) var unprefixednewPath = this.removePrefix(newPath)

Loading…
Cancel
Save