Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve doctor's checking of the shell plugins #316

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/ui/ui-doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// Licensed under the MIT License.

import chalk from "chalk";
import { checkLegacyConfigs, checkShellConfigs } from "../utils/shell.js";
import { checkLegacyConfigs, checkShellConfigPlugin, checkShellConfigs } from "../utils/shell.js";

export const render = async () => {
let errors = 0;
errors += await renderLegacyConfigIssues();
errors += await renderShellPluginIssues();
errors += renderShellConfigIssues();

process.exit(errors);
Expand Down Expand Up @@ -44,3 +45,31 @@ const renderShellConfigIssues = (): number => {
}
return 0;
};

const renderShellPluginIssues = async (): Promise<number> => {
const { shellsWithoutPlugin, shellsWithBadPlugin } = await checkShellConfigPlugin();
if (shellsWithoutPlugin.length == 0) {
process.stdout.write(chalk.green("✓") + " all shells have plugins\n");
} else {
process.stderr.write(chalk.red("•") + " the following shells do not have the plugin installed:\n");
shellsWithoutPlugin.forEach((shell) => {
process.stderr.write(chalk.red(" - ") + shell + "\n");
});
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"));
}

if (shellsWithBadPlugin.length == 0) {
process.stdout.write(chalk.green("✓") + " all shells have correct plugins\n");
} else {
process.stderr.write(chalk.red("•") + " the following shells have plugins incorrectly installed:\n");
shellsWithBadPlugin.forEach((shell) => {
process.stderr.write(chalk.red(" - ") + shell + "\n");
});
process.stderr.write(chalk.yellow(" remove and regenerate the plugins according to the README, only whitespace can be after the shell plugins\n"));
}

if (shellsWithoutPlugin.length > 0 || shellsWithBadPlugin.length > 0) {
return 1;
}
return 0;
};
21 changes: 21 additions & 0 deletions src/utils/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ export const checkLegacyConfigs = async (): Promise<Shell[]> => {
return shellsWithLegacyConfig;
};

export const checkShellConfigPlugin = async () => {
const shellsWithoutPlugin: Shell[] = [];
const shellsWithBadPlugin: Shell[] = [];
for (const shell of supportedShells) {
const profilePath = await getProfilePath(shell);
if (profilePath != null && fs.existsSync(profilePath)) {
const profile = await fsAsync.readFile(profilePath, "utf8");

const profileContainsSource = profile.includes(getShellSourceCommand(shell));
const profileEndsWithSource = profile.trimEnd().endsWith(getShellSourceCommand(shell));

if (!profileContainsSource) {
shellsWithoutPlugin.push(shell);
} else if (!profileEndsWithSource) {
shellsWithBadPlugin.push(shell);
}
}
}
return { shellsWithoutPlugin, shellsWithBadPlugin };
};

const getProfilePath = async (shell: Shell): Promise<string | undefined> => {
switch (shell) {
case Shell.Bash:
Expand Down
Loading