Skip to content

add --log-level option to vti diagnostics #2752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 19 additions & 4 deletions vti/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from 'commander';
import { diagnostics } from './commands/diagnostics';
import { Command, Option } from 'commander';
import { diagnostics, logLevels } from './commands/diagnostics';

function getVersion(): string {
const { version }: { version: string } = require('../package.json');
Expand All @@ -13,8 +13,23 @@ function getVersion(): string {
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 (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't use logLevels from import?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yoyo930021 Because logLevels type is readonly ["ERROR", "WARN", "INFO", "HINT"], we cannot call logLevels.includes with unknown value.

related issue: microsoft/TypeScript#26255

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use as keyword or is function to force it.
Repeated text will cause follow-up maintenance problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yoyo930021 ok. I've removed repeated text. e905cb2

logLevelOption !== 'ERROR' &&
logLevelOption !== 'WARN' &&
logLevelOption !== 'INFO' &&
logLevelOption !== 'HINT'
) {
throw new Error(`Invalid log level: ${logLevelOption}`);
}
await diagnostics(workspace, logLevelOption);
});

program.parse(process.argv);
Expand Down
24 changes: 20 additions & 4 deletions vti/src/commands/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ 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;

export async function diagnostics(workspace: string | null, logLevel: LogLevel) {
console.log('====================================');
console.log('Getting Vetur diagnostics');
let workspaceUri;
Expand All @@ -38,7 +41,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 +115,20 @@ function range2Location(range: Range): SourceLocation {
};
}

async function getDiagnostics(workspaceUri: URI) {
function logLevel2Severity(logLevel: LogLevel): DiagnosticSeverity {
switch (logLevel) {
case 'ERROR':
return DiagnosticSeverity.Error;
case 'WARN':
return DiagnosticSeverity.Warning;
case 'INFO':
return DiagnosticSeverity.Information;
case 'HINT':
return DiagnosticSeverity.Hint;
}
}

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 +163,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