Skip to content

Commit

Permalink
Merge pull request #5 from rwietter/fix/unescaped-spaces
Browse files Browse the repository at this point in the history
fix error when opening directories that contains spaces
  • Loading branch information
rwietter authored Sep 1, 2024
2 parents d479da5 + c196d68 commit 87af2b7
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 108 deletions.
Binary file added exterm-0.1.2.vsix
Binary file not shown.
179 changes: 84 additions & 95 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,97 +1,86 @@
{
"name": "exterm",
"displayName": "Exterm",
"publisher": "rwietter",
"description": "Exterm is a VS Code extension that allows you to open projects and directories directly in your terminal emulator of choice.",
"maintainers": [
"Maurício Witter <[email protected]>"
],
"version": "0.0.1",
"engines": {
"vscode": "^1.85.0"
},
"categories": [
"Other"
],
"repository": {
"type": "git",
"url": "https://github.com/rwietter/exterm-vscode"
},
"activationEvents": [
"onStartupFinished"
],
"icon": "exterm.png",
"keywords": [
"terminal",
"external",
"open",
"project"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/rwietter/exterm-vscode/issues"
},
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "exterm.openProjectInExternalTerminal",
"title": "Exterm: Open in External Terminal"
}
],
"configuration": {
"title": "exterm",
"properties": {
"exterm.terminalKind": {
"type": "string",
"enum": [
"alacritty",
"kitty",
"wezterm",
"xterm",
"gnome-terminal",
"konsole",
"xfce4-terminal",
"kermit",
"urxvt",
"cmd.exe"
],
"default": "wezterm",
"description": "Set your preferred terminal emulator."
}
}
},
"menus": {
"explorer/context": [
{
"when": "explorerResourceIsFolder",
"command": "exterm.openProjectInExternalTerminal",
"group": "navigation"
}
]
}
},
"scripts": {
"prod::vscode:prepublish": "yarn run prod::esbuild-base -- --minify",
"prod::esbuild-base": "esbuild ./src/extension.ts --bundle --outfile=out/main.js --external:vscode --format=cjs --platform=node",
"dev::esbuild": "npm run esbuild-base -- --sourcemap",
"dev::esbuild-watch": "npm run esbuild-base -- --sourcemap --watch",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "yarn run compile && yarn run lint",
"lint": "eslint src --ext ts",
"test": "vscode-test"
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/node": "18.x",
"@types/vscode": "^1.85.0",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"@vscode/test-cli": "^0.0.4",
"@vscode/test-electron": "^2.3.8",
"esbuild": "^0.19.9",
"eslint": "^8.54.0",
"typescript": "^5.3.2"
}
"name": "exterm",
"displayName": "Exterm",
"publisher": "rwietter",
"description": "Exterm is a VS Code extension that allows you to open projects and directories directly in your terminal emulator of choice.",
"maintainers": ["Maurício Witter <[email protected]>"],
"version": "0.1.2",
"engines": {
"vscode": "^1.85.0"
},
"categories": ["Other"],
"repository": {
"type": "git",
"url": "https://github.com/rwietter/exterm-vscode"
},
"activationEvents": ["onStartupFinished"],
"icon": "exterm.png",
"keywords": ["terminal", "external", "open", "project"],
"license": "MIT",
"bugs": {
"url": "https://github.com/rwietter/exterm-vscode/issues"
},
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "exterm.openProjectInExternalTerminal",
"title": "Exterm: Open in External Terminal"
}
],
"configuration": {
"title": "exterm",
"properties": {
"exterm.terminalKind": {
"type": "string",
"enum": [
"alacritty",
"kitty",
"wezterm",
"xterm",
"gnome-terminal",
"konsole",
"xfce4-terminal",
"kermit",
"urxvt",
"cmd.exe"
],
"default": "wezterm",
"description": "Set your preferred terminal emulator."
}
}
},
"menus": {
"explorer/context": [
{
"when": "explorerResourceIsFolder",
"command": "exterm.openProjectInExternalTerminal",
"group": "navigation"
}
]
}
},
"scripts": {
"prod::vscode:prepublish": "yarn run prod::esbuild-base -- --minify",
"prod::esbuild-base": "esbuild ./src/extension.ts --bundle --outfile=out/main.js --external:vscode --format=cjs --platform=node",
"dev::esbuild": "npm run esbuild-base -- --sourcemap",
"dev::esbuild-watch": "npm run esbuild-base -- --sourcemap --watch",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "yarn run compile && yarn run lint",
"lint": "eslint src --ext ts",
"test": "vscode-test"
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/node": "18.x",
"@types/vscode": "^1.85.0",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"@vscode/test-cli": "^0.0.4",
"@vscode/test-electron": "^2.3.8",
"esbuild": "^0.19.9",
"eslint": "^8.54.0",
"typescript": "^5.3.2"
}
}
53 changes: 40 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,65 @@
import * as child_process from 'node:child_process';
import * as vscode from 'vscode';
import * as cp from 'child_process';

