Skip to content

Commit

Permalink
fix: potential extension activation error
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed May 18, 2022
1 parent 8522b9a commit e5cdae5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export async function activate(context: vscode.ExtensionContext) {

const vitestCmd = getVitestCommand(
vscode.workspace.workspaceFolders[0].uri.fsPath,
)
) ?? {
cmd: 'npx',
args: ['vitest'],
}
const vitestVersion = await getVitestVersion(vitestCmd)
console.dir({ vitestVersion })

Expand All @@ -63,14 +66,26 @@ export async function activate(context: vscode.ExtensionContext) {
context,
)
registerRunHandler(ctrl, testWatcher)
context.subscriptions.push(
vscode.commands.registerCommand(Command.UpdateSnapshot, (test) => {
updateSnapshot(ctrl, test)
}),
)
}
else {
const msg = 'Because Vitest version < 0.8.0, run/debug/watch tests from Vitest extension disabled.\n'
context.subscriptions.push(
vscode.commands.registerCommand(Command.ToggleWatching, () => {
vscode.window.showWarningMessage(msg)
}),
vscode.commands.registerCommand(Command.UpdateSnapshot, () => {
vscode.window.showWarningMessage(msg)
}),
)
// v0.8.0 introduce a breaking change in json format
// https://github.com/vitest-dev/vitest/pull/1034
// so we need to disable run & debug in version < 0.8.0
vscode.window.showWarningMessage(
'Because Vitest version < 0.8.0, run & debug tests from Vitest plugin disabled.\n',
)
vscode.window.showWarningMessage(msg)
}

vscode.window.visibleTextEditors.forEach(x =>
Expand All @@ -90,9 +105,6 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.workspace.onDidChangeTextDocument(e =>
fileDiscoverer.discoverTestFromDoc(ctrl, e.document),
),
vscode.commands.registerCommand(Command.UpdateSnapshot, (test) => {
updateSnapshot(ctrl, test)
}),
)
}

Expand Down
3 changes: 3 additions & 0 deletions src/pure/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export function getVitestPath(projectRoot: string): string | undefined {
export function getVitestCommand(
projectRoot: string,
): { cmd: string; args: string[] } | undefined {
if (!projectRoot)
return

const node_modules = path.resolve(projectRoot, 'node_modules')
try {
if (!existsSync(node_modules))
Expand Down

0 comments on commit e5cdae5

Please sign in to comment.