Commit bf80ca98 authored by ioedeveloper's avatar ioedeveloper

Changed remixd set and createDir implementation, changed fileProvider's exist implementation

parent 3a1a03a8
...@@ -121,9 +121,7 @@ module.exports = class CompilerImports extends Plugin { ...@@ -121,9 +121,7 @@ module.exports = class CompilerImports extends Plugin {
if (provider.type === 'localhost' && !provider.isConnected()) { if (provider.type === 'localhost' && !provider.isConnected()) {
return reject(new Error(`file provider ${provider.type} not available while trying to resolve ${url}`)) return reject(new Error(`file provider ${provider.type} not available while trying to resolve ${url}`))
} }
provider.exists(url, (error, exist) => { provider.exists(url).then(exist => {
if (error) return reject(error)
/* /*
if the path is absolute and the file does not exist, we can stop here if the path is absolute and the file does not exist, we can stop here
Doesn't make sense to try to resolve "localhost/node_modules/localhost/node_modules/<path>" and we'll end in an infinite loop. Doesn't make sense to try to resolve "localhost/node_modules/localhost/node_modules/<path>" and we'll end in an infinite loop.
...@@ -162,6 +160,8 @@ module.exports = class CompilerImports extends Plugin { ...@@ -162,6 +160,8 @@ module.exports = class CompilerImports extends Plugin {
if (error) return reject(error) if (error) return reject(error)
resolve(content) resolve(content)
}) })
}).catch(error => {
return reject(error)
}) })
} }
}) })
......
...@@ -109,10 +109,11 @@ class ContextView { ...@@ -109,10 +109,11 @@ class ContextView {
if (filename !== this._deps.config.get('currentFile')) { if (filename !== this._deps.config.get('currentFile')) {
const provider = this._deps.fileManager.fileProviderOf(filename) const provider = this._deps.fileManager.fileProviderOf(filename)
if (provider) { if (provider) {
provider.exists(filename, (error, exist) => { provider.exists(filename).then(exist => {
if (error) return console.log(error)
this._deps.fileManager.open(filename) this._deps.fileManager.open(filename)
jumpToLine(lineColumn) jumpToLine(lineColumn)
}).catch(error => {
if (error) return console.log(error)
}) })
} }
} else { } else {
......
...@@ -121,7 +121,7 @@ class FileManager extends Plugin { ...@@ -121,7 +121,7 @@ class FileManager extends Plugin {
try { try {
path = this.limitPluginScope(path) path = this.limitPluginScope(path)
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
const result = provider.exists(path, () => {}) const result = provider.exists(path)
return result return result
} catch (e) { } catch (e) {
......
...@@ -63,11 +63,10 @@ class FileProvider { ...@@ -63,11 +63,10 @@ class FileProvider {
}) })
} }
exists (path, cb) { async exists (path) {
// todo check the type (directory/file) as well #2386 // todo check the type (directory/file) as well #2386
// currently it is not possible to have a file and folder with same path // currently it is not possible to have a file and folder with same path
const ret = this._exists(path) const ret = this._exists(path)
if (cb) cb(null, ret)
return ret return ret
} }
......
...@@ -74,16 +74,15 @@ module.exports = class RemixDProvider extends FileProvider { ...@@ -74,16 +74,15 @@ module.exports = class RemixDProvider extends FileProvider {
}) })
} }
exists (path, cb) { exists (path) {
if (!this._isReady) return cb && cb('provider not ready') if (!this._isReady) throw new Error('provider not ready')
const unprefixedpath = this.removePrefix(path) const unprefixedpath = this.removePrefix(path)
return this._appManager.call('remixd', 'exists', { path: unprefixedpath }) return this._appManager.call('remixd', 'exists', { path: unprefixedpath })
.then((result) => { .then((result) => {
if (cb) return cb(null, result)
return result return result
}).catch((error) => { })
if (cb) return cb(error) .catch((error) => {
throw new Error(error) throw new Error(error)
}) })
} }
......
...@@ -229,7 +229,7 @@ module.exports = class Filepanel extends ViewPlugin { ...@@ -229,7 +229,7 @@ module.exports = class Filepanel extends ViewPlugin {
/** these are called by the react component, action is already finished whent it's called */ /** these are called by the react component, action is already finished whent it's called */
async setWorkspace (workspace) { async setWorkspace (workspace) {
this._deps.fileManager.removeTabsOf(this._deps.fileProviders.workspace) this._deps.fileManager.closeAllFiles()
if (workspace.isLocalhost) { if (workspace.isLocalhost) {
this.call('manager', 'activatePlugin', 'remixd') this.call('manager', 'activatePlugin', 'remixd')
} else if (await this.call('manager', 'isActive', 'remixd')) { } else if (await this.call('manager', 'isActive', 'remixd')) {
......
...@@ -18,7 +18,9 @@ class TestTabLogic { ...@@ -18,7 +18,9 @@ class TestTabLogic {
// Checking to ignore the value which contains only whitespaces // Checking to ignore the value which contains only whitespaces
if (!path || !(/\S/.test(path))) return if (!path || !(/\S/.test(path))) return
const fileProvider = this.fileManager.fileProviderOf(path.split('/')[0]) const fileProvider = this.fileManager.fileProviderOf(path.split('/')[0])
fileProvider.exists(path, (e, res) => { if (!res) fileProvider.createDir(path) }) fileProvider.exists(path).then(res => {
if (!res) fileProvider.createDir(path)
})
} }
pathExists (path) { pathExists (path) {
......
...@@ -39,10 +39,11 @@ Renderer.prototype._errorClick = function (errFile, errLine, errCol) { ...@@ -39,10 +39,11 @@ Renderer.prototype._errorClick = function (errFile, errLine, errCol) {
// TODO: refactor with this._components.contextView.jumpTo // TODO: refactor with this._components.contextView.jumpTo
var provider = self._deps.fileManager.fileProviderOf(errFile) var provider = self._deps.fileManager.fileProviderOf(errFile)
if (provider) { if (provider) {
provider.exists(errFile, (error, exist) => { provider.exists(errFile).then(exist => {
if (error) return console.log(error)
self._deps.fileManager.open(errFile) self._deps.fileManager.open(errFile)
editor.gotoLine(errLine, errCol) editor.gotoLine(errLine, errCol)
}).catch(error => {
if (error) return console.log(error)
}) })
} }
} else { } else {
......
...@@ -36,14 +36,12 @@ module.exports = { ...@@ -36,14 +36,12 @@ module.exports = {
async.whilst( async.whilst(
() => { return exist }, () => { return exist },
(callback) => { (callback) => {
fileProvider.exists(name + counter + prefix + '.' + ext, (error, currentExist) => { fileProvider.exists(name + counter + prefix + '.' + ext).then(currentExist => {
if (error) { exist = currentExist
callback(error) if (exist) counter = (counter | 0) + 1
} else { callback()
exist = currentExist }).catch(error => {
if (exist) counter = (counter | 0) + 1 if (error) console.log(error)
callback()
}
}) })
}, },
(error) => { cb(error, name + counter + prefix + '.' + ext) } (error) => { cb(error, name + counter + prefix + '.' + ext) }
......
...@@ -368,8 +368,7 @@ export const FileExplorer = (props: FileExplorerProps) => { ...@@ -368,8 +368,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
const name = `${parentFolder}/${file.name}` const name = `${parentFolder}/${file.name}`
filesProvider.exists(name, (error, exist) => { filesProvider.exists(name).then(exist => {
if (error) console.log(error)
if (!exist) { if (!exist) {
loadFile(name) loadFile(name)
} else { } else {
...@@ -383,6 +382,8 @@ export const FileExplorer = (props: FileExplorerProps) => { ...@@ -383,6 +382,8 @@ export const FileExplorer = (props: FileExplorerProps) => {
fn: () => {} fn: () => {}
}) })
} }
}).catch(error => {
if (error) console.log(error)
}) })
}) })
} }
...@@ -730,6 +731,7 @@ export const FileExplorer = (props: FileExplorerProps) => { ...@@ -730,6 +731,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
const renderFiles = (file: File, index: number) => { const renderFiles = (file: File, index: number) => {
if (!file || !file.path || typeof file === 'string' || typeof file === 'number' || typeof file === 'boolean') return
const labelClass = state.focusEdit.element === file.path const labelClass = state.focusEdit.element === file.path
? 'bg-light' : state.focusElement.findIndex(item => item.key === file.path) !== -1 ? 'bg-light' : state.focusElement.findIndex(item => item.key === file.path) !== -1
? 'bg-secondary' : state.mouseOverElement === file.path ? 'bg-secondary' : state.mouseOverElement === file.path
......
...@@ -85,11 +85,10 @@ export class RemixdClient extends PluginClient { ...@@ -85,11 +85,10 @@ export class RemixdClient extends PluginClient {
} }
} }
set (args: SharedFolderArgs): Promise<void> { set (args: SharedFolderArgs) {
try { try {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.readOnly) return reject(new Error('Cannot write file: read-only mode selected')) if (this.readOnly) return reject(new Error('Cannot write file: read-only mode selected'))
const isFolder = args.path.endsWith('/')
const path = utils.absolutePath(args.path, this.currentSharedFolder) const path = utils.absolutePath(args.path, this.currentSharedFolder)
const exists = fs.existsSync(path) const exists = fs.existsSync(path)
...@@ -99,31 +98,25 @@ export class RemixdClient extends PluginClient { ...@@ -99,31 +98,25 @@ export class RemixdClient extends PluginClient {
return reject(new Error('trying to write "undefined" ! stopping.')) return reject(new Error('trying to write "undefined" ! stopping.'))
} }
this.trackDownStreamUpdate[path] = path this.trackDownStreamUpdate[path] = path
if (isFolder) { if (!exists && args.path.indexOf('/') !== -1) {
fs.mkdirp(path).then(() => { // the last element is the filename and we should remove it
let splitPath = args.path.split('/') this.createDir({ path: args.path.substr(0, args.path.lastIndexOf('/')) })
}
splitPath = splitPath.filter(dir => dir) try {
const dir = '/' + splitPath.join('/') fs.writeFile(path, args.content, 'utf8', (error: Error) => {
if (error) {
this.emit('folderAdded', dir) console.log(error)
resolve() return reject(error)
}).catch((e: Error) => reject(e)) }
resolve(true)
})
} catch (e) {
return reject(e)
}
if (!exists) {
this.emit('fileAdded', args.path)
} else { } else {
fs.ensureFile(path).then(() => { this.emit('fileChanged', args.path)
fs.writeFile(path, args.content, 'utf8', (error: Error) => {
if (error) {
console.log(error)
return reject(error)
}
resolve()
})
}).catch((e: Error) => reject(e))
if (!exists) {
this.emit('fileAdded', args.path)
} else {
this.emit('fileChanged', args.path)
}
} }
}) })
} catch (error) { } catch (error) {
...@@ -131,24 +124,22 @@ export class RemixdClient extends PluginClient { ...@@ -131,24 +124,22 @@ export class RemixdClient extends PluginClient {
} }
} }
createDir (args: SharedFolderArgs): Promise<void> { createDir (args: SharedFolderArgs) {
try { try {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.readOnly) return reject(new Error('Cannot create folder: read-only mode selected')) if (this.readOnly) return reject(new Error('Cannot create folder: read-only mode selected'))
const path = utils.absolutePath(args.path, this.currentSharedFolder) const paths = args.path.split('/').filter(value => value)
const exists = fs.existsSync(path) if (paths.length && paths[0] === '') paths.shift()
let currentCheck = ''
if (exists && !isRealPath(path)) return reject(new Error('')) paths.forEach((value) => {
this.trackDownStreamUpdate[path] = path currentCheck = currentCheck ? currentCheck + '/' + value : value
fs.mkdirp(path).then(() => { const path = utils.absolutePath(currentCheck, this.currentSharedFolder)
let splitPath = args.path.split('/') if (!fs.existsSync(path)) {
fs.mkdirp(path)
splitPath = splitPath.filter(dir => dir) this.emit('folderAdded', currentCheck)
const dir = '/' + splitPath.join('/') }
})
this.emit('folderAdded', dir) resolve(true)
resolve()
}).catch((e: Error) => reject(e))
}) })
} catch (error) { } catch (error) {
throw new Error(error) throw new Error(error)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment