Skip to content

Commit

Permalink
fix: unhide doctor and better plugin doctor hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Oct 27, 2022
1 parent 3716011 commit 31dbb61
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 3,604 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"nyc": "^15.1.0",
"prettier": "^2.7.1",
"pretty-quick": "^3.1.0",
"sfdx-cli": "^7.173.0",
"shx": "0.3.4",
"sinon": "^11.1.1",
"sinon-chai": "^3.7.0",
Expand Down Expand Up @@ -121,4 +120,4 @@
"publishConfig": {
"access": "public"
}
}
}
37 changes: 20 additions & 17 deletions src/commands/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ export default class Doctor extends SfdxCommand {
public static description = messages.getMessage('commandDescription');
public static examples = messages.getMessage('examples').split(os.EOL);

// Hide for now
public static hidden = true;

protected static flagsConfig = {
command: flags.string({
char: 'c',
Expand All @@ -49,7 +46,8 @@ export default class Doctor extends SfdxCommand {

// Array of promises that are various doctor tasks to perform
// such as running a command and running diagnostics.
private tasks: Array<Promise<void>> = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private tasks: Array<Promise<any>> = [];
private doctor: SfDoctor;
private outputDir: string;
private filesWrittenMsgs: string[] = [];
Expand All @@ -76,23 +74,28 @@ export default class Doctor extends SfdxCommand {

if (pluginFlag) {
// verify the plugin flag matches an installed plugin
if (!this.config.plugins.some((p) => p.name === pluginFlag)) {
throw new SfError(messages.getMessage('pluginNotInstalledError', [pluginFlag]), 'UnknownPluginError');
}

this.ux.styledHeader(`Running diagnostics for plugin: ${pluginFlag}`);
const listeners = lifecycle.getListeners(`sf-doctor-${pluginFlag}`);
if (listeners.length) {
// run the diagnostics for a specific plugin
this.tasks.push(lifecycle.emit(`sf-doctor-${pluginFlag}`, this.doctor));
const plugin = this.config.plugins.find((p) => p.name === pluginFlag);
if (plugin) {
const eventName = `sf-doctor-${pluginFlag}`;
const hasDoctorHook = Object.keys(plugin.hooks).some((hook) => hook === eventName);
if (hasDoctorHook) {
this.ux.styledHeader(`Running diagnostics for plugin: ${pluginFlag}`);
this.tasks.push(this.config.runHook(eventName, { doctor: this.doctor }));
} else {
this.ux.log(`${pluginFlag} doesn't have diagnostic tests to run.`);
}
} else {
this.ux.log("Plugin doesn't have diagnostic tests to run.");
throw new SfError(messages.getMessage('pluginNotInstalledError', [pluginFlag]), 'UnknownPluginError');
}
} else {
this.ux.styledHeader('Running all diagnostics');
// run all diagnostics
this.tasks.push(lifecycle.emit('sf-doctor', this.doctor));

// Fire events for plugins that have sf-doctor hooks
this.config.plugins.forEach((plugin) => {
const eventName = `sf-doctor-${plugin.name}`;
if (Object.keys(plugin.hooks).find((hook) => hook === eventName)) {
this.tasks.push(this.config.runHook(eventName, { doctor: this.doctor }));
}
});
this.tasks = [...this.tasks, ...this.doctor.diagnose()];
}

Expand Down
9 changes: 7 additions & 2 deletions src/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const PINNED_SUGGESTIONS = [
messages.getMessage('pinnedSuggestions.checkSfdcStatus'),
];

// private config from the CLI
// eslint-disable-next-line no-underscore-dangle
let __cliConfig: Config;

export class Doctor implements SfDoctor {
// singleton instance
private static instance: SfDoctor;
Expand All @@ -55,8 +59,9 @@ export class Doctor implements SfDoctor {
// Contains all gathered data and results of diagnostics.
private diagnosis: SfDoctorDiagnosis;

private constructor(private readonly config: Config, versionDetail: VersionDetail) {
private constructor(config: Config, versionDetail: VersionDetail) {
this.id = Date.now();
__cliConfig = config;
const sfdxEnvVars = new Env().entries().filter((e) => e[0].startsWith('SFDX_'));
const sfEnvVars = new Env().entries().filter((e) => e[0].startsWith('SF_'));
const cliConfig = omit(config, ['plugins', 'pjson', 'userPJSON', 'options']) as CliConfig;
Expand Down Expand Up @@ -107,7 +112,7 @@ export class Doctor implements SfDoctor {
* @returns An array of diagnostic promises.
*/
public diagnose(): Array<Promise<void>> {
return new Diagnostics(this, this.config).run();
return new Diagnostics(this, __cliConfig).run();
}

/**
Expand Down
Loading

0 comments on commit 31dbb61

Please sign in to comment.