Skip to content

Commit

Permalink
fix: init doctor in hook
Browse files Browse the repository at this point in the history
@W-12647786@
  • Loading branch information
peternhale committed Mar 10, 2023
1 parent d0ba454 commit f77c7ed
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 831 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ docs
package.json.bak.

# -- CLEAN ALL
*.tsbuildinfo
.eslintcache
.wireit
node_modules

# --
Expand Down
150 changes: 127 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/plugin-info",
"description": "Plugin for accessing cli info from the command line",
"version": "3.0.0",
"version": "2.5.1",
"author": "Salesforce",
"bugs": "https://github.com/forcedotcom/cli/issues",
"main": "lib/index.js",
Expand All @@ -23,39 +23,41 @@
"@oclif/plugin-command-snapshot": "^3.3.8",
"@salesforce/cli-plugins-testkit": "^3.2.24",
"@salesforce/dev-config": "^3.1.0",
"@salesforce/dev-scripts": "^3.1.1",
"@salesforce/plugin-command-reference": "^1.6.4",
"@salesforce/dev-scripts": "^4.1.2",
"@salesforce/plugin-command-reference": "^1.6.5",
"@salesforce/prettier-config": "^0.0.2",
"@salesforce/ts-sinon": "1.4.2",
"@swc/core": "^1.3.35",
"@salesforce/ts-sinon": "^1.4.6",
"@swc/core": "^1.3.39",
"@types/fs-extra": "^9.0.13",
"@types/marked": "^4.0.8",
"@types/marked-terminal": "^3.1.3",
"@types/proxy-from-env": "^1.0.1",
"@types/semver": "^7.3.8",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.53.0",
"@types/sinon-chai": "^3.2.9",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"chai": "^4.3.7",
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.6.0",
"eslint": "^8.35.0",
"eslint-config-prettier": "^8.7.0",
"eslint-config-salesforce": "^1.1.1",
"eslint-config-salesforce-license": "^0.2.0",
"eslint-config-salesforce-typescript": "^1.1.1",
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jsdoc": "^39.6.10",
"eslint-plugin-sf-plugin": "^1.6.3",
"eslint-plugin-sf-plugin": "^1.11.0",
"husky": "^7.0.4",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"oclif": "^3.6.3",
"prettier": "^2.8.3",
"oclif": "^3.7.0",
"prettier": "^2.8.4",
"pretty-quick": "^3.1.0",
"shx": "0.3.4",
"sinon": "^11.1.1",
"sinon-chai": "^3.7.0",
"ts-node": "^10.4.0",
"typescript": "^4.9.5"
"typescript": "^4.9.5",
"wireit": "^0.9.5"
},
"config": {},
"engines": {
Expand Down Expand Up @@ -107,27 +109,129 @@
},
"repository": "salesforcecli/plugin-info",
"scripts": {
"build": "sf-build",
"build": "wireit",
"clean": "sf-clean",
"clean-all": "sf-clean all",
"clean:lib": "shx rm -rf lib && shx rm -rf coverage && shx rm -rf .nyc_output && shx rm -f oclif.manifest.json",
"compile": "sf-compile",
"compile": "wireit",
"docs": "sf-docs",
"format": "sf-format",
"lint": "sf-lint",
"format": "wireit",
"lint": "wireit",
"postpack": "shx rm -f oclif.manifest.json",
"posttest": "yarn lint && yarn test:deprecation-policy && yarn test:command-reference",
"prepack": "sf-prepack",
"prepare": "sf-install",
"pretest": "sf-compile-test",
"test": "sf-test",
"test:command-reference": "./bin/dev commandreference:generate --erroronwarnings",
"test:deprecation-policy": "./bin/dev snapshot:compare",
"test:json-schema": "./bin/dev schema:compare",
"test": "wireit",
"test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel",
"test:only": "wireit",
"version": "oclif-dev readme"
},
"publishConfig": {
"access": "public"
},
"wireit": {
"build": {
"dependencies": [
"compile",
"lint"
]
},
"compile": {
"command": "tsc -p . --pretty --incremental",
"files": [
"src/**/*.ts",
"tsconfig.json",
"messages/**"
],
"output": [
"lib/**",
"*.tsbuildinfo"
],
"clean": "if-file-deleted"
},
"format": {
"command": "prettier --write \"+(src|test|schemas)/**/*.+(ts|js|json)|command-snapshot.json\"",
"files": [
"src/**/*.ts",
"test/**/*.ts",
"schemas/**/*.json",
"command-snapshot.json",
".prettier*"
],
"output": []
},
"lint": {
"command": "eslint src test --color --cache --cache-location .eslintcache",
"files": [
"src/**/*.ts",
"test/**/*.ts",
"messages/**",
".eslint*"
],
"output": []
},
"test:compile": {
"command": "tsc -p \"./test\" --pretty",
"files": [
"test/**/*.ts",
"tsconfig.json",
"test/tsconfig.json"
],
"output": []
},
"test": {
"dependencies": [
"test:compile",
"test:only",
"test:command-reference",
"test:deprecation-policy",
"lint",
"test:json-schema"
]
},
"test:only": {
"command": "nyc mocha \"test/**/*.test.ts\"",
"env": {
"FORCE_COLOR": "2"
},
"files": [
"test/**/*.ts",
"src/**/*.ts",
"tsconfig.json",
".mocha*",
"test/tsconfig.json",
"!*.nut.ts",
".nycrc"
],
"output": []
},
"test:command-reference": {
"command": "\"./bin/dev\" commandreference:generate --erroronwarnings",
"files": [
"src/**/*.ts",
"messages/**",
"package.json"
],
"output": [
"tmp/root"
]
},
"test:deprecation-policy": {
"command": "\"./bin/dev\" snapshot:compare",
"files": [
"src/**/*.ts"
],
"output": [],
"dependencies": [
"compile"
]
},
"test:json-schema": {
"command": "\"./bin/dev\" schema:compare",
"files": [
"src/**/*.ts",
"schemas"
],
"output": []
}
}
}
28 changes: 4 additions & 24 deletions src/commands/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import * as open from 'open';
import got from 'got';
import * as ProxyAgent from 'proxy-agent';
import { getProxyForUrl } from 'proxy-from-env';
import { PluginVersionDetail } from '@oclif/core/lib/interfaces';
import { Doctor as SFDoctor, SfDoctor, SfDoctorDiagnosis } from '../doctor';
import { DiagnosticStatus } from '../diagnostics';

