config.ts 2.3 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 38
  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
  , debug:    process.env['WECHATY_DEBUG']    || false
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
39
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
40

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

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

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

62 63
  const cgroup = '/proc/1/cgroup'
  try { accessSync(cgroup, F_OK) }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
64 65
  catch (e) { return false }

66
  const line = execSync(`sort -n ${cgroup} | head -1`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
67 68 69 70 71 72 73 74 75 76
                .toString()
                .replace(/\n$/, '')

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

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

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