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