/* @flow */

var fs           = require('fs');
var EventEmitter = require('events');
import { has } from 'lodash/has';
import { formatVersionsAndFilterPrivate, getNpmData } from './ramda';
import { NpmConfig, httpsGetPromise } from './helpers';

exports.rootDir = process.cwd() + '/';
exports.dependencies = {};
exports.event = new EventEmitter();

exports.backpat = function(callback: Function) {
  if (typeof callback !== 'function') {
    throw new TypeError('backpat should accept type function as input parameter');
  }
  fs.readFile(exports.rootDir + '/package.json', function(err, data) {
    if (err) throw new Error(err);
    var pkgjsn = JSON.parse(data.toString('utf8'));

    if (has('dependencies', pkgjsn)) {
      const deps = formatVersionsAndFilterPrivate(pkgjsn.dependencies);
      getNpmData(deps);
    }

    if (has('devDependencies', pkgjsn)) {
      const devDeps = formatVersionsAndFilterPrivate(pkgjsn.dependencies);
      getNpmData(devDeps);
    }

    for (var k in exports.dependencies) {
      helpers.gatherDetails(k);
    }

    // Manually inject Node because it's certainly part of your stack
    exports.dependencies.node = {
      name        : 'Node.js',
      version     : process.versions.node,
      description : 'A JavaScript runtime ✨🐢🚀✨',
      downloads   : 10000000 // A fake number since Node isn't downloaded on npm
    };
  });
};

@@ -10,94 +10,18 @@
 */%0A%0A
-var fs           = require('fs');%0Avar EventEmitter = require('events');%0A
 import
- %7B
  has
- %7D
  fro
@@ -119,34 +119,23 @@
 t %7B 
-NpmConfig, httpsGetPromise
+readPackageJson
  %7D f
@@ -162,140 +162,27 @@
 port
-s.rootDir = process.cwd() + '/';%0Aexports.dependencies = %7B%7D;%0Aexports.event = new EventEmitter();%0A%0Aexports.backpat = function(callback
+ function backpat(f
 : Fu
@@ -208,16 +208,9 @@
 eof 
-callback
+f
  !==
@@ -252,271 +252,126 @@
 ror(
-'backpat should accept type function as input parameter');%0A  %7D%0A  fs.readFile(exports.rootDir + '/package.json', function(err, data) %7B%0A    if (err) throw new Error(err);%0A    var pkgjsn = JSON.parse(data.toString('utf8'));%0A%0A    if (has('dependencies', pkgjsn))
+%60Expected type function but received $%7B typeof f %7D instead%60);%0A  %7D%0A  return readPackageJson(f).then((packageJson) =%3E
  %7B%0A
-  
     
@@ -387,46 +387,38 @@
 s = 
-formatVersionsAndFilterPrivate(pkgjsn.
+%7B%7D;%0A    if (has(packageJson, '
 depe
@@ -429,274 +429,100 @@
 cies
-);
+')) %7B
 %0A      
-getNpmData(deps);%0A    %7D%0A%0A    if (has('devDependencies', pkgjsn)) %7B%0A      const devDeps = formatVersionsAndFilterPrivate(pkgjsn.dependencies);%0A      getNpmData(devDeps);%0A    %7D%0A%0A    for (var k in exports.dependencies) %7B%0A      helpers.gatherDetails(k
+deps.dependencies = formatVersionsAndFilterPrivate(packageJson.dependencies
 );%0A  
-  %7D%0A%0A
     
@@ -591,22 +591,21 @@
 ack%0A    
-export
+  dep
 s.depend
@@ -620,16 +620,18 @@
 ode = %7B%0A
+  
       na
@@ -659,16 +659,18 @@
 ,%0A      
+  
 version 
@@ -704,16 +704,18 @@
 ,%0A      
+ 
+ 
 descript
@@ -749,16 +749,18 @@
  %E2%9C%A8%F0%9F%90%A2%F0%9F%9A%80%E2%9C%A8',%0A
+  
       do
@@ -840,16 +840,237 @@
     
+  
 %7D;%0A  
+ 
-%7D);%0A%7D;
+ %7D%0A    if (has(packageJson, 'devDependencies')) %7B%0A      deps.dependencies = formatVersionsAndFilterPrivate(packageJson.devDependencies);%0A    %7D%0A    getNpmData(deps);%0A    return deps;%0A  %7D);%0A%7D%0A%0A// getNpmData(devDeps);%0A  //%0A  //
 %0A

write functionality for index functionality