Skip to content

Commit 728655c

Browse files
authored
feat(cmd): Support watch command (#10)
1 parent 6421120 commit 728655c

5 files changed

Lines changed: 50 additions & 6 deletions

File tree

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.2.0",
2+
"version": "0.3.0",
33
"configurations": [
44
{
55
"name": "Launch Extension",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vscode-vitest",
33
"displayName": "Vitest Runner for VSCode that actually work",
4-
"version": "0.2.0",
4+
"version": "0.3.0",
55
"main": "dist/index.js",
66
"icon": "logo.png",
77
"license": "MIT",

src/codelens.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as ts from 'typescript';
22
import * as vscode from 'vscode';
33
import {convertCancellationTokenToAbortSignal} from './utils';
4-
import {DebugVitestCommand, RunVitestCommand} from './vscode';
4+
import {DebugVitestCommand, RunVitestCommand, WatchVitestCommand} from './vscode';
55
import {TestTreeBuilder} from "./test-tree/build";
66
import {TestTreeNode} from './test-tree/types';
77

@@ -32,6 +32,10 @@ export class CodeLensProvider implements vscode.CodeLensProvider {
3232
new vscode.CodeLens(
3333
new vscode.Range(start, end),
3434
new DebugVitestCommand(testNode.name, document.fileName)
35+
),
36+
new vscode.CodeLens(
37+
new vscode.Range(start, end),
38+
new WatchVitestCommand(testNode.name, document.fileName)
3539
)
3640
];
3741
});

src/run.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ function getCwd(testFile: string) {
1212
return path.dirname(configFilePath);
1313
}
1414

15-
function buildVitestArgs({ caseName, casePath, sanitize = true }: { caseName: string, casePath: string, sanitize?: boolean }) {
15+
function buildVitestArgs({ caseName, casePath, sanitize = true, command = 'run' }: { caseName: string, casePath: string, sanitize?: boolean, command?: 'run' | 'watch' }) {
1616
let sanitizedCasePath = casePath;
1717
if (sanitize) {
1818
sanitizedCasePath = JSON.stringify(casePath);
1919
caseName = JSON.stringify(caseName);
2020
}
2121

22-
const args = ['vitest', 'run', '--testNamePattern', caseName, sanitizedCasePath];
22+
const args = ['vitest', command, '--testNamePattern', caseName, sanitizedCasePath];
2323

2424
const rootDir = getCwd(casePath);
2525
if (rootDir) {
@@ -57,6 +57,28 @@ export async function runInTerminal(text: string, filename: string) {
5757
terminal.show();
5858
}
5959

60+
export async function watchInTerminal(text: string, filename: string) {
61+
let terminalAlreadyExists = true;
62+
if (!terminal || terminal.exitStatus) {
63+
terminalAlreadyExists = false;
64+
terminal?.dispose();
65+
terminal = vscode.window.createTerminal(`vscode-vitest-runner`);
66+
}
67+
68+
const vitestArgs = buildVitestArgs({ command: 'watch', caseName: text, casePath: filename });
69+
const npxArgs = ['npx', ...vitestArgs];
70+
71+
if (terminalAlreadyExists) {
72+
// CTRL-C to stop the previous run
73+
terminal.sendText('\x03');
74+
}
75+
76+
await saveFile(filename);
77+
78+
terminal.sendText(npxArgs.join(' '), true);
79+
terminal.show();
80+
}
81+
6082
function buildDebugConfig(
6183
casePath: string,
6284
text: string

src/vscode.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import { debugInTerminal, runInTerminal } from './run';
2+
import { debugInTerminal, runInTerminal, watchInTerminal } from './run';
33

44
export class RunVitestCommand implements vscode.Command {
55
static ID = 'vitest.runTest';
@@ -12,6 +12,17 @@ export class RunVitestCommand implements vscode.Command {
1212
}
1313
}
1414

15+
export class WatchVitestCommand implements vscode.Command {
16+
static ID = 'vitest.watchTest';
17+
title = 'Watch(Vitest)';
18+
command = WatchVitestCommand.ID;
19+
arguments?: [string, string];
20+
21+
constructor(text: string, filename: string) {
22+
this.arguments = [text, filename];
23+
}
24+
}
25+
1526
export class DebugVitestCommand implements vscode.Command {
1627
static ID = 'vitest.debugTest';
1728
title = 'Debug(Vitest)';
@@ -30,6 +41,13 @@ vscode.commands.registerCommand(
3041
}
3142
);
3243

44+
vscode.commands.registerCommand(
45+
WatchVitestCommand.ID,
46+
(text: string, filename: string) => {
47+
watchInTerminal(text, filename);
48+
}
49+
);
50+
3351
vscode.commands.registerCommand(
3452
DebugVitestCommand.ID,
3553
(text: string, filename: string) => {

0 commit comments

Comments
 (0)