-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell.ts
More file actions
194 lines (160 loc) · 4.6 KB
/
Copy pathshell.ts
File metadata and controls
194 lines (160 loc) · 4.6 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { LinuxDistro } from '@codifycli/schemas';
import * as pty from '@homebridge/node-pty-prebuilt-multiarch';
import cp from 'node:child_process';
import * as fs from 'node:fs/promises';
import util from 'node:util';
import os from 'node:os';
import path from 'node:path';
import stripAnsi from 'strip-ansi';
import { ShellValidationError } from '../common/errors.js';
const exec = util.promisify(cp.exec);
export enum Shell {
ZSH = 'zsh',
BASH = 'bash',
SH = 'sh',
KSH = 'ksh',
CSH = 'csh',
FISH = 'fish',
}
export const ShellUtils = {
getShell(): Shell | undefined {
const shell = process.env.SHELL || '';
if (shell.endsWith('bash')) {
return Shell.BASH
}
if (shell.endsWith('zsh')) {
return Shell.ZSH
}
if (shell.endsWith('sh')) {
return Shell.SH
}
if (shell.endsWith('csh')) {
return Shell.CSH
}
if (shell.endsWith('ksh')) {
return Shell.KSH
}
if (shell.endsWith('fish')) {
return Shell.FISH
}
return undefined;
},
getDefaultShell(): string {
return process.env.SHELL!;
},
getPrimaryShellRc(): string {
return this.getShellRcFiles()[0];
},
getShellRcFiles(): string[] {
const shell = process.env.SHELL || os.userInfo().shell || '';
const homeDir = os.homedir();
if (shell.endsWith('bash')) {
// Linux typically uses .bashrc, macOS uses .bash_profile
if (ShellUtils.isLinux()) {
return [
path.join(homeDir, '.bashrc'),
path.join(homeDir, '.bash_profile'),
path.join(homeDir, '.profile'),
];
}
return [
path.join(homeDir, '.bash_profile'),
path.join(homeDir, '.bashrc'),
path.join(homeDir, '.profile'),
];
}
if (shell.endsWith('zsh')) {
return [
path.join(homeDir, '.zshrc'),
path.join(homeDir, '.zprofile'),
path.join(homeDir, '.zshenv'),
];
}
if (shell.endsWith('sh')) {
return [
path.join(homeDir, '.profile'),
]
}
if (shell.endsWith('ksh')) {
return [
path.join(homeDir, '.profile'),
path.join(homeDir, '.kshrc'),
]
}
if (shell.endsWith('csh')) {
return [
path.join(homeDir, '.cshrc'),
path.join(homeDir, '.login'),
path.join(homeDir, '.logout'),
]
}
if (shell.endsWith('fish')) {
return [
path.join(homeDir, '.config/fish/config.fish'),
]
}
// Default to bash-style files
return [
path.join(homeDir, '.bashrc'),
path.join(homeDir, '.bash_profile'),
path.join(homeDir, '.profile'),
];
},
isMacOS(): boolean {
return os.platform() === 'darwin';
},
isLinux(): boolean {
return os.platform() === 'linux';
},
async validateShell(): Promise<void> {
const SENTINEL = 'CODIFY_SHELL_CHECK_OK';
const TIMEOUT_MS = 10_000;
const shell = ShellUtils.getDefaultShell();
const output: string[] = [];
await new Promise<void>((resolve, reject) => {
const mPty = pty.spawn(shell, ['-i', '-c', `echo '${SENTINEL}'`], {
cols: 80,
rows: 24,
env: { ...process.env as Record<string, string>, TERM_PROGRAM: 'codify' },
});
mPty.onData((data) => output.push(data));
const timer = setTimeout(() => {
mPty.kill();
const captured = stripAnsi(output.join('').trim());
reject(new ShellValidationError(true, captured, ShellUtils.getShellRcFiles()));
}, TIMEOUT_MS);
mPty.onExit(() => {
clearTimeout(timer);
const captured = stripAnsi(output.join('').trim());
const lines = captured
.split('\n')
.map((l) => l.trim())
.filter(Boolean)
.filter((l) => !l.includes(`echo '${SENTINEL}'`) && l !== SENTINEL);
const matchesSentinel = lines.length === 0;
if (!matchesSentinel) {
reject(new ShellValidationError(false, lines.join('\n'), ShellUtils.getShellRcFiles()));
} else {
resolve();
}
});
});
},
async getLinuxDistro(): Promise<LinuxDistro | undefined> {
for (const candidate of ['/etc/os-release', '/usr/lib/os-release']) {
let osRelease: string;
try {
osRelease = await fs.readFile(candidate, 'utf8');
} catch {
continue;
}
for (const line of osRelease.split('\n')) {
if (line.startsWith('ID=')) {
const distroId = line.slice(3).trim().replaceAll('"', '');
return Object.values(LinuxDistro).includes(distroId as LinuxDistro) ? distroId as LinuxDistro : undefined;
}
}
}
return undefined;
},
}