As far i know(let me know if you have a better solution) to start powershell in a directory you have to do something like this:
powershell.exe -NoExit -command "& {Set-Location C:\my\path\here}"
Now I'm trying to reproduce this with spawn
from node-pty
like this:
const shell = isWindows ? 'powershell.exe' : 'bash';
const args = !isNullOrEmptyOrWhiteSpaced(shellStartFolder) ?
["-command", `"& {Set-Location ${shellStartFolder}}"`] : [];
return spawn(shell, args, {
name: 'xterm-color',
cols: DefaultTerminalSize.cols,
rows: DefaultTerminalSize.rows,
cwd: isWindows ? process.env.USERPROFILE : process.env.HOME,
env: process.env as INonUndefinedEnv
});
but pseudo terminal (i'm using xterm.js) end up like this (give shellStartFolder
is "C:\\"
)
What am I missing?
As mentioned in the comments, the spawn()
function already takes an argument that specifies the initial working directory: cwd
.
const shell = isWindows ? 'powershell.exe' : 'bash';
const args = [];
return spawn(shell, args, {
name: 'xterm-color',
cols: DefaultTerminalSize.cols,
rows: DefaultTerminalSize.rows,
cwd: isWindows ? (!isNullOrEmptyOrWhiteSpaced(shellStartFolder) ? shellStartFolder : process.env.USERPROFILE) : process.env.HOME,
env: process.env as INonUndefinedEnv
});