config.ts 2.2 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2
/**
 * Wechaty - Wechaty for Bot, Connect ChatBots, Chat as a Service
3
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
4 5
 * https://github.com/wechaty/wechaty/
 */
6 7
import { execSync } from 'child_process'
import { accessSync, F_OK } from 'fs'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
8

9 10
/* tslint:disable:variable-name */
/* tslint:disable:no-var-requires */
11
const Config = require('../package.json').wechaty
12

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
13 14 15
/**
 * 1. ENVIRONMENT VARIABLES + PACKAGES.JSON (default)
 */
16
Object.assign(Config, {
17 18 19
  head:       process.env['WECHATY_HEAD']      || Config.DEFAULT_HEAD
  , puppet:   process.env['WECHATY_PUPPET']    || Config.DEFAULT_PUPPET
  , apihost:  process.env['WECHATY_APIHOST']   || Config.DEFAULT_APIHOST
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
20
  , validApiHost
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
21 22
})

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23 24 25 26 27 28 29 30
function validApiHost(apihost) {
  if (/^[a-zA-Z0-9\.\-\_]+:?[0-9]*$/.test(apihost)) {
    return true
  }
  throw new Error('validApiHost() fail for ' + apihost)
}
validApiHost(Config.apihost)

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
31 32 33 34
/**
 * 2. ENVIRONMENT VARIABLES (only)
 */
Object.assign(Config, {
35 36 37
  port:       process.env['WECHATY_PORT']      || null // 0 for disable port
  , profile:  process.env['WECHATY_PROFILE']   || null // DO NOT set DEFAULT_PROFILE, because sometimes user do not want to save session
  , token:    process.env['WECHATY_TOKEN']     || null // DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
38
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
39

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
40 41 42 43 44
/**
 * 3. Service Settings
 */
Object.assign(Config, {
  // get PORT form cloud service env, ie: heroku
45
  httpPort: process.env['PORT'] || process.env['WECHATY_PORT'] || Config.DEFAULT_PORT
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
46 47 48 49 50 51
})

/**
 * 4. Envioronment Identify
 */
Object.assign(Config, {
52
  isDocker:   isWechatyDocker()
53
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
54

55 56 57 58 59
function isWechatyDocker() {
  const isCi = require('is-ci')
  if (isCi) {
    return false
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
60

61 62
  const cgroup = '/proc/1/cgroup'
  try { accessSync(cgroup, F_OK) }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
  catch (e) { return false }

  const line = execSync(`head -1 ${cgroup}`)
                .toString()
                .replace(/\n$/, '')

  if (/\/$/.test(line)) {
    return false
  }
  // instead of '/', docker will end with container id
  return true
}

76 77 78 79 80 81 82 83 84 85
/**
 * 5. live setting
 */
Config.puppetInstance = function(instance) {
  if (typeof instance !== 'undefined') {  // null is valid here
    Config._puppetInstance = instance
  }
  return Config._puppetInstance
}

86 87
// module.exports = Config.default = Config.Config = Config
export default Config