/
cli.ts
141 lines (123 loc) · 4.43 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as path from 'path'
import * as fs from 'mz/fs'
import { Injectable } from '@angular/core'
import { CLIHandler, CLIEvent, AppService, ConfigService, HostWindowService, ProfilesService, NotificationsService } from 'tabby-core'
import { TerminalService } from './services/terminal.service'
@Injectable()
export class TerminalCLIHandler extends CLIHandler {
firstMatchOnly = true
priority = 0
constructor (
private hostWindow: HostWindowService,
private terminal: TerminalService,
) {
super()
}
async handle (event: CLIEvent): Promise<boolean> {
const op = event.argv._[0]
if (op === 'open') {
this.handleOpenDirectory(path.resolve(event.cwd, event.argv.directory))
} else if (op === 'run') {
this.handleRunCommand(event.argv.command)
} else {
return false
}
return true
}
private async handleOpenDirectory (directory: string) {
if (directory.length > 1 && (directory.endsWith('/') || directory.endsWith('\\'))) {
directory = directory.substring(0, directory.length - 1)
}
if (await fs.exists(directory)) {
if ((await fs.stat(directory)).isDirectory()) {
this.terminal.openTab(undefined, directory)
this.hostWindow.bringToFront()
}
}
}
private handleRunCommand (command: string[]) {
this.terminal.openTab({
type: 'local',
name: '',
options: {
command: command[0],
args: command.slice(1),
},
}, null, true)
this.hostWindow.bringToFront()
}
}
@Injectable()
export class OpenPathCLIHandler extends CLIHandler {
firstMatchOnly = true
priority = -100
constructor (
private terminal: TerminalService,
private profiles: ProfilesService,
private hostWindow: HostWindowService,
private notifications: NotificationsService,
) {
super()
}
async handle (event: CLIEvent): Promise<boolean> {
const op = event.argv._[0]
const opAsPath = op ? path.resolve(event.cwd, op) : null
const profile = await this.terminal.getDefaultProfile()
if (opAsPath && (await fs.lstat(opAsPath)).isDirectory()) {
this.terminal.openTab(profile, opAsPath)
this.hostWindow.bringToFront()
return true
}
if (opAsPath && await fs.exists(opAsPath)) {
if (opAsPath.endsWith('.sh') || opAsPath.endsWith('.command')) {
profile.options!.pauseAfterExit = true
profile.options?.args?.push(opAsPath)
this.terminal.openTab(profile)
this.hostWindow.bringToFront()
return true
} else if (opAsPath.endsWith('.bat')) {
const psProfile = (await this.profiles.getProfiles()).find(x => x.id === 'cmd')
if (psProfile) {
psProfile.options!.pauseAfterExit = true
psProfile.options?.args?.push(opAsPath)
this.terminal.openTab(psProfile)
this.hostWindow.bringToFront()
return true
}
} else if (opAsPath.endsWith('.ps1')) {
const cmdProfile = (await this.profiles.getProfiles()).find(x => x.id === 'powershell')
if (cmdProfile) {
cmdProfile.options!.pauseAfterExit = true
cmdProfile.options?.args?.push(opAsPath)
this.terminal.openTab(cmdProfile)
this.hostWindow.bringToFront()
return true
}
} else {
this.notifications.error('Cannot handle scripts of this type')
}
}
return false
}
}
@Injectable()
export class AutoOpenTabCLIHandler extends CLIHandler {
firstMatchOnly = true
priority = -1000
constructor (
private app: AppService,
private config: ConfigService,
private terminal: TerminalService,
) {
super()
}
async handle (event: CLIEvent): Promise<boolean> {
if (!event.secondInstance && this.config.store.terminal.autoOpen && !this.config.store.enableWelcomeTab) {
this.app.ready$.subscribe(() => {
this.terminal.openTab()
})
return true
}
return false
}
}