diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bd553f918..7a718a5b83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Add `--log-level` option for `vti diagnostics` to configure log level to print. #2752. - 🙌 Semantic tokens for typescript and highlight `.value` if using composition API. Thanks to contribution from [@jasonlyu123](https://github.com/jasonlyu123). #2802 #1904 # 2434 +- Add paths option for `vti diagnostics` to diagnose only sub files or directories. #2455. ### 0.33.1 | 2021-03-07 | [VSIX](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/octref/vsextensions/vetur/0.33.1/vspackage) diff --git a/vti/src/cli.ts b/vti/src/cli.ts index 0596393039..e027ea3818 100644 --- a/vti/src/cli.ts +++ b/vti/src/cli.ts @@ -15,7 +15,7 @@ function validateLogLevel(logLevelInput: unknown): logLevelInput is LogLevel { program.name('vti').description('Vetur Terminal Interface').version(getVersion()); program - .command('diagnostics [workspace]') + .command('diagnostics [workspace] [paths...]') .description('Print all diagnostics') .addOption( new Option('-l, --log-level ', 'Log level to print') @@ -23,12 +23,12 @@ function validateLogLevel(logLevelInput: unknown): logLevelInput is LogLevel { // logLevels is readonly array but .choices need read-write array (because of weak typing) .choices((logLevels as unknown) as string[]) ) - .action(async (workspace, options) => { + .action(async (workspace, paths, options) => { const logLevelOption: unknown = options.logLevel; if (!validateLogLevel(logLevelOption)) { throw new Error(`Invalid log level: ${logLevelOption}`); } - await diagnostics(workspace, logLevelOption); + await diagnostics(workspace, paths, logLevelOption); }); program.parse(process.argv); diff --git a/vti/src/commands/diagnostics.ts b/vti/src/commands/diagnostics.ts index 93632edecb..02ec65a5b7 100644 --- a/vti/src/commands/diagnostics.ts +++ b/vti/src/commands/diagnostics.ts @@ -33,7 +33,7 @@ const logLevel2Severity = { HINT: DiagnosticSeverity.Hint }; -export async function diagnostics(workspace: string | null, logLevel: LogLevel) { +export async function diagnostics(workspace: string | null, paths: string[], logLevel: LogLevel) { console.log('===================================='); console.log('Getting Vetur diagnostics'); let workspaceUri; @@ -47,7 +47,7 @@ export async function diagnostics(workspace: string | null, logLevel: LogLevel) workspaceUri = URI.file(process.cwd()); } - const errCount = await getDiagnostics(workspaceUri, logLevel2Severity[logLevel]); + const errCount = await getDiagnostics(workspaceUri, paths, logLevel2Severity[logLevel]); console.log('===================================='); if (errCount === 0) { @@ -121,10 +121,28 @@ function range2Location(range: Range): SourceLocation { }; } -async function getDiagnostics(workspaceUri: URI, severity: DiagnosticSeverity) { +async function getDiagnostics(workspaceUri: URI, paths: string[], severity: DiagnosticSeverity) { const clientConnection = await prepareClientConnection(workspaceUri); - const files = glob.sync('**/*.vue', { cwd: workspaceUri.fsPath, ignore: ['node_modules/**'] }); + let files: string[]; + if (paths.length === 0) { + files = glob.sync('**/*.vue', { cwd: workspaceUri.fsPath, ignore: ['node_modules/**'] }); + } else { + // Could use `flatMap` once available: + const listOfPaths = paths.map(inputPath => { + const absPath = path.resolve(workspaceUri.fsPath, inputPath); + + if (fs.lstatSync(absPath).isFile()) { + return [inputPath]; + } + + const directory = URI.file(absPath); + const directoryFiles = glob.sync('**/*.vue', { cwd: directory.fsPath, ignore: ['node_modules/**'] }); + return directoryFiles.map(f => path.join(inputPath, f)); + }); + + files = listOfPaths.reduce((acc: string[], paths) => [...acc, ...paths], []); + } if (files.length === 0) { console.log('No input files');