lib/util.js
function runExecIntoFile( label, filePath, options, execOptions, genCommand, cb ) {
  // escape the file paths
  var fileTempOutPath = path.join( outDir, path.basename( filePath, path.extname( filePath ) ) )
    , escapedFilePath = filePath.replace( /\s/g, '\\ ' )
    , escapedFileTempOutPath = fileTempOutPath.replace( /\s/g, '\\ ' )
    , cmd = genCommand( options, escapedFilePath, escapedFileTempOutPath )
    ;

  exec( cmd, execOptions,
    function( error /* , stdout, stderr */ ) {
      if ( error !== null ) {
        error = new Error( 'Error extracting [[ ' +
          path.basename( filePath ) + ' ]], exec error: ' + error.message );
        cb( error, null );
        return;
      }

      fs.exists( fileTempOutPath + '.txt', function( exists ) {
        if ( exists ) {
          fs.readFile( fileTempOutPath + '.txt', 'utf8', function( error2, text ) {
            if ( error2 ) {
              error2 = new Error( 'Error reading' + label +
                ' output at [[ ' + fileTempOutPath + ' ]], error: ' + error2.message );
              cb( error2, null );
            } else {
              fs.unlink( fileTempOutPath + '.txt', function( error3 ) {
                if ( error3 ) {
                  error3 = new Error( 'Error, ' + label +
                    ' , cleaning up temp file [[ ' + fileTempOutPath +
                    ' ]], error: ' + error3.message );
                  cb( error3, null );
                } else {
                  cb( null, text.toString() );
                }
              });
            }
          });
        } else {
          error = new Error( 'Error reading ' + label +
            ' output at [[ ' + fileTempOutPath + ' ]], file does not exist' );
          cb( error, null );
        }
      });
    }
  );
}
function runExecIntoFile( label, filePath, options, execOptions, genCommand, cb ) {
  // escape the file paths
  var fileTempOutPath = path.join( outDir, path.basename( filePath, path.extname( filePath ) ) )
    , escapedFilePath = filePath.replace( /\s/g, '\\ ' )
    , escapedFileTempOutPath = fileTempOutPath.replace( /\s/g, '\\ ' )
    , cmd = genCommand( options, escapedFilePath, escapedFileTempOutPath )
    ;

  exec( cmd, execOptions,
    function( error /* , stdout, stderr */ ) {
      if ( error !== null ) {
        error = new Error( 'Error extracting [[ ' +
          path.basename( filePath ) + ' ]], exec error: ' + error.message );
        cb( error, null );
        return;
      }

      fs.exists( fileTempOutPath + '.txt', function( exists ) {
        if ( exists ) {
          fs.readFile( fileTempOutPath + '.txt', 'utf8', function( error2, text ) {
            if ( error2 ) {
              error2 = new Error( 'Error reading' + label +
                ' output at [[ ' + fileTempOutPath + ' ]], error: ' + error2.message );
              cb( error2, null );
            } else {
              fs.unlink( fileTempOutPath + '.txt', function( error3 ) {
                if ( error3 ) {
                  error3 = new Error( 'Error, ' + label +
                    ' , cleaning up temp file [[ ' + fileTempOutPath +
                    ' ]], error: ' + error3.message );
                  cb( error3, null );
                } else {
                  cb( null, text.toString() );
                }
              });
            }
          });
        } else {
          error = new Error( 'Error reading ' + label +
            ' output at [[ ' + fileTempOutPath + ' ]], file does not exist' );
          cb( error, null );
        }
      });
    }
  );
}
function runExecIntoFile ( label , filePath , options , execOptions , genCommand , cb ) { // escape the file paths var fileTempOutPath = path . join ( outDir , path . basename ( filePath , path . extname ( filePath ) ) ) , escapedFilePath = filePath . replace ( / \s / g , '\\ ' ) , escapedFileTempOutPath = fileTempOutPath . replace ( / \s / g , '\\ ' ) , cmd = genCommand ( options , escapedFilePath , escapedFileTempOutPath ) ; exec ( cmd , execOptions , function ( error /* , stdout, stderr */ ) { if ( error !== null ) { error = new Error ( 'Error extracting [[ ' + path . basename ( filePath ) + ' ]], exec error: ' + error . message ) ; cb ( error , null ) ; return ; } fs . exists ( fileTempOutPath + '.txt' , function ( exists ) { if ( exists ) { fs . readFile ( fileTempOutPath + '.txt' , 'utf8' , function ( error2 , text ) { if ( error2 ) { error2 = new Error ( 'Error reading' + label + ' output at [[ ' + fileTempOutPath + ' ]], error: ' + error2 . message ) ; cb ( error2 , null ) ; } else { fs . unlink ( fileTempOutPath + '.txt' , function ( error3 ) { if ( error3 ) { error3 = new Error ( 'Error, ' + label + ' , cleaning up temp file [[ ' + fileTempOutPath + ' ]], error: ' + error3 . message ) ; cb ( error3 , null ) ; } else { cb ( null , text . toString ( ) ) ; } } ) ; } } ) ; } else { error = new Error ( 'Error reading ' + label + ' output at [[ ' + fileTempOutPath + ' ]], file does not exist' ) ; cb ( error , null ) ; } } ) ; } ) ; }
1) builds an exec command using provided `genCommand` callback
2) runs that command against an input file path
resulting in an output file
3) reads that output file in
4) cleans the output file up
5) executes a callback with the contents of the file

@param {string} label Name for the extractor, e.g. `Tesseract`
@param {string} filePath path to file to be extractor
@param {object} options extractor options as provided
via user configuration
@param {object} execOptions execution options passed to
`exec` commmand as provided via user configuration
@param {function} genCommand function used to generate
the command to be executed
@param {string} cb callback that is passed error/text
1 ) builds an exec command using provided genCommand callback 2 ) runs that command against an input file path resulting in an output file 3 ) reads that output file in 4 ) cleans the output file up 5 ) executes a callback with the contents of the file