Skip to content

Commit

Permalink
feat: improve doctor's checking of the shell plugins (#316)
Browse files Browse the repository at this point in the history
* feat: improve doctor's checking of the shell plugins

Signed-off-by: Chapman Pendery <[email protected]>

* style: fmt

Signed-off-by: Chapman Pendery <[email protected]>

---------

Signed-off-by: Chapman Pendery <[email protected]>
  • Loading branch information
cpendery authored Jan 3, 2025
1 parent 3d90596 commit a0d3857
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
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

0 comments on commit a0d3857

Please sign in to comment.