_ = require 'underscore-plus'
ProviderBase = require './provider-base'
path = require 'path'
fs = require 'fs-plus'
{itemForGitDiff} = require '../utils'

# Borrowed and modified from fuzzy-finder's git-diff-view.coffee
eachModifiedFilePaths = (fn) ->
  for repo in atom.project.getRepositories() when repo?
    workingDirectory = repo.getWorkingDirectory()
    for filePath of repo.statuses
      filePath = path.join(workingDirectory, filePath)
      if fs.isFileSync(filePath)
        fn(repo, filePath)

getItemsForFilePath = (repo, filePath) ->
  atom.workspace.open(filePath, activateItem: false).then (editor) ->
    # When file was completely new file, getLineDiffs return null, so need guard.
    diffs = repo.getLineDiffs(filePath, editor.getText()) ? []
    diffs.map (diff) ->
      itemForGitDiff(diff, {editor, filePath})

module.exports =
class GitDiffAll extends ProviderBase
  refreshOnDidSave: true
  showProjectHeader: true
  showFileHeader: true

  getItems: ->
    promises = []
    eachModifiedFilePaths (repo, filePath) =>
      promise = getItemsForFilePath(repo, filePath).then (items) =>
        @updateItems(_.compact(items))
      promises.push(promise)

    Promise.all(promises).then =>
      @finishUpdateItems()

@@ -1,8 +1,29 @@
+/** @babel */%0A%0Aconst 
 _ = requ
@@ -25,18 +25,18 @@
  require
- '
+(%22
 undersco
@@ -42,18 +42,25 @@
 ore-plus
-'%0A
+%22)%0Aconst 
 Provider
@@ -73,18 +73,18 @@
  require
- '
+(%22
 ./provid
@@ -90,18 +90,25 @@
 der-base
-'%0A
+%22)%0Aconst 
 path = r
@@ -117,16 +117,23 @@
 uire
- '
+(%22
 path
-'%0A
+%22)%0Aconst 
 fs =
@@ -144,19 +144,26 @@
 uire
- '
+(%22
 fs-plus
-'%0A
+%22)%0Aconst 
 %7Bite
@@ -188,18 +188,18 @@
 uire
- '
+(%22
 ../utils
 '%0A%0A#
@@ -198,167 +198,100 @@
 tils
-'%0A%0A# Borrowed and modified from fuzzy-finder's git-diff-view.coffee%0AeachModifiedFilePaths = (fn) -%3E%0A  for repo in atom.project.getRepositories() when repo?%0A   
+%22)%0A%0Afunction getModifiedFilePathsForRepository(repository) %7B%0A  const filePaths = %5B%5D%0A%0A  const
  wor
@@ -306,24 +306,30 @@
 ctory = repo
+sitory
 .getWorkingD
@@ -343,16 +343,19 @@
 y()%0A
-  
   for 
+(let 
 file
@@ -363,15 +363,21 @@
 ath 
-of
+in
  repo
+sitory
 .sta
@@ -381,19 +381,20 @@
 statuses
-%0A  
+) %7B%0A
     file
@@ -446,13 +446,12 @@
     
-  
 if 
+(
 fs.i
@@ -473,37 +473,91 @@
 ath)
+) %7B
 %0A      
-  fn(repo, filePath)%0A%0A
+filePaths.push(filePath)%0A    %7D%0A  %7D%0A%0A  return filePaths%0A%7D%0A%0Aasync function 
 getI
@@ -571,19 +571,16 @@
 FilePath
- = 
 (repo, f
@@ -588,21 +588,41 @@
 lePath) 
--%3E
+%7B
 %0A  
+const editor = await 
 atom.wor
@@ -643,16 +643,17 @@
 lePath, 
+%7B
 activate
@@ -667,32 +667,15 @@
 alse
-).then (editor) -%3E%0A    #
+%7D)%0A  //
  Whe
@@ -749,17 +749,19 @@
 uard.%0A  
- 
+let
  diffs =
@@ -810,17 +810,34 @@
 t())
- ? %5B%5D%0A   
+%0A  if (diffs) %7B%0A    return
  dif
@@ -846,24 +846,16 @@
 .map
- 
 (diff
-) -%3E%0A     
+ =%3E
  ite
@@ -895,64 +895,66 @@
 th%7D)
-%0A%0Amodule.exports =%0Aclass GitDiffAll extends ProviderBase
+)%0A  %7D else %7B%0A    return %5B%5D%0A  %7D%0A%7D%0A%0Aconst providerConfig = %7B
 %0A  r
@@ -970,24 +970,25 @@
 idSave: true
+,
 %0A  showProje
@@ -1001,16 +1001,17 @@
 er: true
+,
 %0A  showF
@@ -1029,196 +1029,540 @@
 true
-%0A%0A  getItems: -%3E%0A    promises = %5B%5D%0A    eachModifiedFilePaths (repo, filePath) =%3E%0A      promise = getItemsForFilePath(repo, filePath).then (items) =%3E%0A        @updateItems(_.compact(items))%0A
+,%0A%7D%0A%0Aclass GitDiffAll extends ProviderBase %7B%0A  constructor(...args) %7B%0A    super(...args)%0A    Object.assign(this, providerConfig)%0A  %7D%0A%0A  getItems() %7B%0A    const promises = %5B%5D%0A    const repositories = atom.project%0A      .getRepositories()%0A      .filter(repo =%3E repo != null)%0A%0A    const updateItems = items =%3E this.updateItems(_.compact(items))%0A    for (const repo of repositories) %7B%0A      for (const filePath of getModifiedFilePathsForRepository(repo)) %7B%0A        const promise = getItemsForFilePath(repo, filePath).then(updateItems)%0A  
     
@@ -1586,16 +1586,29 @@
 romise)%0A
+      %7D%0A    %7D
 %0A    Pro
@@ -1634,19 +1634,20 @@
 then
- =%3E%0A      @
+(() =%3E this.
 fini
@@ -1661,9 +1661,44 @@
 eItems()
+)%0A  %7D%0A%7D%0Amodule.exports = GitDiffAll
 %0A

convert to es6: git-diff-all