Skip to content

Commit 7368ef4

Browse files
feat. added jsDoc for Terminal api
1 parent ee24365 commit 7368ef4

File tree

4 files changed

+234
-225
lines changed

4 files changed

+234
-225
lines changed

src/plugins/terminal/scripts/init-alpine.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
echo "$$" > $PREFIX/pid
21
set -e # Exit immediately on Failure
32

43
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/share/bin:/usr/share/sbin:/usr/local/bin:/usr/local/sbin:/system/bin:/system/xbin:$PREFIX/local/bin
@@ -41,6 +40,7 @@ fi
4140

4241

4342
if [ "$#" -eq 0 ]; then
43+
echo "$$" > $PREFIX/pid
4444
chmod +x $PREFIX/axs
4545
$PREFIX/axs
4646
else

src/plugins/terminal/src/android/Executor.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
3636
case "start":
3737
String cmdStart = args.getString(0);
3838
String pid = UUID.randomUUID().toString();
39-
startProcess(pid, cmdStart, callbackContext);
39+
startProcess(pid, cmdStart,args.getString(1), callbackContext);
4040
return true;
4141
case "write":
4242
String pidWrite = args.getString(0);
@@ -48,8 +48,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
4848
stopProcess(pidStop, callbackContext);
4949
return true;
5050
case "exec":
51-
String cmdExec = args.getString(0);
52-
exec(cmdExec, callbackContext);
51+
exec(args.getString(0),args.getString(1), callbackContext);
5352
return true;
5453
case "isRunning":
5554
isProcessRunning(args.getString(0), callbackContext);
@@ -60,24 +59,27 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
6059
}
6160
}
6261

