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

rename io-bot to io-client

上级 f423fc55
#!/usr/bin/env node
const {
IoBot
IoClient
, Config
, log
} = require('../')
......@@ -27,28 +27,28 @@ __________________________________________________
let token = Config.token
if (!token) {
log.error('Bot', 'token not found: please set WECHATY_TOKEN in environment before run io-bot')
log.error('Client', 'token not found: please set WECHATY_TOKEN in environment before run io-client')
// process.exit(-1)
token = Config.DEFAULT_TOKEN
log.warn('Bot', `set token to "${token}" for demo purpose`)
log.warn('Client', `set token to "${token}" for demo purpose`)
}
console.log(welcome)
log.info('Bot', 'Starting for WECHATY_TOKEN: %s', token)
log.info('Client', 'Starting for WECHATY_TOKEN: %s', token)
const ioBot = new IoBot({
const client = new IoClient({
token
, log
})
ioBot.init()
.catch(onError.bind(ioBot))
client.init()
.catch(onError.bind(client))
ioBot.initWeb()
.catch(onError.bind(ioBot))
client.initWeb()
.catch(onError.bind(client))
function onError(e) {
log.error('Bot', 'initWeb() fail: %s', e)
log.error('Client', 'initWeb() fail: %s', e)
this.quit()
process.exit(-1)
}
......
......@@ -8,7 +8,7 @@ const Room = require('./src/room')
const Puppet = require('./src/puppet')
const PuppetWeb = require('./src/puppet-web')
const IoBot = require('./src/io-bot')
const IoClient = require('./src/io-client')
const log = require('./src/brolog-env')
......@@ -26,7 +26,7 @@ Object.assign(Wechaty, {
, Puppet
, PuppetWeb
, IoBot
, IoClient
, version: require('./package.json').version
, log // for convenionce use npmlog with environment variable LEVEL
......
......@@ -10,21 +10,21 @@
"DEFAULT_PORT": 8788,
"DEFAULT_PUPPET_PORT": 18788,
"DEFAULT_PROTOCOL": "io|0.0.1",
"DEFAULT_TOKEN": "WECHATY-TOKEN",
"DEFAULT_TOKEN": "TEST_TOKEN",
"ENDPOINT": "wss://api.wechaty.io/v0/websocket",
"BINARY_CHROMIUM": "/wechaty/bin/xvfb-chromium"
},
"scripts": {
"lint": "npm run eslint && npm run tslint",
"eslint": "eslint \"{src,test}/**/*.js\"",
"tslint": "tslint \"{src,test}/**/*.ts\"",
"eslint": "eslint \"{src,test,bin}/**/*.js\"",
"tslint": "tslint \"{src,test,bin}/**/*.ts\"",
"pretest": "npm run lint",
"test": "npm run test:phantomjs && npm run test:chrome",
"test:phantomjs": "cross-env LC_ALL=C WECHATY_LOG=info WECHATY_HEAD=phantomjs ava --timeout=10m \"{src,test}/**/*.spec.js\"",
"test:chrome": "cross-env LC_ALL=C WECHATY_LOG=silly WECHATY_HEAD=chrome ava --timeout=10m \"{src,test}/**/*.spec.js\"",
"testdev": "cross-env LC_ALL=C WECHATY_LOG=silly ava --serial --verbose --fail-fast --timeout=3m",
"ava": "cross-env LC_ALL=C WECHATY_LOG=verbose ava \"{src,test}/**/*.spec.js\"",
"start": "node bin/io-bot",
"start": "node bin/client",
"demo": "node example/ding-dong-bot.js"
},
"repository": {
......@@ -32,7 +32,7 @@
"url": "git+https://github.com/wechaty/wechaty.git"
},
"bin": {
"io-bot": "bin/io-bot.js",
"wechaty-client": "bin/client.js",
"wechaty-version": "bin/version.js"
},
"keywords": [
......
......@@ -2,70 +2,109 @@
*
* wechaty: Wechat for Bot. and for human who talk to bot/robot
*
* Class Io
* Class IoClient
* http://www.wechaty.io
*
* Licenst: ISC
* https://github.com/zixia/wechaty
* https://github.com/wechaty/wechaty
*
*/
const co = require('co')
/**
* DO NOT use `require('../')` here!
* because it will casue a LOOP require ERROR
*/
const Wechaty = require('./wechaty')
const Config = require('./config')
const Io = require('./io')
const brolog = require('./brolog-env')
class IoBot {
class IoClient {
constructor({
token
, log = Wechaty.log
}) {
token = Config.token || Config.DEFAULT_TOKEN
, endpoint = Config.endpoint
, log = brolog
} = {}) {
if (!log) {
const e = new Error('constructor() log(npmlog/brolog) must be set')
throw e
}
this.log = log
this.log.verbose('IoBot', 'constructor() with token: %s', token)
this.log.verbose('IoClient', 'constructor() with token: %s', token)
if (!token) {
const e = new Error('constructor() token must be set')
this.log.error('IoBot', e)
this.log.error('IoClient', e.message)
throw e
}
this.token = token
}
init() {
this.log.verbose('IoBot', 'init()')
this.log.verbose('IoClient', 'init()')
return co.call(this, function* () {
this.wechaty = yield this.initWechaty()
this.io = yield this.initIo()
return this
}).catch(e => {
this.log.error('IoClient', 'init() exception: %s', e.message)
throw e
})
}
initWechaty() {
this.log.verbose('IoClient', 'initWechaty()')
const wechaty = this.wechaty = new Wechaty({
const wechaty = new Wechaty({
profile: Config.DEFAULT_PROFILE
, token: this.token
})
wechaty
.on('login' , user => this.log.info('IoBot', `${user.name()} logined`))
.on('logout' , user => this.log.info('IoBot', `${user.name()} logouted`))
.on('scan', ({url, code}) => this.log.info('IoBot', `[${code}] ${url}`))
.on('message' , message => {
message.ready()
.then(this.onMessage.bind(this))
.catch(e => this.log.error('IoBot', 'message.ready() %s' , e))
})
.on('login' , user => this.log.info('IoClient', `${user.name()} logined`))
.on('logout' , user => this.log.info('IoClient', `${user.name()} logouted`))
.on('scan', ({url, code}) => this.log.info('IoClient', `[${code}] ${url}`))
.on('message' , message => {
message.ready()
.then(this.onMessage.bind(this))
.catch(e => this.log.error('IoClient', 'message.ready() %s' , e))
})
return wechaty.init()
.then(_ => {
this.log.verbose('IoBot', 'wechaty.init() succ')
return this
})
.catch(e => {
this.log.error('IoBot', 'init() init fail: %s', e)
wechaty.quit()
throw e
})
.then(_ => {
this.log.verbose('IoClient', 'wechaty.init() succ')
return this
})
.catch(e => {
this.log.error('IoClient', 'init() init fail: %s', e)
wechaty.quit()
throw e
})
}
initIo() {
this.log.verbose('IoClient', 'initIo()')
if (!this.token) {
log.verbose('IoClient', 'initIo() skiped for no token set')
return Promise.resolve('no token')
} else {
log.verbose('IoClient', 'initIo(%s)', this.token)
}
const io = new Io({
wechaty: this.wechaty
, token: this.token
, endpoint: this.endpoint
})
return io.init()
.catch(e => {
log.verbose('IoClient', 'initIo() init fail: %s', e.message)
throw e
})
}
initWeb(port = Config.httpPort) {
......@@ -79,7 +118,7 @@ class IoBot {
return new Promise((resolve, reject) => {
app.listen(port, () => {
this.log.verbose('IoBot', 'initWeb() Wechaty IO Bot listening on port ' + port + '!')
this.log.verbose('IoClient', 'initWeb() Wechaty IO Bot listening on port ' + port + '!')
return resolve(this)
......@@ -106,4 +145,4 @@ class IoBot {
}
}
module.exports = IoBot.default = IoBot.IoBot = IoBot
module.exports = IoClient.default = IoClient.IoClient = IoClient
......@@ -24,7 +24,7 @@ class Io {
, token = null
, endpoint = Config.endpoint
, protocol = Config.DEFAULT_PROTOCOL
}) {
} = {}) {
if (!wechaty || !token) {
throw new Error('Io must has wechaty & token set')
}
......
......@@ -26,8 +26,6 @@ class Wechaty extends EventEmitter {
type = Config.puppet
, head = Config.head
, port = Config.port
, endpoint = Config.endpoint
, token = Config.token // token for wechaty.io auth. will connect to wechaty.io cloud management if token is set
, profile = Config.profile // no profile, no session save/restore
} = {}) {
log.verbose('Wechaty', 'contructor()')
......@@ -36,8 +34,6 @@ class Wechaty extends EventEmitter {
this.type = type
this.head = head
this.port = port
this.token = token
this.endpoint = endpoint
this.profile = /\.wechaty\.json$/i.test(profile)
? profile
......@@ -59,7 +55,10 @@ class Wechaty extends EventEmitter {
if (!forceNpm) {
try {
// Synchronous version of fs.access(). This throws if any accessibility checks fail, and does nothing otherwise.
/**
* Synchronous version of fs.access().
* This throws if any accessibility checks fail, and does nothing otherwise.
*/
fs.accessSync(dotGitPath, fs.F_OK)
const ss = require('child_process')
......@@ -96,12 +95,11 @@ class Wechaty extends EventEmitter {
}
init() {
log.info('Wechaty', 'v%s initializing...', this.version())
log.verbose('Wechaty', 'puppet: %s' , this.type)
log.verbose('Wechaty', 'head: %s' , this.head)
log.verbose('Wechaty', 'profile: %s', this.profile)
log.verbose('Wechaty', 'uuid: %s' , this.uuid)
log.info('Wechaty', 'v%s initializing...' , this.version())
log.verbose('Wechaty', 'puppet: %s' , this.type)
log.verbose('Wechaty', 'head: %s' , this.head)
log.verbose('Wechaty', 'profile: %s' , this.profile)
log.verbose('Wechaty', 'uuid: %s' , this.uuid)
if (this.inited) {
log.error('Wechaty', 'init() already inited. return and do nothing.')
......@@ -109,12 +107,6 @@ class Wechaty extends EventEmitter {
}
return co.call(this, function* () {
this.io = yield this.initIo()
.catch(e => { // fail safe
log.error('Wechaty', 'initIo failed: %s', e.message)
this.emit('error', e)
})
this.puppet = yield this.initPuppet()
this.inited = true
......@@ -126,28 +118,6 @@ class Wechaty extends EventEmitter {
})
}
initIo() {
if (!this.token) {
log.verbose('Wechaty', 'initIo() skiped for no token set')
return Promise.resolve('no token')
} else {
log.verbose('Wechaty', 'initIo(%s)', this.token)
}
const Io = require('./io')
const io = new Io({
wechaty: this
, token: this.token
, endpoint: this.endpoint
})
return io.init()
.catch(e => {
log.verbose('Wechaty', 'Wechaty.IO init fail: %s', e.message)
throw e
})
}
initPuppet() {
let puppet
switch (this.type) {
......
......@@ -7,7 +7,7 @@ test('Wechaty Framework', t => {
t.truthy(Wechaty.Contact , 'should export Wechaty.Contact')
t.truthy(Wechaty.Room , 'should export Wechaty.Room')
t.truthy(Wechaty.IoBot , 'should export Wechaty.IoBot')
t.truthy(Wechaty.IoClient , 'should export Wechaty.IoClient')
t.truthy(Wechaty.log , 'should export Wechaty.log')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册