Skip to content

Commit

Permalink
fix: resolve Vitest even if there is no config or package.json script
Browse files Browse the repository at this point in the history
Fixes #380
  • Loading branch information
sheremet-va committed May 14, 2024
1 parent 1104736 commit 463bdac
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions src/api/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,52 @@ export async function resolveVitestPackages(showWarning: boolean): Promise<Vites
// if no viable configs found, try to resolve via package.json
if (!vitest.meta.length && !vitest.warned) {
const pkg = await resolveVitestPackagesViaPackageJson(showWarning)
if (!pkg.meta.length && !pkg.warned)
return resolveVitestWorkspacePackages(showWarning).meta
return pkg.meta
}
return vitest.meta
}

function resolveVitestWorkspacePackages(showWarning: boolean) {
let warned = false
const meta: VitestPackage[] = []
vscode.workspace.workspaceFolders?.forEach((folder) => {
const cwd = normalize(folder.uri.fsPath)
const vitest = resolveVitestPackage(cwd, folder)
if (!vitest)
return

const pkg = _require(vitest.vitestPackageJsonPath)
if (!validateVitestPkg(showWarning, vitest.vitestPackageJsonPath, pkg)) {
warned = true
return
}
const id = normalize(folder.uri.fsPath)
const prefix = `${basename(cwd)}:${basename(id)}`
meta.push({
folder,
id,
cwd,
prefix,
vitestPackageJsonPath: vitest.vitestPackageJsonPath,
vitestNodePath: vitest.vitestNodePath,
version: pkg.version,
})
})
return {
meta,
warned,
}
}

export async function resolveVitestPackagesViaPackageJson(showWarning: boolean): Promise<{ meta: VitestPackage[]; warned: boolean }> {
const config = getConfig()

const packages = await vscode.workspace.findFiles('**/package.json', config.configSearchPatternExclude)
const packages = await vscode.workspace.findFiles(
'**/package.json',
config.configSearchPatternExclude,
)

let warned = false
const meta: VitestPackage[] = []
Expand All @@ -136,20 +173,26 @@ export async function resolveVitestPackagesViaPackageJson(showWarning: boolean):
return
}

for (const [scriptName, script] of scripts) {
const id = `${normalize(pkgPath.fsPath)}/${scriptName}`
const prefix = `package.json:${scriptName}`
meta.push({
folder,
id,
cwd,
prefix,
arguments: script,
vitestPackageJsonPath: vitest.vitestPackageJsonPath,
vitestNodePath: vitest.vitestNodePath,
version: pkg.version,
})
}
// take only the fist script to not pollute the list
const scriptOption = scripts[0]

if (!scriptOption)
return

const [scriptName, script] = scriptOption

const id = `${normalize(pkgPath.fsPath)}/${scriptName}`
const prefix = `${basename(cwd)}/package.json:${scriptName}`
meta.push({
folder,
id,
cwd,
prefix,
arguments: script,
vitestPackageJsonPath: vitest.vitestPackageJsonPath,
vitestNodePath: vitest.vitestNodePath,
version: pkg.version,
})
})

return {
Expand Down

0 comments on commit 463bdac

Please sign in to comment.