Skip to content

Commit

Permalink
Merge pull request #2752 from Monchi/vti-error-only
Browse files Browse the repository at this point in the history
add --log-level option to vti diagnostics
  • Loading branch information
yoyo930021 authored Mar 8, 2021
2 parents f9cd0cf + e905cb2 commit 6671ca4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 18 additions & 4 deletions vti/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
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<string>).includes(logLevelInput);
}

(async () => {
const program = new Command();
program.name('vti').description('Vetur Terminal Interface').version(getVersion());

program
.command('diagnostics [workspace]')
.description('Print all diagnostics')
.action(async workspace => {
await diagnostics(workspace);
.addOption(
new Option('-l, --log-level <logLevel>', '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);
Expand Down
17 changes: 13 additions & 4 deletions vti/src/commands/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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/**'] });
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6671ca4

Please sign in to comment.