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

#40 Typescript transition step 23: put all together, everything broken...

上级 4cf54f74
......@@ -52,5 +52,3 @@ t/
t.*
.DS_Store
/dev.js
/typings
......@@ -2,4 +2,5 @@
{
// 一个制表符等于的空格数。
"editor.tabSize": 2
}
\ No newline at end of file
, "typescript.tsdk": "node_modules/typescript/lib"
}
......@@ -18,7 +18,7 @@
"node": "ts-node",
"lint": "npm run eslint && npm run tslint",
"eslint": "eslint \"{src,test,bin}/**/*.js\"",
"tslint": "tslint \"{src,test,bin}/**/*.ts\"",
"tslint": "tslint \"{src,test,bin}/**/*.ts\" && tsc --noEmit \"src/**/*.ts\"",
"sloc": "sloc . --details --format cli-table --keys total,source,comment --exclude \"node_modules|doc\" && sloc . --exclude \"node_modules|doc\"",
"pretest": "npm run lint",
"test": "npm run test:phantomjs && npm run test:chrome",
......@@ -94,6 +94,12 @@
"ws": "^1.1.1"
},
"devDependencies": {
"@types/body-parser": "0.0.33",
"@types/express": "^4.0.33",
"@types/node": "^6.0.45",
"@types/selenium-webdriver": "^2.53.33",
"@types/socket.io": "^1.4.27",
"@types/ws": "0.0.34",
"apiai": "^2.0.7",
"ava": "^0.16.0",
"babel-eslint": "^6.1.2",
......
......@@ -7,15 +7,15 @@
// const log = require('npmlog')
import log from 'brolog'
const level = process.env.WECHATY_LOG
const level = process.env['WECHATY_LOG']
const levelRegexStr = 'silly|verbose|info|warn|error|silent'
const levelRegex = new RegExp(levelRegexStr, 'i')
if (levelRegex.test(level)) {
//log.level = level.toLowerCase()
// log.level = level.toLowerCase()
log.level(level)
log.verbose('Brolog', 'WECHATY_LOG set level to %s', level)
}
else if (level){
else if (level) {
log.warn('Brolog', 'env WECHATY_LOG(%s) must be one of silly|verbose|info|warn|error|silent', level)
}
......
......@@ -32,9 +32,10 @@ validApiHost(Config.apihost)
* 2. ENVIRONMENT VARIABLES (only)
*/
Object.assign(Config, {
port: process.env['WECHATY_PORT'] || null // 0 for disable port
, profile: process.env['WECHATY_PROFILE'] || null // DO NOT set DEFAULT_PROFILE, because sometimes user do not want to save session
, token: process.env['WECHATY_TOKEN'] || null // DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
port: process.env['WECHATY_PORT'] || null // 0 for disable port
, profile: process.env['WECHATY_PROFILE'] || null // DO NOT set DEFAULT_PROFILE, because sometimes user do not want to save session
, token: process.env['WECHATY_TOKEN'] || null // DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
, debug: process.env['WECHATY_DEBUG'] || false
})
/**
......@@ -62,7 +63,7 @@ function isWechatyDocker() {
try { accessSync(cgroup, F_OK) }
catch (e) { return false }
const line = execSync(`head -1 ${cgroup}`)
const line = execSync(`sort -n ${cgroup} | head -1`)
.toString()
.replace(/\n$/, '')
......
......@@ -137,7 +137,7 @@ class Message {
return '{' + this.type() + '}' + content
}
public from(contact) {
public from(contact?: Contact) {
if (contact) {
if (contact instanceof Contact) {
this.obj.from = contact.id
......@@ -150,7 +150,7 @@ class Message {
return this.obj.from ? Contact.load(this.obj.from) : null
}
public to(contact: Contact|Room|string) {
public to(contact?: Contact|Room|string) {
if (contact) {
if (contact instanceof Contact || contact instanceof Room) {
this.obj.to = contact.id
......@@ -163,14 +163,14 @@ class Message {
return this.obj.to ? Contact.load(this.obj.to) : null
}
public content(content) {
public content(content?) {
if (content) {
this.obj.content = content
}
return this.obj.content
}
public room(room) {
public room(room?) {
if (room) {
if (room instanceof Room) {
this.obj.room = room.id
......@@ -209,7 +209,7 @@ class Message {
})
}
public get(prop) {
public get(prop): string {
if (!prop || !(prop in this.obj)) {
const s = '[' + Object.keys(this.obj).join(',') + ']'
throw new Error(`Message.get(${prop}) must be in: ${s}`)
......
......@@ -19,7 +19,7 @@
import Contact from '../contact'
import Config from '../config'
import FriendRequest from '../friend-request'
import Wechaty from '../wechaty'
// import Wechaty from '../wechaty'
import log from '../brolog-env'
type RecommendInfo = {
......
......@@ -35,8 +35,10 @@ import Watchdog from './watchdog'
const DEFAULT_PUPPET_PORT = 18788 // // W(87) X(88), ascii char code ;-]
class PuppetWeb extends Puppet {
public browser: Browser
private bridge: Bridge
private browser: Browser
private server: Server
private port: number
......
/**
* Wechat for Bot. and for human who can talk with bot/robot
*
* Interface for puppet
* Interface for Puppet
*
* Class Puppet
*
* Licenst: ISC
* https://github.com/zixia/wechaty
* https://github.com/wechaty/wechaty
*
*/
......@@ -17,12 +17,19 @@ import Message from './message'
import Room from './room'
import log from './brolog-env'
class Puppet extends EventEmitter {
private _user: Contact
type ContactGetterFunc = {
(id: string): Promise<any>
}
abstract class Puppet extends EventEmitter {
protected user: Contact
protected userId: string
private _targetState: string
private _currentState: string
public abstract getContact(id: string): Promise<any>
constructor() {
super()
......@@ -38,7 +45,7 @@ class Puppet extends EventEmitter {
}
// targetState : 'live' | 'dead'
public targetState(newState) {
public targetState(newState?) {
if (newState) {
log.verbose('Puppet', 'targetState(%s)', newState)
this._targetState = newState
......@@ -47,7 +54,7 @@ class Puppet extends EventEmitter {
}
// currentState : 'birthing' | 'killing'
public currentState(newState) {
public currentState(newState?) {
if (newState) {
log.verbose('Puppet', 'currentState(%s)', newState)
this._currentState = newState
......@@ -55,47 +62,24 @@ class Puppet extends EventEmitter {
return this._currentState
}
public self(message?: Message): boolean | Contact {
throw new Error('pure virtual interface function')
}
public abstract self(message?: Message): boolean | Contact
public user(contact?: Contact) {
if (contact) {
this._user = contact
}
return this._user
}
// public user(contact?: Contact) {
// if (contact) {
// this._user = contact
// }
// return this._user
// }
/**
* let puppet send message
*
* @param <Message> message - the message to be sent
* @return <Promise>
*/
public send(message): Promise<any> { throw new Error('To Be Implemented') }
public reply(message, reply): Promise<any> { throw new Error('To Be Implemented') }
public reset(reason?: string) { throw new Error('To Be Implementsd') }
public logout(): Promise<any> { throw new Error('To Be Implementsd') }
public quit(): Promise<any> { throw new Error('To Be Implementsd') }
public ding(data?: string): Promise<any> { throw new Error('To Be Implementsd') }
public getContact(id): Promise<any> { // for unit testing
log.verbose('Puppet', `Interface method getContact(${id})`)
throw new Error('Absolute Interface Method should never to be called')
// return Promise.resolve({UserName: 'WeChaty', NickName: 'Puppet'})
}
public abstract send(message: Message): Promise<any>
public abstract reply(message: Message, reply): Promise<any>
// () { throw new Error('To Be Implemented') }
public abstract reset(reason?: string)
public abstract logout(): Promise<any>
public abstract quit(): Promise<any>
public abstract ding(data?: string): Promise<any>
}
// Object.assign(Puppet, {
// Message: require('./message')
// , Contact: require('./contact')
// , Room: require('./room')
// })
// module.exports = Puppet
export default Puppet
export {
Contact
......
......@@ -47,7 +47,7 @@ class Room extends EventEmitter {
private obj: RoomObj
private rawObj: RoomRawObj
constructor(private id) {
constructor(public id: string) {
super()
log.silly('Room', `constructor(${id})`)
// this.id = id
......
......@@ -38,7 +38,7 @@ class UtilLib {
)
}
public static downloadStream(url, cookies) {
public static downloadStream(url: string, cookies): Promise<any> {
// const myurl = 'http://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=3080011908135131569&skey=%40crypt_c117402d_53a58f8fbb21978167a3fc7d3be7f8c9'
url = url.replace(/^https/i, 'http') // use http for better performance
const options = require('url').parse(url)
......@@ -97,7 +97,7 @@ class UtilLib {
* const DEFAULT_IANA_RANGE = {min: 49152, max: 65535}
*
*/
public static getPort(port) {
public static getPort(port:number): Promise<number> {
log.verbose('UtilLib', 'getPort(%d)', port)
let tryPort = nextPort(port || 38788)
......@@ -127,7 +127,7 @@ class UtilLib {
})
})
function nextPort(newPort) {
function nextPort(newPort: number) {
const RANDOM_RANGE = 1024
const n = Math.floor(Math.random() * RANDOM_RANGE)
return newPort + n
......
......@@ -6,30 +6,21 @@
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
/**************************************
*
* Events Function Wrapper
*
*
***************************************/
import Config import './config'
import Contact import './contact'
import Message import './message'
// import Room import './room'
*/
import Config from './config'
import Contact from './contact'
import Message from './message'
// import Room from './room'
import log import './brolog-env'
import log from './brolog-env'
type EventScope = {
say: (content: string, replyTo?: Contact|Contact[]) => void
}
const WechatyEvent = {
list
, wrap
}
const EVENT_CONFIG = {
error: wrapFilehelper
, friend: wrapContact
......@@ -43,33 +34,35 @@ const EVENT_CONFIG = {
, scan: null // NULL
}
function list() {
return Object.keys(EVENT_CONFIG)
}
function wrap(event, callback) {
log.verbose('WechatyEvent', 'wrap(%s, %s)', event, typeof callback)
// if (!(this instanceof Wechaty)) {
// throw new Error('`this` should be Wechaty instance')
// }
if (typeof callback !== 'function') {
throw new Error('`callback` should be function')
class WechatyEvent {
public static list() {
return Object.keys(EVENT_CONFIG)
}
if (!(event in EVENT_CONFIG)) {
throw new Error('event not support: ' + event)
}
const wrapper = EVENT_CONFIG[event]
public static wrap(event, callback) {
log.verbose('WechatyEvent', 'wrap(%s, %s)', event, typeof callback)
/**
* We assign a empty object to each event callback,
* to carry the indenpendent scope
*/
if (wrapper) {
return wrapper(callback)
} else {
return callback
// if (!(this instanceof Wechaty)) {
// throw new Error('`this` should be Wechaty instance')
// }
if (typeof callback !== 'function') {
throw new Error('`callback` should be function')
}
if (!(event in EVENT_CONFIG)) {
throw new Error('event not support: ' + event)
}
const wrapper = EVENT_CONFIG[event]
/**
* We assign a empty object to each event callback,
* to carry the indenpendent scope
*/
if (wrapper) {
return wrapper(callback)
} else {
return callback
}
}
}
......@@ -105,7 +98,7 @@ function wrapContact(callback) {
const contact = argList[0]
const eventScope: EventScope
const eventScope = <EventScope>{}
eventScope.say = (content) => {
const msg = new Message()
msg.to(contact)
......@@ -136,7 +129,7 @@ function wrapRoom(callback) {
throw new Error('room or contact not found')
}
const eventScope: EventScope
const eventScope = <EventScope>{}
eventScope.say = (content, replyTo = null) => {
if (!replyTo) {
replyTo = contact
......@@ -168,10 +161,10 @@ function wrapMessage(callback) {
const msg = argList[0]
const sender = msg.from()
const receiver = msg.to()
// const receiver = msg.to()
const room = msg.room()
const eventScope: EventScope
const eventScope = <EventScope>{}
eventScope.say = (content, replyTo) => {
log.silly('WechatyEvent', 'wrapMessage() say("%s", "%s")', content, replyTo)
......@@ -179,12 +172,12 @@ function wrapMessage(callback) {
return room.say(content, replyTo)
}
const msg = new Message()
msg.to(sender)
msg.content(content)
const m = new Message()
m.to(sender)
m.content(content)
return Config.puppetInstance()
.send(msg)
.send(m)
}
return callback.apply(eventScope, argList)
......@@ -196,8 +189,8 @@ function wrapFilehelper(callback) {
return (...argList) => {
log.silly('WechatyEvent', 'wrapFilehelper() callback')
const eventScope: EventScope
eventScope.say = (content) =>{
const eventScope = <EventScope>{}
eventScope.say = (content) => {
log.silly('WechatyEvent', 'wrapFilehelper() say(%s)', content)
const msg = new Message()
msg.to('filehelper')
......
......@@ -33,11 +33,12 @@ type WechatySetting = {
class Wechaty extends EventEmitter {
private static _instance: Wechaty
private puppet: Puppet
public puppet: Puppet
private inited: boolean = false
private npmVersion: string
private uuid: string
public uuid: string
constructor(private setting: WechatySetting) {
super()
......@@ -108,9 +109,9 @@ class Wechaty extends EventEmitter {
public user(): Contact { return this.puppet && this.puppet.user() }
public reset(reason) {
public reset(reason?: string) {
log.verbose('Wechaty', 'reset() because %s', reason)
this.puppet.reset(reason)
return this.puppet.reset(reason)
}
public init() {
......@@ -150,10 +151,10 @@ class Wechaty extends EventEmitter {
let puppet
switch (this.setting.type) {
case 'web':
puppet = new PuppetWeb( {
head: this.setting.head
, profile: this.setting.profile
})
puppet = new PuppetWeb(
this.setting.head
, this.setting.profile
)
break
default:
throw new Error('Puppet unsupport(yet): ' + this.setting.type)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册