-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathexecutor.ts
More file actions
122 lines (118 loc) · 4.1 KB
/
Copy pathexecutor.ts
File metadata and controls
122 lines (118 loc) · 4.1 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import * as child_process from "child_process";
import * as fs from "fs";
import * as vscode from "vscode";
import { outputChannel } from "../data/constants";
/**
* Options for executeProcess function.
*/
type ProcessOptions = {
/**
* Name of the process. Eg: `Decoding`.
*/
name: string;
/**
* Report to show in progress bar.
*/
report: string;
/**
* Name of the executable. Eg: `java`.
*/
command: string;
/**
* CLI arguments for the command.
*/
args: string[];
/**
* A file or dir which should exist if it was successful.
*/
shouldExist?: string;
/**
* Callback on success.
*/
onSuccess?: CallableFunction;
/**
* Execute command using shell
*/
shell?: boolean;
};
/**
* Executes a child_process and calls a callback if provided.
* @param processOptions Takes a ProcessOptions type to process.
*/
export function executeProcess(processOptions: ProcessOptions): Thenable<void> {
outputChannel.show();
outputChannel.appendLine("-".repeat(processOptions.report.length));
outputChannel.appendLine(processOptions.report);
outputChannel.appendLine("-".repeat(processOptions.report.length));
outputChannel.appendLine(
`${processOptions.command} ${processOptions.args.join(" ")}`
);
return vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: "APKLab",
cancellable: true,
},
(progress, token) => {
return new Promise<void>((resolve) => {
progress.report({ message: processOptions.report });
const cp = child_process.spawn(
processOptions.command,
processOptions.args,
{
shell: processOptions.shell,
}
);
cp.stdout.on("data", (data) =>
outputChannel.appendLine(data.toString().trim())
);
cp.stderr.on("data", (data) =>
outputChannel.appendLine(data.toString().trim())
);
cp.on("error", (data) => {
outputChannel.appendLine(data.toString().trim());
vscode.window.showErrorMessage(
`APKLab: ${processOptions.name} process failed.`
);
resolve();
});
cp.on("exit", async (code) => {
if (
code === 0 &&
(processOptions.shouldExist
? fs.existsSync(processOptions.shouldExist)
: true)
) {
outputChannel.appendLine(
`${processOptions.name} process was successful`
);
vscode.window.showInformationMessage(
`APKLab: ${processOptions.name} process was successful.`
);
if (processOptions.onSuccess) {
await processOptions.onSuccess();
}
} else {
outputChannel.appendLine(
code !== 0
? `${processOptions.name} process exited with code ${code}`
: `${processOptions.name} process didn't create ${processOptions.shouldExist}`
);
vscode.window.showErrorMessage(
`APKLab: ${processOptions.name} process failed.`
);
}
resolve();
});
token.onCancellationRequested(() => {
outputChannel.appendLine(
`User canceled the ${processOptions.name} process`
);
if (!cp.killed) {
cp.kill();
}
});
});
}
);
}