【问题标题】:How do I run a system command from an ionic3 app?如何从 ionic3 应用程序运行系统命令?
【发布时间】:2020-09-26 18:42:27
【问题描述】:
  • 什么?我想在 Linux(或未来可能是 Windows 或 Android)上运行 Chromium 的应用程序运行命令
  • 为什么?例如通过 cec-client 控制一些音频/电视设备,例如。
echo "tx 20:36" | cec-client RPI -s -d 4
  • 但是,关于脱壳、生成命令概念的问题是通用的。

我已经整理了以下 SystemCall 类,其中包含我发现的不同帖子的多次尝试,但是我遇到了这个错误“找不到模块“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

我的包裹现在显示

感谢您的帮助,我现在在浑水里游泳。

【问题讨论】:

    标签: angular linux typescript cordova ionic3


    【解决方案1】:

    您不能像在混合客户端应用程序中那样运行系统调用,因为代码是在 Web 视图中执行的。

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

    【讨论】:

    • 不幸的是,它看起来像 cordova-plugin-shell-exec。仅适用于 Android,因此不适用于当前的目标 Linux 平台。我想知道如何使用 file-saver.js 在 Windows7 上的硬盘上的任何位置读取和写入文件,这不是在“webview”之外考虑的吗? webview 是他们所说的在浏览器的“沙箱”中运行的东西,感谢您花时间解释。
    • 是的,您可以将 webview 视为沙盒浏览器。所有浏览器都已允许下载文件或选择要上传的文件。但这需要从用户操作开始。但是在普通的浏览器应用程序上,您无法执行 shell 命令
    猜你喜欢
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2021-11-24
    • 2017-08-17
    • 1970-01-01
    • 2010-09-10
    相关资源
    最近更新 更多