Skip to content

Commit

Permalink
fix: windows compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Jul 26, 2021
1 parent 8adccf7 commit 4c4096b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion addons/settings/locales/zh-CN.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Shell Path#!settings.label.terminal.shell.path": "Shell 路径",
"Path of shell, $SHELL or %COMSPEC% by default#!settings.comments.0.terminal.shell.path": "Shell 的路径, 默认使用 $SHELL 或 %COMSPEC%",
"Path of shell, $SHELL or PowerShell (on Windows) by default#!settings.comments.0.terminal.shell.path": "Shell 的路径, 默认使用 $SHELL 或 PowerShell (在 Windows 上)",
"Shell Args#!settings.label.terminal.shell.args": "Shell 命令行参数",
"Arguments of shell command line#!settings.comments.0.terminal.shell.args": "Shell 命令行的参数",
"Shell Args#!settings.label.terminal.shell.windowsArgs": "Shell 命令行参数",
Expand Down
2 changes: 1 addition & 1 deletion build/build-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const importGlob = () => ({
const cwd = path.dirname(meta.importer)
const files = await fastglob(id, { cwd })
return `
${files.map((module, index) => `import * as module$${index} from '${path.resolve(cwd, module)}'`).join(';')}
${files.map((module, index) => `import * as module$${index} from '${path.posix.join(cwd, module)}'`).join(';')}
export default {${files.map((module, index) => `"${module}": module$${index}`).join(',')}}
`
},
Expand Down
10 changes: 9 additions & 1 deletion main/lib/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,15 @@ function handleTerminalMessages() {
}
})
ipcMain.handle('get-shells', async () => {
if (process.platform === 'win32') return []
if (process.platform === 'win32') {
return [
'powershell.exe',
'cmd.exe',
'wsl.exe',
// 'bash.exe',
// 'git-cmd.exe',
]
}
try {
const { stdout } = await execa('grep "^/" /etc/shells')
return stdout.trim().split('\n')
Expand Down
13 changes: 9 additions & 4 deletions main/utils/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const BIN_PATH = app.isPackaged

function getDefaultShell() {
return process.platform === 'win32'
? process.env.COMSPEC : process.env.SHELL
? 'powershell.exe' // process.env.COMSPEC
: process.env.SHELL
}

function getDefaultEnv() {
Expand All @@ -29,10 +30,14 @@ function getDefaultEnv() {
return defaultEnv
}

function loginExecute(command: string) {
const shell = getDefaultShell()
async function loginExecute(command: string) {
const env = getDefaultEnv()
return execa(`${shell} -lic '${command}'`, { env })
if (process.platform === 'win32') {
return execa(command, { env })
} else {
const shell = getDefaultShell()
return execa(`${shell} -lic '${command}'`, { env })
}
}

export {
Expand Down
2 changes: 1 addition & 1 deletion renderer/components/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default {
}
:global(body) {
margin: 0;
font-family: Fira Code, Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace;
font-family: Fira Code, Cascadia Code, Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace;
font-size: 14px;
user-select: none;
cursor: default;
Expand Down
8 changes: 6 additions & 2 deletions renderer/hooks/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,21 @@ export async function openLauncher(launcher: Launcher, { command }: OpenLauncher
}

export async function startLauncher(launcher: Launcher) {
const settings = unref(useSettings())
const shellPath = settings['terminal.shell.path']
return openLauncher(launcher, {
command: getLauncherCommand(launcher),
command: getLauncherCommand(launcher, shellPath),
})
}

export async function runLauncherScript(launcher: Launcher, index: number) {
const settings = unref(useSettings())
const shellPath = settings['terminal.shell.path']
return openLauncher(launcher, {
command: getLauncherCommand({
...launcher,
...launcher.scripts![index],
}),
}, shellPath),
})
}

Expand Down
23 changes: 18 additions & 5 deletions renderer/utils/launcher.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import * as path from 'path'
import { quote } from 'shell-quote'
import type { Launcher } from '../../typings/launcher'

export function getLauncherCommand(launcher: Launcher) {
export function getLauncherCommand(launcher: Launcher, shellPath: string) {
let command = launcher.command
if (launcher.login) {
command = command ? `$SHELL -lic ${quote([command])}`
command = command
? `$SHELL -lic ${quote([command])}`
: '$SHELL -li'
}
if (launcher.directory) {
const directory = launcher.directory.replace(' ', '\\ ')
command = command ? `cd ${directory} && (${command})`
: `cd ${directory}`
const isPowerShell = process.platform === 'win32'
&& (!shellPath || path.basename(shellPath) === 'powershell.exe')
&& !launcher.remote
if (isPowerShell) {
command = command
? `Set-Location -Path ${directory}; if ($?) { ${command} }`
: `Set-Location -Path ${directory}`
} else {
command = command
? `cd ${directory} && (${command})`
: `cd ${directory}`
}
}
if (launcher.remote) {
command = command ? `ssh -t ${launcher.remote} ${quote([command])}`
command = command
? `ssh -t ${launcher.remote} ${quote([command])}`
: `ssh -t ${launcher.remote}`
}
return command
Expand Down
2 changes: 1 addition & 1 deletion renderer/utils/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const windowsCMDShells = ['cmd.exe']
// `git-cmd.exe --no-cd` has no title feature like windows built-in cmd.exe
// but invoke MinGW core when arguments `--command=usr/bin/bash.exe` specified
// TODO: judge title feature with both command and arguments
const windowsStandardShells = ['bash.exe', 'git-cmd.exe']
const windowsStandardShells = ['bash.exe', 'wsl.exe', 'git-cmd.exe']

export function getWindowsProcessInfo(shell: string, title: string): Partial<TerminalTab> | null {
shell = path.basename(shell)
Expand Down
2 changes: 1 addition & 1 deletion resources/settings.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "string",
"label": "Shell Path",
"comments": [
"Path of shell, $SHELL or %COMSPEC% by default"
"Path of shell, $SHELL or PowerShell (on Windows) by default"
],
"default": ""
},
Expand Down

0 comments on commit 4c4096b

Please sign in to comment.