diff --git a/CHANGELOG.md b/CHANGELOG.md index bc6d560236..1942f54ef0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### Not Released Yet + +- Add `--log-level` option for `vti diagnostics` to configure log level to print. #2752. + ### 0.33.1 | 2021-03-07 | [VSIX](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/octref/vsextensions/vetur/0.33.1/vspackage) - Added new ts and js snippets for the Composition API. Thanks to contribution from [@Namchee](https://github.com/Namchee). #2741 diff --git a/vti/src/cli.ts b/vti/src/cli.ts index 196d9dd7ee..0596393039 100644 --- a/vti/src/cli.ts +++ b/vti/src/cli.ts @@ -1,11 +1,15 @@ -import { Command } from 'commander'; -import { diagnostics } from './commands/diagnostics'; +import { Command, Option } from 'commander'; +import { diagnostics, LogLevel, logLevels } from './commands/diagnostics'; function getVersion(): string { const { version }: { version: string } = require('../package.json'); return `v${version}`; } +function validateLogLevel(logLevelInput: unknown): logLevelInput is LogLevel { + return typeof logLevelInput === 'string' && (logLevels as ReadonlyArray).includes(logLevelInput); +} + (async () => { const program = new Command(); program.name('vti').description('Vetur Terminal Interface').version(getVersion()); @@ -13,8 +17,18 @@ function getVersion(): string { program .command('diagnostics [workspace]') .description('Print all diagnostics') - .action(async workspace => { - await diagnostics(workspace); + .addOption( + new Option('-l, --log-level ', 'Log level to print') + .default('WARN') + // logLevels is readonly array but .choices need read-write array (because of weak typing) + .choices((logLevels as unknown) as string[]) + ) + .action(async (workspace, options) => { + const logLevelOption: unknown = options.logLevel; + if (!validateLogLevel(logLevelOption)) { + throw new Error(`Invalid log level: ${logLevelOption}`); + } + await diagnostics(workspace, logLevelOption); }); program.parse(process.argv); diff --git a/vti/src/commands/diagnostics.ts b/vti/src/commands/diagnostics.ts index 5866637758..93632edecb 100644 --- a/vti/src/commands/diagnostics.ts +++ b/vti/src/commands/diagnostics.ts @@ -24,7 +24,16 @@ import chalk from 'chalk'; import { codeFrameColumns, SourceLocation } from '@babel/code-frame'; import { Range } from 'vscode-languageclient'; -export async function diagnostics(workspace: string | null) { +export type LogLevel = typeof logLevels[number]; +export const logLevels = ['ERROR', 'WARN', 'INFO', 'HINT'] as const; +const logLevel2Severity = { + ERROR: DiagnosticSeverity.Error, + WARN: DiagnosticSeverity.Warning, + INFO: DiagnosticSeverity.Information, + HINT: DiagnosticSeverity.Hint +}; + +export async function diagnostics(workspace: string | null, logLevel: LogLevel) { console.log('===================================='); console.log('Getting Vetur diagnostics'); let workspaceUri; @@ -38,7 +47,7 @@ export async function diagnostics(workspace: string | null) { workspaceUri = URI.file(process.cwd()); } - const errCount = await getDiagnostics(workspaceUri); + const errCount = await getDiagnostics(workspaceUri, logLevel2Severity[logLevel]); console.log('===================================='); if (errCount === 0) { @@ -112,7 +121,7 @@ function range2Location(range: Range): SourceLocation { }; } -async function getDiagnostics(workspaceUri: URI) { +async function getDiagnostics(workspaceUri: URI, severity: DiagnosticSeverity) { const clientConnection = await prepareClientConnection(workspaceUri); const files = glob.sync('**/*.vue', { cwd: workspaceUri.fsPath, ignore: ['node_modules/**'] }); @@ -147,7 +156,7 @@ async function getDiagnostics(workspaceUri: URI) { /** * Ignore eslint errors for now */ - res = res.filter(r => r.source !== 'eslint-plugin-vue'); + res = res.filter(r => r.source !== 'eslint-plugin-vue').filter(r => r.severity && r.severity <= severity); if (res.length > 0) { res.forEach(d => { const location = range2Location(d.range);