Skip to content

Commit

Permalink
fix(core): botpress shouldn't change cwd (resolves #52)
Browse files Browse the repository at this point in the history
  • Loading branch information
epaminond committed May 29, 2018
1 parent 8d05b69 commit 14ed105
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/channels/botpress-channel-slack/src/index.js
Expand Up @@ -39,7 +39,7 @@ module.exports = {
},

init(bp) {
checkVersion(bp, __dirname)
checkVersion(bp, bp.botpressPath)

bp.middlewares.register({
name: 'slack.sendMessages',
Expand Down
7 changes: 3 additions & 4 deletions packages/core/botpress/src/botpress.js
Expand Up @@ -138,9 +138,7 @@ class botpress {
}, 30 * 1000)
}

// change the current working directory to botpress's installation path
// the bot's location is kept in this.projectLocation
process.chdir(path.join(__dirname, '../'))
this.botpressPath = path.join(__dirname, '../')

const { projectLocation, botfile } = this

Expand All @@ -159,7 +157,8 @@ class botpress {
const db = createDatabase({
sqlite: { location: dbLocation },
postgres: botfile.postgres,
logger
logger,
botpressPath: this.botpressPath
})

const kvs = db._kvs
Expand Down
14 changes: 8 additions & 6 deletions packages/core/botpress/src/database/index.js
Expand Up @@ -13,16 +13,18 @@ import pick from 'lodash/pick'

import tables from './tables'
import kvs from './kvs'
import path from 'path'

const initializeCoreDatabase = knex => {
const initializeCoreDatabase = (knex, botpressPath) => {
if (!knex) {
throw new Error('You must initialize the database before')
}

return Promise.mapSeries(tables, fn => fn(knex)).then(() => knex.migrate.latest())
const directory = path.join(botpressPath, './migrations/')
return Promise.mapSeries(tables, fn => fn(knex)).then(() => knex.migrate.latest({ directory }))
}

const createKnex = async ({ sqlite, postgres }) => {
const createKnex = async ({ sqlite, postgres, botpressPath }) => {
const commonConfig = {
useNullAsDefault: true
}
Expand All @@ -43,16 +45,16 @@ const createKnex = async ({ sqlite, postgres }) => {

const _knex = knex(Object.assign(commonConfig, dbConfig))

await initializeCoreDatabase(_knex)
await initializeCoreDatabase(_knex, botpressPath)
return _knex
}

module.exports = ({ sqlite, postgres, logger }) => {
module.exports = ({ sqlite, postgres, logger, botpressPath }) => {
let knex = null

const getDb = async () => {
if (!knex) {
knex = await createKnex({ sqlite, postgres })
knex = await createKnex({ sqlite, postgres, botpressPath })
}

return knex
Expand Down
2 changes: 1 addition & 1 deletion packages/core/botpress/src/licensing.js
Expand Up @@ -8,7 +8,7 @@ import listeners from './listeners'
import { resolveProjectFile } from './util'

module.exports = ({ logger, version, projectLocation, db, botfile, bp }) => {
const licensesPath = path.join(__dirname, '../licenses')
const licensesPath = path.join(bp.botpressPath, './licenses')

const getLicenses = () => {
const packageJsonPath = resolveProjectFile('package.json', projectLocation, true)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/botpress/src/server/static.js
Expand Up @@ -130,16 +130,16 @@ module.exports = bp => {

app.use(staticMiddleware(path.join(bp.projectLocation, 'static')))

app.use(staticMiddleware(path.join(__dirname, '../lib/web')))
app.use(staticMiddleware(path.join(bp.botpressPath, './lib/web')))

app.get('*', (req, res, next) => {
// If browser requests HTML and request isn't an API request
if (/html/i.test(req.headers.accept) && !/^\/api\//i.test(req.url)) {
if (req.url && /^\/lite\//i.test(req.url)) {
return res.sendFile(path.join(__dirname, '../lib/web/lite.html'))
return res.sendFile(path.join(bp.botpressPath, './lib/web/lite.html'))
}

return res.sendFile(path.join(__dirname, '../lib/web/index.html'))
return res.sendFile(path.join(bp.botpressPath, './lib/web/index.html'))
}
next()
})
Expand Down
2 changes: 1 addition & 1 deletion packages/functionals/botpress-analytics/src/index.js
Expand Up @@ -48,7 +48,7 @@ const outgoingMiddleware = (event, next) => {

module.exports = {
init: function(bp) {
checkVersion(bp, __dirname)
checkVersion(bp, bp.botpressPath)

bp.middlewares.register({
name: 'analytics.incoming',
Expand Down
4 changes: 1 addition & 3 deletions packages/functionals/botpress-audience/src/index.js
@@ -1,10 +1,8 @@
/* global __dirname */

import checkVersion from 'botpress-version-manager'

module.exports = {
init: function(bp) {
checkVersion(bp, __dirname)
checkVersion(bp, bp.botpressPath)
},

ready: function(bp) {
Expand Down
2 changes: 1 addition & 1 deletion packages/functionals/botpress-broadcast/src/index.js
Expand Up @@ -10,7 +10,7 @@ let knex = null

module.exports = {
init: function(bp) {
checkVersion(bp, __dirname)
checkVersion(bp, bp.botpressPath)
deamon(bp)
bp.db.get().then(_knex => {
knex = _knex
Expand Down
2 changes: 1 addition & 1 deletion packages/functionals/botpress-hitl/src/index.js
Expand Up @@ -69,7 +69,7 @@ module.exports = {
},

init: async (bp, configurator) => {
checkVersion(bp, __dirname)
checkVersion(bp, bp.botpressPath)

bp.middlewares.register({
name: 'hitl.captureInMessages',
Expand Down
2 changes: 1 addition & 1 deletion packages/functionals/botpress-scheduler/src/index.js
Expand Up @@ -11,7 +11,7 @@ module.exports = {
config: {},

init: async function(bp) {
checkVersion(bp, __dirname)
checkVersion(bp, bp.botpressPath)

await db(bp).bootstrap()
const d = deamon(bp)
Expand Down

0 comments on commit 14ed105

Please sign in to comment.