From d9d23f7892339e2c78eb4644605c8305937de42a Mon Sep 17 00:00:00 2001 From: Akin Date: Sun, 28 Jan 2018 16:55:17 +0000 Subject: [PATCH] Bugfix/check path exists workspace (#1382) * check folder exists using fs access * add pathIsDir to workspace pass null if dir does not exists --- .../src/Editor/NeovimEditor/NeovimEditor.tsx | 4 +- browser/src/Services/Workspace/Workspace.ts | 25 ++- .../Services/Workspace/WorkspaceCommands.ts | 6 +- vim/core/oni-plugin-git/index.js | 192 ++++++++---------- 4 files changed, 115 insertions(+), 112 deletions(-) diff --git a/browser/src/Editor/NeovimEditor/NeovimEditor.tsx b/browser/src/Editor/NeovimEditor/NeovimEditor.tsx index e2c22d4468..5893175fe6 100644 --- a/browser/src/Editor/NeovimEditor/NeovimEditor.tsx +++ b/browser/src/Editor/NeovimEditor/NeovimEditor.tsx @@ -397,8 +397,8 @@ export class NeovimEditor extends Editor implements IEditor { this._actions.setNeovimError(true) }) - this._neovimInstance.onDirectoryChanged.subscribe(newDirectory => { - this._workspace.changeDirectory(newDirectory) + this._neovimInstance.onDirectoryChanged.subscribe(async newDirectory => { + await this._workspace.changeDirectory(newDirectory) }) this._neovimInstance.on("action", (action: any) => { diff --git a/browser/src/Services/Workspace/Workspace.ts b/browser/src/Services/Workspace/Workspace.ts index 6997c6145a..208181614d 100644 --- a/browser/src/Services/Workspace/Workspace.ts +++ b/browser/src/Services/Workspace/Workspace.ts @@ -6,6 +6,9 @@ */ import { remote } from "electron" +import { stat } from "fs" +import { promisify } from "util" + import "rxjs/add/observable/defer" import "rxjs/add/observable/from" import "rxjs/add/operator/concatMap" @@ -26,6 +29,8 @@ import { convertTextDocumentEditsToFileMap } from "./../Language/Edits" import * as WorkspaceCommands from "./WorkspaceCommands" import { WorkspaceConfiguration } from "./WorkspaceConfiguration" +const fsStat = promisify(stat) + // Candidate interface to promote to Oni API export interface IWorkspace extends Oni.Workspace { activeWorkspace: string @@ -60,13 +65,15 @@ export class Workspace implements IWorkspace { return this._onDirectoryChangedEvent } - public changeDirectory(newDirectory: string) { - if (newDirectory) { + public async changeDirectory(newDirectory: string) { + const exists = await this.pathIsDir(newDirectory) + const dir = exists ? newDirectory : null + if (newDirectory && exists) { process.chdir(newDirectory) } - this._activeWorkspace = newDirectory - this._onDirectoryChangedEvent.dispatch(newDirectory) + this._activeWorkspace = dir + this._onDirectoryChangedEvent.dispatch(dir) } public async applyEdits(edits: types.WorkspaceEdit): Promise { @@ -111,6 +118,16 @@ export class Workspace implements IWorkspace { public get onFocusLost(): IEvent { return this._onFocusLostEvent } + + public pathIsDir = async (p: string) => { + try { + const stats = await fsStat(p) + return stats.isDirectory() + } catch (error) { + Log.info(error) + return false + } + } } let _workspace: Workspace = null diff --git a/browser/src/Services/Workspace/WorkspaceCommands.ts b/browser/src/Services/Workspace/WorkspaceCommands.ts index 81c24754fa..f77576358d 100644 --- a/browser/src/Services/Workspace/WorkspaceCommands.ts +++ b/browser/src/Services/Workspace/WorkspaceCommands.ts @@ -30,13 +30,13 @@ export const activateCommands = ( remote.dialog.showOpenDialog( remote.getCurrentWindow(), dialogOptions, - (folder: string[]) => { + async (folder: string[]) => { if (!folder || !folder[0]) { return } const folderToOpen = folder[0] - workspace.changeDirectory(folderToOpen) + await workspace.changeDirectory(folderToOpen) }, ) } @@ -117,7 +117,7 @@ export const activateCommands = ( "workspace.closeFolder", "Workspace: Close Folder", "Close the current folder", - () => workspace.changeDirectory(null), + async () => await workspace.changeDirectory(null), () => !!workspace.activeWorkspace, ), ] diff --git a/vim/core/oni-plugin-git/index.js b/vim/core/oni-plugin-git/index.js index 41f8fac355..95819a395f 100644 --- a/vim/core/oni-plugin-git/index.js +++ b/vim/core/oni-plugin-git/index.js @@ -1,111 +1,97 @@ -const fs = require('fs'); -const path = require('path'); -const { promisify } = require('util'); -const fsStat = promisify(fs.stat); +const fs = require("fs") +const path = require("path") +const { promisify } = require("util") +const fsStat = promisify(fs.stat) const activate = Oni => { - const React = Oni.dependencies.React; - let isLoaded = false; - try { - - const pathIsDir = async p => { - try { - const stats = await fsStat(p); - return stats.isDirectory(); - } catch (error) { - return error; - } - }; - - const updateBranchIndicator = async evt => { - if (!evt) { - return; - } - const filePath = evt.filePath || evt.bufferFullPath; - const gitId = 'oni.status.git' - const gitBranchIndicator = Oni.statusBar.createItem(1, gitId); - - isLoaded = true; - let dir; - try { - const isDir = await pathIsDir(filePath); - const dir = isDir ? filePath : path.dirname(filePath); - let branchName; - try { - branchName = await Oni.services.git.getBranch(dir); - } catch (e) { - gitBranchIndicator.hide(); - return; - // return console.warn('[Oni.plugin.git]: No branch name found', e); - // branchName = 'Not a Git Repo'; + const React = Oni.dependencies.React + let isLoaded = false + try { + const updateBranchIndicator = async evt => { + if (!evt) { + return + } + const filePath = evt.filePath || evt.bufferFullPath + const gitId = "oni.status.git" + const gitBranchIndicator = Oni.statusBar.createItem(1, gitId) + + isLoaded = true + let dir + try { + const isDir = await Oni.workspace.pathIsDir(filePath) + const dir = isDir ? filePath : path.dirname(filePath) + let branchName + try { + branchName = await Oni.services.git.getBranch(dir) + } catch (e) { + gitBranchIndicator.hide() + return + // return console.warn('[Oni.plugin.git]: No branch name found', e); + // branchName = 'Not a Git Repo'; + } + + const props = { + style: { + height: "100%", + width: "100%", + display: "flex", + alignItems: "center", + }, + } + + const branchContainerProps = { + style: { + minWidth: "10px", + textAlign: "center", + padding: "2px 4px 0 0", + }, + } + + const branchIcon = Oni.ui.createIcon({ + name: "code-fork", + size: Oni.ui.iconSize.Default, + }) + + const branchContainer = React.createElement( + "span", + branchContainerProps, + branchIcon, + ) + + const branchNameContainer = React.createElement( + "div", + { width: "100%" }, + " " + branchName, + ) + + const gitBranch = React.createElement( + "div", + props, + branchContainer, + branchNameContainer, + ) + + gitBranchIndicator.setContents(gitBranch) + gitBranchIndicator.show() + } catch (e) { + console.log("[Oni.plugin.git]: ", e) + return gitBranchIndicator.hide() + } } - const props = { - style: { - height: '100%', - width: '100%', - display: 'flex', - alignItems: 'center', - }, - }; - - const branchContainerProps = { - style: { - minWidth: '10px', - textAlign: 'center', - padding: '2px 4px 0 0', - } - }; - - const branchIcon = Oni.ui.createIcon({ - name: 'code-fork', - size: Oni.ui.iconSize.Default, - }); - - const branchContainer = React.createElement( - 'span', - branchContainerProps, - branchIcon, - ); - - const branchNameContainer = React.createElement( - 'div', - { width: '100%'}, - ' ' + branchName, - ); - - const gitBranch = React.createElement( - 'div', - props, - branchContainer, - branchNameContainer, - ); - - - gitBranchIndicator.setContents(gitBranch); - gitBranchIndicator.show(); - } catch (e) { - console.log('[Oni.plugin.git]: ', e); - return gitBranchIndicator.hide(); - } - }; + if (!isLoaded) { + updateBranchIndicator(Oni.editors.activeEditor.activeBuffer) + } - if (!isLoaded) { - updateBranchIndicator(Oni.editors.activeEditor.activeBuffer); + Oni.editors.activeEditor.onBufferEnter.subscribe( + async evt => await updateBranchIndicator(evt), + ) + Oni.workspace.onFocusGained.subscribe(async buffer => await updateBranchIndicator(buffer)) + } catch (e) { + console.warn("[Oni.plugin.git] ERROR", e) } - - Oni.editors.activeEditor.onBufferEnter.subscribe( - async evt => await updateBranchIndicator(evt) - ); - Oni.workspace.onFocusGained.subscribe( - async buffer => await updateBranchIndicator(buffer) - ); - - } catch (e) { - console.warn('[Oni.plugin.git] ERROR', e); - } -}; +} module.exports = { - activate, -}; + activate, +}