From b8927a7d7380a4577e291ea0665a1096557c9cd0 Mon Sep 17 00:00:00 2001
From: Matheus Faria <matheus.sousa.faria@gmail.com>
Date: Wed, 24 Jan 2018 16:46:36 -0200
Subject: [PATCH] Refactor module import path and order

---
 scripts/bot/brain.coffee        |  9 +--------
 scripts/bot/index.coffee        |  8 +++++++-
 scripts/events/configure.coffee | 12 +++---------
 scripts/events/error.coffee     |  6 ++----
 scripts/events/respond.coffee   |  7 +++----
 scripts/lib/security.coffee     |  2 +-
 6 files changed, 17 insertions(+), 27 deletions(-)

diff --git a/scripts/bot/brain.coffee b/scripts/bot/brain.coffee
index e7510b1..ea904db 100644
--- a/scripts/bot/brain.coffee
+++ b/scripts/bot/brain.coffee
@@ -1,5 +1,3 @@
-fs = require 'fs'
-path = require 'path'
 natural = require 'natural'
 
 brain = {}
@@ -10,15 +8,10 @@ PorterStemmer = natural.PorterStemmer
 if lang != 'en'
   PorterStemmer = require '../../node_modules/natural/lib/natural/stemmers/porter_stemmer_' + lang + '.js'
 
-events = {}
 nodes = {}
 error_count = 0
 err_nodes = 0
 
-eventsPath = path.join __dirname, '..', 'events'
-for event in fs.readdirSync(eventsPath).sort()
-  events[event.replace /\.coffee$/, ''] = require path.join eventsPath, event
-
 classifyInteraction = (interaction, classifier) ->
   if Array.isArray interaction.expect
     for doc in interaction.expect
@@ -53,7 +46,7 @@ brain.train = () ->
 
   for interaction in global.config.interactions
     {name, event} = interaction
-    global.nodes[name] = new events[event] interaction
+    global.nodes[name] = new global.events[event] interaction
     # count error nodes
     if name.substr(0,5) == "error"
       err_nodes++
diff --git a/scripts/bot/index.coffee b/scripts/bot/index.coffee
index 0687466..b71d7ee 100644
--- a/scripts/bot/index.coffee
+++ b/scripts/bot/index.coffee
@@ -1,10 +1,16 @@
 require 'coffeescript/register'
 
+fs = require 'fs'
 path = require 'path'
 
 {regexEscape, loadConfigfile} = require '../lib/common'
 {getUserRoles, checkRole} = require '../lib/security'
-brain = require './brain'
+brain = require '../bot/brain'
+
+global.events = {}
+eventsPath = path.join __dirname, '..', 'events'
+for event in fs.readdirSync(eventsPath).sort()
+  global.events[event.replace /\.coffee$/, ''] = require path.join eventsPath, event
 
 typing = (res, t) ->
   res.robot.adapter.callMethod 'stream-notify-room',
diff --git a/scripts/events/configure.coffee b/scripts/events/configure.coffee
index e62226f..aa0658f 100644
--- a/scripts/events/configure.coffee
+++ b/scripts/events/configure.coffee
@@ -1,12 +1,8 @@
 require 'coffeescript/register'
 
-path = require 'path'
-natural = require 'natural'
-
+brain = require '../bot/brain'
 { msgVariables, stringElseRandomKey, loadConfigfile, getConfigFilePath } = require  '../lib/common'
-{ checkRole } = require '../lib/security.coffee'
-
-answers = {}
+{checkRole} = require '../lib/security'
 
 class configure
   constructor: (@interaction) ->
@@ -41,10 +37,8 @@ class configure
     return
 
   retrain: (msg) ->
-    console.log 'inside retrain'
-    scriptPath = path.join __dirname, '..'
     global.config = loadConfigfile getConfigFilePath()
-    global.train()
+    brain.train()
 
     type = @interaction.type?.toLowerCase() or 'random'
     switch type
diff --git a/scripts/events/error.coffee b/scripts/events/error.coffee
index ebc34da..db72250 100644
--- a/scripts/events/error.coffee
+++ b/scripts/events/error.coffee
@@ -1,8 +1,6 @@
-path = require 'path'
-natural = require 'natural'
+require 'coffeescript/register'
 
-{ msgVariables, stringElseRandomKey } = require path.join '..', 'lib', 'common.coffee'
-answers = {}
+{msgVariables, stringElseRandomKey} = require '../lib/common'
 
 class error
   constructor: (@interaction) ->
diff --git a/scripts/events/respond.coffee b/scripts/events/respond.coffee
index 18a7e14..08db513 100644
--- a/scripts/events/respond.coffee
+++ b/scripts/events/respond.coffee
@@ -1,8 +1,7 @@
-path = require 'path'
-natural = require 'natural'
+require 'coffeescript/register'
+
+{msgVariables, stringElseRandomKey} = require '../lib/common'
 
-{ msgVariables, stringElseRandomKey } = require path.join '..', 'lib', 'common.coffee'
-answers = {}
 livechat_department = (process.env.LIVECHAT_DEPARTMENT_ID || null )
 
 class respond
diff --git a/scripts/lib/security.coffee b/scripts/lib/security.coffee
index 13636da..a8ed174 100644
--- a/scripts/lib/security.coffee
+++ b/scripts/lib/security.coffee
@@ -21,7 +21,7 @@ security.checkRole = (msg, role) ->
     else
       return true
   else
-    msg.robot.logger.info 'Role ' + role + ' não encontrado'
+    msg.robot.logger.info 'Role ' + role + ' not found'
     return false
 
 module.exports = security
-- 
GitLab