'use strict';
const fs = require('fs');
const path = require('path');
const basename = path.basename(module.filename);

/**
 * Create or update the given game definition in the
 * database, complete with aliases, game modes, genres
 * and platforms.
 *
 * @param  {Object} db Sequelize instance with models.
 * @param  {Object} definition Complete game definition to sync.
 * @return {Promise} Transaction containing the entire upsert.
 */
function upsertGame(db, definition) {
  return db.sequelize.transaction((t) => {
    let game;
    return db.Game.findOne({
      transaction: t,
      where: {
        title: definition.info.title,
      },
    }).then((entity) => {
      if (entity) {
        return entity.update(definition.info, {
          transaction: t,
        });
      } else {
        return db.Game.create(definition.info, {
          transaction: t,
        });
      }
    }).then((entity) => {
      game = entity;
      return Promise.all([
        db.Genre.findAll({
          transaction: t,
          where: {
            short: {
              $in: definition.genres,
            },
          },
        }),
        db.Platform.findAll({
          transaction: t,
          where: {
            short: {
              $in: definition.platforms,
            },
          },
        }),
      ]);
    }).then(([genres, platforms]) => {
      game.setGenres(genres);
      game.setPlatforms(platforms);
      return game.save({transaction: t});
    }).then((entity) => {
      const aliases = definition.aliases.map((alias) => {
        return db.GameAlias.upsert({
          name: alias,
          GameId: game.id,
        }, {
          transaction: t,
        });
      });
      return Promise.all(aliases);
    }).then((created) => {
      const modes = definition.modes.map((mode) => {
        return db.GameMode.upsert(Object.assign({
          GameId: game.id,
        }, mode), {
          transaction: t,
        });
      });
      return Promise.all(modes);
    }).then((created) => {
      console.log(`Upsert complete for: ${game.name}`);
    });
  });
}

module.exports = {
  initialize: (db) => {
    const ops = [];
    fs
      .readdirSync(__dirname)
      .filter((file) => {
        return (file.indexOf('.') !== 0)
            && (file !== basename)
            && (file.slice(-3) === '.js');
      })
      .forEach((file) => {
        let game = require(path.resolve(__dirname, file));
        ops.push(upsertGame(db, game));
        delete require.cache[require.resolve(path.resolve(__dirname, file))];
      });
    return Promise.all(ops);
  },
};

@@ -17,13 +17,65 @@
 nst 
-fs = 
+%7Bpromisify%7D = require('util');%0Aconst readdir = promisify(
 requ
@@ -83,16 +83,25 @@
 re('fs')
+.readdir)
 ;%0Aconst 
@@ -331,41 +331,54 @@
 ct%7D 
-db Sequelize instance with models
+ctx Application context, including db instance
 .%0A *
@@ -527,18 +527,19 @@
 ertGame(
-db
+ctx
 , defini
@@ -555,16 +555,20 @@
  return 
+ctx.
 db.seque
@@ -610,32 +610,36 @@
 ame;%0A    return 
+ctx.
 db.Game.findOne(
@@ -882,24 +882,28 @@
      return 
+ctx.
 db.Game.crea
@@ -1052,16 +1052,20 @@
         
+ctx.
 db.Genre
@@ -1227,16 +1227,20 @@
         
+ctx.
 db.Platf
@@ -1652,24 +1652,28 @@
      return 
+ctx.
 db.GameAlias
@@ -1923,16 +1923,20 @@
  return 
+ctx.
 db.GameM
@@ -2128,14 +2128,10 @@
    c
-onsole
+tx
 .log
@@ -2160,19 +2160,20 @@
  $%7Bgame.
-nam
+titl
 e%7D%60);%0A  
@@ -2191,53 +2191,49 @@
 %0A%7D%0A%0A
-module.exports = %7B%0A  initialize: (db) =%3E
+async function initialize(ctx, client)
  %7B%0A
-  
   co
@@ -2252,31 +2252,35 @@
 ;%0A  
-  fs%0A      .
+const games = await 
 readdir
-Sync
 (__d
@@ -2286,23 +2286,25 @@
 dirname)
+;
 %0A  
-    
+games
 .filter(
@@ -2315,28 +2315,24 @@
 e) =%3E %7B%0A    
-  
-  
 return (file
@@ -2352,36 +2352,32 @@
  !== 0)%0A        
-    
 && (file !== bas
@@ -2387,28 +2387,24 @@
 me)%0A        
-   
- 
 && (file.sli
@@ -2428,21 +2428,10 @@
 ;%0A  
-    %7D)%0A      
+%7D)
 .for
@@ -2455,31 +2455,23 @@
     
-    let game = require(
+let fullpath = 
 path
@@ -2495,23 +2495,52 @@
 e, file)
-)
 ;%0A    
+let game = require(fullpath);%0A
     ops.
@@ -2555,18 +2555,19 @@
 ertGame(
-db
+ctx
 , game))
@@ -2568,20 +2568,16 @@
 game));%0A
-    
     dele
@@ -2613,53 +2613,28 @@
 lve(
+full
 path
-.resolve(__dirname, file)
 )%5D;%0A
-    
   %7D);%0A  
   re
@@ -2629,24 +2629,21 @@
   %7D);%0A  
-  return
+await
  Promise
@@ -2657,12 +2657,47 @@
 s);%0A
-  %7D,%0A%7D
+%7D%0A%0Amodule.exports.initialize = initialize
 ;%0A

Use fancy async functions for initialization