簡體   English   中英

如何從 ionic3 應用程序運行系統命令?

[英]How do I run a system command from an ionic3 app?

  • 什么? 我想從在Linux上運行的應用程序中運行命令(或者將來可能是 Windows 或 Android)
  • 為什么? 例如通過 cec-client 控制一些音頻/電視設備,例如。
echo "tx 20:36" | cec-client RPI -s -d 4
  • 然而,圍繞脫殼、生成命令概念的問題是通用的。

我已經整理了以下 SystemCall class ,其中包含我發現的不同帖子的多次嘗試但是我遇到了這個錯誤“找不到模塊“child_process”

  • 我了解您不應該從 JS/TS 運行系統調用,但是此應用程序將在受控環境中運行。
  • 希望我不需要本地服務器或 php 但如果您認為您有解決方案,我一定會考慮。 我需要對不在遠程服務器上的本地硬件進行調用。
  • 我被困在 Ionic3 太多重大更改無法遷移到 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}`);
        }
    }

}

我目前正在將 SystemCall 注入 app.component.ts 並調用 SystemCall.Run() 進行測試。

奇怪的是,當我將鼠標懸停在導入行上時,VSCode 會顯示 exec 等的簽名。 在此處輸入圖像描述

我已經運行了命令

npm install child_process --save

我的包裹現在顯示在此處輸入圖像描述

謝謝你的幫助,現在我在渾水里游泳。

您不能像在混合客戶端應用程序中那樣運行系統調用,因為代碼是在 web 視圖中執行的。

可以使用 Cordova 插件,例如cordova-plugin-shell-exec

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM