Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support to monorepo and fix varius issues #10

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@
"package": "yarn build && yarn vsce package",
"format:check": "prettier --config ./prettier.config.json --c ./src/**/*.ts",
"format:write": "yarn format:check --write"
},
"dependencies": {
"find-up": "^5.0.0"
}
}
59 changes: 42 additions & 17 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,65 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as findUp from 'find-up';
import { configFiles } from './vitest-config-files';

function buildVitestArgs(text: string) {
return ['vitest', 'run', '-t', text];
function getCwd(testFile: string) {
const configFilePath = findUp.sync(configFiles, { cwd: testFile });

if (!configFilePath) {
return;
}
return path.dirname(configFilePath);
}

function buildCdArgs(path: string) {
return ['cd', path];
function buildVitestArgs({ caseName, casePath, sanitize = true }: { caseName: string, casePath: string, sanitize?: boolean }) {
let sanitizedCasePath = casePath;
if (sanitize) {
sanitizedCasePath = JSON.stringify(casePath);
caseName = JSON.stringify(caseName);
}

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

const rootDir = getCwd(casePath);
if (rootDir) {
args.push('--root', rootDir);
}

return args;
}

let terminal: vscode.Terminal | undefined;

export function runInTerminal(text: string, filename: string) {
const casePath = path.dirname(filename);
const terminal = vscode.window.createTerminal(`vitest - ${text}`);
let terminalAlreadyExists = true;
if (!terminal || terminal.exitStatus) {
terminalAlreadyExists = false;
terminal?.dispose();
terminal = vscode.window.createTerminal(`vscode-vitest-runner`);
}
Comment on lines +35 to +40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻 hope this change is released soon. Would like this behavior.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forked and published my own package given this is not maintained https://marketplace.visualstudio.com/items?itemName=rluvaton.vscode-vitest


const casePathStr = JSON.stringify(casePath);
const caseNameStr = JSON.stringify(text);
const vitestArgs = buildVitestArgs({ caseName: text, casePath: filename });
const npxArgs = ['npx', ...vitestArgs];

const cdArgs = buildCdArgs(casePathStr);
terminal.sendText(cdArgs.join(' '), true);
if (terminalAlreadyExists) {
// CTRL-C to stop the previous run
terminal.sendText('\x03');
}

const vitestArgs = buildVitestArgs(caseNameStr);
const npxArgs = ['npx', ...vitestArgs];
terminal.sendText(npxArgs.join(' '), true);
terminal.show();
}

function buildDebugConfig(
cwd: string,
casePath: string,
text: string
): vscode.DebugConfiguration {
return {
name: 'Debug vitest case',
request: 'launch',
runtimeArgs: buildVitestArgs(text),
cwd,
runtimeArgs: buildVitestArgs({ caseName: text, casePath: casePath, sanitize: false }),
cwd: getCwd(casePath) || path.dirname(casePath),
runtimeExecutable: 'npx',
skipFiles: ['<node_internals>/**'],
type: 'pwa-node',
Expand All @@ -43,7 +69,6 @@ function buildDebugConfig(
}

export function debugInTermial(text: string, filename: string) {
const casePath = path.dirname(filename);
const config = buildDebugConfig(casePath, text);
const config = buildDebugConfig(filename, text);
vscode.debug.startDebugging(undefined, config);
}
16 changes: 16 additions & 0 deletions src/vitest-config-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

const CONFIG_NAMES = [
'vitest.config',
'vite.config',
]

const CONFIG_EXTENSIONS = [
'.ts',
'.mts',
'.cts',
'.js',
'.mjs',
'.cjs',
]

export const configFiles = CONFIG_NAMES.reduce((arr, name) => arr.concat(CONFIG_EXTENSIONS.map(ext => name + ext)), [] as string[]);