3

I have Angular 2 app packaged in Electron. I wonder if it is possible to run shell script from that app.

Thanks for any help.

1

1 Answer 1

4

I was able to achieve that using ngx-childprocess

In 3 steps:

  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);});
       }
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

The callback arrow function you use as the third parameter of childProcessService.childProcess.exec function has to specify all three 'err', 'stdout' and 'stderr' (or whichever name you choose) to get the output of the script in the second one. If you use a single one (data), as it was before the edit, you get only null if the script has been successfully run. But most of us are interested in the output of the script, so that's why we have to specify all three and consider/read the second for example if you want to see the output of the script in stdout.
hi,is there any way to run .bat file using ngx-childprocess??

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.