Skip to content

Commit a07877a

Browse files
Added Executor (#1338)
* feat. executor * remove unused file
1 parent 240e0f6 commit a07877a

File tree

6 files changed

+118
-1
lines changed

6 files changed

+118
-1
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"cordova-plugin-system": {},
3636
"cordova-plugin-advanced-http": {
3737
"ANDROIDBLACKLISTSECURESOCKETPROTOCOLS": "SSLv3,TLSv1"
38-
}
38+
},
39+
"com.foxdebug.acode.exec": {}
3940
},
4041
"platforms": [
4142
"android"
@@ -61,6 +62,7 @@
6162
"@types/url-parse": "^1.4.11",
6263
"autoprefixer": "^10.4.19",
6364
"babel-loader": "^9.1.3",
65+
"com.foxdebug.acode.exec": "file:src/plugins/Executor",
6466
"cordova-android": "^13.0.0",
6567
"cordova-clipboard": "^1.3.0",
6668
"cordova-plugin-advanced-http": "^3.3.1",

src/plugins/Executor/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function exec(cmd,success,failure) {
2+
const ACTION = 'exec';
3+
cordova.exec(success, failure, 'Executor', ACTION, [cmd]);
4+
}
5+
6+
export default {
7+
exec,
8+
};

src/plugins/Executor/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "com.foxdebug.acode.exec",
3+
"version": "1.0.0",
4+
"description": "Execute linux commands",
5+
"cordova": {
6+
"id": "com.foxdebug.acode.exec",
7+
"platforms": [
8+
"android"
9+
]
10+
},
11+
"keywords": [
12+
"ecosystem:cordova",
13+
"cordova-android"
14+
],
15+
"author": "",
16+
"license": "ISC"
17+
}

src/plugins/Executor/plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version='1.0' encoding='utf-8'?>
2+
<plugin id="com.foxdebug.acode.exec" version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android"><name>Executor</name><js-module name="Executor" src="www/Executor.js"><clobbers target="cordova.plugins.Executor" /></js-module><platform name="android"><config-file parent="/*" target="res/xml/config.xml"><feature name="Executor"><param name="android-package" value="com.foxdebug.acode.exec.Executor" /></feature></config-file><config-file parent="/*" target="AndroidManifest.xml"></config-file><source-file src="src/android/Executor.java" target-dir="src/com/foxdebug/acode/exec/Executor" /></platform></plugin>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.foxdebug.acode.exec;
2+
3+
import org.apache.cordova.CordovaPlugin;
4+
import org.apache.cordova.CallbackContext;
5+
6+
import org.json.JSONArray;
7+
import org.json.JSONException;
8+
9+
import java.io.BufferedReader;
10+
import java.io.InputStreamReader;
11+
12+
public class Executor extends CordovaPlugin {
13+
14+
@Override
15+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
16+
cordova.getThreadPool().execute(new Runnable() {
17+
@Override
18+
public void run() {
19+
if ("exec".equals(action)) {
20+
try {
21+
String cmd = args.getString(0);
22+
exec(cmd, callbackContext);
23+
} catch (JSONException e) {
24+
callbackContext.error("Invalid arguments");
25+
}
26+
} else {
27+
callbackContext.error("Unknown action");
28+
}
29+
}
30+
});
31+
32+
return true;
33+
}
34+
35+
private void exec(String cmd, CallbackContext callbackContext) {
36+
try {
37+
if (cmd != null && !cmd.isEmpty()) {
38+
Process process = Runtime.getRuntime().exec(cmd);
39+
40+
// Capture stdout
41+
BufferedReader stdOutReader = new BufferedReader(
42+
new InputStreamReader(process.getInputStream()));
43+
StringBuilder stdOut = new StringBuilder();
44+
String line;
45+
while ((line = stdOutReader.readLine()) != null) {
46+
stdOut.append(line).append("\n");
47+
}
48+
49+
// Capture stderr
50+
BufferedReader stdErrReader = new BufferedReader(
51+
new InputStreamReader(process.getErrorStream()));
52+
StringBuilder stdErr = new StringBuilder();
53+
while ((line = stdErrReader.readLine()) != null) {
54+
stdErr.append(line).append("\n");
55+
}
56+
57+
int exitCode = process.waitFor();
58+
if (exitCode == 0) {
59+
callbackContext.success(stdOut.toString().trim());
60+
} else {
61+
// Return stderr if command fails
62+
String errorOutput = stdErr.toString().trim();
63+
if (errorOutput.isEmpty()) {
64+
errorOutput = "Command exited with code: " + exitCode;
65+
}
66+
callbackContext.error(errorOutput);
67+
}
68+
} else {
69+
callbackContext.error("Expected one non-empty string argument.");
70+
}
71+
} catch (Exception e) {
72+
e.printStackTrace();
73+
callbackContext.error("Exception: " + e.getMessage());
74+
}
75+
}
76+
77+
}

0 commit comments

Comments
 (0)