-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathteardown-tab-completion.mts
More file actions
70 lines (60 loc) · 2.04 KB
/
Copy pathteardown-tab-completion.mts
File metadata and controls
70 lines (60 loc) · 2.04 KB
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
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { homePath } from '../../constants/paths.mts'
import {
COMPLETION_CMD_PREFIX,
getBashrcDetails,
} from '../../util/cli/completion.mts'
import type { CResult } from '../../types.mts'
export function findRemainingCompletionSetups(bashrc: string): string[] {
return bashrc
.split(/\r?\n/)
.map(s => s.trim())
.filter(s => s.startsWith(COMPLETION_CMD_PREFIX))
.map(s => s.slice(COMPLETION_CMD_PREFIX.length).trim())
}
export async function teardownTabCompletion(
targetName: string,
): Promise<CResult<{ action: string; left: string[] }>> {
const result = getBashrcDetails(targetName)
if (!result.ok) {
return result
}
const { completionCommand, sourcingCommand, toAddToBashrc } = result.data
// Remove from ~/.bashrc if found
const bashrc = homePath ? path.join(homePath, '.bashrc') : ''
if (bashrc && existsSync(bashrc)) {
const content = readFileSync(bashrc, 'utf8')
if (content.includes(toAddToBashrc)) {
const newContent = content
// Try to remove the whole thing with comment first
.replaceAll(toAddToBashrc, '')
// Comment may have been edited away, try to remove the command at least
.replaceAll(sourcingCommand, '')
.replaceAll(completionCommand, '')
writeFileSync(bashrc, newContent, 'utf8')
return {
ok: true,
data: {
action: 'removed',
left: findRemainingCompletionSetups(newContent),
},
message: 'Removed completion from ~/.bashrc',
}
}
const left = findRemainingCompletionSetups(content)
return {
ok: true,
data: {
action: 'missing',
left,
},
message: `Completion was not found in ~/.bashrc${left.length ? ' (you may need to manually edit your .bashrc to clean this up...)' : ''}`,
}
}
return {
ok: true, // Eh. I think this makes most sense.
data: { action: 'not found', left: [] },
message: '~/.bashrc not found, skipping',
}
}