const supportedTerminals = {
const terminalWorkingDirectory = {
'kitty': '-d',
'gnome-terminal': '--working-directory',
'xfce4-terminal': '--working-directory',
'alacritty': '--working-directory',
'kermit': '-w',
'wezterm': 'start --cwd',
'urxvt': '-cd',
'cmd.exe': '/c', // TODO: test this flag
'xterm': '-e',
'konsole': '--workdir',
'cmd.exe': '/c', // TODO: I do not use Windows, so I am not sure if this is correct
} as const;

type DefaultPlatformTerminal = Partial<Record<typeof process.platform, TerminalKind>>;

const defaultPlatformTerminal: DefaultPlatformTerminal = {
win32: 'cmd.exe',
linux: 'kitty',
darwin: 'alacritty',
}

type TerminalKind = keyof typeof supportedTerminals;
type TerminalKind = keyof typeof terminalWorkingDirectory;

export function activate(context: vscode.ExtensionContext) {
let openProjectInExternalTerminal = vscode.commands.registerCommand('exterm.openProjectInExternalTerminal', (uri: vscode.Uri | undefined) => {
const openProjectInExternalTerminal = vscode.commands.registerCommand('exterm.openProjectInExternalTerminal', (uri: vscode.Uri | undefined) => {

if (!uri) return vscode.window.showInformationMessage('No item selected. Please select a directory to open in an external terminal.');

if (!uri) return vscode.window.showInformationMessage('No directory or file selected.');
const fsPathNormalized = uri.fsPath?.replace(/ /g, '\\ ');

const defaultPlatformTerminalCommand = process.platform === 'win32' ? 'cmd.exe' : 'kitty';
const defaultTerminal = defaultPlatformTerminal[process.platform] || 'kitty';
const terminalConfig = vscode.workspace.getConfiguration('exterm');
const userTerm: TerminalKind | undefined = terminalConfig.get('terminalKind');

let terminalKind: TerminalKind = userTerm || defaultPlatformTerminalCommand
let flagDirectory = supportedTerminals[terminalKind];
let directoryPath = uri.fsPath;
const terminal: TerminalKind = userTerm || defaultTerminal
const flagDirectory = terminalWorkingDirectory[terminal];
const directoryPath = fsPathNormalized;

vscode.window.showInformationMessage(`Opening ${directoryPath} in ${terminalKind}...`);
const termArgs = [flagDirectory, directoryPath];

cp.spawn(terminalKind, [flagDirectory, directoryPath], {
const child = child_process.spawn(terminal, termArgs, {
detached: false,
shell: true,
windowsHide: false
windowsHide: true
});

child.stderr.on('data', (err) => {
vscode.window.showErrorMessage(`Exterm Error: ${err.toString()}`);
});

child.on('close', (code) => {
if (code !== 0) {
vscode.window.showErrorMessage(`Exterm Exited: terminal process exited with code ${code}`);
return;
}
});

child.on('error', (err) => {
vscode.window.showErrorMessage(`Exterm Error: failed to start terminal: ${err.message}`);
});
});

Expand Down

0 comments on commit 87af2b7

Please sign in to comment.