Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 102 additions & 48 deletions app/src/lib/shells/win32.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { spawn, ChildProcess } from 'child_process'
import * as Path from 'path'
import { assertNever } from '../fatal-error'
import { enumerateValues, HKEY, RegistryValueType } from 'registry-js'

import { pathExists } from '../file-system'
import { assertNever } from '../fatal-error'
import { IFoundShell } from './found-shell'

export enum Shell {
Expand Down Expand Up @@ -43,75 +45,127 @@ export async function getAvailableShells(): Promise<
},
]

const powerShellPath = await findPowerShell()
if (powerShellPath != null) {
shells.push({
shell: Shell.PowerShell,
path: powerShellPath,
})
}

const hyperPath = await findHyper()
if (hyperPath != null) {
shells.push({
shell: Shell.Hyper,
path: hyperPath,
})
}

const gitBashPath = await findGitBash()
if (gitBashPath != null) {
shells.push({
shell: Shell.GitBash,
path: gitBashPath,
})
}

return shells
}

async function findPowerShell(): Promise<string | null> {
const powerShell = enumerateValues(
HKEY.HKEY_LOCAL_MACHINE,
'Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\PowerShell.exe'
)
if (powerShell.length > 0) {
const first = powerShell[0]

// NOTE:
// on Windows 7 these are both REG_SZ, which technically isn't supposed
// to contain unexpanded references to environment variables. But given
// it's also %SystemRoot% and we do the expanding here I think this is
// a fine workaround to do to support the maximum number of setups.

if (
first.type === RegistryValueType.REG_EXPAND_SZ ||
first.type === RegistryValueType.REG_SZ
) {
const path = first.data.replace(
/^%SystemRoot%/i,
process.env.SystemRoot || 'C:\\Windows'

if (powerShell.length === 0) {
return null
}

const first = powerShell[0]

// NOTE:
// on Windows 7 these are both REG_SZ, which technically isn't supposed
// to contain unexpanded references to environment variables. But given
// it's also %SystemRoot% and we do the expanding here I think this is
// a fine workaround to do to support the maximum number of setups.

if (
first.type === RegistryValueType.REG_EXPAND_SZ ||
first.type === RegistryValueType.REG_SZ
) {
const path = first.data.replace(
/^%SystemRoot%/i,
process.env.SystemRoot || 'C:\\Windows'
)

if (await pathExists(path)) {

This comment was marked as spam.

return path
} else {
log.debug(
`[PowerShell] registry entry found but does not exist at '${path}'`
)
shells.push({
shell: Shell.PowerShell,
path,
})
}
}

return null
}

async function findHyper(): Promise<string | null> {
const hyper = enumerateValues(
HKEY.HKEY_CURRENT_USER,
'Software\\Classes\\Directory\\Background\\shell\\Hyper\\command'
)
if (hyper.length > 0) {
const first = hyper[0]
if (first.type === RegistryValueType.REG_SZ) {
// Registry key is structured as "{installationPath}\app-x.x.x\Hyper.exe" "%V"

// This regex is designed to get the path to the version-specific Hyper.
// commandPieces = ['"{installationPath}\app-x.x.x\Hyper.exe"', '"', '{installationPath}\app-x.x.x\Hyper.exe', ...]
const commandPieces = first.data.match(/(["'])(.*?)\1/)
const path = commandPieces
? commandPieces[2]
: process.env.LocalAppData.concat('\\hyper\\Hyper.exe') // fall back to the launcher in install root
shells.push({
shell: Shell.Hyper,
path: path,
})

if (hyper.length === 0) {
return null
}

const first = hyper[0]
if (first.type === RegistryValueType.REG_SZ) {
// Registry key is structured as "{installationPath}\app-x.x.x\Hyper.exe" "%V"

// This regex is designed to get the path to the version-specific Hyper.
// commandPieces = ['"{installationPath}\app-x.x.x\Hyper.exe"', '"', '{installationPath}\app-x.x.x\Hyper.exe', ...]
const commandPieces = first.data.match(/(["'])(.*?)\1/)
const path = commandPieces
? commandPieces[2]
: process.env.LocalAppData.concat('\\hyper\\Hyper.exe') // fall back to the launcher in install root

if (await pathExists(path)) {
return path
} else {
log.debug(`[Hyper] registry entry found but does not exist at '${path}'`)
}
}

const gitBash = enumerateValues(
return null
}

async function findGitBash(): Promise<string | null> {
const registryPath = enumerateValues(
HKEY.HKEY_LOCAL_MACHINE,
'SOFTWARE\\GitForWindows'
)

if (gitBash.length > 0) {
const installPathEntry = gitBash.find(e => e.name === 'InstallPath')
if (
installPathEntry &&
installPathEntry.type === RegistryValueType.REG_SZ
) {
shells.push({
shell: Shell.GitBash,
path: Path.join(installPathEntry.data, 'git-bash.exe'),
})
if (registryPath.length === 0) {
return null
}

const installPathEntry = registryPath.find(e => e.name === 'InstallPath')
if (installPathEntry && installPathEntry.type === RegistryValueType.REG_SZ) {
const path = Path.join(installPathEntry.data, 'git-bash.exe')

if (await pathExists(path)) {
return path
} else {
log.debug(
`[Git Bash] registry entry found but does not exist at '${path}'`
)
}
}

return shells
return null
}

export function launch(
Expand Down