11/* Interact with MakeMKV CLI */
22
33const fs = require ( 'fs' ) ,
4+ os = require ( 'os' ) ,
45 EventEmitter = require ( 'events' ) . EventEmitter ,
56 parseCsv = require ( 'csv-parse/lib/sync' ) ,
67 path = require ( 'path' ) ,
@@ -10,7 +11,6 @@ const fs = require('fs'),
1011
1112const ALL_DRIVES = constants . ALL_DRIVES ,
1213 DRIVE_STATE_FLAG = constants . DRIVE_STATE_FLAG ,
13- NEWLINE_CHAR = constants . NEWLINE_CHAR ,
1414 TRACK_ATTRIBUTES = constants . TRACK_ATTRIBUTES ;
1515
1616const CONVERSION_PROFILE = settings . CONVERSION_PROFILE ,
@@ -151,44 +151,61 @@ class MakeMkv {
151151
152152 let newCallback = ( exitCode , stdErr , stdOut ) => {
153153
154- this . busyDevices . delete ( ALL_DRIVES ) ;
154+ console . debug ( stdOut ) ;
155+ console . debug ( stdErr ) ;
155156
156- if ( exitCode ) {
157- // @TODO : handle errors.
158- }
157+ this . busyDevices . delete ( ALL_DRIVES ) ;
159158
160159 let discData = { } ,
161- parsedOutput = parseCsv ( stdOut ) ;
160+ parsedOutput = parseCsv (
161+ stdOut . join ( os . EOL ) ,
162+ { relax_column_count : true }
163+ ) ;
162164
163165 parsedOutput . map ( ( dataRow ) => {
164166
165167 dataRow = this . _parseOutputRow ( dataRow ) ;
166168
167- if ( dataRow . attributeType !== this . ATTRIBUTE_TYPE_DRIVE_INFO ) {
169+ if ( ! dataRow ) {
168170 return ;
169171 }
170- if ( ! dataRow . driveId ) {
172+
173+ console . debug (
174+ `AttributeType: ${ dataRow . attributeType }
175+ DriveId: ${ dataRow . driveId } `
176+ ) ;
177+
178+ if ( dataRow . attributeType == this . ATTRIBUTE_TYPE_DRIVE_INFO ) {
179+ console . log ( dataRow ) ;
180+ }
181+
182+ if (
183+ ! dataRow
184+ || dataRow . attributeType !== this . ATTRIBUTE_TYPE_DRIVE_INFO
185+ || ! dataRow . driveId
186+ ) {
171187 return ;
172188 }
173189
174- // Maintain an index => drive mapping.
175- // @TODO : Is this even used? Feels very legacy to me...
176- this . driveMap [ dataRow . driveIndex ] = dataRow . driveId ;
190+ console . debug ( dataRow ) ;
177191
178192 // Assign the flags to string representations
179193 dataRow . driveState = DRIVE_STATE_FLAG [ dataRow . driveState ] ;
194+
180195 // @TODO : Bitwise determination of the disc filesystem flags.
181196
182197 // Add the processed row into the data.
183198 discData [ dataRow . driveId ] = dataRow ;
184199
185200 } ) ;
186201
202+ console . debug ( "Got data for drives." ) ;
203+ console . debug ( discData ) ;
187204 callback ( discData ) ;
188205
189206 } ;
190207
191- this . _spawnMakemkv ( ALL_DRIVES , [ 'info' ] , newCallback ) ;
208+ this . _spawnMakemkv ( ALL_DRIVES , [ 'info' , 'disc:9999' ] , newCallback ) ;
192209
193210 }
194211
@@ -275,18 +292,18 @@ class MakeMkv {
275292 The output lines appear in a certain order::
276293
277294 TCOUNT (track count)
278- CINFO (Disc level information)
279- ...
280- TINFO (Track level information)
295+ CINFO (Disc information)
281296 ...
282- SINFO (Stream level information)
297+ TINFO (Track 0 information)
283298 ...
284- TINFO (Next track level information
285- ...
286- SINFO (Next stream level information)
299+ SINFO (Track 0, Stream 0 information)
287300 ...
301+ SINFO (Track 0, Stream 1 information)
288302 ...
303+ TINFO (Track 1 information)
289304 ...
305+ SINFO (Track 1, Stream 0 information)
306+ ... repeating ...
290307
291308 @param {int } exitCode MakeMKV exit code
292309 @param {array } stdErr Array of lines representing the error stream.
@@ -379,10 +396,12 @@ class MakeMkv {
379396
380397 let [ attrType , attrFlag ] = dataRow [ 0 ] . split ( ':' ) ;
381398
399+ // Zip the keys and values into an obj and add the attribute type.
382400 let parseData = ( keys ) => {
383401 dataRow [ 0 ] = attrFlag ;
384- let data = keys . map ( ( key , idx ) => [ key , dataRow [ idx ] ] ) ;
385- data . attributeType = attrType ;
402+ let data = { attributeType : attrType } ;
403+ keys . forEach ( ( key , idx ) => data [ key ] = dataRow [ idx ] ) ;
404+ return data ;
386405 } ;
387406
388407 switch ( attrType ) {
@@ -405,6 +424,9 @@ class MakeMkv {
405424 case this . ATTRIBUTE_TYPE_STREAM_INFO :
406425 return parseData ( this . HEADERS_DISC_INFO_STREAM ) ;
407426
427+ default :
428+ return false ;
429+
408430 }
409431
410432 }
@@ -431,7 +453,7 @@ class MakeMkv {
431453 */
432454 _spawn ( args , binaryPath , callback ) {
433455
434- console . log ( `Spawning ${ binaryPath } with args ${ args } ` ) ;
456+ console . log ( `Spawning " ${ binaryPath } " with args: " ${ args } " ` ) ;
435457
436458 let process = spawn ( binaryPath , args ) ;
437459
@@ -446,12 +468,15 @@ class MakeMkv {
446468 process . stderr . setEncoding ( 'utf-8' ) ;
447469
448470 process . stdout . on (
449- 'data' , ( data ) => stdOut . push . apply ( data . split ( NEWLINE_CHAR ) )
471+ 'data' , ( data ) => { stdOut . push ( ... data . split ( os . EOL ) ) }
450472 ) ;
451473 process . stderr . on (
452- 'data' , ( data ) => stdErr . push . apply ( data . split ( NEWLINE_CHAR ) )
474+ 'data' , ( data ) => { stdErr . push ( ... data . split ( os . EOL ) ) }
453475 ) ;
454- process . on ( 'exit' , ( code ) => callback ( code , stdErr , stdOut ) ) ;
476+ process . on ( 'exit' , ( code ) => {
477+ console . debug ( `MakeMKV exited with code ${ code } .` ) ;
478+ callback ( code , stdErr , stdOut ) ;
479+ } ) ;
455480
456481 }
457482
@@ -479,6 +504,7 @@ class MakeMkv {
479504 return this . _waitAvailable ( driveId , callback , ...args ) ;
480505 }
481506 } ;
507+ console . debug ( `Waiting for ${ driveId } to become available.` ) ;
482508 setTimeout ( timer , 1000 ) ;
483509 }
484510
0 commit comments