Skip to content

Commit

Permalink
feat: Provide cwd for che terminal creation
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Nikitenko <[email protected]>

rh-pre-commit.version: 2.2.0
rh-pre-commit.check-secrets: ENABLED
  • Loading branch information
RomanNikitenko committed Dec 18, 2024
1 parent 987aa8c commit 2453ed0
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions code/extensions/che-terminal/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2022 Red Hat, Inc.
* Copyright (c) 2022-2024 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -44,7 +44,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<Api> {

// containerName is undefined in case the user closed the QuickPick
if (containerName) {
const pty = new MachineExecPTY(machineExecClient, containerName);
const cwd = await getCurrentWorkingDirectory();
const pty = new MachineExecPTY(machineExecClient, containerName, undefined, cwd);
const terminal = vscode.window.createTerminal({ name: `${containerName} container`, pty });
terminal.show();
}
Expand Down Expand Up @@ -127,3 +128,29 @@ export class MachineExecPTY implements vscode.Pseudoterminal {

export function deactivate(): void {
}

/**
* Provides current working directory for a terminal creation:
*
* @returns:
* - value of the PROJECTS_ROOT env variable if there is no any workspace folder
* - path to a folder if there is only one workspace folder
* - IDE proposes an user to select a folder if there is more then 1 workspace folder,
* current function returns path to the selected folder
*/
async function getCurrentWorkingDirectory(): Promise<string | undefined> {
const folders = vscode.workspace.workspaceFolders;
if (folders === undefined || folders.length < 1) {
return process.env.PROJECTS_ROOT as string;
}

if (folders.length === 1) {
return folders[0].uri.path
}

const options = {
placeHolder: "Select current working directory for new terminal"
};
const workspace: vscode.WorkspaceFolder = await vscode.commands.executeCommand('_workbench.pickWorkspaceFolder', [options]);
return workspace.uri.path;
}

0 comments on commit 2453ed0

Please sign in to comment.