11/* eslint-disable no-console */
22
33import { mkdirSync , writeFileSync } from 'node:fs'
4- import { dirname , resolve } from 'pathe'
4+ import { dirname , relative , resolve } from 'pathe'
55import type { UserConfig as ViteUserConfig } from 'vite'
66import type { File , Suite , Task } from '@vitest/runner'
77import { CoverageProviderMap } from '../../integrations/coverage'
@@ -12,6 +12,7 @@ import type { Vitest, VitestOptions } from '../core'
1212import { FilesNotFoundError , GitNotFoundError } from '../errors'
1313import { getNames , getTests } from '../../utils'
1414import type { UserConfig , VitestEnvironment , VitestRunMode } from '../types/config'
15+ import type { WorkspaceSpec } from '../pool'
1516
1617export interface CliOptions extends UserConfig {
1718 /**
@@ -26,6 +27,10 @@ export interface CliOptions extends UserConfig {
2627 * Output collected tests as JSON or to a file
2728 */
2829 json ?: string | boolean
30+ /**
31+ * Output collected test files only
32+ */
33+ filesOnly ?: boolean
2934}
3035
3136/**
@@ -184,6 +189,48 @@ export function processCollected(ctx: Vitest, files: File[], options: CliOptions
184189 return formatCollectedAsString ( files ) . forEach ( test => console . log ( test ) )
185190}
186191
192+ export function outputFileList ( files : WorkspaceSpec [ ] , options : CliOptions ) {
193+ if ( typeof options . json !== 'undefined' ) {
194+ return outputJsonFileList ( files , options )
195+ }
196+
197+ return formatFilesAsString ( files , options ) . map ( file => console . log ( file ) )
198+ }
199+
200+ function outputJsonFileList ( files : WorkspaceSpec [ ] , options : CliOptions ) {
201+ if ( typeof options . json === 'boolean' ) {
202+ return console . log ( JSON . stringify ( formatFilesAsJSON ( files ) , null , 2 ) )
203+ }
204+ if ( typeof options . json === 'string' ) {
205+ const jsonPath = resolve ( options . root || process . cwd ( ) , options . json )
206+ mkdirSync ( dirname ( jsonPath ) , { recursive : true } )
207+ writeFileSync ( jsonPath , JSON . stringify ( formatFilesAsJSON ( files ) , null , 2 ) )
208+ }
209+ }
210+
211+ function formatFilesAsJSON ( files : WorkspaceSpec [ ] ) {
212+ return files . map ( ( file ) => {
213+ const result : any = {
214+ file : file . moduleId ,
215+ }
216+
217+ if ( file . project . name ) {
218+ result . projectName = file . project . name
219+ }
220+ return result
221+ } )
222+ }
223+
224+ function formatFilesAsString ( files : WorkspaceSpec [ ] , options : CliOptions ) {
225+ return files . map ( ( file ) => {
226+ let name = relative ( options . root || process . cwd ( ) , file . moduleId )
227+ if ( file . project . name ) {
228+ name = `[${ file . project . name } ] ${ name } `
229+ }
230+ return name
231+ } )
232+ }
233+
187234function processJsonOutput ( files : File [ ] , options : CliOptions ) {
188235 if ( typeof options . json === 'boolean' ) {
189236 return console . log ( JSON . stringify ( formatCollectedAsJSON ( files ) , null , 2 ) )
0 commit comments