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

half work with electron

上级 ef1fd124
......@@ -28,8 +28,8 @@ let token = process.env.WECHATY_TOKEN
if (!token) {
log.error('Bot', 'token not found: please set WECHATY_TOKEN in environment before run io-bot')
// process.exit(-1)
log.warn('Bot', 'set token to "DEMO" for demo purpose')
token = 'DEMO'
log.warn('Bot', `set token to "${token}" for demo purpose`)
} else {
console.log(welcome)
log.info('Bot', 'Starting for WECHATY_TOKEN: %s', token)
......@@ -42,16 +42,14 @@ const ioBot = new IoBot({
})
ioBot.init()
.catch(e => {
log.error('Bot', 'init() fail: %s', e)
bot.quit()
process.exit(-1)
})
.catch(onError.bind(ioBot))
ioBot.initWeb()
.catch(e => {
log.error('Bot', 'initWeb() fail: %s', e)
bot.quit()
process.exit(-1)
})
.catch(onError.bind(ioBot))
function onError(e) {
log.error('Bot', 'initWeb() fail: %s', e)
this.quit()
process.exit(-1)
}
module.exports = require('./src/wechaty')
const Wechaty = require('./src/wechaty')
const Message = require('./src/message')
const Contact = require('./src/contact')
const Room = require('./src/room')
const Puppet = require('./src/puppet')
const PuppetWeb = require('./src/puppet-web')
const IoBot = require('./src/io-bot')
const log = require('./src/npmlog-env')
Object.assign(Wechaty, {
Message
, Contact
, Room
, Puppet
, PuppetWeb
, IoBot
, version: require('./package.json').version
, log // for convenionce use npmlog with environment variable LEVEL
})
module.exports = Wechaty
......@@ -14,6 +14,7 @@ const Wechaty = require('./wechaty')
class IoBot {
constructor({
token = 'EPP'
, head = false
, profile = 'wechaty-epp'
, log = null
}) {
......@@ -32,17 +33,19 @@ class IoBot {
this.token = token
this.profile = profile
this.head = head
}
init() {
this.log.verbose('IoBot', 'init()')
this.wechaty = new Wechaty({
const wechaty = this.wechaty = new Wechaty({
profile: this.profile
, token: this.token
, head: this.head
})
this.wechaty
wechaty
.on('login' , user => this.log.info('IoBot', `${user.name} logined`))
.on('logout' , user => this.log.info('IoBot', `${user} logouted`))
.on('scan', ({url, code}) => this.log.info('IoBot', `[${code}] ${url}`))
......@@ -53,11 +56,12 @@ class IoBot {
})
return this.wechaty.init()
return wechaty.init()
.then(_ => this)
.catch(e => {
this.log.error('IoBot', 'init() init fail: %s', e)
bot.quit()
console.log(e)
wechaty.quit()
return e
})
}
......
......@@ -14,6 +14,8 @@ const co = require('co')
const log = require('./npmlog-env')
const Util = require('./util')
const PuppetWeb = require('./puppet-web')
class Wechaty extends EventEmitter {
constructor({
......@@ -23,7 +25,7 @@ class Wechaty extends EventEmitter {
, endpoint = process.env.WECHATY_ENDPOINT // wechaty.io api endpoint
, token = process.env.WECHATY_TOKEN // token for wechaty.io auth
, profile = process.env.WECHATY_PROFILE // no profile, no session save/restore
} = {}) {
}) {
super()
this.type = type
this.head = head
......@@ -61,6 +63,7 @@ class Wechaty extends EventEmitter {
log.info('Wechaty', 'v%s initializing...', this.npmVersion)
log.verbose('Wechaty', 'puppet: %s' , this.type)
log.verbose('Wechaty', 'head: %s' , this.head)
console.log(process.env.WECHATY_HEAD)
log.verbose('Wechaty', 'profile: %s', this.profile)
if (this.inited) {
......@@ -155,10 +158,15 @@ class Wechaty extends EventEmitter {
}
quit() {
log.verbose('Wechaty', 'quit()')
const puppetBeforeDie = this.puppet
this.puppet = null
this.inited = false
if (!puppetBeforeDie) {
return Promise.resolve()
}
return puppetBeforeDie.quit()
.catch(e => {
log.error('Wechaty', 'quit() exception: %s', e.message)
......@@ -232,28 +240,6 @@ class Wechaty extends EventEmitter {
}
const Message = require('./message')
const Contact = require('./contact')
const Room = require('./room')
// const Puppet = require('./puppet')
const PuppetWeb = require('./puppet-web')
Object.assign(Wechaty, {
Message
, Contact
, Room
// Do not include IoBot here, because it will be a circle dependence
// const IoBot = require('./io-bot')
//, IoBot
, log // for convenionce use npmlog with environment variable LEVEL
// Puppet
})
Wechaty.version = require('../package.json').version
/**
* Expose `Wechaty`.
*/
......
const test = require('tape')
const log = require('../src/npmlog-env')
test('Wechaty Framework', function(t) {
const Wechaty = require('../')
......@@ -7,10 +6,13 @@ test('Wechaty Framework', function(t) {
t.ok(Wechaty.Message , 'should export Wechaty.Message')
t.ok(Wechaty.Contact , 'should export Wechaty.Contact')
t.ok(Wechaty.Room , 'should export Wechaty.Room')
t.ok(Wechaty.IoBot , 'should export Wechaty.IoBot')
// t.ok(Wechaty.Puppet , 'should export Wechaty.Puppet')
// t.ok(Wechaty.Puppet.Web , 'should export Wechaty.Puppet.Web')
t.ok(Wechaty.log , 'should export Wechaty.log')
t.ok(Wechaty.Puppet , 'should export Wechaty.Puppet')
t.ok(Wechaty.PuppetWeb , 'should export Wechaty.PuppetWeb')
const bot = new Wechaty()
t.equal(bot.version(), Wechaty.version, 'should export version in package.json')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册