1- 'use strict'
2-
31const EventEmitter = require ( 'events' )
42const lib = require ( './lib' )
53const beginReady = require ( './begin-ready' )
@@ -55,7 +53,7 @@ class ExiftoolProcess extends EventEmitter {
5553 * @returns {Promise.<number> } A promise to spawn exiftool in stay_open
5654 * mode, resolved with pid.
5755 */
58- open ( encoding , options ) {
56+ async open ( encoding , options ) {
5957 let _encoding = encoding
6058 let _options = options
6159 // if encoding is not a string and options are not given, treat it as options
@@ -65,45 +63,43 @@ class ExiftoolProcess extends EventEmitter {
6563 }
6664 this . _assignEncoding ( _encoding )
6765 if ( this . _open ) {
68- return Promise . reject ( new Error ( 'Exiftool process is already open' ) )
66+ throw new Error ( 'Exiftool process is already open' )
67+ }
68+ const exiftoolProcess = await lib . spawn ( this . _bin , _options )
69+ //console.log(`Started exiftool process %s`, process.pid);
70+ this . emit ( events . OPEN , exiftoolProcess . pid )
71+ this . _process = exiftoolProcess
72+
73+ this . _process . on ( 'exit' , this . _exitListener . bind ( this ) )
74+ if ( ! lib . isReadable ( this . _process . stdout ) ) {
75+ lib . killProcess ( this . _process )
76+ throw new Error ( 'Process was not spawned with a readable stdout, check stdio options.' )
77+ }
78+ if ( ! lib . isWritable ( this . _process . stdin ) ) {
79+ lib . killProcess ( this . _process )
80+ throw new Error ( 'Process was not spawned with a writable stdin, check stdio options.' )
6981 }
70- return lib . spawn ( this . _bin , _options )
71- . then ( ( exiftoolProcess ) => {
72- //console.log(`Started exiftool process %s`, process.pid);
73- this . emit ( events . OPEN , exiftoolProcess . pid )
74- this . _process = exiftoolProcess
75-
76- this . _process . on ( 'exit' , this . _exitListener . bind ( this ) )
77- if ( ! lib . isReadable ( this . _process . stdout ) ) {
78- lib . killProcess ( this . _process )
79- throw new Error ( 'Process was not spawned with a readable stdout, check stdio options.' )
80- }
81- if ( ! lib . isWritable ( this . _process . stdin ) ) {
82- lib . killProcess ( this . _process )
83- throw new Error ( 'Process was not spawned with a writable stdin, check stdio options.' )
84- }
85-
86- // if process was spawned, stderr is readable (see lib/spawn)
87-
88- this . _process . stdout . setEncoding ( this . _encoding )
89- this . _process . stderr . setEncoding ( this . _encoding )
90-
91- // resolve-write streams
92- this . _stdoutResolveWs = beginReady . setupResolveWriteStreamPipe ( this . _process . stdout )
93- this . _stderrResolveWs = beginReady . setupResolveWriteStreamPipe ( this . _process . stderr )
94-
95- // handle errors so that Node does not crash
96- this . _stdoutResolveWs . on ( 'error' , console . error ) // eslint-disable-line no-console
97- this . _stderrResolveWs . on ( 'error' , console . error ) // eslint-disable-line no-console
98-
99- // debug
100- // exiftoolProcess.stdout.pipe(process.stdout)
101- // exiftoolProcess.stderr.pipe(process.stderr)
102-
103- this . _open = true
104-
105- return exiftoolProcess . pid
106- } )
82+
83+ // if process was spawned, stderr is readable (see lib/spawn)
84+
85+ this . _process . stdout . setEncoding ( this . _encoding )
86+ this . _process . stderr . setEncoding ( this . _encoding )
87+
88+ // resolve-write streams
89+ this . _stdoutResolveWs = beginReady . setupResolveWriteStreamPipe ( this . _process . stdout )
90+ this . _stderrResolveWs = beginReady . setupResolveWriteStreamPipe ( this . _process . stderr )
91+
92+ // handle errors so that Node does not crash
93+ this . _stdoutResolveWs . on ( 'error' , console . error ) // eslint-disable-line no-console
94+ this . _stderrResolveWs . on ( 'error' , console . error ) // eslint-disable-line no-console
95+
96+ // debug
97+ // exiftoolProcess.stdout.pipe(process.stdout)
98+ // exiftoolProcess.stderr.pipe(process.stderr)
99+
100+ this . _open = true
101+
102+ return exiftoolProcess . pid
107103 }
108104
109105 _exitListener ( ) {
@@ -120,13 +116,13 @@ class ExiftoolProcess extends EventEmitter {
120116 return this . _open
121117 }
122118
123- _executeCommand ( command , args , argsNoSplit , debug ) {
119+ async _executeCommand ( command , args , argsNoSplit , debug ) {
124120 //test this!
125121 if ( ! this . _open ) {
126- return Promise . reject ( new Error ( 'exiftool is not open' ) )
122+ throw new Error ( 'exiftool is not open' )
127123 }
128124 if ( this . _process . signalCode === 'SIGTERM' ) {
129- return Promise . reject ( new Error ( 'Could not connect to the exiftool process' ) )
125+ throw new Error ( 'Could not connect to the exiftool process' )
130126 }
131127
132128 const proc = debug === true ? process : this . _process
@@ -159,12 +155,12 @@ class ExiftoolProcess extends EventEmitter {
159155 * @returns {Promise.<{{data, error}}> } A promise to write metadata,
160156 * resolved with data from stdout and stderr.
161157 */
162- writeMetadata ( file , data , args , debug ) {
158+ async writeMetadata ( file , data , args , debug ) {
163159 if ( ! lib . isString ( file ) ) {
164160 throw new Error ( 'File must be a string' )
165161 }
166162 if ( ! lib . checkDataObject ( data ) ) {
167- return Promise . reject ( new Error ( 'Data argument is not an object' ) )
163+ throw new Error ( 'Data argument is not an object' )
168164 }
169165
170166 const writeArgs = lib . mapDataToTagArray ( data )
0 commit comments