63-
private void exec(String cmd, CallbackContext callbackContext) {
62+
private void exec(String cmd,String alpine, CallbackContext callbackContext) {
6463
try {
6564
if (cmd != null && !cmd.isEmpty()) {
66-
ProcessBuilder builder = new ProcessBuilder("sh", "-c", cmd);
65+
String xcmd = cmd;
66+
if(alpine.equals("true")){
67+
xcmd = "source $PREFIX/init-sandbox.sh "+cmd;
68+
}
69+
70+
ProcessBuilder builder = new ProcessBuilder("sh", "-c", xcmd);
6771

6872
// Set environment variables
6973
Map<String, String> env = builder.environment();
7074
env.put("PREFIX", context.getFilesDir().getAbsolutePath());
7175
env.put("NATIVE_DIR", context.getApplicationInfo().nativeLibraryDir);
7276

7377
try {
74-
int target = context.getPackageManager()
75-
.getPackageInfo(context.getPackageName(), 0)
76-
.applicationInfo.targetSdkVersion;
77-
env.put("FDROID", String.valueOf(target <= 28));
78-
} catch (PackageManager.NameNotFoundException e) {
79-
e.printStackTrace();
80-
}
78+
int target = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.targetSdkVersion;
79+
env.put("FDROID", String.valueOf(target <= 28));
80+
} catch (PackageManager.NameNotFoundException e) {
81+
e.printStackTrace();
82+
}
8183

8284

8385
Process process = builder.start();
@@ -118,24 +120,27 @@ private void exec(String cmd, CallbackContext callbackContext) {
118120
}
119121
}
120122

121-
private void startProcess(String pid, String cmd, CallbackContext callbackContext) {
123+
private void startProcess(String pid, String cmd,String alpine, CallbackContext callbackContext) {
122124
cordova.getThreadPool().execute(() -> {
123125
try {
124-
ProcessBuilder builder = new ProcessBuilder("sh", "-c", cmd);
126+
String xcmd = cmd;
127+
if(alpine.equals("true")){
128+
xcmd = "source $PREFIX/init-sandbox.sh "+cmd;
129+
}
130+
ProcessBuilder builder = new ProcessBuilder("sh", "-c", xcmd);
125131

126132
// Set environment variables
127133
Map<String, String> env = builder.environment();
128134
env.put("PREFIX", context.getFilesDir().getAbsolutePath());
129135
env.put("NATIVE_DIR", context.getApplicationInfo().nativeLibraryDir);
130136

131137
try {
132-
int target = context.getPackageManager()
133-
.getPackageInfo(context.getPackageName(), 0)
134-
.applicationInfo.targetSdkVersion;
135-
env.put("FDROID", String.valueOf(target <= 28));
136-
} catch (PackageManager.NameNotFoundException e) {
137-
e.printStackTrace();
138-
}
138+
int target = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.targetSdkVersion;
139+
env.put("FDROID", String.valueOf(target <= 28));
140+
} catch (PackageManager.NameNotFoundException e) {
141+
e.printStackTrace();
142+
}
143+
139144

140145

141146
Process process = builder.start();

src/plugins/terminal/www/Executor.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
/**
2-
* Executor module for interacting with shell processes on Cordova.
3-
* Allows starting processes with real-time streaming, writing input,
4-
* stopping processes, and traditional one-time execution.
5-
*
62
* @module Executor
3+
* @description
4+
* This module provides an interface to run shell commands from a Cordova app.
5+
* It supports real-time process streaming, writing input to running processes,
6+
* stopping them, and executing one-time commands.
77
*/
88

99
const exec = require('cordova/exec');
1010

1111
const Executor = {
1212
/**
13-
* Starts a shell process and sets up real-time streaming for stdout, stderr, and exit events.
13+
* Starts a shell process and enables real-time streaming of stdout, stderr, and exit status.
1414
*
15-
* @param {string} command - The shell command to execute (e.g., `"sh"`, `"ls -al"`).
16-
* @param {(type: 'stdout' | 'stderr' | 'exit', data: string) => void} onData - Callback to handle real-time output:
15+
* @param {string} command - The shell command to run (e.g., `"sh"`, `"ls -al"`).
16+
* @param {(type: 'stdout' | 'stderr' | 'exit', data: string) => void} onData - Callback that receives real-time output:
1717
* - `"stdout"`: Standard output line.
1818
* - `"stderr"`: Standard error line.
19-
* - `"exit"`: Process exit code.
20-
*
21-
* @returns {Promise<string>} Resolves with the process ID (PID).
19+
* - `"exit"`: Exit code of the process.
20+
* @param {boolean} alpine - Whether to run the command inside the Alpine sandbox environment (`true`) or on Android directly (`false`).
21+
* @returns {Promise<string>} Resolves with a unique process ID (UUID) used for future references like `write()` or `stop()`.
2222
*
2323
* @example
2424
* Executor.start('sh', (type, data) => {
@@ -28,30 +28,32 @@ const Executor = {
2828
* Executor.stop(uuid);
2929
* });
3030
*/
31-
start(command, onData) {
31+
start(command, onData, alpine = false) {
3232
return new Promise((resolve, reject) => {
3333
exec(
3434
(message) => {
35+
// Stream stdout, stderr, or exit notifications
3536
if (message.startsWith("stdout:")) return onData("stdout", message.slice(7));
3637
if (message.startsWith("stderr:")) return onData("stderr", message.slice(7));
3738
if (message.startsWith("exit:")) return onData("exit", message.slice(5));
38-
// First message is PID
39+
40+
// First message is always the process UUID
3941
resolve(message);
4042
},
4143
reject,
4244
"Executor",
4345
"start",
44-
[command]
46+
[command, String(alpine)]
4547
);
4648
});
4749
},
4850

4951
/**
50-
* Sends input to the stdin of a running process.
52+
* Sends input to a running process's stdin.
5153
*
5254
* @param {string} uuid - The process ID returned by {@link Executor.start}.
53-
* @param {string} input - The input string to send to the process.
54-
* @returns {Promise<string>} Resolves when the input is successfully written.
55+
* @param {string} input - Input string to send (e.g., shell commands).
56+
* @returns {Promise<string>} Resolves once the input is written.
5557
*
5658
* @example
5759
* Executor.write(uuid, 'ls /data');
@@ -63,10 +65,10 @@ const Executor = {
6365
},
6466

6567
/**
66-
* Stops a running process.
68+
* Terminates a running process.
6769
*
6870
* @param {string} uuid - The process ID returned by {@link Executor.start}.
69-
* @returns {Promise<string>} Resolves when the process is terminated.
71+
* @returns {Promise<string>} Resolves when the process has been stopped.
7072
*
7173
* @example
7274
* Executor.stop(uuid);
@@ -76,37 +78,40 @@ const Executor = {
7678
exec(resolve, reject, "Executor", "stop", [uuid]);
7779
});
7880
},
81+
82+
/**
83+
* Checks if a process is still running.
84+
*
85+
* @param {string} uuid - The process ID returned by {@link Executor.start}.
86+
* @returns {Promise<boolean>} Resolves `true` if the process is running, `false` otherwise.
87+
*
88+
* @example
89+
* const isAlive = await Executor.isRunning(uuid);
90+
*/
7991
isRunning(uuid) {
8092
return new Promise((resolve, reject) => {
81-
exec((result)=>{
82-
if(result === "running"){
83-
resolve(true)
84-
}else if(result === "exited"){
85-
resolve(false)
86-
}else{
87-
resolve(false)
88-
}
93+
exec((result) => {
94+
resolve(result === "running");
8995
}, reject, "Executor", "isRunning", [uuid]);
9096
});
9197
},
9298

9399
/**
94-
* Executes a shell command and waits for it to finish.
95-
* Unlike `start()`, this is a one-time execution and does not stream real-time output.
100+
* Executes a shell command once and waits for it to finish.
101+
* Unlike {@link Executor.start}, this does not stream output.
96102
*
97-
* @param {string} command - The command to execute.
98-
* @returns {Promise<string>} Resolves with stdout if the command succeeds, rejects with stderr or error message if it fails.
103+
* @param {string} command - The shell command to execute.
104+
* @param {boolean} alpine - Whether to run the command in the Alpine sandbox (`true`) or Android environment (`false`).
105+
* @returns {Promise<string>} Resolves with standard output on success, rejects with an error or standard error on failure.
99106
*
100107
* @example
101-
* Executor.execute('ls -l').then(output => {
102-
* console.log(output);
103-
* }).catch(error => {
104-
* console.error(error);
105-
* });
108+
* Executor.execute('ls -l')
109+
* .then(console.log)
110+
* .catch(console.error);
106111
*/
107-
execute(command) {
112+
execute(command, alpine = false) {
108113
return new Promise((resolve, reject) => {
109-
exec(resolve, reject, "Executor", "exec", [command]);
114+
exec(resolve, reject, "Executor", "exec", [command, String(alpine)]);
110115
});
111116
}
112117
};

0 commit comments

Comments
 (0)