在Electron打包的Angular应用程序中运行Shell脚本

3

我有一个打包在Electron中的Angular 2应用程序。我想知道是否可能从该应用程序运行shell脚本。

感谢您的任何帮助。


我有一个类似的问题 https://stackoverflow.com/questions/47993447/how-to-run-requireexpress-from-a-typescript-angular-component-in-electron-a - bluesky777
1个回答

4
我使用 ngx-childprocess 成功实现了这一点。 3个步骤:
  1. in your electron/angular app install ngx-childprocess

    yarn add ngx-childprocess
    or
    npm install ngx-childprocess --save
    
  2. add gx-childprocess into app.module

    imports: [
        NgxChildProcessModulem
        ....
    
  3. run the script (in this case I am running java jar)

    import { ChildProcessService } from 'ngx-childprocess';
    ... 
    export class AppComponent {
    
       constructor(private childProcessService: ChildProcessService) {
            console.log('isElectronApp ' + childProcessService.isElectronApp);
            let options: string[] = [];
            childProcessService.childProcess.exec('java -jar child-process-test-1.0.jar', 
                                                  options,
                                                  (data) => {console.log(data);});
       }
    }
    

1
你在childProcessService.childProcess.exec函数中使用的回调箭头函数必须指定所有三个参数'err'、'stdout'和'stderr'(或者你选择的任何名称),以获取脚本的输出。如果你只使用一个参数(data),就像编辑之前一样,如果脚本成功运行,你只会得到null。但是我们大多数人都对脚本的输出感兴趣,所以我们必须指定所有三个参数,并考虑/读取第二个参数,例如,如果你想在stdout中看到脚本的输出。 - orouwk
你好,请问有没有办法使用ngx-childprocess运行.bat文件? - anirudh talluri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接