config.ts 6.4 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
2
 *   Wechaty - https://github.com/chatie/wechaty
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 *   Copyright 2016-2017 Huan LI <zixia@zixia.net>
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
17
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
18
 */
19 20 21 22
import * as os from 'os'

// const isCi      = require('is-ci')
// const isDocker  = require('is-docker')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23

24 25
import { log }    from 'brolog'

26
import { Puppet } from './puppet'
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
const logLevel = process.env['WECHATY_LOG']
if (logLevel) {
  log.level(logLevel.toLowerCase())
  log.silly('Brolog', 'WECHATY_LOG set level to %s', logLevel)
}

/**
 * to handle unhandled exceptions
 */
if (/verbose|silly/i.test(logLevel)) {
  log.info('Config', 'registering process.on("unhandledRejection") for development/debug')
  process.on('unhandledRejection', (reason, promise) => {
    log.error('Config', '###########################')
    log.error('Config', 'unhandledRejection: %s %s', reason, promise)
    log.error('Config', '###########################')
    promise.catch(err => {
      log.error('Config', 'unhandledRejection::catch(%s)', err.message)
      console.error('Config', err) // I don't know if log.error has similar full trace print support like console.error
    })
  })
48
}
49

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
50
export type PuppetName = 'web' | 'android' | 'ios'
51
export type HeadName = 'chrome' | 'phantomjs' | 'firefox'
52

53
export interface ConfigSetting {
54

55
  DEFAULT_HEAD: HeadName
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
56
  DEFAULT_PUPPET: PuppetName
57 58 59
  DEFAULT_APIHOST: string
  DEFAULT_PROFILE: string
  DEFAULT_TOKEN:  string
60 61
  DEFAULT_PROTOCOL: string
  CMD_CHROMIUM: string
62 63 64 65 66 67 68
  DEFAULT_PORT: number

  port: number
  profile: string
  token: string
  debug: boolean

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69
  puppet: PuppetName
70
  head: HeadName
71 72

  apihost: string
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
73
  validApiHost: (host: string) => boolean
74 75 76

  httpPort: number

77 78 79
  _puppetInstance: Puppet | null
  puppetInstance(): Puppet
  puppetInstance(empty: null): void
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
80
  puppetInstance(instance: Puppet): void
81
  puppetInstance(instance?: Puppet | null): Puppet | void
82

83
  dockerMode: boolean
84 85

}
86 87
/* tslint:disable:variable-name */
/* tslint:disable:no-var-requires */
88
export const Config: ConfigSetting = require('../package.json').wechaty
89

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
90 91 92
/**
 * 1. ENVIRONMENT VARIABLES + PACKAGES.JSON (default)
 */
93
Object.assign(Config, {
94
  apihost:    process.env['WECHATY_APIHOST']   || Config.DEFAULT_APIHOST,
L
lijiarui 已提交
95
  head:       process.env['WECHATY_HEAD']      || Config.DEFAULT_HEAD,
96
  puppet:     process.env['WECHATY_PUPPET']    || Config.DEFAULT_PUPPET,
L
lijiarui 已提交
97
  validApiHost,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
98 99
})

