Skip to content

Commit

Permalink
fix: vitest env detect error #61
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Aug 6, 2022
1 parent 4344ef2 commit 9edf4c1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode'
import semver from 'semver'
import type { WorkspaceConfiguration, WorkspaceFolder } from 'vscode'
import { isVitestEnv } from './pure/isVitestEnv'
import { isDefinitelyVitestEnv, mayBeVitestEnv } from './pure/isVitestEnv'
import { getVitestCommand, getVitestVersion, isNodeAvailable } from './pure/utils'
import { log } from './log'
export const extensionId = 'zxch3n.vitest-explorer'
Expand Down Expand Up @@ -57,13 +57,14 @@ export async function detectVitestEnvironmentFolders() {
return

for (const folder of vscode.workspace.workspaceFolders) {
if ((await isVitestEnv(folder) || getConfig(folder).enable) && !getRootConfig().disabledWorkspaceFolders.includes(folder.name))
if ((await mayBeVitestEnv(folder) || getConfig(folder).enable) && !getRootConfig().disabledWorkspaceFolders.includes(folder.name))
vitestFolders.push(folder)
}
}

export interface VitestWorkspaceConfig {
workspace: vscode.WorkspaceFolder
isUsingVitestForSure: boolean
cmd: string
args: string[]
version?: string
Expand All @@ -74,6 +75,7 @@ export interface VitestWorkspaceConfig {
export async function getVitestWorkspaceConfigs(): Promise<VitestWorkspaceConfig[]> {
return await Promise.all(vitestEnvironmentFolders.map(async (workspace) => {
const cmd = getVitestCommand(workspace.uri.fsPath)
const isUsingVitestForSure = getConfig(workspace).enable || await isDefinitelyVitestEnv(workspace) || (!!cmd)

const version = await getVitestVersion(cmd, getConfig(workspace).env || undefined).catch(async (e) => {
log.info(e.toString())
Expand All @@ -94,6 +96,7 @@ export async function getVitestWorkspaceConfigs(): Promise<VitestWorkspaceConfig
? {
workspace,
version,
isUsingVitestForSure,
cmd: cmd.cmd,
args: cmd.args,
isCompatible: isCompatibleVitestConfig({ version, workspace }),
Expand All @@ -102,6 +105,7 @@ export async function getVitestWorkspaceConfigs(): Promise<VitestWorkspaceConfig
: {
version,
workspace,
isUsingVitestForSure,
cmd: 'npx',
args: ['vitest'],
isCompatible: isCompatibleVitestConfig({ version, workspace }),
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function activate(context: vscode.ExtensionContext) {
log.error(msg)
// if the vitest detection is false positive, we may still reach here.
// but we can still use `.version` to filter some false positive
if (workspaceConfigs.some(x => x.version))
if (workspaceConfigs.some(x => x.isUsingVitestForSure))
vscode.window.showWarningMessage(msg)

context.subscriptions.push(
Expand Down Expand Up @@ -72,7 +72,7 @@ function workspacesCompatibilityCheck(workspaceConfigs: VitestWorkspaceConfig[])
})

// prompt error message if we can get the version from vitest, but it's not compatible with the extension
workspaceConfigs.filter(x => !x.isCompatible && x.version).forEach((config) => {
workspaceConfigs.filter(x => !x.isCompatible && x.isUsingVitestForSure).forEach((config) => {
vscode.window.showWarningMessage('Because Vitest version < 0.12.0'
+ `, run/debug/watch tests are disabled in workspace "${config.workspace.name}" \n`)
})
Expand Down
47 changes: 44 additions & 3 deletions src/pure/isVitestEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,47 @@ import { readFile, readdir } from 'fs-extra'
import type { WorkspaceFolder } from 'vscode'
import { getVitestPath } from './utils'

export async function isVitestEnv(projectRoot: string | WorkspaceFolder): Promise<boolean> {
export async function isDefinitelyVitestEnv(projectRoot: string | WorkspaceFolder): Promise<boolean> {
if (typeof projectRoot !== 'string')
return isVitestEnv(projectRoot.uri.fsPath)
return isDefinitelyVitestEnv(projectRoot.uri.fsPath)

if (getVitestPath(projectRoot))
return true

if (!existsSync(path.join(projectRoot, 'package.json')))
return false

const pkgPath = path.join(projectRoot, 'package.json') as string
const pkg = JSON.parse(await readFile(pkgPath, 'utf-8')) as any
if (existsSync(pkg)) {
if (pkg.devDependencies && pkg.devDependencies.vitest)
return true

if (pkg.dependencies && pkg.dependencies.vitest)
return true
}

if (
existsSync(path.join(projectRoot, 'vitest.config.js'))
|| existsSync(path.join(projectRoot, 'vitest.config.ts'))
)
return true

// monorepo
if (existsSync(path.join(projectRoot, 'packages'))) {
const dirs = await readdir(path.join(projectRoot, 'packages'))
for (const dir of dirs) {
if (await isDefinitelyVitestEnv(dir))
return true
}
}

return false
}

export async function mayBeVitestEnv(projectRoot: string | WorkspaceFolder): Promise<boolean> {
if (typeof projectRoot !== 'string')
return mayBeVitestEnv(projectRoot.uri.fsPath)

if (getVitestPath(projectRoot))
return true
Expand Down Expand Up @@ -41,7 +79,10 @@ export async function isVitestEnv(projectRoot: string | WorkspaceFolder): Promis
// monorepo
if (existsSync(path.join(projectRoot, 'packages'))) {
const dirs = await readdir(path.join(projectRoot, 'packages'))
return dirs.some(dir => isVitestEnv(dir))
for (const dir of dirs) {
if (await mayBeVitestEnv(dir))
return true
}
}

return false
Expand Down
7 changes: 6 additions & 1 deletion src/pure/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ export function getVitestPath(projectRoot: string): string | undefined {
}
}

/**
* if this function return a cmd, then this project is definitely using vitest
* @param projectRoot
* @returns
*/
export function getVitestCommand(
projectRoot: string,
): { cmd: string; args: string[] } | undefined {
if (!projectRoot)
if (!projectRoot || projectRoot.length < 5)
return

const node_modules = path.resolve(projectRoot, 'node_modules')
Expand Down

0 comments on commit 9edf4c1

Please sign in to comment.