const md5 = require('md5')
const execS = require('child_process').exec

const { replaceSpaceCharacters } = require('./helpers/util')

const convert = ({ files, outputPath, name }) => {
  process.env['PATH'] = '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'

  return new Promise((resolve, reject) => {
    execS('which convert', (error, stdout, stderr) => {
      if (error) reject(error)
      else {
        const fileString = files.map(replaceSpaceCharacters).join(' ')
        const outputName = name || `ALCHEMY-${md5(fileString).substr(0, 6)}.pdf`
        const command = `convert ${fileString} ${outputPath}${outputName}`
        console.log(command)

        execS(command, (error) => {
          if (error) reject(error)
          else resolve(outputName)
        })
      }
    })
  })
}

module.exports = {
  convert
}

@@ -385,23 +385,9 @@
 or)%0A
-      else %7B%0A  
+%0A
     
@@ -451,18 +451,16 @@
 in(' ')%0A
-  
       co
@@ -530,18 +530,16 @@
 )%7D.pdf%60%0A
-  
       co
@@ -605,26 +605,24 @@
 ame%7D%60%0A      
-  
 console.log(
@@ -631,18 +631,16 @@
 mmand)%0A%0A
-  
       ex
@@ -669,26 +669,24 @@
 %3E %7B%0A        
- 
- 
 if (error) r
@@ -698,18 +698,16 @@
 (error)%0A
-  
         
@@ -741,21 +741,11 @@
     
- 
- 
 %7D)%0A
-      %7D%0A
     

Fix bug