Skip to content

Commit

Permalink
Change drive letter before cd in case of cmd.exe
Browse files Browse the repository at this point in the history
Fixes #4

Check for cmd.exe in config, similar to wsl bash check
and send 1st two characters of the path before doing cd to change drive letter.
  • Loading branch information
saaketp committed Jan 3, 2018
1 parent d81a040 commit 98db083
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ export function activate(context: vscode.ExtensionContext) {

let dir = path.dirname(uri.fsPath);

if(isWslBash(vscode.workspace.getConfiguration('terminal'))) {
// c:\workspace\foo to /mnt/c/workspace/foo
dir = dir.replace(/(\w):/, '/mnt/$1').replace(/\\/g, '/')
}

let terminal = vscode.window.createTerminal();
terminal.show(false);

switch(kindOfShell(vscode.workspace.getConfiguration('terminal'))) {
case "wslbash":
// c:\workspace\foo to /mnt/c/workspace/foo
dir = dir.replace(/(\w):/, '/mnt/$1').replace(/\\/g, '/')
break;
case "cmd":
terminal.sendText(dir.slice(0,2));
}

terminal.sendText(`cd "${dir}"`);
});

Expand All @@ -38,19 +43,21 @@ export function activate(context: vscode.ExtensionContext) {
export function deactivate() {
}

function isWslBash(terminalSettings) {
function kindOfShell(terminalSettings) {
const windowsShellPath = terminalSettings.integrated.shell.windows;

if(!windowsShellPath) {
return false;
return undefined;
}

// Detect WSL bash according to the implementation of VS Code terminal.
// For more details, refer to https://goo.gl/AuwULb
const is32ProcessOn64Windows = process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
const system32 = is32ProcessOn64Windows ? 'Sysnative' : 'System32';
const expectedWslBashPath = path.join(process.env.windir, system32, 'bash.exe');
var shellKindByPath = {}
shellKindByPath[path.join(process.env.windir, system32, 'bash.exe').toLowerCase()] = "wslbash";
shellKindByPath[path.join(process.env.windir, system32, 'cmd.exe').toLowerCase()] = "cmd";

// %windir% can give WINDOWS instead of Windows
return windowsShellPath.toLowerCase() === expectedWslBashPath.toLowerCase();
return shellKindByPath[windowsShellPath.toLowerCase()]
}

0 comments on commit 98db083

Please sign in to comment.