From c78eafaea0838fe2082211d2c2ca41b1c7997d22 Mon Sep 17 00:00:00 2001 From: purocean Date: Thu, 15 Aug 2019 19:17:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=87=E6=8D=A2=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/TreeNode.vue | 4 ++-- src/server/main.ts | 9 ++++++++- src/server/shell.ts | 26 +++++++++++++++++++++++--- src/server/wsl.ts | 4 ++-- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/TreeNode.vue b/frontend/src/components/TreeNode.vue index 0c84e80c..2a31b898 100644 --- a/frontend/src/components/TreeNode.vue +++ b/frontend/src/components/TreeNode.vue @@ -109,9 +109,9 @@ export default { File.openInOS(this.item) }, revealInXterminal () { - const path = this.currentRepo ? this.currentRepo.path + this.item.path : '~' + const path = this.currentRepo ? this.currentRepo.path + this.item.path : '' - this.$bus.emit('xterm-run', `cd '${path.replace('\'', '\\\'')}'`) + this.$bus.emit('xterm-run', `--yank-note-run-command-cd-- ${path}`) }, async createFile () { let filename = await this.$modal.input({ diff --git a/src/server/main.ts b/src/server/main.ts index 7c2cf114..1aeea7da 100644 --- a/src/server/main.ts +++ b/src/server/main.ts @@ -6,6 +6,7 @@ import * as xStatic from 'koa-static' import * as mime from 'mime' import * as request from 'request' import * as pty from 'node-pty' +import * as os from 'os' import { STATIC_DIR, HOME_DIR, HELP_DIR } from './constant' import init from './init' import file from './file' @@ -218,7 +219,13 @@ const server = (port = 3000) => { }) ptyProcess.on('data', (data: any) => socket.emit('output', data)) ptyProcess.on('exit', () => socket.disconnect()) - socket.on('input', (data: any) => ptyProcess.write(data)) + socket.on('input', (data: any) => { + if (data.startsWith(shell.CD_COMMAND_PREFIX)) { + ptyProcess.write(shell.transformCdCommand(data.toString())) + } else { + ptyProcess.write(data) + } + }) socket.on('resize', (size: any) => ptyProcess.resize(size[0], size[1])) socket.on('disconnect', () => ptyProcess.kill()) }) diff --git a/src/server/shell.ts b/src/server/shell.ts index 370222fa..425b3d96 100644 --- a/src/server/shell.ts +++ b/src/server/shell.ts @@ -1,14 +1,34 @@ -import config from './config' import * as os from 'os' +import config from './config' +import { toWslPath } from './wsl' const configKey = 'shell' -const defaultVal = os.platform() === 'win32' ? 'cmd.exe' : 'bash' +const CD_COMMAND_PREFIX = '--yank-note-run-command-cd--' +const defaultShell = os.platform() === 'win32' ? 'cmd.exe' : 'bash' const getShell = () => { - return config.get(configKey, defaultVal) + return config.get(configKey, defaultShell).toLowerCase() +} + +const transformCdCommand = (command: string) => { + const path = command.replace(CD_COMMAND_PREFIX, '').trim() + + if (os.platform() !== 'win32') { + return `cd '${path.replace(/\'/g, '\\\'')}'` + } + + // 使用 wsl 做 shell,需要先转换路径 + if (getShell().indexOf('wsl.exe') > -1) { + return `cd '${toWslPath(path).replace(/\'/g, '\\\'')}'` + } + + // windows 下的切换目录 + return `cd /d "${path}"` } export default { + CD_COMMAND_PREFIX, getShell, + transformCdCommand, } diff --git a/src/server/wsl.ts b/src/server/wsl.ts index 1ad5821b..26aca129 100644 --- a/src/server/wsl.ts +++ b/src/server/wsl.ts @@ -23,11 +23,11 @@ export const getIsWsl = () => { export const isWsl = getIsWsl() export const toWslPath = (path: string) => { - return execFileSync('wslpath', ['-u', `${path.replace(/\\/g, '/')}`]).toString().trim() + return execFileSync('wsl.exe', ['--', 'wslpath', '-u', `${path.replace(/\\/g, '/')}`]).toString().trim() } export const toWinPath = (path: string) => { - return execFileSync('wslpath', ['-w', path]).toString().trim() + return execFileSync('wsl.exe', ['--', 'wslpath', '-w', path]).toString().trim() } export const getWinTempPath = () => {