Expand Down Expand Up @@ -53,8 +52,8 @@ export default class Doctor extends SfCommand<SfDoctorDiagnosis> {
// such as running a command and running diagnostics.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private tasks: Array<Promise<any>> = [];
private doctor: SfDoctor;
private outputDir: string;
private doctor!: SfDoctor;
private outputDir: string = process.cwd();
private filesWrittenMsgs: string[] = [];

public async run(): Promise<SfDoctorDiagnosis> {
Expand Down Expand Up @@ -158,7 +157,7 @@ export default class Doctor extends SfCommand<SfDoctorDiagnosis> {
const info = `
\`\`\`
${diagnosis.cliConfig.userAgent}
${this.formatPlugins(diagnosis.versionDetail.pluginVersions).join(os.EOL)}
${(diagnosis.versionDetail.pluginVersions ?? []).join(os.EOL)}
\`\`\`
${
diagnosis.sfdxEnvVars.length
Expand Down Expand Up @@ -248,7 +247,7 @@ ${this.doctor
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises
cp.on('exit', async (code) => {
this.doctor.setExitCode(code);
this.doctor.setExitCode(code ?? 0);
await this.doctor.writeStdout(`\nCommand exit code: ${code}\n`);
this.doctor.closeStdout();
this.doctor.closeStderr();
Expand All @@ -259,23 +258,4 @@ ${this.doctor
});
this.tasks.push(execPromise);
}

private formatPlugins(plugins: Record<string, PluginVersionDetail>): string[] {
return Object.entries(plugins)
.map(([name, plugin]) => ({ name, ...plugin }))
.sort((a, b) => (a.name > b.name ? 1 : -1))
.map((plugin) =>
`${this.getFriendlyName(plugin.name)} ${plugin.version} (${plugin.type}) ${
plugin.type === 'link' ? plugin.root : ''
}`.trim()
);
}

private getFriendlyName(name: string): string {
const scope = this.config.pjson.oclif.scope;
if (!scope) return name;
const match = name.match(`@${scope}/plugin-(.+)`);
if (!match) return name;
return match[1];
}
}
4 changes: 2 additions & 2 deletions src/commands/info/releasenotes/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const HIDE_FOOTER = 'SFDX_HIDE_RELEASE_NOTES_FOOTER';
// or any library that is using the messages framework can also be loaded this way.
const messages = Messages.loadMessages('@salesforce/plugin-info', 'display');

export default class Display extends SfCommand<DisplayOutput> {
export default class Display extends SfCommand<DisplayOutput | undefined> {
private static helpers = ['stable', 'stable-rc', 'latest', 'latest-rc', 'rc'];

public static readonly summary = messages.getMessage('summary');
Expand All @@ -52,7 +52,7 @@ export default class Display extends SfCommand<DisplayOutput> {
loglevel,
};

public async run(): Promise<DisplayOutput> {
public async run(): Promise<DisplayOutput | undefined> {
const logger = Logger.childFromRoot(this.constructor.name);
const { flags } = await this.parse(Display);
const env = new Env();
Expand Down
2 changes: 1 addition & 1 deletion src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class Diagnostics {
const testName = 'salesforcedx plugin not installed';
let status: DiagnosticStatus['status'] = 'pass';

const plugins = Object.keys(this.diagnosis.versionDetail.pluginVersions ?? {});
const plugins = Object.keys(this.config.versionDetails.pluginVersions ?? {});
if (plugins?.some((p) => p === 'salesforcedx')) {
status = 'fail';
const bin = this.diagnosis.cliConfig.bin;
Expand Down
31 changes: 26 additions & 5 deletions src/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Messages, SfError } from '@salesforce/core';
import { Env, omit } from '@salesforce/kit';
import { AnyJson, KeyValue } from '@salesforce/ts-types';
import { Interfaces } from '@oclif/core';
import { PluginVersionDetail } from '@oclif/core/lib/interfaces';
import { Diagnostics, DiagnosticStatus } from './diagnostics';

export interface SfDoctor {
Expand All @@ -34,7 +35,7 @@ export interface SfDoctor {
type CliConfig = Partial<Interfaces.Config> & { nodeEngine: string };

export interface SfDoctorDiagnosis {
versionDetail: Interfaces.VersionDetails;
versionDetail: Omit<Interfaces.VersionDetails, 'pluginVersions'> & { pluginVersions: string[] };
sfdxEnvVars: Array<KeyValue<string>>;
sfEnvVars: Array<KeyValue<string>>;
cliConfig: CliConfig;
Expand Down Expand Up @@ -66,8 +67,8 @@ export class Doctor implements SfDoctor {

// Contains all gathered data and results of diagnostics.
private diagnosis: SfDoctorDiagnosis;
private stdoutWriteStream: fs.WriteStream;
private stderrWriteStream: fs.WriteStream;
private stdoutWriteStream: fs.WriteStream | undefined;
private stderrWriteStream: fs.WriteStream | undefined;

private constructor(config: Interfaces.Config) {
this.id = Date.now();
Expand All @@ -78,8 +79,10 @@ export class Doctor implements SfDoctor {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
cliConfig.nodeEngine = config.pjson.engines.node as string;

const { pluginVersions, ...versionDetails } = config.versionDetails;

this.diagnosis = {
versionDetail: config.versionDetails,
versionDetail: { ...versionDetails, pluginVersions: formatPlugins(config, pluginVersions ?? {}) },
sfdxEnvVars,
sfEnvVars,
cliConfig,
Expand Down Expand Up @@ -210,7 +213,7 @@ export class Doctor implements SfDoctor {
}

public writeStderr(contents: string): Promise<boolean> {
if (!this.stdoutWriteStream) {
if (!this.stderrWriteStream) {
throw new SfError(messages.getMessage('doctorNotInitializedError'), 'SfDoctorInitError');
}
return this.writeFile(this.stderrWriteStream, contents);
Expand Down Expand Up @@ -265,3 +268,21 @@ export class Doctor implements SfDoctor {
}
}
}

export function formatPlugins(config: Interfaces.Config, plugins: Record<string, PluginVersionDetail>): string[] {
function getFriendlyName(name: string): string {
const scope = config?.pjson?.oclif?.scope;
if (!scope) return name;
const match = name.match(`@${scope}/plugin-(.+)`);
if (!match) return name;
return match[1];
}
return Object.entries(plugins)
.map(([name, plugin]) => ({ name, ...plugin }))
.sort((a, b) => (a.name > b.name ? 1 : -1))
.map((plugin) =>
`${getFriendlyName(plugin.name)} ${plugin.version} (${plugin.type}) ${
plugin.type === 'link' ? plugin.root : ''
}`.trim()
);
}
2 changes: 1 addition & 1 deletion src/shared/getInfoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { join } from 'path';
import * as fs from 'fs';
import { PJSON } from '@oclif/config';
import { PJSON } from '@oclif/core/lib/interfaces';

export interface PjsonWithInfo extends PJSON {
oclif: PJSON['oclif'] & {
Expand Down
Loading

0 comments on commit f77c7ed

Please sign in to comment.