@@ -30,24 +30,6 @@ function _remove_undefined(arr) {
3030 return arr . filter ( elm => { return elm !== undefined ; } ) ;
3131}
3232
33- /**
34- * Remove undefined item and insert space between arguments.
35- * @param { ellipsis } ...arr - an array to insert space between each item.
36- */
37- function _join_spc ( ...arr ) {
38- arr = _remove_undefined ( arr ) ;
39- let _result = [ ] ;
40- for ( let index = 0 ; index < arr . length ; ++ index ) {
41- let _item = arr [ index ] ;
42- if ( Array . isArray ( _item ) ) {
43- _result . push ( _join_spc ( ..._item ) ) ;
44- } else {
45- _result . push ( _item ) ;
46- }
47- }
48- return _result . join ( ' ' ) ;
49- }
50-
5133/* Return plugin directory */
5234function _plugin_dir ( ) { return path . join ( __dirname , '..' ) ; }
5335
@@ -77,39 +59,9 @@ function _global_options(argv) {
7759 return flags ;
7860}
7961
80- /* Send error code, and exit the program. */
81- function _exit_error ( code ) {
82- process . exitCode = code ;
83- throw 'Error occurs from Emacs: ' + code ;
84- }
85-
86- /* Return ture if error occurs. */
87- function _trigger_error ( str ) {
88- // XXX The method here to detect error, and send exit code is fragile.
89- // The better way should just grab it from Emacs program itself; but Emacs
90- // return exit code immediately with `child_process.exec` call
91- return str . includes ( 'toplevel form:' ) || str . includes ( 'top-level()' ) ;
92- }
93-
94- /* Return true if falg is already true; else we test str for error flag. */
95- function _test_error ( str , flag ) {
96- if ( flag ) return true ;
97- return _trigger_error ( str ) ;
98- }
99-
100- /* Display all terminal output */
101- function _exec_out ( error , stdout , stderr ) {
102- let trigger_error = false ;
103- //if (error) { console.log(error); } /* ignore node error */
104- if ( stdout ) {
105- console . log ( stdout ) ;
106- trigger_error = _test_error ( stdout , trigger_error ) ;
107- }
108- if ( stderr ) {
109- console . log ( stderr ) ;
110- trigger_error = _test_error ( stderr , trigger_error ) ;
111- }
112- if ( trigger_error ) _exit_error ( 1 ) ; // Trigger error!
62+ /* Print data from process. */
63+ function _log ( data ) {
64+ console . log ( data . toString ( ) . replace ( / \n $ / , '' ) ) ;
11365}
11466
11567/**
@@ -120,12 +72,24 @@ function _exec_out(error, stdout, stderr) {
12072async function e_call ( argv , script , ...args ) {
12173 let _script = 'lisp/' + script + '.el' ;
12274 let _path = path . join ( _plugin_dir ( ) , _script ) ;
123- let cmd = _join_spc ( 'emacs' , '-Q' , '--batch' , '--script' , _path , args , _global_options ( argv ) ) ;
75+
76+ let cmd_base = [ '-Q' , '--batch' , '--script' , _path ] . concat ( args ) ;
77+ let cmd_args = args ;
78+ let cmd_global = _global_options ( argv ) ;
79+ let cmd = cmd_base . concat ( cmd_args ) . concat ( cmd_global ) ;
80+ cmd = _remove_undefined ( cmd ) ;
12481 console . log ( 'Starting Eask...' ) ;
12582 console . log ( '~' ) ;
126- console . log ( '~ $ ' + cmd ) ;
83+ console . log ( '~ $ ' + cmd . join ( ' ' ) ) ;
12784 console . log ( '~' ) ;
128- await child_process . exec ( cmd , _exec_out ) ;
85+ let process = child_process . spawn ( 'emacs' , cmd ) ;
86+
87+ process . stdout . on ( 'data' , function ( data ) { _log ( data ) ; } ) ;
88+ process . stderr . on ( 'data' , function ( data ) { _log ( data ) ; } ) ;
89+
90+ process . on ( 'exit' , function ( code ) {
91+ console . log ( '\nExit with code ' + code . toString ( ) ) ;
92+ } ) ;
12993}
13094
13195/*
0 commit comments