cache dscl output

This commit is contained in:
Eugene Pankov
2021-02-07 13:43:58 +01:00
parent 61a46e3b4a
commit 6bad2a2167

View File

@@ -8,6 +8,8 @@ import { Shell } from '../api/interfaces'
/** @hidden */ /** @hidden */
@Injectable() @Injectable()
export class MacOSDefaultShellProvider extends ShellProvider { export class MacOSDefaultShellProvider extends ShellProvider {
private cachedShell?: string
constructor ( constructor (
private hostApp: HostAppService, private hostApp: HostAppService,
) { ) {
@@ -18,14 +20,29 @@ export class MacOSDefaultShellProvider extends ShellProvider {
if (this.hostApp.platform !== Platform.macOS) { if (this.hostApp.platform !== Platform.macOS) {
return [] return []
} }
const shellEntry = (await exec(`/usr/bin/dscl . -read /Users/${process.env.LOGNAME} UserShell`))[0].toString()
return [{ return [{
id: 'default', id: 'default',
name: 'User default', name: 'User default',
command: shellEntry.split(' ')[1].trim(), command: await this.getDefaultShellCached(),
args: ['--login'], args: ['--login'],
hidden: true, hidden: true,
env: {}, env: {},
}] }]
} }
private async getDefaultShellCached () {
if (!this.cachedShell) {
this.cachedShell = await this.getDefaultShell()
} else {
this.getDefaultShell().then(shell => {
this.cachedShell = shell
})
}
return this.cachedShell!
}
private async getDefaultShell () {
const shellEntry = (await exec(`/usr/bin/dscl . -read /Users/${process.env.LOGNAME} UserShell`))[0].toString()
return shellEntry.split(' ')[1].trim()
}
} }