forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutor.js
More file actions
219 lines (200 loc) · 6.46 KB
/
Executor.js
File metadata and controls
219 lines (200 loc) · 6.46 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* @class Executor
* @description
* This class provides an interface to run shell commands from a Cordova app.
* It supports real-time process streaming, writing input to running processes,
* stopping them, and executing one-time commands.
*/
const exec = require('cordova/exec');
class Executor {
constructor(BackgroundExecutor = false) {
this.ExecutorType = BackgroundExecutor ? "BackgroundExecutor" : "Executor";
}
spawnStream(cmd, callback){
exec((port)=>{
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
ws.binaryType = "arraybuffer";
ws.onopen = ()=>{
callback(ws);
};
}, null, "Executor", "spawn", [cmd]);
}
/**
* Starts a shell process and enables real-time streaming of stdout, stderr, and exit status.
*
* @param {string} command - The shell command to run (e.g., `"sh"`, `"ls -al"`).
* @param {(type: 'stdout' | 'stderr' | 'exit', data: string) => void} onData - Callback that receives real-time output:
* - `"stdout"`: Standard output line.
* - `"stderr"`: Standard error line.
* - `"exit"`: Exit code of the process.
* @param {boolean} [alpine=false] - Whether to run the command inside the Alpine sandbox environment (`true`) or on Android directly (`false`).
* @returns {Promise<string>} Resolves with a unique process ID (UUID) used for future references like `write()` or `stop()`.
*
* @example
* const executor = new Executor();
* executor.start('sh', (type, data) => {
* //console.log(`[${type}] ${data}`);
* }).then(uuid => {
* executor.write(uuid, 'echo Hello World');
* executor.stop(uuid);
* });
*/
start(command, onData, alpine = false) {
return new Promise((resolve, reject) => {
let first = true;
exec(
async (message) => {
//console.log(message);
if (first) {
first = false;
await new Promise(resolve => setTimeout(resolve, 100));
// First message is always the process UUID
resolve(message);
} else {
const match = message.match(/^([^:]+):(.*)$/);
if (match) {
const prefix = match[1]; // e.g. "stdout"
const content = match[2]; // output
onData(prefix, content);
} else {
onData("unknown", message);
}
}
},
reject,
this.ExecutorType,
"start",
[command, String(alpine)]
);
});
}
/**
* Sends input to a running process's stdin.
*
* @param {string} uuid - The process ID returned by {@link Executor#start}.
* @param {string} input - Input string to send (e.g., shell commands).
* @returns {Promise<string>} Resolves once the input is written.
*
* @example
* executor.write(uuid, 'ls /sdcard');
*/
write(uuid, input) {
//console.log("write: " + input + " to " + uuid);
return new Promise((resolve, reject) => {
exec(resolve, reject, this.ExecutorType, "write", [uuid, input]);
});
}
/**
* Moves the executor service to the background (stops foreground notification).
*
* @returns {Promise<string>} Resolves when the service is moved to background.
*
* @example
* executor.moveToBackground();
*/
moveToBackground() {
return new Promise((resolve, reject) => {
exec(resolve, reject, this.ExecutorType, "moveToBackground", []);
});
}
/**
* Moves the executor service to the foreground (shows notification).
*
* @returns {Promise<string>} Resolves when the service is moved to foreground.
*
* @example
* executor.moveToForeground();
*/
moveToForeground() {
return new Promise((resolve, reject) => {
exec(resolve, reject, this.ExecutorType, "moveToForeground", []);
});
}
/**
* Terminates a running process.
*
* @param {string} uuid - The process ID returned by {@link Executor#start}.
* @returns {Promise<string>} Resolves when the process has been stopped.
*
* @example
* executor.stop(uuid);
*/
stop(uuid) {
return new Promise((resolve, reject) => {
exec(resolve, reject, this.ExecutorType, "stop", [uuid]);
});
}
/**
* Checks if a process is still running.
*
* @param {string} uuid - The process ID returned by {@link Executor#start}.
* @returns {Promise<boolean>} Resolves `true` if the process is running, `false` otherwise.
*
* @example
* const isAlive = await executor.isRunning(uuid);
*/
isRunning(uuid) {
return new Promise((resolve, reject) => {
exec(
(result) => {
resolve(result === "running");
},
reject,
this.ExecutorType,
"isRunning",
[uuid]
);
});
}
/**
* Stops the executor service completely.
*
* @returns {Promise<string>} Resolves when the service has been stopped.
*
* Note: This does not gurantee that all running processes have been killed, but the service will no longer be active. Use with caution.
*
* @example
* executor.stopService();
*/
stopService() {
return new Promise((resolve, reject) => {
exec(resolve, reject, this.ExecutorType, "stopService", []);
});
}
/**
* Executes a shell command once and waits for it to finish.
* Unlike {@link Executor#start}, this does not stream output.
*
* @param {string} command - The shell command to execute.
* @param {boolean} [alpine=false] - Whether to run the command in the Alpine sandbox (`true`) or Android environment (`false`).
* @returns {Promise<string>} Resolves with standard output on success, rejects with an error or standard error on failure.
*
* @example
* executor.execute('ls -l')
* .then(//console.log)
* .catch(console.error);
*/
execute(command, alpine = false) {
return new Promise((resolve, reject) => {
exec(resolve, reject, this.ExecutorType, "exec", [command, String(alpine)]);
});
}
/**
* Loads a native library from the specified path.
*
* @param {string} path - The path to the native library to load.
* @returns {Promise<string>} Resolves when the library has been loaded.
*
* @example
* executor.loadLibrary('/path/to/library.so');
*/
loadLibrary(path) {
return new Promise((resolve, reject) => {
exec(resolve, reject, this.ExecutorType, "loadLibrary", [path]);
});
}
}
//backward compatibility
const executorInstance = new Executor();
executorInstance.BackgroundExecutor = new Executor(true);
module.exports = executorInstance;