-2
  • What? I would like to run a command from app running in Chromium on Linux (or potentially Windows or Android in the future)
  • Why? to control for example some audio/TV equipment like via cec-client eg.
echo "tx 20:36" | cec-client RPI -s -d 4
  • however the question around concept of shelling, spawning commands is generic.

I have put together the following SystemCall class which contains several attempts from different posts I found however I am stuck on this error "Cannot find module "child_process"

  • I understand you are not supposed to run system calls from JS/TS however this app will run in a controlled environment.
  • Hopefully I will not need a local server or php but if you think you have a solution I will certainly consider. I need to make a call against the local hardware not on a remote server.
  • I am stuck at Ionic3 too many breaking changes to move to Ionic5.

// 
//----------------------------------------------------------------------
// FRAMEWORKS IMPORTS
//----------------------------------------------------------------------
import { Injectable } from '@angular/core' // 20160731
//
import { exec, ChildProcess, execSync} from 'child_process'
// import * as child from 'child_process';
//
//
//----------------------------------------------------------------------
// JS LIBRARY
//----------------------------------------------------------------------
// declare var plyr: any; // Magic makes JS variable available to TS :)
// declare var execSync 
//
/** - 20200607 */
@Injectable()
export class SystemCall {

    constructor(
        public ChildProcess: ChildProcess,

    ) {

    }

    Run() {
        this.Shell('notepad.exe')
        // this.Run1()
    }

    // https://stackoverflow.com/questions/5321884/how-do-i-run-the-system-commands-in-javascript
    Run0() {
        var spawn = require('child_process').spawn
        var Run = spawn('ls', ['-l']);
        // let Run = spawn('notepad.exe', [])
        Run.stdout.on('data', function (data) {
            console.log(data);
        });
    }

    Run1() {
        const { exec } = require("child_process");

        exec("dir", (error, stdout, stderr) => {
            if (error) {
                console.log(`error: ${error.message}`);
                return;
            }
            if (stderr) {
                console.log(`stderr: ${stderr}`);
                return;
            }
            console.log(`stdout: ${stdout}`);
        });
    }


    // https://stackoverflow.com/questions/1880198/how-to-execute-shell-command-in-javascript/52575123#52575123
    Run3() {
        const execSync = require('child_process').execSync;
        // import { execSync } from 'child_process';  // replace ^ if using ES modules
        const output = execSync('notepad.exe', { encoding: 'utf-8' });  // the default is 'buffer'
        console.log('Output was:\n', output);
    }

    // https://stackoverflow.com/questions/36546860/require-nodejs-child-process-with-typescript-systemjs-and-electron
    Run4() {
        // var foo: child.ChildProcess = child.exec('notepad.exe');
        // console.log(typeof foo.on);
    }

    // https://stackoverflow.com/questions/1880198/how-to-execute-shell-command-in-javascript/31897900#31897900
    /**
     * Execute simple shell command (async wrapper).
     * @param {String} cmd
     * @return {Object} { stdout: String, stderr: String }
     */
    async Shell(cmd) {
        return new Promise(function (resolve, reject) {
            exec(cmd, (err, stdout, stderr) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({ stdout, stderr });
                }
            });
        });
    }

    async  Run5() {
        let stdout = await this.Shell('cmd.exe /c dir')
        for (let line of stdout.toString().split('\n')) {
            console.log(`ls: ${line}`);
        }
    }

}

I am currently injecting SystemCall into app.component.ts and calling SystemCall.Run() to test out.

What's strange is that VSCode reveals the signatures of exec, etc.. when I hover with the mouse over the import line?? enter image description here

I have run the command

npm install child_process --save

my packages now shows enter image description here

Thanks for helping out, right now I am swimming in murky waters.

1 Answer 1

1

You cannot run system calls just like that in a hybrid client app, since the code is executed in a web view.

It may be possible using a Cordova plugin, such as cordova-plugin-shell-exec.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, it looks like cordova-plugin-shell-exec. is for Android only so it won't work with current target Linux platform. I was wondering how am I able use to file-saver.js to read and write files anywhere on my harddisk on Windows7, isn't that considered outside of "webview"? is webview what they refer to as running in a browser's "sandbox" thanks for taking the time to explain.
Yes you can see webview as a sandbox browser. Downloading files or picking files to upload is already allowed in all browsers. But this needs to be initiated from a user action. But on a normal browser app you cannot execute shell commands

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.