Skip to content

Commit a0d3857

Browse files
authored
feat: improve doctor's checking of the shell plugins (#316)
* feat: improve doctor's checking of the shell plugins Signed-off-by: Chapman Pendery <cpendery@vt.edu> * style: fmt Signed-off-by: Chapman Pendery <cpendery@vt.edu> --------- Signed-off-by: Chapman Pendery <cpendery@vt.edu>
1 parent 3d90596 commit a0d3857

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/ui/ui-doctor.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// Licensed under the MIT License.
33

44
import chalk from "chalk";
5-
import { checkLegacyConfigs, checkShellConfigs } from "../utils/shell.js";
5+
import { checkLegacyConfigs, checkShellConfigPlugin, checkShellConfigs } from "../utils/shell.js";
66

77
export const render = async () => {
88
let errors = 0;
99
errors += await renderLegacyConfigIssues();
10+
errors += await renderShellPluginIssues();
1011
errors += renderShellConfigIssues();
1112

1213
process.exit(errors);
@@ -44,3 +45,31 @@ const renderShellConfigIssues = (): number => {
4445
}
4546
return 0;
4647
};
48+
49+
const renderShellPluginIssues = async (): Promise<number> => {
50+
const { shellsWithoutPlugin, shellsWithBadPlugin } = await checkShellConfigPlugin();
51+
if (shellsWithoutPlugin.length == 0) {
52+
process.stdout.write(chalk.green("✓") + " all shells have plugins\n");
53+
} else {
54+
process.stderr.write(chalk.red("•") + " the following shells do not have the plugin installed:\n");
55+
shellsWithoutPlugin.forEach((shell) => {
56+
process.stderr.write(chalk.red(" - ") + shell + "\n");
57+
});
58+
process.stderr.write(chalk.yellow(" review the README to generate the missing shell plugins, this warning can be ignored if you prefer manual startup\n"));
59+
}
60+
61+
if (shellsWithBadPlugin.length == 0) {
62+
process.stdout.write(chalk.green("✓") + " all shells have correct plugins\n");
63+
} else {
64+
process.stderr.write(chalk.red("•") + " the following shells have plugins incorrectly installed:\n");
65+
shellsWithBadPlugin.forEach((shell) => {
66+
process.stderr.write(chalk.red(" - ") + shell + "\n");
67+
});
68+
process.stderr.write(chalk.yellow(" remove and regenerate the plugins according to the README, only whitespace can be after the shell plugins\n"));
69+
}
70+
71+
if (shellsWithoutPlugin.length > 0 || shellsWithBadPlugin.length > 0) {
72+
return 1;
73+
}
74+
return 0;
75+
};

src/utils/shell.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ export const checkLegacyConfigs = async (): Promise<Shell[]> => {
8484
return shellsWithLegacyConfig;
8585
};
8686

87+
export const checkShellConfigPlugin = async () => {
88+
const shellsWithoutPlugin: Shell[] = [];
89+
const shellsWithBadPlugin: Shell[] = [];
90+
for (const shell of supportedShells) {
91+
const profilePath = await getProfilePath(shell);
92+
if (profilePath != null && fs.existsSync(profilePath)) {
93+
const profile = await fsAsync.readFile(profilePath, "utf8");
94+
95+
const profileContainsSource = profile.includes(getShellSourceCommand(shell));
96+
const profileEndsWithSource = profile.trimEnd().endsWith(getShellSourceCommand(shell));
97+
98+
if (!profileContainsSource) {
99+
shellsWithoutPlugin.push(shell);
100+
} else if (!profileEndsWithSource) {
101+
shellsWithBadPlugin.push(shell);
102+
}
103+
}
104+
}
105+
return { shellsWithoutPlugin, shellsWithBadPlugin };
106+
};
107+
87108
const getProfilePath = async (shell: Shell): Promise<string | undefined> => {
88109
switch (shell) {
89110
case Shell.Bash:

0 commit comments

Comments
 (0)