1- const exec = require ( "child_process" ) . exec ;
1+ const { exec } = require ( "child_process" ) ;
22const nodeConsole = require ( "console" ) ;
33const { ipcRenderer } = require ( "electron" ) ;
44
5+ const terminalConsole = new nodeConsole . Console ( process . stdout , process . stderr ) ;
6+ let child ;
7+
58ipcRenderer . send ( "run-command" , "ls" ) ;
69ipcRenderer . on ( "run-command-result" , ( event , result ) => {
710 if ( result . error ) {
@@ -11,77 +14,76 @@ ipcRenderer.on("run-command-result", (event, result) => {
1114 }
1215} ) ;
1316
14- const terminalConsole = new nodeConsole . Console ( process . stdout , process . stderr ) ;
15- let child ;
17+ const printBoth = ( str ) => {
18+ console . log ( `Javascript: ${ str } ` ) ;
19+ terminalConsole . log ( `Javascript: ${ str } ` ) ;
20+ } ;
1621
17- function print_both ( str ) {
18- console . log ( "Javascript: " + str ) ;
19- terminalConsole . log ( "Javascript: " + str ) ;
20- }
21-
22- function send_to_program ( str ) {
22+ const sendToProgram = ( str ) => {
2323 child . stdin . write ( str ) ;
24- child . stdout . on ( "data" , function ( data ) {
25- print_both (
26- "Following data has been piped from python program: " +
27- data . toString ( "utf8" )
24+ child . stdout . on ( "data" , ( data ) => {
25+ printBoth (
26+ `Following data has been piped from python program: ${ data . toString (
27+ "utf8"
28+ ) } `
2829 ) ;
2930 } ) ;
30- }
31+ } ;
3132
32- // starts program execution from within javascript and
33- function start_code_function ( evt ) {
34- print_both ( "Initiating program" ) ;
33+ const startCodeFunction = ( ) => {
34+ printBoth ( "Initiating program" ) ;
3535
36- child = exec (
37- "python -i ./python/pythonExample.py " ,
38- function ( error , stdout , stderr ) {
39- if ( error !== null ) {
40- print_both ( "exec error: " + error ) ;
41- }
36+ child = exec ( "python -i ./python/pythonExample.py" , ( error ) => {
37+ if ( error ) {
38+ printBoth ( `exec error: ${ error } ` ) ;
4239 }
43- ) ;
40+ } ) ;
4441
45- child . stdout . on ( "data" , function ( data ) {
46- print_both (
47- "Following data has been piped from python program: " +
48- data . toString ( "utf8" )
42+ child . stdout . on ( "data" , ( data ) => {
43+ printBoth (
44+ `Following data has been piped from python program: ${ data . toString (
45+ "utf8"
46+ ) } `
4947 ) ;
5048 } ) ;
51- }
49+ } ;
5250
53- // sends data to program
54- function send_code_function ( evt ) {
55- let string_to_send = document . getElementById ( "string_to_send" ) . value ;
56- print_both ( 'Sending "' + string_to_send + '" to program' ) ;
57- send_to_program ( string_to_send ) ;
58- }
51+ const sendCodeFunction = ( ) => {
52+ const stringToSend = document . getElementById ( "string_to_send" ) . value ;
53+ printBoth ( `Sending "${ stringToSend } " to program` ) ;
54+ sendToProgram ( stringToSend ) ;
55+ } ;
5956
60- // sends termination message to python program and closed stream
61- // that receives information from it
62- function stop_code_function ( evt ) {
63- print_both ( "Terminated program" ) ;
64- send_to_program ( "terminate" ) ;
57+ const stopCodeFunction = ( ) => {
58+ printBoth ( "Terminated program" ) ;
59+ sendToProgram ( "terminate" ) ;
6560 child . stdin . end ( ) ;
66- }
61+ } ;
6762
68- // requests main.js to open a file from the filesystem
69- function open_file_function ( evt ) {
70- print_both ( "From guiExample.js sending a request to main.js via ipc" ) ;
71- ipcRenderer . send ( "open_json_file" ) ;
72- }
63+ const openFileFunctionSync = ( ) => {
64+ printBoth ( "From guiExample.js sending a request to main.js via ipc" ) ;
65+ ipcRenderer . send ( "open_json_file_sync" ) ;
66+ } ;
7367
74- document . addEventListener ( "DOMContentLoaded" , function ( ) {
68+ const openFileFunctionAsync = ( ) => {
69+ printBoth ( "From guiExample.js sending a request to main.js via ipc" ) ;
70+ ipcRenderer . send ( "open_json_file_async" ) ;
71+ } ;
72+
73+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
7574 document
7675 . getElementById ( "start_code" )
77- . addEventListener ( "click" , start_code_function ) ;
76+ . addEventListener ( "click" , startCodeFunction ) ;
7877 document
7978 . getElementById ( "send_code" )
80- . addEventListener ( "click" , send_code_function ) ;
79+ . addEventListener ( "click" , sendCodeFunction ) ;
8180 document
8281 . getElementById ( "stop_code" )
83- . addEventListener ( "click" , stop_code_function ) ;
82+ . addEventListener ( "click" , stopCodeFunction ) ;
83+ document
84+ . getElementById ( "open_file_sync" )
85+ . addEventListener ( "click" , openFileFunctionSync ) ;
8486 document
85- . getElementById ( "open_file " )
86- . addEventListener ( "click" , open_file_function ) ;
87+ . getElementById ( "open_file_async " )
88+ . addEventListener ( "click" , openFileFunctionAsync ) ;
8789} ) ;
0 commit comments