From a6943178bf694e25fb0a6ca7d6c94da9a8483114 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Wed, 12 Feb 2020 22:40:45 +0800 Subject: [PATCH] code clean --- bin/io-client.ts | 10 ++---- src/config.spec.ts | 1 - src/config.ts | 78 +++++++++++---------------------------------- src/io.ts | 1 + src/user/message.ts | 4 +-- src/wechaty.ts | 15 +++++---- 6 files changed, 31 insertions(+), 78 deletions(-) diff --git a/bin/io-client.ts b/bin/io-client.ts index 14ac5097..ae4e53ae 100644 --- a/bin/io-client.ts +++ b/bin/io-client.ts @@ -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) } diff --git a/src/config.spec.ts b/src/config.spec.ts index 10a3710c..02652443 100755 --- a/src/config.spec.ts +++ b/src/config.spec.ts @@ -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') diff --git a/src/config.ts b/src/config.ts index 51a1ec7d..1b35805e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -19,11 +19,8 @@ // tslint:disable-next-line:no-reference /// -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 diff --git a/src/io.ts b/src/io.ts index 03d8f5b8..07f61196 100644 --- a/src/io.ts +++ b/src/io.ts @@ -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) diff --git a/src/user/message.ts b/src/user/message.ts index e26a1933..3bd3c4b8 100644 --- a/src/user/message.ts +++ b/src/user/message.ts @@ -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 [] diff --git a/src/wechaty.ts b/src/wechaty.ts index f9ddd759..bba1584f 100644 --- a/src/wechaty.ts +++ b/src/wechaty.ts @@ -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 } -- GitLab