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

wip...

上级 0ef71ddb
......@@ -1079,7 +1079,7 @@ Initialize the bot, return Promise.
**Kind**: instance method of [<code>Wechaty</code>](#Wechaty)
**Example**
```js
await bot.init()
await bot.start()
// do other stuff with bot here
```
<a name="Wechaty+start"></a>
......@@ -1175,7 +1175,7 @@ Quit the bot
**Kind**: instance method of [<code>Wechaty</code>](#Wechaty)
**Example**
```js
await bot.quit()
await bot.stop()
```
<a name="Wechaty+stop"></a>
......
......@@ -95,10 +95,10 @@ bot
// .catch(e => log.error('Bot', 'on message rejected: %s' , e))
})
bot.init()
bot.start()
.catch(e => {
log.error('Bot', 'init() fail:' + e)
bot.quit()
bot.stop()
process.exit(-1)
})
......
......@@ -77,10 +77,10 @@ bot
console.log(`${url}\n[${code}] Scan QR Code in above url to login: `)
})
bot.init()
bot.start()
.catch(e => {
log.error('Bot', 'init() fail: %s', e)
bot.quit()
bot.stop()
process.exit(-1)
})
......@@ -157,5 +157,5 @@ async function onLogin() {
// setTimeout(main, SLEEP * 1000)
await bot.logout()
await bot.quit()
await bot.stop()
}
......@@ -116,9 +116,9 @@ bot
})
bot.init()
bot.start()
.catch(e => {
log.error('Bot', 'init() fail: %s', e)
bot.quit()
bot.stop()
process.exit(-1)
})
......@@ -63,5 +63,5 @@ Wechaty.instance({ profile: config.default.DEFAULT_PROFILE })
.on('friend', onFriend)
.on('room-join', onRoomJoin)
.init()
.start()
.catch(e => console.error(e))
......@@ -117,4 +117,4 @@ Please see https://github.com/Chatie/wechaty/tree/master/examples/hot-reload-bot
`)
bot.init();
bot.start();
......@@ -68,8 +68,8 @@ bot
}
// }
})
.init()
.catch(e => console.error('bot.init() error: ' + e))
.start()
.catch(e => console.error('bot.start() error: ' + e))
async function saveMediaFile(message: MediaMessage) {
const filename = message.filename()
......
......@@ -44,5 +44,5 @@ bot
await m.say('roger') // 1. reply others' msg
console.log(`RECV: ${m}, REPLY: "roger"`) // 2. log message
})
.init()
.start()
.catch(e => console.error(e))
......@@ -244,7 +244,7 @@ bot
}
}
})
.init()
.start()
.catch(e => console.error(e))
async function manageDingRoom() {
......
......@@ -75,8 +75,8 @@ bot
}
})
.init()
.catch(e => console.error('bot.init() error: ' + e))
.start()
.catch(e => console.error('bot.start() error: ' + e))
async function speechToText(mp3Stream: Readable): Promise<string> {
const wavStream = mp3ToWav(mp3Stream)
......
......@@ -5,7 +5,7 @@ const version: string = require('../package.json').version
if (minor(version) % 2 === 0) { // production release
console.log(`${version} is production release`)
process.exit(1)
process.exit(1) // exit 1 for not development
}
// development release
......
......@@ -13,7 +13,13 @@ if [ $# -eq 0 ]; then
echo
echo " deprecate all the 0.15.* versions."
echo " \$ $0 0.15"
echo " \$ $0 0.15.*"
echo " \$ $0 ^0.15.1"
echo
else
npm deprecate "wechaty@$1" "WARNING: this version(odd number) is coming from a developing branch which means it is very possible UNSTABLE. Please use the latest version with even number if you are in production. You can know more about the Wechaty version numbering design at https://github.com/chatie/wechaty/issues/905"
message="${2:-WARNING: this version(odd number) is coming from a developing branch which means it is very possible UNSTABLE. Please use the latest version with even number if you are in production. You can know more about the Wechaty version numbering design at https://github.com/chatie/wechaty/issues/905}"
npm deprecate "wechaty@$1" "$message"
fi
......@@ -10,3 +10,5 @@ pkg.publishConfig.tag = 'next'
fs.writeFileSync(PACKAGE_JSON, JSON.stringify(pkg, null, 2))
// console.log(JSON.stringify(pkg, null, 2))
console.log('set package.json:publicConfig.tag to next.')
文件模式从 100644 更改为 100755
......@@ -70,7 +70,7 @@ test('puppetInstance()', async t => {
}, Error, 'should throw when not initialized')
config.puppetInstance(bak)
const EXPECTED = <Puppet>{userId: 'test'}
const EXPECTED: Puppet = {userId: 'test'} as any
const mockPuppet = EXPECTED
config.puppetInstance(mockPuppet)
......
......@@ -22,7 +22,7 @@ import * as path from 'path'
import * as readPkgUp from 'read-pkg-up'
import * as Raven from 'raven'
import Brolog from 'brolog'
import { log } from 'brolog'
import Puppet from './puppet'
......@@ -62,17 +62,16 @@ Raven.context(function () {
})
*/
export const log = new Brolog()
const logLevel = process.env['WECHATY_LOG'] || 'info'
const logLevel = process.env['WECHATY_LOG']
if (logLevel) {
log.level(logLevel.toLowerCase() as any)
log.silly('Brolog', 'WECHATY_LOG set level to %s', logLevel)
log.silly('Config', 'WECHATY_LOG set level to %s', logLevel)
}
/**
* to handle unhandled exceptions
*/
if (/verbose|silly/i.test(log.level())) {
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', '###########################')
......@@ -101,7 +100,7 @@ export interface DefaultSetting {
/* tslint:disable:variable-name */
/* tslint:disable:no-var-requires */
export const DEFAULT_SETTING = pkg.wechaty as DefaultSetting
const DEFAULT_SETTING = pkg.wechaty as DefaultSetting
export class Config {
public default = DEFAULT_SETTING
......@@ -196,11 +195,23 @@ export interface Sayable {
say(content: string, replyTo?: any|any[]): Promise<boolean>
}
// export interface Sleepable {
// sleep(millisecond: number): Promise<void>
// }
export type WechatEvent = 'friend'
| 'login'
| 'logout'
| 'message'
| 'room-join'
| 'room-leave'
| 'room-topic'
| 'scan'
export type WechatyEvent = WechatEvent
| 'error'
| 'heartbeat'
| 'start'
| 'stop'
export {
log,
Raven,
}
......
......@@ -227,7 +227,7 @@ export class IoClient {
try {
if (this.options.wechaty) {
await this.options.wechaty.quit()
await this.options.wechaty.stop()
// this.wechaty = null
} else { log.warn('IoClient', 'quit() no this.wechaty') }
......
......@@ -53,7 +53,7 @@ interface IoEvent {
}
export class Io {
public uuid: string
public cuid: string
private protocol : string
private eventBuffer : IoEvent[] = []
......@@ -72,14 +72,14 @@ export class Io {
options.apihost = options.apihost || config.apihost
options.protocol = options.protocol || config.default.DEFAULT_PROTOCOL
this.uuid = options.wechaty.uuid
this.cuid = options.wechaty.cuid
this.protocol = options.protocol + '|' + options.wechaty.uuid
log.verbose('Io', 'instantiated with apihost[%s], token[%s], protocol[%s], uuid[%s]',
this.protocol = options.protocol + '|' + options.wechaty.cuid
log.verbose('Io', 'instantiated with apihost[%s], token[%s], protocol[%s], cuid[%s]',
options.apihost,
options.token,
options.protocol,
this.uuid,
this.cuid,
)
}
......@@ -115,7 +115,7 @@ export class Io {
const wechaty = this.options.wechaty
wechaty.on('error' , error => this.send({ name: 'error', payload: error }))
wechaty.on('heartbeat', data => this.send({ name: 'heartbeat', payload: { uuid: this.uuid, data } }))
wechaty.on('heartbeat', data => this.send({ name: 'heartbeat', payload: { cuid: this.cuid, data } }))
wechaty.on('login', user => this.send({ name: 'login', payload: user }))
wechaty.on('logout' , user => this.send({ name: 'logout', payload: user }))
wechaty.on('message', message => this.ioMessage(message))
......@@ -150,7 +150,7 @@ export class Io {
// case 'heartbeat':
// ioEvent.payload = {
// uuid: this.uuid
// cuid: this.cuid
// , data: data
// }
// break
......@@ -218,7 +218,7 @@ export class Io {
this.reconnectTimeout = null
const name = 'sys'
const payload = 'Wechaty version ' + this.options.wechaty.version() + ` with UUID: ${this.uuid}`
const payload = 'Wechaty version ' + this.options.wechaty.version() + ` with CUID: ${this.cuid}`
const initEvent: IoEvent = {
name,
......
......@@ -710,7 +710,7 @@ export class MediaMessage extends Message {
try {
await super.ready()
let url: string|null = null
let url: string | undefined
switch (this.type()) {
case MsgType.EMOTICON:
url = await this.bridge.getMsgEmoticon(this.id)
......
......@@ -184,15 +184,6 @@ export class Misc {
})
}
// credit - http://stackoverflow.com/a/2117523/1123955
public static guid(): string {
/* tslint:disable:no-bitwise */
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
/**
*
* @param port is just a suggestion.
......
......@@ -29,7 +29,7 @@ import {
MediaMessage,
} from '../message'
import {
ScanInfo,
ScanData,
} from '../puppet'
import Firer from './firer'
......@@ -58,7 +58,7 @@ function onDing(this: PuppetWeb, data): void {
this.emit('watchdog', { data })
}
async function onScan(this: PuppetWeb, data: ScanInfo): Promise<void> {
async function onScan(this: PuppetWeb, data: ScanData): Promise<void> {
log.verbose('PuppetWebEvent', 'onScan({code: %d, url: %s})', data.code, data.url)
if (this.state.off()) {
......@@ -78,7 +78,7 @@ async function onScan(this: PuppetWeb, data: ScanInfo): Promise<void> {
log.verbose('PuppetWebEvent', 'onScan() there has user when got a scan event. emit logout and set it to null')
const bak = this.user || this.userId || ''
this.user = this.userId = null
this.user = this.userId = undefined
this.emit('logout', bak)
}
......@@ -171,7 +171,7 @@ function onLogout(this: PuppetWeb, data) {
}
const bak = this.user || this.userId || ''
this.userId = this.user = null
this.userId = this.user = undefined
this.emit('logout', bak)
}
......
......@@ -30,7 +30,7 @@ import PuppetWebFriendRequest from './friend-request'
config.puppetInstance({
userId: 'xxx',
} as Puppet)
} as any as Puppet)
test('PuppetWebFriendRequest.receive smoke testing', async t => {
/* tslint:disable:max-line-length */
......
......@@ -38,7 +38,7 @@ import Profile from '../profile'
import {
Puppet,
PuppetOptions,
ScanInfo,
ScanData,
} from '../puppet'
import Room from '../room'
import Misc from '../misc'
......@@ -63,7 +63,7 @@ export type ScanFoodType = 'scan' | 'login' | 'logout'
export class PuppetWeb extends Puppet {
public bridge : Bridge
public scanInfo : ScanInfo | null
public scanInfo : ScanData | null
public puppetWatchdog : Watchdog<PuppetFoodType>
public scanWatchdog : Watchdog<ScanFoodType>
......
......@@ -25,6 +25,7 @@ import {
import {
Sayable,
WechatyEvent,
log,
} from './config'
import Contact from './contact'
......@@ -35,11 +36,8 @@ import {
} from './message'
import Profile from './profile'
import Room from './room'
import {
WechatyEvent,
} from './wechaty'
export interface ScanInfo {
export interface ScanData {
avatar: string, // Image Data URL
url: string, // QR Code URL
code: number, // Code
......@@ -55,10 +53,10 @@ export interface PuppetOptions {
* Abstract Puppet Class
*/
export abstract class Puppet extends EventEmitter implements Sayable {
protected userId?: string
protected user?: Contact
public userId?: string
public user?: Contact
protected state: StateSwitch
public state: StateSwitch
constructor(public options: PuppetOptions) {
super()
......@@ -94,7 +92,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public on(event: 'room-join', listener: (room: Room, inviteeList: Contact[], inviter: Contact) => void) : this
public on(event: 'room-leave', listener: (room: Room, leaverList: Contact[]) => void) : this
public on(event: 'room-topic', listener: (room: Room, topic: string, oldTopic: string, changer: Contact) => void) : this
public on(event: 'scan', listener: (info: ScanInfo) => void) : this
public on(event: 'scan', listener: (info: ScanData) => void) : this
public on(event: 'watchdog', listener: (data: WatchdogFood) => void) : this
public on(event: never, listener: never) : never
......@@ -111,7 +109,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public abstract self() : Contact
protected abstract getContact(id: string): Promise<any>
public abstract getContact(id: string): Promise<any>
/**
* Message
......
......@@ -17,6 +17,7 @@
*
* @ignore
*/
import * as cuid from 'cuid'
import { EventEmitter } from 'events'
import * as os from 'os'
......@@ -28,11 +29,12 @@ import {
import {
config,
log,
PuppetName,
Raven,
Sayable,
log,
VERSION,
WechatyEvent,
} from './config'
import Contact from './contact'
......@@ -45,28 +47,13 @@ import Profile from './profile'
import Puppet from './puppet'
import PuppetWeb from './puppet-web/'
import Room from './room'
import Misc from './misc'
// import Misc from './misc'
export interface WechatyOptions {
puppet?: PuppetName,
profile?: string,
}
export type WechatEvent = 'friend'
| 'login'
| 'logout'
| 'message'
| 'room-join'
| 'room-leave'
| 'room-topic'
| 'scan'
export type WechatyEvent = WechatEvent
| 'error'
| 'heartbeat'
| 'start'
| 'stop'
/**
* Main bot class.
*
......@@ -99,10 +86,10 @@ export class Wechaty extends EventEmitter implements Sayable {
private state = new StateSwitch('Wechaty', log)
/**
* the uuid
* the cuid
* @private
*/
public uuid: string
public cuid: string
/**
* get the singleton instance of Wechaty
......@@ -141,7 +128,7 @@ export class Wechaty extends EventEmitter implements Sayable {
this.profile = new Profile(options.profile)
this.uuid = Misc.guid()
this.cuid = cuid()
}
/**
......@@ -176,20 +163,6 @@ export class Wechaty extends EventEmitter implements Sayable {
return Wechaty.version(forceNpm)
}
/**
* Initialize the bot, return Promise.
*
* @deprecated
* @returns {Promise<void>}
* @example
* await bot.init()
* // do other stuff with bot here
*/
public async init(): Promise<void> {
log.warn('Wechaty', 'init() DEPRECATED and will be removed after Jun 2018. Use start() instead.')
await this.start()
}
/**
* Start the bot, return Promise.
*
......@@ -200,15 +173,14 @@ export class Wechaty extends EventEmitter implements Sayable {
*/
public async start(): Promise<void> {
log.info('Wechaty', 'v%s starting...' , this.version())
log.verbose('Wechaty', 'puppet: %s' , this.options.puppet)
log.verbose('Wechaty', 'profile: %s' , this.options.profile)
log.verbose('Wechaty', 'uuid: %s' , this.uuid)
if (this.state.on() === true) {
log.error('Wechaty', 'start() already started. return and do nothing.')
return
} else if (this.state.on() === 'pending') {
log.error('Wechaty', 'start() another task is starting. return and do nothing.')
log.verbose('Wechaty', 'puppet: %s' , this.options.puppet)
log.verbose('Wechaty', 'profile: %s' , this.options.profile)
log.verbose('Wechaty', 'cuid: %s' , this.cuid)
if (this.state.on()) {
log.silly('Wechaty', 'start() on a starting/started instance')
await this.state.ready()
log.silly('Wechaty', 'start() state.ready() resolved')
return
}
......@@ -442,19 +414,6 @@ export class Wechaty extends EventEmitter implements Sayable {
return puppet
}
/**
* Quit the bot
*
* @deprecated use stop() instead
* @returns {Promise<void>}
* @example
* await bot.quit()
*/
public async quit(): Promise<void> {
log.warn('Wechaty', 'quit() DEPRECATED and will be removed after Jun 2018. Use stop() instead.')
await this.stop()
}
/**
* Stop the bot
*
......@@ -466,15 +425,12 @@ export class Wechaty extends EventEmitter implements Sayable {
log.verbose('Wechaty', 'stop()')
if (this.state.off()) {
if (this.state.off() === 'pending') { // current() !== 'on' || !this.state.stable()) {
const err = new Error(`stop() on a pending stop instance.`)
log.error('Wechaty', err.message)
this.emit('error', err)
} else {
log.warn('Wechaty', 'stop() on an already stopped instance.')
}
log.silly('Wechaty', 'stop() on an stopping/stopped instance')
await this.state.ready('off')
log.silly('Wechaty', 'stop() state.ready(off) resolved')
return
}
this.state.off('pending')
if (!this.puppet) {
......@@ -641,7 +597,8 @@ export class Wechaty extends EventEmitter implements Sayable {
if (!this.puppet) {
throw new Error('no puppet')
}
await this.puppet.reset(reason)
await this.puppet.stop()
await this.puppet.start()
return
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册