/* @flow */
'use strict'

const os = require('os')

const depDiff = require('dependency-diff')
const semver = require('semver')

function deltaToMarkdown (
  diff /* : Object */,
  oldPkg /* : Object */
) /* : Promise<string> */ {
  let result = os.EOL + '### Changed' + os.EOL

  const oldDependencies = oldPkg.dependencies || {}

  const depDeltas = diff.dependencies || []

  depDeltas
    .forEach(({ name, operation, version }) => {
      let line
      if (operation === 'delete') {
        line = `-   no longer depend upon ${name}`
      } else if (operation === 'new') {
        line = `-   depend upon ${name} ${version}`
      } else if (operation === 'edit') {
        const oldVersion = oldDependencies[name]
        if (semver.gt(oldVersion, version)) {
          line = `-   update ${name} to ${version} (from ${oldVersion})`
        } else if (semver.lt(oldVersion, version)) {
          line = `-   rollback ${name} to ${version} (from ${oldVersion})`
        }
      }
      if (line) {
        result += os.EOL + line + os.EOL
      }
    })

  return Promise.resolve(result)
}

function diffPackages (
  oldPkg /* : Object */,
  pkg /* : Object */
) /* : Object */ {
  return depDiff().left(oldPkg).right(pkg).toObject()
}

module.exports = {
  deltaToMarkdown,
  diffPackages
}

@@ -228,56 +228,8 @@
 / %7B%0A
-  let result = os.EOL + '### Changed' + os.EOL%0A%0A
   co
@@ -328,60 +328,144 @@
 %0A%0A  
-depDeltas%0A    .forEach((%7B name, operation, version %7D
+// serial iteration with Promises%0A  return depDeltas.reduce((promise, %7B name, operation, version %7D) =%3E %7B%0A    return promise.then((result
 ) =%3E
@@ -1026,36 +1026,31 @@
     
-if (line) %7B%0A        
+return line ? (
 result +
 = os
@@ -1045,17 +1045,16 @@
 result +
-=
  os.EOL 
@@ -1072,24 +1072,26 @@
 .EOL
-%0A      %7D
+) : result
 %0A    %7D)%0A
 %0A  r
@@ -1086,25 +1086,20 @@
 %0A    %7D)%0A
-%0A
   
-return
+%7D,
  Promise
@@ -1107,22 +1107,48 @@
 resolve(
-result
+os.EOL + '### Changed' + os.EOL)
 )%0A%7D%0A%0Afun

refactor `deltaToMarkdown()` for serial Promises