From fc060acd88ce50c010b174c391689f3308121820 Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Wed, 5 Jul 2017 16:22:44 +0200 Subject: [PATCH] open new tabs from cli (fixes #67) --- app/main.js | 4 ++-- .../src/components/appRoot.component.ts | 2 +- terminus-core/src/services/hostApp.service.ts | 11 ++++++----- terminus-terminal/src/buttonProvider.ts | 18 ++++++++++++++---- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/main.js b/app/main.js index 00f26b2b..194531dc 100644 --- a/app/main.js +++ b/app/main.js @@ -8,8 +8,8 @@ if (process.argv.indexOf('--debug') !== -1) { let app = electron.app -let secondInstance = app.makeSingleInstance((argv) => { - app.window.webContents.send('host:second-instance') +let secondInstance = app.makeSingleInstance((argv, cwd) => { + app.window.webContents.send('host:second-instance', argv, cwd) }) if (secondInstance) { diff --git a/terminus-core/src/components/appRoot.component.ts b/terminus-core/src/components/appRoot.component.ts index b839a1af..d57e51db 100644 --- a/terminus-core/src/components/appRoot.component.ts +++ b/terminus-core/src/components/appRoot.component.ts @@ -93,7 +93,7 @@ export class AppRootComponent { this.docking.dock() }) - this.hostApp.secondInstance.subscribe(() => { + this.hostApp.secondInstance$.subscribe(() => { this.onGlobalHotkey() }) this.hotkeys.globalHotkey.subscribe(() => { diff --git a/terminus-core/src/services/hostApp.service.ts b/terminus-core/src/services/hostApp.service.ts index b955116f..22e8dfc4 100644 --- a/terminus-core/src/services/hostApp.service.ts +++ b/terminus-core/src/services/hostApp.service.ts @@ -1,3 +1,4 @@ +import { Subject } from 'rxjs' import { Injectable, NgZone, EventEmitter } from '@angular/core' import { ElectronService } from '../services/electron.service' import { Logger, LogService } from '../services/log.service' @@ -20,7 +21,7 @@ export class HostAppService { quitRequested = new EventEmitter() ready = new EventEmitter() shown = new EventEmitter() - secondInstance = new EventEmitter() + secondInstance$ = new Subject<{ argv: string[], cwd: string }>() private logger: Logger @@ -39,16 +40,16 @@ export class HostAppService { electron.ipcRenderer.on('host:quit-request', () => this.zone.run(() => this.quitRequested.emit())) - electron.ipcRenderer.on('uncaughtException', (err) => { + electron.ipcRenderer.on('uncaughtException', ($event, err) => { this.logger.error('Unhandled exception:', err) }) electron.ipcRenderer.on('host:window-shown', () => { - this.shown.emit() + this.zone.run(() => this.shown.emit()) }) - electron.ipcRenderer.on('host:second-instance', () => { - this.secondInstance.emit() + electron.ipcRenderer.on('host:second-instance', ($event, argv: string[], cwd: string) => { + this.zone.run(() => this.secondInstance$.next({ argv, cwd })) }) this.ready.subscribe(() => { diff --git a/terminus-terminal/src/buttonProvider.ts b/terminus-terminal/src/buttonProvider.ts index eeec98f9..aed8f4d7 100644 --- a/terminus-terminal/src/buttonProvider.ts +++ b/terminus-terminal/src/buttonProvider.ts @@ -1,5 +1,7 @@ +import * as fs from 'mz/fs' +import * as path from 'path' import { Injectable } from '@angular/core' -import { HotkeysService, ToolbarButtonProvider, IToolbarButton, AppService, ConfigService } from 'terminus-core' +import { HotkeysService, ToolbarButtonProvider, IToolbarButton, AppService, ConfigService, HostAppService } from 'terminus-core' import { SessionsService } from './services/sessions.service' import { ShellsService } from './services/shells.service' @@ -12,6 +14,7 @@ export class ButtonProvider extends ToolbarButtonProvider { private sessions: SessionsService, private config: ConfigService, private shells: ShellsService, + hostApp: HostAppService, hotkeys: HotkeysService, ) { super() @@ -20,11 +23,18 @@ export class ButtonProvider extends ToolbarButtonProvider { this.openNewTab() } }) + hostApp.secondInstance$.subscribe(async ({argv, cwd}) => { + if (argv.length === 2) { + let arg = path.resolve(cwd, argv[1]) + if (await fs.exists(arg)) { + this.openNewTab(arg) + } + } + }) } - async openNewTab (): Promise { - let cwd = null - if (this.app.activeTab instanceof TerminalTabComponent) { + async openNewTab (cwd?: string): Promise { + if (!cwd && this.app.activeTab instanceof TerminalTabComponent) { cwd = await this.app.activeTab.session.getWorkingDirectory() } let command = this.config.store.terminal.shell