Skip to content

Commit 8c68122

Browse files
authored
feat: allow commands to be run in the last active terminal (#43)
1 parent c0862f5 commit 8c68122

File tree

8 files changed

+99
-45
lines changed

8 files changed

+99
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44
.vscode-test/
55
*.vsix
66
.task
7+
test-workspace/.vscode

CHANGELOG.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
## Unreleased
44

55
- Improve error handling in when Taskfiles contain errors (#25 by @pd93).
6-
- Added a new command: `Task: Show Debug Panel` to show the Task debug panel (#25 by @pd93).
6+
- Added a new command: `Task: Show Debug Panel` to show the Task debug panel
7+
(#25 by @pd93).
78
- Added the ability to sort tasks in the tree view (#20 by @pd93).
8-
- Configurable via `task.tree.sort` setting (values: `"default"` (default), `"alphanumeric"` or `"none"`).
9-
- Added a cancel/timeout to file watcher to improve performance when making lots of file changes (#35 by @pd93).
10-
- For example, `git stash pop` of a lot of `.yml` files would cause a huge lag spike as multiple update calls were made.
11-
- This extension is now also published on the [Open VSX Registry](https://open-vsx.org/extension/task/vscode-task) (#26, #46 by @pd93).
9+
- Configurable via `task.tree.sort` setting (values: `"default"` (default),
10+
`"alphanumeric"` or `"none"`).
11+
- Added a cancel/timeout to file watcher to improve performance when making lots
12+
of file changes (#35 by @pd93).
13+
- For example, `git stash pop` of a lot of `.yml` files would cause a huge lag
14+
spike as multiple update calls were made.
15+
- Allow commands to be run from last active terminal instead of the output panel
16+
(#12, #43 by @pd93).
17+
- Configurable via `task.outputTo` setting (values: `output` (default) or
18+
`terminal`).
19+
- This extension is now also published on the
20+
[Open VSX Registry](https://open-vsx.org/extension/task/vscode-task) (#26, #46
21+
by @pd93).
1222
- This means you can now install it in [VSCodeium](https://vscodium.com/).
1323

1424
## v0.1.1 - 2021-03-27
1525

16-
- Fixed some installations (e.g. Brew) not detecting the Task version correctly (#13, #14 by @pd93).
26+
- Fixed some installations (e.g. Brew) not detecting the Task version correctly
27+
(#13, #14 by @pd93).
1728

1829
## v0.1.0 - 2023-03-26
1930

@@ -26,12 +37,15 @@
2637
- Ability to initialize a Taskfile in the current workspace.
2738
- If no Taskfile is detected a button will appear in the sidebar.
2839
- Refresh on save.
29-
- Configurable via `task.updateOn` setting (values: `"save"` (default) or `"manual"`).
40+
- Configurable via `task.updateOn` setting (values: `"save"` (default) or
41+
`"manual"`).
3042
- Toggle tree nesting on/off
31-
- Configurable via `task.tree.nesting` setting (values: `true` (default) or `false`).
43+
- Configurable via `task.tree.nesting` setting (values: `true` (default) or
44+
`false`).
3245
- Change the path to the Task binary.
3346
- Can also be set to the name of a binary in your `$PATH`.
3447
- Configurable via `task.path` setting (defaults to `"task"`).
3548
- Version checks on startup.
36-
- Configurable via `task.checkForUpdates` setting (values: `true` (default) or `false`).
49+
- Configurable via `task.checkForUpdates` setting (values: `true` (default) or
50+
`false`).
3751
- Sidebar icon provided by @drite93.

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737

3838
## Configuration
3939

40-
| Setting | Type | Allowed Values | Default | Description |
41-
| ----------------- | --------- | --------------------------------- | ----------- | ----------------------------------------------------------------------- |
42-
| `updateOn` | `string` | `"manual"`, `"save"` | `"save"` | When the list of tasks should be updated. |
43-
| `path` | `string` | | `"task"` | Path to the Task binary. Can also the name of a binary in your `$PATH`. |
44-
| `checkForUpdates` | `boolean` | | `true` | Check if there is a newer version of Task on startup. |
45-
| `tree.nesting` | `boolean` | | `true` | Whether to nest tasks by their namespace in the tree view. |
46-
| `tree.sort` | `sort` | `default`, `alphanumeric`, `none` | `"default"` | The order in which to display tasks in the tree view. |
40+
| Setting | Type | Allowed Values | Default | Description |
41+
| ----------------- | --------- | --------------------------------- | ----------- | -------------------------------------------------------------------------------------------- |
42+
| `updateOn` | `string` | `"manual"`, `"save"` | `"save"` | When the list of tasks should be updated. |
43+
| `path` | `string` | | `"task"` | Path to the Task binary. Can also the name of a binary in your `$PATH`. |
44+
| `outputTo` | `string` | `"output"`, `"terminal"` | `"output"` | Where to print the output of tasks. Note that the output panel does not support ANSI colors. |
45+
| `checkForUpdates` | `boolean` | | `true` | Check if there is a newer version of Task on startup. |
46+
| `tree.nesting` | `boolean` | | `true` | Whether to nest tasks by their namespace in the tree view. |
47+
| `tree.sort` | `sort` | `default`, `alphanumeric`, `none` | `"default"` | The order in which to display tasks in the tree view. |

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@
258258
"default": "task",
259259
"description": "Path to the Task binary. Can also the name of a binary in your `$PATH`."
260260
},
261+
"outputTo": {
262+
"type": "string",
263+
"enum": [
264+
"output",
265+
"terminal"
266+
],
267+
"default": "output",
268+
"description": "Where to print the output of tasks. Note that the output panel does not support ANSI colors."
269+
},
261270
"checkForUpdates": {
262271
"type": "boolean",
263272
"default": true,
@@ -305,6 +314,7 @@
305314
"@types/glob": "^8.1.0",
306315
"@types/mocha": "^10.0.1",
307316
"@types/node": "18.x",
317+
"@types/strip-ansi": "3.0.0",
308318
"@types/vscode": "^1.76.0",
309319
"@typescript-eslint/eslint-plugin": "^5.59.1",
310320
"@typescript-eslint/parser": "^5.59.0",
@@ -320,6 +330,7 @@
320330
"dependencies": {
321331
"@octokit/types": "^9.0.0",
322332
"octokit": "^2.0.14",
323-
"semver": "^7.3.8"
333+
"semver": "^7.3.8",
334+
"strip-ansi": "6.0.1"
324335
}
325336
}

src/services/taskfile.ts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as semver from 'semver';
77
import { log, settings } from '../utils';
88
import { Octokit } from 'octokit';
99
import { Endpoints } from "@octokit/types";
10+
import stripAnsi = require('strip-ansi');
1011

1112
const octokit = new Octokit();
1213
type ReleaseRequest = Endpoints["GET /repos/{owner}/{repo}/releases/latest"]["parameters"];
@@ -225,37 +226,49 @@ class TaskfileService {
225226
}
226227

227228
public async runTask(taskName: string, dir?: string): Promise<void> {
228-
return await new Promise((resolve) => {
229+
if (settings.outputTo === "terminal") {
229230
log.info(`Running task: "${taskName}" in: "${dir}"`);
231+
var terminal: vscode.Terminal;
232+
if (vscode.window.activeTerminal !== undefined) {
233+
terminal = vscode.window.activeTerminal;
234+
} else {
235+
terminal = vscode.window.createTerminal("Task");
236+
}
237+
terminal.show();
238+
terminal.sendText(this.command(taskName));
239+
} else {
240+
return await new Promise((resolve) => {
241+
log.info(`Running task: "${taskName}" in: "${dir}"`);
242+
243+
// Spawn a child process
244+
let child = cp.spawn(this.command(), [taskName], { cwd: dir });
245+
246+
// Open the output
247+
TaskfileService.outputChannel.clear();
248+
TaskfileService.outputChannel.show();
249+
250+
// Listen for stderr
251+
child.stderr.setEncoding('utf8');
252+
child.stderr.on("data", data => {
253+
TaskfileService.outputChannel.append(stripAnsi(data.toString()));
254+
});
230255

231-
// Spawn a child process
232-
let child = cp.spawn(this.command(), [taskName], { cwd: dir });
233-
234-
// Clear the output channel and show it
235-
TaskfileService.outputChannel.clear();
236-
TaskfileService.outputChannel.show();
237-
238-
// Listen for stderr
239-
child.stderr.setEncoding('utf8');
240-
child.stderr.on("data", data => {
241-
TaskfileService.outputChannel.append(data.toString());
242-
});
243-
244-
// Listen for stdout
245-
child.stdout.setEncoding('utf8');
246-
child.stdout.on("data", data => {
247-
TaskfileService.outputChannel.append(data.toString());
248-
});
256+
// Listen for stdout
257+
child.stdout.setEncoding('utf8');
258+
child.stdout.on("data", data => {
259+
TaskfileService.outputChannel.append(stripAnsi(data.toString()));
260+
});
249261

250-
// When the task finishes, print the exit code and resolve the promise
251-
child.on('close', code => {
252-
log.info(`Task completed with code ${code}`);
253-
TaskfileService.outputChannel.append(`task: completed with code ${code}\n`);
254-
this.lastTaskName = taskName;
255-
this.lastTaskDir = dir;
256-
return resolve();
262+
// When the task finishes, print the exit code and resolve the promise
263+
child.on('close', code => {
264+
log.info(`Task completed with code ${code}`);
265+
TaskfileService.outputChannel.append(`task: completed with code ${code}\n`);
266+
this.lastTaskName = taskName;
267+
this.lastTaskDir = dir;
268+
return resolve();
269+
});
257270
});
258-
});
271+
}
259272
}
260273

261274
public async goToDefinition(task: models.Task, preview: boolean = false): Promise<void> {

src/utils/settings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Settings {
55
private static _instance: Settings;
66
public updateOn!: string;
77
public path!: string;
8+
public outputTo!: string;
89
public checkForUpdates!: boolean;
910
public treeNesting!: boolean;
1011
public treeSort!: string;
@@ -27,6 +28,7 @@ class Settings {
2728
// Set the properties
2829
this.updateOn = config.get("updateOn") ?? "change";
2930
this.path = config.get("path") ?? "task";
31+
this.outputTo = config.get("outputTo") ?? "output";
3032
this.checkForUpdates = config.get("checkForUpdates") ?? true;
3133
this.treeNesting = config.get("tree.nesting") ?? true;
3234
this.treeSort = config.get("tree.sort") ?? "default";

test-workspace/Taskfile.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ tasks:
3636
desc: Namespaced task that is defined in the root file
3737
cmds:
3838
- task: hello
39+
40+
task-prints-color:
41+
cmds:
42+
- echo -e "Prints in {{.RED}}red{{.NC}}"
43+
vars:
44+
RED: \033[1;31m
45+
NC: \033[0m

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@
469469
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.13.tgz#da4bfd73f49bd541d28920ab0e2bf0ee80f71c91"
470470
integrity sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw==
471471

472+
473+
version "3.0.0"
474+
resolved "https://registry.yarnpkg.com/@types/strip-ansi/-/strip-ansi-3.0.0.tgz#9b63d453a6b54aa849182207711a08be8eea48ae"
475+
integrity sha512-wVhzc+WJ/JNdV25MeaK0skxGdbdOFeqYv1sqY8yPXbsshZ0XwSbWWwfKzj836cPW+e+PpqUNvKoiac9ZqCdyRQ==
476+
472477
"@types/vscode@^1.76.0":
473478
version "1.76.0"
474479
resolved "https://registry.yarnpkg.com/@types/vscode/-/vscode-1.76.0.tgz#967c0fbe09921818bbf201f1cbcb81b981c6249c"
@@ -2305,7 +2310,7 @@ string_decoder@~1.1.1:
23052310
dependencies:
23062311
safe-buffer "~5.1.0"
23072312

2308-
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
2313+
strip-ansi@6.0.1, strip-ansi@^6.0.0, strip-ansi@^6.0.1:
23092314
version "6.0.1"
23102315
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
23112316
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==

0 commit comments

Comments
 (0)