提交 a6943178 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

code clean

上级 83c94888
......@@ -46,10 +46,7 @@ __________________________________________________
let token = config.token
if (!token) {
log.error('Client', 'token not found: please set WECHATY_TOKEN in environment before run io-client')
// process.exit(-1)
token = config.default.DEFAULT_TOKEN
log.warn('Client', `set token to "${token}" for demo purpose`)
throw new Error('token not found: please set WECHATY_TOKEN in environment before run io-client')
}
console.info(welcome)
......@@ -63,14 +60,11 @@ const client = new IoClient({
client.start()
.catch(onError.bind(client))
// client.initWeb()
// .catch(onError.bind(client))
async function onError (
this : IoClient,
e : Error,
) {
log.error('Client', 'initWeb() fail: %s', e)
log.error('Client', 'start() fail: %s', e)
await this.quit()
process.exit(-1)
}
......@@ -26,7 +26,6 @@ import { config } from './config'
test('important variables', async t => {
// t.true('puppet' in config, 'should exist `puppet` in Config')
t.true('apihost' in config, 'should exist `apihost` in Config')
t.true('profile' in config, 'should exist `profile` in Config')
t.true('token' in config, 'should exist `token` in Config')
// t.ok(config.default.DEFAULT_PUPPET , 'should export DEFAULT_PUPPET')
......
......@@ -19,11 +19,8 @@
// tslint:disable-next-line:no-reference
/// <reference path="./typings.d.ts" />
import fs from 'fs'
import os from 'os'
import path from 'path'
import qrImage from 'qr-image'
import Raven from 'raven'
import readPkgUp from 'read-pkg-up'
......@@ -34,7 +31,10 @@ import {
PuppetModuleName,
PUPPET_NAME_DEFAULT,
} from './puppet-config'
import { VERSION } from './version'
import {
VERSION,
GIT_COMMIT_HASH,
} from './version'
const pkg = readPkgUp.sync({ cwd: __dirname })!.packageJson
......@@ -50,7 +50,7 @@ Raven
{
release: VERSION,
tags: {
git_commit: '',
git_commit: GIT_COMMIT_HASH,
platform: process.env.WECHATY_DOCKER
? 'docker'
: os.platform(),
......@@ -82,6 +82,7 @@ if (logLevel) {
*/
if (log.level() === 'verbose' || log.level() === 'silly') {
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)
......@@ -91,15 +92,19 @@ if (log.level() === 'verbose' || log.level() === 'silly') {
console.error('Config', err) // I don't know if log.error has similar full trace print support like console.error
})
})
process.on('uncaughtException', function (error) {
const origin = arguments[1] // to compatible with node 12 or below version typings
log.error('Config', '###########################')
log.error('Config', 'uncaughtException: %s %s', error, origin)
log.error('Config', '###########################')
})
}
export interface DefaultSetting {
DEFAULT_HEAD : number,
DEFAULT_PORT : number,
// DEFAULT_PUPPET : PuppetName,
DEFAULT_APIHOST : string,
// DEFAULT_PROFILE : string,
DEFAULT_TOKEN : string,
DEFAULT_PROTOCOL : string,
}
......@@ -109,8 +114,7 @@ export class Config {
public default = DEFAULT_SETTING
public apihost = process.env.WECHATY_APIHOST || DEFAULT_SETTING.DEFAULT_APIHOST
public head = ('WECHATY_HEAD' in process.env) ? (!!process.env.WECHATY_HEAD) : (!!(DEFAULT_SETTING.DEFAULT_HEAD))
public apihost = process.env.WECHATY_APIHOST || DEFAULT_SETTING.DEFAULT_APIHOST
public systemPuppetName (): PuppetModuleName {
return (
......@@ -118,59 +122,19 @@ export class Config {
).toLowerCase() as PuppetModuleName
}
// DEPRECATED: Use WECHATY_NAME instead
public profile = process.env.WECHATY_PROFILE
public name = process.env.WECHATY_NAME
public name = process.env.WECHATY_NAME || process.env.WECHATY_PROFILE // replace WECHATY_PROFILE
// DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
public token = process.env.WECHATY_TOKEN
public token = process.env.WECHATY_TOKEN // DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
public debug = !!(process.env.WECHATY_DEBUG)
public httpPort = process.env.PORT || process.env.WECHATY_PORT || DEFAULT_SETTING.DEFAULT_PORT
public docker = !!(process.env.WECHATY_DOCKER)
// private _puppetInstance: Puppet | null = null
constructor () {
log.verbose('Config', 'constructor()')
this.validApiHost(this.apihost)
if (this.profile) {
log.warn('Config', 'constructor() WECHATY_PROFILE is DEPRECATED, use WECHATY_NAME instead.')
}
}
public gitRevision (): string | null {
const dotGitPath = path.join(__dirname, '..', '.git') // only for ts-node, not for dist
// const gitLogArgs = ['log', '--oneline', '-1']
// TODO: use git rev-parse HEAD ?
const gitArgs = ['rev-parse', 'HEAD']
try {
// Make sure this is a Wechaty repository
fs.statSync(dotGitPath).isDirectory()
const ss = require('child_process')
.spawnSync('git', gitArgs, { cwd: __dirname })
if (ss.status !== 0) {
throw new Error(ss.error)
}
const revision = ss.stdout
.toString()
.trim()
.slice(0, 7)
return revision
} catch (e) { /* fall safe */
/**
* 1. .git not exist
* 2. git log fail
*/
log.silly('Wechaty', 'version() form development environment is not availble: %s', e.message)
return null
}
}
public validApiHost (apihost: string): boolean {
......@@ -186,11 +150,7 @@ export const CHATIE_OFFICIAL_ACCOUNT_ID = 'gh_051c89260e5d'
export function qrCodeForChatie (): FileBox {
const CHATIE_OFFICIAL_ACCOUNT_QRCODE = 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
const name = 'qrcode-for-chatie.png'
const type = 'png'
const qrStream = qrImage.image(CHATIE_OFFICIAL_ACCOUNT_QRCODE, { type })
return FileBox.fromStream(qrStream, name)
return FileBox.fromQRCode(CHATIE_OFFICIAL_ACCOUNT_QRCODE)
}
// http://jkorpela.fi/chars/spaces.html
......
......@@ -150,6 +150,7 @@ export class Io {
this.state.on(true)
return
} catch (e) {
log.warn('Io', 'start() exception: %s', e.message)
this.state.off(true)
......
......@@ -692,9 +692,7 @@ export class Message extends Accessory implements Sayable {
* define magic code `8197` to identify @xxx
* const AT_SEPRATOR = String.fromCharCode(8197)
*/
const AT_SEPRATOR = AT_SEPRATOR_REGEX
const atList = this.text().split(AT_SEPRATOR)
const atList = this.text().split(AT_SEPRATOR_REGEX)
// console.log('atList: ', atList)
if (atList.length === 0) return []
......
......@@ -57,9 +57,13 @@ import {
isProduction,
log,
Raven,
VERSION,
} from './config'
import {
VERSION,
GIT_COMMIT_HASH,
} from './version'
import {
AnyFunction,
Sayable,
......@@ -1067,12 +1071,9 @@ export class Wechaty extends Accessory implements Sayable {
/**
* @ignore
*/
public static version (forceNpm = false): string {
if (!forceNpm) {
const revision = config.gitRevision()
if (revision) {
return `#git[${revision}]`
}
public static version (gitHash = false): string {
if (gitHash && GIT_COMMIT_HASH) {
return `#git[${GIT_COMMIT_HASH}]`
}
return VERSION
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册