Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"cordova-plugin-system": {},
"cordova-plugin-advanced-http": {
"ANDROIDBLACKLISTSECURESOCKETPROTOCOLS": "SSLv3,TLSv1"
}
},
"com.foxdebug.acode.exec": {}
},
"platforms": [
"android"
Expand All @@ -61,6 +62,7 @@
"@types/url-parse": "^1.4.11",
"autoprefixer": "^10.4.19",
"babel-loader": "^9.1.3",
"com.foxdebug.acode.exec": "file:src/plugins/Executor",
"cordova-android": "^13.0.0",
"cordova-clipboard": "^1.3.0",
"cordova-plugin-advanced-http": "^3.3.1",
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/Executor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function exec(cmd,success,failure) {
const ACTION = 'exec';
cordova.exec(success, failure, 'Executor', ACTION, [cmd]);
}

export default {
exec,
};
17 changes: 17 additions & 0 deletions src/plugins/Executor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "com.foxdebug.acode.exec",
"version": "1.0.0",
"description": "Execute linux commands",
"cordova": {
"id": "com.foxdebug.acode.exec",
"platforms": [
"android"
]
},
"keywords": [
"ecosystem:cordova",
"cordova-android"
],
"author": "",
"license": "ISC"
}
2 changes: 2 additions & 0 deletions src/plugins/Executor/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='utf-8'?>
<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>
77 changes: 77 additions & 0 deletions src/plugins/Executor/src/android/Executor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.foxdebug.acode.exec;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Executor extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
if ("exec".equals(action)) {
try {
String cmd = args.getString(0);
exec(cmd, callbackContext);
} catch (JSONException e) {
callbackContext.error("Invalid arguments");
}
} else {
callbackContext.error("Unknown action");
}
}
});

return true;
}

private void exec(String cmd, CallbackContext callbackContext) {
try {
if (cmd != null && !cmd.isEmpty()) {
Process process = Runtime.getRuntime().exec(cmd);

// Capture stdout
BufferedReader stdOutReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder stdOut = new StringBuilder();
String line;
while ((line = stdOutReader.readLine()) != null) {
stdOut.append(line).append("\n");
}

// Capture stderr
BufferedReader stdErrReader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
StringBuilder stdErr = new StringBuilder();
while ((line = stdErrReader.readLine()) != null) {
stdErr.append(line).append("\n");
}

int exitCode = process.waitFor();
if (exitCode == 0) {
callbackContext.success(stdOut.toString().trim());
} else {
// Return stderr if command fails
String errorOutput = stdErr.toString().trim();
if (errorOutput.isEmpty()) {
errorOutput = "Command exited with code: " + exitCode;
}
callbackContext.error(errorOutput);
}
} else {
callbackContext.error("Expected one non-empty string argument.");
}
} catch (Exception e) {
e.printStackTrace();
callbackContext.error("Exception: " + e.getMessage());
}
}

}