-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathdoctor.ts
83 lines (71 loc) · 2.28 KB
/
doctor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {
trackCommandMetadataUsage,
trackCommandUsage,
} from '../lib/usageTracking';
import { logger } from '@hubspot/local-dev-lib/logger';
import fs from 'fs';
import { Doctor } from '../lib/doctor/Doctor';
import { EXIT_CODES } from '../lib/enums/exitCodes';
import path from 'path';
import { ArgumentsCamelCase, BuilderCallback, Options } from 'yargs';
import { getCwd } from '@hubspot/local-dev-lib/path';
import { addGlobalOptions } from '../lib/commonOpts';
const { i18n } = require('../lib/lang');
export interface DoctorOptions {
'output-dir'?: string;
}
export const command = 'doctor';
export const describe = i18n(`commands.doctor.describe`);
export const handler = async ({
outputDir,
}: ArgumentsCamelCase<DoctorOptions>) => {
const doctor = new Doctor();
trackCommandUsage(command, undefined, doctor.accountId || undefined);
const output = await doctor.diagnose();
const totalCount = (output?.errorCount || 0) + (output?.warningCount || 0);
if (totalCount > 0) {
trackCommandMetadataUsage(
command,
{ successful: false, type: totalCount },
doctor.accountId || undefined
);
}
if (!outputDir) {
if (output?.diagnosis) {
logger.log(output.diagnosis);
} else {
logger.error(i18n(`commands.doctor.errors.generatingDiagnosis`));
return process.exit(EXIT_CODES.ERROR);
}
return process.exit(EXIT_CODES.SUCCESS);
}
if (!path.isAbsolute(outputDir)) {
outputDir = path.join(getCwd(), outputDir);
}
const outputFile = path.join(
outputDir,
`hubspot-doctor-${new Date().toISOString()}.json`
);
try {
fs.writeFileSync(outputFile, JSON.stringify(output, null, 4));
logger.success(
i18n(`commands.doctor.outputWritten`, { filename: outputFile })
);
} catch (e) {
logger.error(
i18n(`commands.doctor.errors.unableToWriteOutputFile`, {
file: outputFile,
errorMessage: e instanceof Error ? e.message : e,
})
);
return process.exit(EXIT_CODES.ERROR);
}
return process.exit(EXIT_CODES.SUCCESS);
};
export const builder: BuilderCallback<DoctorOptions, DoctorOptions> = yargs => {
yargs.option<keyof DoctorOptions, Options>('output-dir', {
describe: i18n(`commands.doctor.options.outputDir`),
type: 'string',
});
addGlobalOptions(yargs);
};