forked from wehkamp/bot-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (60 loc) · 1.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// local development file
const envFile = './.env'
// load config for .env file - they are optional
const config = getConfig(envFile)
// will exit with error message when invalid!
validateToken(config)
// convert config into Hubot start
const params = convertConfigIntoParameters(config)
params.push('hubot')
params.push('--adapter')
params.push('slack')
// feed it to cross env - this will start Hubot with Slack
require('cross-env')(params)
function getConfig (envFile) {
const fs = require('fs')
if (fs.existsSync(envFile)) {
return fs
.readFileSync('./.env', 'utf-8')
.split('\n') // thanks Windows!
.filter(l => l && l.indexOf('=') !== -1 && l.indexOf('#') !== 0)
}
return ['ENVIRONMENT=production']
}
function validateToken (config) {
const tokenStart = 'xoxb-'
// check for Hubot token in config
let token = config.find(c => c.indexOf('HUBOT_SLACK_TOKEN') === 0)
if (token && token.indexOf('=') !== -1) return token.split('=')[1]
else token = process.env.HUBOT_SLACK_TOKEN
if (!token || token.length === 0) {
console.log(
'\x1b[33mNo HUBOT_SLACK_TOKEN found.\x1b[0m Please add it to your environment variables (for production) or to your .env file (for local development).\n'
)
process.exit()
} else if (
token.indexOf(tokenStart) !== 0 ||
token.length <= tokenStart.length
) {
console.warn(
'\x1b[33mInvalid HUBOT_SLACK_TOKEN found.\x1b[0m Please check your environment variable (for production) or your .env file (for local development).\n'
)
process.exit()
}
}
function convertConfigIntoParameters (config) {
const params = []
config.forEach((c, index) => {
c = c.trim()
// should the parameter be quoted?
if (c.indexOf(' ') !== -1 && c.indexOf('="') === -1) {
c = c.replace('=', '="') + '"'
}
params.push(c)
// add cross-env
if (index !== config.length - 1) {
params.push('cross-env')
}
})
return params
}