100
function validApiHost(apihost: string): boolean {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
101 102 103 104 105 106 107
  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 (李卓桓) 已提交
108 109 110 111
/**
 * 2. ENVIRONMENT VARIABLES (only)
 */
Object.assign(Config, {
L
lijiarui 已提交
112 113 114 115
  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 (李卓桓) 已提交
116
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
117

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
118 119 120 121 122
/**
 * 3. Service Settings
 */
Object.assign(Config, {
  // get PORT form cloud service env, ie: heroku
L
lijiarui 已提交
123
  httpPort: process.env['PORT'] || process.env['WECHATY_PORT'] || Config.DEFAULT_PORT,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
124 125 126 127 128 129
})

/**
 * 4. Envioronment Identify
 */
Object.assign(Config, {
130
  dockerMode: !!process.env('WECHATY_DOCKER'),
131
  isGlobal:  isWechatyInstalledGlobal(),
132
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
133

134 135 136 137 138 139 140 141 142 143
function isWechatyInstalledGlobal() {
  /**
   * TODO:
   * 1. check /node_modules/wechaty
   * 2. return true if exists
   * 3. otherwise return false
   */
   return false
}

144 145 146 147
/**
 * @DEPRECATED on Jun 2017 by zixia
 */
// function dockerMode() {
148
  /**
149
   * false for Continuous Integration System
150
   */
151 152 153
  // if (isCi) {
  //   return false
  // }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
154

155
  /**
156
   * false Cloud9 IDE
157
   */
158 159 160 161 162 163
  // const c9 = Object.keys(process.env)
  //                 .filter(k => /^C9_/.test(k))
  //                 .length
  // if (c9 > 7 && process.env['C9_PORT']) {
  //   return false
  // }
Huan (李卓桓)'s avatar
lint  
Huan (李卓桓) 已提交
164

165 166 167
  /**
   * return indentify result by NPM module `is-docker`
   */
168 169
  // return isDocker()
// }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
170

171 172 173
/**
 * 5. live setting
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
174 175 176 177
function puppetInstance(): Puppet
function puppetInstance(empty: null): void
function puppetInstance(instance: Puppet): void

178
function puppetInstance(instance?: Puppet | null): Puppet | void {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
179 180

  if (instance === undefined) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
181
    if (!this._puppetInstance) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
182
      throw new Error('no puppet instance')
183
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
184
    return this._puppetInstance
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
185 186

  } else if (instance === null) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
187
    log.verbose('Config', 'puppetInstance(null)')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
188
    this._puppetInstance = null
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
189
    return
190
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
191

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
192
  log.verbose('Config', 'puppetInstance(%s)', instance.constructor.name)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
193
  this._puppetInstance = instance
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
194 195
  return

196 197
}

198
Object.assign(Config, {
L
lijiarui 已提交
199
  puppetInstance,
200 201
})

Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
202 203 204 205
export type WatchdogFoodName = 'HEARTBEAT'
                              | 'POISON'
                              | 'SCAN'

206
export interface WatchdogFood {
L
lijiarui 已提交
207 208 209
  data: any,
  timeout?: number,  // millisecond
  type?: WatchdogFoodName,
210 211
}

212
export interface ScanInfo {
L
lijiarui 已提交
213 214
  url: string,
  code: number,
215 216
}

217 218 219
/**
 * from Message
 */
220
export interface RecommendInfo {
L
lijiarui 已提交
221 222 223 224
  UserName:   string,
  NickName:   string,  // display_name
  Content:    string,  // request message
  HeadImgUrl: string,  // message.RecommendInfo.HeadImgUrl
225

L
lijiarui 已提交
226 227
  Ticket:     string,  // a pass token
  VerifyFlag: number,
228

229 230
}

231
export interface Sayable {
232
  say(content: string, replyTo?: any|any[]): Promise<boolean>
233
}
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
234

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
235 236 237
export interface Sleepable {
  sleep(millisecond: number): Promise<void>
}
238

239
import * as Raven from 'raven'
240 241
Raven.disableConsoleAlerts()

242
Raven
243 244 245 246 247 248 249 250 251 252 253
.config(
  process.env.NODE_ENV === 'production'
    && 'https://f6770399ee65459a82af82650231b22c:d8d11b283deb441e807079b8bb2c45cd@sentry.io/179672',
  {
    release: require('../package.json').version,
    tags: {
      git_commit: 'c0deb10c4',
      platform: os.platform(),
    },
  },
)
254 255 256 257 258 259 260 261 262 263 264 265 266 267
.install()

/*
try {
    doSomething(a[0])
} catch (e) {
    Raven.captureException(e)
}

Raven.context(function () {
  doSomething(a[0])
})
 */

268 269
export {
  log,
270
  Raven,
271 272
}

273
export default Config