echo "tx 20:36" | cec-client RPI -s -d 4
我整理了以下SystemCall类,其中包含来自发现的不同帖子的几次尝试,但是我被卡在此错误上“找不到模块” child_process“
// //---------------------------------------------------------------------- // 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()进行测试。
我已经运行了命令
npm install child_process --save
感谢您的帮助,现在我正在浑浊的水中游泳。
什么?我想从在Linux(或将来可能在Windows或Android)上运行Chromium的应用程序中运行命令,为什么?例如通过cec-client控制某些音频/电视设备,例如....
您无法像在混合客户端应用程序中那样运行系统调用,因为代码是在Web视图中执行的。
[可能使用Cordova插件,例如cordova-plugin-shell-exec。