-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.ts
60 lines (52 loc) · 1.92 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { commands, window, ExtensionContext, Uri } from 'vscode';
import { escapeRegExpForPath, normalizePath, quote } from './util';
export function activate({ subscriptions }: ExtensionContext) {
let NEXT_TERM_ID = 1;
const singleWatch = commands.registerCommand(
'jestcontextmenurunner.singleWatch',
(uri: Uri) => {
window.showInformationMessage(
`Running tests in watch mode for ${getFileName(uri)} file.`
);
executeCommandInTerminal(NEXT_TERM_ID++, `${uri.fsPath}`, ['--watch']);
}
);
const single = commands.registerCommand(
'jestcontextmenurunner.single',
(uri: Uri) => {
window.showInformationMessage(
`Running tests for ${getFileName(uri)} file.`
);
executeCommandInTerminal(NEXT_TERM_ID++, uri.fsPath);
}
);
const all = commands.registerCommand('jestcontextmenurunner.all', () => {
window.showInformationMessage('Running all tests.');
executeCommandInTerminal(NEXT_TERM_ID++);
});
const allWatch = commands.registerCommand(
'jestcontextmenurunner.allWatch',
() => {
window.showInformationMessage('Running all tests in watch mode.');
executeCommandInTerminal(NEXT_TERM_ID++, '', ['--watch']);
}
);
subscriptions.push(singleWatch);
subscriptions.push(single);
subscriptions.push(all);
subscriptions.push(allWatch);
}
function getFileName(uri: Uri): string {
const searchResult = /^.+(\\|\/)(?<fileName>([^\\\/\n]+)(\.)?[^\n\.]+)$/.exec(
uri.fsPath
);
return searchResult?.groups?.fileName || '';
}
function executeCommandInTerminal(id: number, fileName = '', params: string[] = []): void {
const terminal = window.createTerminal(`Ext Terminal #${id}`);
let command = `npm run env -- jest --color`;
command += params.length > 0 ? ` ${params.join(' ')}` : '';
command += fileName ? ` ${quote(escapeRegExpForPath(normalizePath(fileName)))}` : '';
terminal.show(true);
terminal.sendText(command);
}