Skip to content

Commit

Permalink
fix: use vscode stat API instead of Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 2, 2024
1 parent 58992dc commit 2659b4d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { stat } from 'node:fs/promises'
import { relative } from 'node:path'
import * as vscode from 'vscode'
import { normalize } from 'pathe'
Expand Down Expand Up @@ -39,7 +38,7 @@ export class ExtensionWatcher extends vscode.Disposable {

watcher.onDidChange(async (file) => {
const filepath = normalize(file.fsPath)
if (await this.shouldIgnoreFile(filepath)) {
if (await this.shouldIgnoreFile(file)) {
return
}
log.verbose?.('[VSCODE] File changed:', relative(api.workspaceFolder.uri.fsPath, file.fsPath))
Expand All @@ -48,16 +47,29 @@ export class ExtensionWatcher extends vscode.Disposable {

watcher.onDidCreate(async (file) => {
const filepath = normalize(file.fsPath)
if (await this.shouldIgnoreFile(filepath)) {
if (await this.shouldIgnoreFile(file)) {
return
}
log.verbose?.('[VSCODE] File created:', relative(api.workspaceFolder.uri.fsPath, file.fsPath))
api.onFileCreated(filepath)
})
}

private async shouldIgnoreFile(filepath: string) {
const stats = await stat(filepath).catch(() => null)
return !stats || stats.isDirectory() || mm.isMatch(filepath, this.ignorePattern)
private async shouldIgnoreFile(file: vscode.Uri) {
try {
const stats = await vscode.workspace.fs.stat(file)
if (
// if not a file
stats.type !== vscode.FileType.File
// if not a symlinked file
&& stats.type !== (vscode.FileType.File | vscode.FileType.SymbolicLink)
) {
return false
}
return mm.isMatch(file.fsPath, this.ignorePattern)
}
catch {
return false
}
}
}
28 changes: 26 additions & 2 deletions src/worker/vitest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { readFileSync } from 'node:fs'
import type { Vitest as VitestCore, WorkspaceProject } from 'vitest/node'
import { relative } from 'pathe'
import mm from 'micromatch'
import type { VitestMethods } from '../api/rpc'
import { VitestWatcher } from './watcher'
import { VitestCoverage } from './coverage'
Expand Down Expand Up @@ -195,9 +197,13 @@ export class Vitest implements VitestMethods {

for (const file of files) {
this.updateLastChanged(file)
const content = readFileSync(file, 'utf-8')
let content: string | null = null
for (const project of this.ctx.projects) {
if (await project.isTargetFile(file, content)) {
if (this.isTestFile(
project,
file,
() => content ?? (content = readFileSync(file, 'utf-8')),
)) {
testFiles.push(file)
project.testFilesList?.push(file)
this.ctx.changedTests.add(file)
Expand All @@ -212,6 +218,24 @@ export class Vitest implements VitestMethods {
}
}

isTestFile(project: WorkspaceProject, file: string, getContent: () => string) {
const relativeId = relative(project.config.dir || project.config.root, file)
if (mm.isMatch(relativeId, project.config.exclude)) {
return false
}
if (mm.isMatch(relativeId, project.config.include)) {
return true
}
if (
project.config.includeSource?.length
&& mm.isMatch(relativeId, project.config.includeSource)
) {
const source = getContent()
return source.includes('import.meta.vitest')
}
return false
}

unwatchTests() {
return this.watcher.stopTracking()
}
Expand Down

0 comments on commit 2659b4d

Please sign in to comment.