diff --git a/bin/io-bot.js b/bin/io-bot.js index 01be6d7d91e6da34128bc644c5955b328935f3fa..1d3eb832d38e9e64fb35e9dac7c3a927cf49910a 100755 --- a/bin/io-bot.js +++ b/bin/io-bot.js @@ -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) +} diff --git a/index.js b/index.js index 4779e10570e250c4a455f0b72b335b862f83859f..754da243cebe65e263f5a332801c86fa5e180ba5 100644 --- a/index.js +++ b/index.js @@ -1 +1,28 @@ -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 diff --git a/src/io-bot.js b/src/io-bot.js index b8b167e5e68190fad4d50edd17a4ad528f9a49a0..777334448d85981ecdfc34d57531a50d7a081558 100644 --- a/src/io-bot.js +++ b/src/io-bot.js @@ -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 }) } diff --git a/src/wechaty.js b/src/wechaty.js index 167f4bbd33cf16c09ed9dafc373968f34c33954f..c4bcfa5ca840c23e4dace1d43a9fcb955df18666 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -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`. */ diff --git a/test/wechaty.spec.js b/test/wechaty.spec.js index 6b338c3ee9736cced84984a5dc683d62a121fb7b..9cf7a15f426a64f76d76a4c6ca846990b603ce3b 100644 --- a/test/wechaty.spec.js +++ b/test/wechaty.spec.js @@ -1,5 +1,4 @@ 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')