Skip to content

Commit a926cb8

Browse files
committed
Use es2018.promise
1 parent 68ae9f7 commit a926cb8

File tree

9 files changed

+34
-47
lines changed

9 files changed

+34
-47
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# TODO
22

3-
- [ ] Windows support
3+
- [x] Windows support
44
- [ ] Tests
55
- [ ] `'workspace.rootPath' is deprecated and should no longer be used. Please use 'workspace.workspaceFolders' instead. More details: https://aka.ms/vscode-eliminating-rootpath`
66
- [x] Run tasks from collapsible sidebar panel

src/Deferred.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function noop() {}
33
export default class Deferred {
44
resolve: Function;
55
reject: Function;
6-
promise: Thenable<any>;
6+
promise: Promise<any>;
77

88
constructor() {
99
this.resolve = noop;

src/Gradle.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function getTasksArgs(): string {
1212
return workspace.getConfiguration().get('gradle.tasks.args', '');
1313
}
1414

15-
function getTasks(): Thenable<GradleTask[]> {
15+
function getTasks(): Promise<GradleTask[]> {
1616
const cmd = getCommand();
1717
const args = ['--console', 'plain', 'tasks'].concat(
1818
getTasksArgs().split(' ')
@@ -28,7 +28,7 @@ function getTasks(): Thenable<GradleTask[]> {
2828
function runTask(
2929
task: GradleTask,
3030
outputChannel: OutputChannel
31-
): Thenable<void> {
31+
): Promise<string> {
3232
const cmd = getCommand();
3333
const args = [task.label];
3434
const options = { cwd: workspace.rootPath };
@@ -39,11 +39,9 @@ function runTask(
3939
outputChannel.show();
4040
outputChannel.append(`Running ${cmd} ${task.label}\n`);
4141

42-
return ProcessRegistry.create(cmd, args, options, outputChannel).then(
43-
() => statusbar.dispose(),
44-
err => {
42+
return ProcessRegistry.create(cmd, args, options, outputChannel).finally(
43+
() => {
4544
statusbar.dispose();
46-
return Promise.reject(err);
4745
}
4846
);
4947
}

src/GradleOutputParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const TASK_REGEX: RegExp = /$\s*([a-z0-9]+)\s-\s(.*)$/gim;
55
export default class GradleOutputParser {
66
private constructor() {}
77

8-
static parseTasks(stdout: string) {
8+
static parseTasks(stdout: string): GradleTask[] {
99
const tasks: GradleTask[] = [];
1010
let match: RegExpExecArray | null = null;
1111
while ((match = TASK_REGEX.exec(stdout)) !== null) {

src/Process.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class Process {
5252
});
5353
}
5454

55-
complete(): Thenable<string> {
55+
complete(): Promise<string> {
5656
return this.deferred.promise;
5757
}
5858
}

src/ProcessRegistry.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import { Writable } from 'stream';
55

66
import ProcessLogger from './ProcessLogger';
77

8-
const processes: Set<ChildProcess> = new Set();
8+
const processes: Set<Process> = new Set();
99

10-
function add(process: ChildProcess) {
10+
function add(process: Process) {
1111
processes.add(process);
1212
}
1313

14-
function remove(process: ChildProcess) {
14+
function remove(process: Process) {
1515
processes.delete(process);
1616
}
1717

1818
function killAll() {
19-
processes.forEach((process: ChildProcess, _: ChildProcess) => {
20-
process.kill('SIGINT');
19+
processes.forEach((process: Process, _: Process) => {
20+
process.childProcess.kill('SIGINT');
2121
});
2222
window.showInformationMessage(
2323
`Killed ${processes.size} process${processes.size === 1 ? '' : 'es'}`
@@ -30,28 +30,16 @@ function create(
3030
args?: ReadonlyArray<string>,
3131
options?: ExecOptions,
3232
outputChannel?: OutputChannel
33-
): Thenable<string> {
33+
): Promise<string> {
3434
let processLogger: ProcessLogger | undefined;
35-
3635
if (outputChannel) {
3736
processLogger = new ProcessLogger(outputChannel);
3837
}
39-
4038
const process = new Process(command, args, options, processLogger);
41-
const childProcess = process.childProcess;
42-
43-
add(childProcess);
44-
45-
return process.complete().then(
46-
stdout => {
47-
remove(childProcess);
48-
return stdout;
49-
},
50-
err => {
51-
remove(childProcess);
52-
return Promise.reject(err);
53-
}
54-
);
39+
add(process);
40+
return process.complete().finally(() => {
41+
remove(process);
42+
});
5543
}
5644

5745
export default { remove, killAll, create };

src/TaskRegistry.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,19 @@ function getTasks(): GradleTask[] {
2222
return Array.from(tasks);
2323
}
2424

25-
function refresh(): Thenable<void> {
25+
function refresh(): Promise<void> {
2626
const statusbar: Disposable = window.setStatusBarMessage(
2727
'Refreshing gradle tasks'
2828
);
2929
return Gradle.getTasks().then(
3030
gradleTasks => {
3131
clear();
3232
addAll(gradleTasks);
33-
statusbar.dispose();
3433
changeHandlers.forEach(handler => handler());
3534
},
36-
err => {
37-
statusbar.dispose();
38-
return Promise.reject(err);
39-
}
40-
);
35+
).finally(() => {
36+
statusbar.dispose();
37+
});
4138
}
4239

4340
function registerChangeHandler(handler: () => void) {

src/extension.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,30 @@ function gradleKillCommand() {
1818
ProcessRegistry.killAll();
1919
}
2020

21-
function gradleRefreshTasksCommand(): Thenable<void> {
22-
return TaskRegistry.refresh().then(undefined, err => {
21+
async function gradleRefreshTasksCommand(): Promise<void> {
22+
try {
23+
TaskRegistry.refresh();
24+
} catch (err) {
2325
window.showErrorMessage(`Unable to refresh gradle tasks: ${err.message}`);
24-
});
26+
}
2527
}
2628

27-
async function gradleRunTaskCommand(taskName?: string): Promise<Error | void> {
29+
async function gradleRunTaskCommand(
30+
taskName?: string
31+
): Promise<string | Error | void> {
2832
if (taskName) {
29-
return Gradle.runTask(new GradleTask(taskName), outputChannel);
33+
return await Gradle.runTask(new GradleTask(taskName), outputChannel);
3034
}
3135

3236
let tasks: GradleTask[] = TaskRegistry.getTasks();
3337

3438
if (!tasks.length) {
35-
return Promise.reject('No tasks found. Try running gradle:refresh');
39+
throw new Error('No tasks found. Try running gradle:refresh');
3640
}
3741

3842
const pickedTask: GradleTask | void = await window.showQuickPick(tasks);
3943
if (pickedTask) {
40-
return Gradle.runTask(pickedTask, outputChannel);
44+
return await Gradle.runTask(pickedTask, outputChannel);
4145
}
4246
}
4347

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"module": "commonjs",
44
"target": "es6",
55
"outDir": "out",
6-
"lib": ["es6"],
6+
"lib": ["es6","es2018.promise"],
77
"sourceMap": true,
88
"rootDir": "src",
99
"strict": true,

0 commit comments

Comments
 (0)