import GitHub from 'github';

exports.handler = function(event, context, callback) {
  updateDependencies()
    .then((result) => callback(null, result))
    .catch(callback);
};

async function updateDependencies() {
  const github = new GitHub();

  const result = await github.repos.getContent({
    user: 'nerdlabs',
    repo: 'react-amp-layout',
    path: 'package.json'
  });

  const {encoding = 'base64', content} = result;
  const fileContent = new Buffer(content, encoding).toString('utf-8');
  const data = JSON.parse(fileContent);
  const updatedDependencies = await getUpdatedDependencies(data);
  return updatedDependencies;
}

// TODO: extract into its own file
import RegistryClient from 'npm-registry-client';
const noop = () => {};
const config = {
  log: {
    error: noop,
    warn: noop,
    info: noop,
    verbose: noop,
    silly: noop,
    http: noop,
    pause: noop,
    resume: noop
  }
};
const client = new RegistryClient(config);

function getPackage(name) {
  return new Promise((resolve, reject) => {
    const uri = `https://registry.npmjs.org/${name}`;

    client.get(uri, {}, (error, data) => error ? reject(error) : resolve(data));
  });
}

// TODO: extract into its own file
import semver from 'semver';

async function getUpdatedDependencies(manifest) {
  const {dependencies = {}, devDependencies = {}} = manifest;
  const allDependencies = {...dependencies, ...devDependencies};

  const packages = await Promise.all(
    Object.keys(allDependencies).map(getPackage)
  );

  const latestVersions = packages.map((manifest) => ({
    name: manifest.name,
    latest: manifest['dist-tags'].latest
  }));

  return latestVersions.filter(({name, latest}) => {
    const current = allDependencies[name];
    return semver.gt(latest, current);
  });
}

@@ -239,24 +239,237 @@
  GitHub();%0A%0A
+  // XXX: this is static at the moment, later it will of course be configurable%0A  // TODO: do we want to check multiple file paths (mono-repo)?%0A  // TODO: do we want to check multiple repositories per invocation?%0A
   const resu
@@ -505,16 +505,16 @@
 ntent(%7B%0A
-
     user
@@ -815,16 +815,548 @@
 (data);%0A
+%0A  // for every updated dependency:%0A  // - check whether a PR for this update already exists: https://mikedeboer.github.io/node-github/#api-pullRequests-getAll%0A  // - update version number in package.json%0A  // - create a new branch: https://mikedeboer.github.io/node-github/#api-gitdata-createReference%0A  // - update the package.json file in the newly created branch: https://mikedeboer.github.io/node-github/#api-repos-updateFile%0A  // - create a new pull request: https://mikedeboer.github.io/node-github/#api-pullRequests-create%0A%0A
   return
@@ -1465,16 +1465,17 @@
 lient';%0A
+%0A
 const no
@@ -1489,16 +1489,17 @@
  =%3E %7B%7D;%0A
+%0A
 const co
@@ -1658,16 +1658,17 @@
 %0A  %7D%0A%7D;%0A
+%0A
 const cl
@@ -2031,24 +2031,92 @@
 manifest) %7B%0A
+  // XXX: are there any other *Dependencies that we should include?%0A
   const %7Bdep
@@ -2404,16 +2404,70 @@
 t.name,%0A
+    // XXX: should we also check for other dist-tags?%0A
     late
@@ -2556,24 +2556,24 @@
 test%7D) =%3E %7B%0A
-
     const cu
@@ -2603,16 +2603,168 @@
 %5Bname%5D;%0A
+    // XXX: maybe also verify that this update would not already be covered by the%0A    // current semver range to reduce the amount of unneccessary PRs%0A
     retu

add source code comments for TODOs and execution flows