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