Skip to content

Commit

Permalink
feat(cmd): Support watch command (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
AielloChan committed Mar 5, 2024
1 parent 6421120 commit 728655c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.2.0",
"version": "0.3.0",
"configurations": [
{
"name": "Launch Extension",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vscode-vitest",
"displayName": "Vitest Runner for VSCode that actually work",
"version": "0.2.0",
"version": "0.3.0",
"main": "dist/index.js",
"icon": "logo.png",
"license": "MIT",
Expand Down
6 changes: 5 additions & 1 deletion src/codelens.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as ts from 'typescript';
import * as vscode from 'vscode';
import {convertCancellationTokenToAbortSignal} from './utils';
import {DebugVitestCommand, RunVitestCommand} from './vscode';
import {DebugVitestCommand, RunVitestCommand, WatchVitestCommand} from './vscode';
import {TestTreeBuilder} from "./test-tree/build";
import {TestTreeNode} from './test-tree/types';

Expand Down Expand Up @@ -32,6 +32,10 @@ export class CodeLensProvider implements vscode.CodeLensProvider {
new vscode.CodeLens(
new vscode.Range(start, end),
new DebugVitestCommand(testNode.name, document.fileName)
),
new vscode.CodeLens(
new vscode.Range(start, end),
new WatchVitestCommand(testNode.name, document.fileName)
)
];
});
Expand Down
26 changes: 24 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ function getCwd(testFile: string) {
return path.dirname(configFilePath);
}

function buildVitestArgs({ caseName, casePath, sanitize = true }: { caseName: string, casePath: string, sanitize?: boolean }) {
function buildVitestArgs({ caseName, casePath, sanitize = true, command = 'run' }: { caseName: string, casePath: string, sanitize?: boolean, command?: 'run' | 'watch' }) {
let sanitizedCasePath = casePath;
if (sanitize) {
sanitizedCasePath = JSON.stringify(casePath);
caseName = JSON.stringify(caseName);
}

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

const rootDir = getCwd(casePath);
if (rootDir) {
Expand Down Expand Up @@ -57,6 +57,28 @@ export async function runInTerminal(text: string, filename: string) {
terminal.show();
}

export async function watchInTerminal(text: string, filename: string) {
let terminalAlreadyExists = true;
if (!terminal || terminal.exitStatus) {
terminalAlreadyExists = false;
terminal?.dispose();
terminal = vscode.window.createTerminal(`vscode-vitest-runner`);
}

const vitestArgs = buildVitestArgs({ command: 'watch', caseName: text, casePath: filename });
const npxArgs = ['npx', ...vitestArgs];

if (terminalAlreadyExists) {
// CTRL-C to stop the previous run
terminal.sendText('\x03');
}

await saveFile(filename);

terminal.sendText(npxArgs.join(' '), true);
terminal.show();
}

function buildDebugConfig(
casePath: string,
text: string
Expand Down
20 changes: 19 additions & 1 deletion src/vscode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { debugInTerminal, runInTerminal } from './run';
import { debugInTerminal, runInTerminal, watchInTerminal } from './run';

export class RunVitestCommand implements vscode.Command {
static ID = 'vitest.runTest';
Expand All @@ -12,6 +12,17 @@ export class RunVitestCommand implements vscode.Command {
}
}

export class WatchVitestCommand implements vscode.Command {
static ID = 'vitest.watchTest';
title = 'Watch(Vitest)';
command = WatchVitestCommand.ID;
arguments?: [string, string];

constructor(text: string, filename: string) {
this.arguments = [text, filename];
}
}

export class DebugVitestCommand implements vscode.Command {
static ID = 'vitest.debugTest';
title = 'Debug(Vitest)';
Expand All @@ -30,6 +41,13 @@ vscode.commands.registerCommand(
}
);

vscode.commands.registerCommand(
WatchVitestCommand.ID,
(text: string, filename: string) => {
watchInTerminal(text, filename);
}
);

vscode.commands.registerCommand(
DebugVitestCommand.ID,
(text: string, filename: string) => {
Expand Down

0 comments on commit 728655c

Please sign in to comment.