1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | import os from 'os'
|
24 |
|
25 | import Raven from 'raven'
|
26 | import readPkgUp from 'read-pkg-up'
|
27 |
|
28 | import {
|
29 | FileBox,
|
30 | MemoryCard,
|
31 | log,
|
32 | } from 'wechaty-puppet'
|
33 |
|
34 | import {
|
35 | PuppetModuleName,
|
36 | PUPPET_NAME_DEFAULT,
|
37 | } from './puppet-config'
|
38 | import {
|
39 | VERSION,
|
40 | GIT_COMMIT_HASH,
|
41 | } from './version'
|
42 |
|
43 | const pkg = readPkgUp.sync({ cwd: __dirname })!.packageJson
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | Raven.disableConsoleAlerts()
|
49 |
|
50 | Raven
|
51 | .config(
|
52 | isProduction()
|
53 | && 'https://f6770399ee65459a82af82650231b22c:d8d11b283deb441e807079b8bb2c45cd@sentry.io/179672',
|
54 | {
|
55 | release: VERSION,
|
56 | tags: {
|
57 | git_commit: GIT_COMMIT_HASH,
|
58 | platform: process.env.WECHATY_DOCKER
|
59 | ? 'docker'
|
60 | : os.platform(),
|
61 | },
|
62 | },
|
63 | )
|
64 | .install()
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | if (log.level() === 'verbose' || log.level() === 'silly') {
|
82 | log.info('Config', 'registering process.on("unhandledRejection") for development/debug')
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 | process.on('unhandledRejection', (reason: Error | any, promise) => {
|
89 | log.error('Config', '###########################')
|
90 | log.error('Config', 'unhandledRejection: %s %s', reason.stack || reason, promise)
|
91 | log.error('Config', '###########################')
|
92 | promise.catch(err => {
|
93 | log.error('Config', 'process.on(unhandledRejection) promise.catch(%s)', err.message)
|
94 | console.error('Config', err)
|
95 | })
|
96 | })
|
97 |
|
98 | process.on('uncaughtException', function (error) {
|
99 | const origin = arguments[1]
|
100 |
|
101 | log.error('Config', '###########################')
|
102 | log.error('Config', 'uncaughtException: %s %s', error.stack, origin)
|
103 | log.error('Config', '###########################')
|
104 | })
|
105 | }
|
106 |
|
107 | export interface DefaultSetting {
|
108 | DEFAULT_PORT : number,
|
109 | DEFAULT_APIHOST : string,
|
110 | DEFAULT_PROTOCOL : string,
|
111 | }
|
112 |
|
113 | const DEFAULT_SETTING = pkg.wechaty as DefaultSetting
|
114 |
|
115 | export class Config {
|
116 |
|
117 | public default = DEFAULT_SETTING
|
118 |
|
119 | public apihost = process.env.WECHATY_APIHOST || DEFAULT_SETTING.DEFAULT_APIHOST
|
120 |
|
121 | public systemPuppetName (): PuppetModuleName {
|
122 | return (
|
123 | process.env.WECHATY_PUPPET || PUPPET_NAME_DEFAULT
|
124 | ).toLowerCase() as PuppetModuleName
|
125 | }
|
126 |
|
127 | public name = process.env.WECHATY_NAME
|
128 |
|
129 |
|
130 | public token = process.env.WECHATY_TOKEN
|
131 |
|
132 | public debug = !!(process.env.WECHATY_DEBUG)
|
133 |
|
134 | public httpPort = process.env.PORT || process.env.WECHATY_PORT || DEFAULT_SETTING.DEFAULT_PORT
|
135 | public docker = !!(process.env.WECHATY_DOCKER)
|
136 |
|
137 | constructor () {
|
138 | log.verbose('Config', 'constructor()')
|
139 | this.validApiHost(this.apihost)
|
140 | }
|
141 |
|
142 | public validApiHost (apihost: string): boolean {
|
143 | if (/^[a-zA-Z0-9.\-_]+:?[0-9]*$/.test(apihost)) {
|
144 | return true
|
145 | }
|
146 | throw new Error('validApiHost() fail for ' + apihost)
|
147 | }
|
148 |
|
149 | }
|
150 |
|
151 | export const CHATIE_OFFICIAL_ACCOUNT_ID = 'gh_051c89260e5d'
|
152 |
|
153 | export function qrCodeForChatie (): FileBox {
|
154 | const CHATIE_OFFICIAL_ACCOUNT_QRCODE = 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
|
155 | return FileBox.fromQRCode(CHATIE_OFFICIAL_ACCOUNT_QRCODE)
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 | export const FOUR_PER_EM_SPACE = String.fromCharCode(0x2005)
|
161 |
|
162 | export const AT_SEPARATOR_REGEX = /[\u2005\u0020]/
|
163 |
|
164 | export function qrcodeValueToImageUrl (qrcodeValue: string): string {
|
165 | return [
|
166 | 'https://wechaty.github.io/qrcode/',
|
167 | encodeURIComponent(qrcodeValue),
|
168 | ].join('')
|
169 | }
|
170 |
|
171 | export function isProduction (): boolean {
|
172 | return process.env.NODE_ENV === 'production'
|
173 | || process.env.NODE_ENV === 'prod'
|
174 | }
|
175 |
|
176 | export {
|
177 | log,
|
178 | FileBox,
|
179 | MemoryCard,
|
180 | Raven,
|
181 |
|
182 | VERSION,
|
183 | }
|
184 |
|
185 | export const config = new Config()
|