From 1774b9dee6f7d963dd5ac244e252c5a5a4cbbb99 Mon Sep 17 00:00:00 2001 From: Matheus Faria <matheus.sousa.faria@gmail.com> Date: Mon, 22 Jan 2018 17:58:19 -0200 Subject: [PATCH] Removing config file path from global scope --- Dockerfile | 2 +- docker-compose.yml | 2 +- scripts/bot/index.coffee | 3 +-- scripts/events/configure.coffee | 5 ++--- scripts/index.coffee | 16 +++++++--------- scripts/lib/common.coffee | 3 +++ 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index 07fdcdd..ed7f306 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM node:alpine LABEL mantainer "Diego Dorgam <diego.dorgam@rocket.chat>" ENV HUBOT_LANG='en' \ - HUBOT_CORPUS='corpus.yml' \ + HUBOT_CORPUS='training_data/corpus.yml' \ HUBOT_ADAPTER=rocketchat \ HUBOT_OWNER=RocketChat \ HUBOT_NAME=HubotNatural \ diff --git a/docker-compose.yml b/docker-compose.yml index 31af483..afaa03e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -50,7 +50,7 @@ services: - HUBOT_OWNER=RocketChat - HUBOT_DESCRIPTION='Hubot natural language processing' - HUBOT_LOG_LEVEL=debug - - HUBOT_CORPUS=corpus.yml + - HUBOT_CORPUS=training_data/corpus.yml - HUBOT_LANG=pt - RESPOND_TO_DM=true - RESPOND_TO_LIVECHAT=true diff --git a/scripts/bot/index.coffee b/scripts/bot/index.coffee index 7e1baa4..5a71474 100644 --- a/scripts/bot/index.coffee +++ b/scripts/bot/index.coffee @@ -135,9 +135,8 @@ clearErrors = (res) -> key = 'errors_'+res.envelope.room+'_'+res.envelope.user.id res.robot.brain.set(key, 0) -module.exports = (_config, _configPath, robot) -> +module.exports = (_config, robot) -> global.config = _config - global.configPath = _configPath global.usersAndRoles = getUserRoles(robot) diff --git a/scripts/events/configure.coffee b/scripts/events/configure.coffee index e1b520d..74621f6 100644 --- a/scripts/events/configure.coffee +++ b/scripts/events/configure.coffee @@ -1,10 +1,9 @@ path = require 'path' natural = require 'natural' -{msgVariables, stringElseRandomKey, loadConfigfile} = require path.join '..', 'lib', 'common.coffee' +{msgVariables, stringElseRandomKey, loadConfigfile, getConfigFilePath} = require '../lib/common' {checkRole} = require path.join '..', 'lib', 'security.coffee' answers = {} -# usersAndRoles = getUserRoles() class configure constructor: (@interaction) -> @@ -40,7 +39,7 @@ class configure retrain: (msg) -> console.log 'inside retrain' scriptPath = path.join __dirname, '..' - global.config = loadConfigfile(global.configPath) + global.config = loadConfigfile getConfigFilePath() global.train() type = @interaction.type?.toLowerCase() or 'random' diff --git a/scripts/index.coffee b/scripts/index.coffee index 998df38..fd66a27 100644 --- a/scripts/index.coffee +++ b/scripts/index.coffee @@ -1,18 +1,16 @@ -path = require 'path' +require 'coffeescript/register' -{loadConfigfile} = require path.join path.join __dirname, 'lib', 'common.coffee' -chatbot = require path.join __dirname, 'bot', 'index.coffee' +path = require 'path' +fs = require 'fs' -hubotPath = module.parent.filename -hubotPath = path.dirname hubotPath for [1..4] -corpus = (process.env.HUBOT_CORPUS || 'corpus.yml') -global.configPath = path.join hubotPath, 'training_data', corpus +{loadConfigfile, getConfigFilePath} = require './lib/common' +chatbot = require './bot/index' try - global.config = loadConfigfile global.configPath + global.config = loadConfigfile getConfigFilePath() catch err process.exit() -chatbot = chatbot.bind null, global.config, global.configPath +chatbot = chatbot.bind null, global.config module.exports = chatbot diff --git a/scripts/lib/common.coffee b/scripts/lib/common.coffee index 63c178f..3ea95ae 100644 --- a/scripts/lib/common.coffee +++ b/scripts/lib/common.coffee @@ -24,6 +24,9 @@ common.regexEscape = (string) -> #http://stackoverflow.com/a/6969486 string.replace /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&" +common.getConfigFilePath = () -> + return process.env.HUBOT_CORPUS || 'training_data/corpus.yml' + common.loadConfigfile = (filepath) -> try console.log("Loading corpus: " + filepath) -- GitLab