diff --git a/package.json b/package.json index ee7b544a457a8ac515253ad9fdff3142d864f283..1aebb07a43cb7589dbb8ce370b7ff8c0e76ec84a 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,10 @@ "DEFAULT_HEAD": "phantomjs", "DEFAULT_PUPPET": "web", "DEFAULT_PROFILE": "default-profile-name", - "DEFAULT_PORT": 18788, - "DEFAULT_PUPPET_PORT": 28788, + "DEFAULT_PORT": 8788, + "DEFAULT_PUPPET_PORT": 18788, + "DEFAULT_PROTOCOL": "io|0.0.1", + "ENDPOINT": "wss://api.wechaty.io/v0/websocket", "BINARY_CHROMIUM": "/wechaty/bin/xvfb-chromium" }, "scripts": { @@ -76,7 +78,6 @@ "body-parser": "^1.15.2", "chromedriver": "^2.23.1", "co": "^4.6.0", - "eventemitter2": "^2.1.0", "express": "^4.13.4", "npmlog": "^4.0.0", "phantomjs-prebuilt": "^2.1.12", diff --git a/src/config.js b/src/config.js index 11444ac33b5ffc4116a9e1d704038c3f215544e0..d827a19aaddfd4bc99089ebc0883be3946c4cbcf 100644 --- a/src/config.js +++ b/src/config.js @@ -1,8 +1,15 @@ const Config = require('../package.json').wechaty Object.assign(Config, { - isDocker: !!process.env.WECHATY_DOCKER - , head: process.env.WECHATY_HEAD || Config.DEFAULT_HEAD + isDocker: !!process.env.WECHATY_DOCKER + + , head: process.env.WECHATY_HEAD || Config.DEFAULT_HEAD + , puppet: process.env.WECHATY_PUPPET || Config.DEFAULT_PUPPET + , endpoint: process.env.WECHATY_ENDPOINT || Config.ENDPOINT // wechaty.io api endpoint + , port: process.env.WECHATY_PORT || 0 // 0 for disable port + + , profile: process.env.WECHATY_PROFILE + , token: process.env.WECHATY_TOKEN }) module.exports = Config.default = Config.Config = Config diff --git a/src/config.spec.js b/src/config.spec.js index 7d4b4748dc76976ee7fb4e50bbb8de31cdf669b5..a96bb9db7c929cfaa124bc1d63548cee3f005d59 100644 --- a/src/config.spec.js +++ b/src/config.spec.js @@ -1,15 +1,26 @@ import { test } from 'ava' import { Config } from './config' -test('Config Module Exports', t => { +test('Config list vars & methods', t => { t.truthy(Config.default , 'should export default') t.truthy(Config.Config , 'should export Config') - t.true(typeof Config.isDocker !== 'undefined' , 'should identify docker env by isDocker') + t.true('isDocker' in Config, 'should identify docker env by `isDocker`') + t.true('head' in Config, 'should exist `head` in Config') + t.true('puppet' in Config, 'should exist `puppet` in Config') + t.true('endpoint' in Config, 'should exist `endpoint` in Config') + t.true('port' in Config, 'should exist `port` in Config') + t.true('profile' in Config, 'should exist `profile` in Config') + t.true('token' in Config, 'should exist `token` in Config') - t.truthy(Config.DEFAULT_PUPPET , 'should export DEFAULT_PUPPET') - t.truthy(Config.DEFAULT_PORT , 'should export DEFAULT_PORT') - t.truthy(Config.DEFAULT_PROFILE , 'should export DEFAULT_PROFILE') + t.truthy(Config.DEFAULT_PUPPET , 'should export DEFAULT_PUPPET') + t.truthy(Config.DEFAULT_PORT , 'should export DEFAULT_PORT') + t.truthy(Config.DEFAULT_PROFILE , 'should export DEFAULT_PROFILE') + t.truthy(Config.DEFAULT_HEAD , 'should export DEFAULT_HEAD') + t.truthy(Config.DEFAULT_PUPPET_PORT , 'should export DEFAULT_PUPPET_PORT') + t.truthy(Config.DEFAULT_PROTOCOL , 'should export DEFAULT_PROTOCOL') + t.truthy(Config.ENDPOINT , 'should export ENDPOINT') + t.truthy(Config.BINARY_CHROMIUM , 'should export BINARY_CHROMIUM') }) diff --git a/src/io.js b/src/io.js index a1c43a9a16b31f848cf7efb83a1220334f0cc5ba..dbcb3ac09e6f5c9119e3086e26ab56cfaf060809 100644 --- a/src/io.js +++ b/src/io.js @@ -15,14 +15,15 @@ const co = require('co') const log = require('./npmlog-env') const Contact = require('./contact') +const Config = require('./config') class Io { constructor({ wechaty = null , token = null - , endpoint = 'wss://api.wechaty.io/v0/websocket' - , protocol = 'io|0.0.1' + , endpoint = Config.endpoint + , protocol = Config.DEFAULT_PROTOCOL }) { if (!wechaty || !token) { throw new Error('Io must has wechaty & token set') diff --git a/src/wechaty.js b/src/wechaty.js index d76542d257e5b674f4b57d2c6f66048c04ec14d2..264c62b89070ab5bf5605a278f74d1117e0686e4 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -22,12 +22,12 @@ const Config = require('./config') class Wechaty extends EventEmitter { constructor({ - type = process.env.WECHATY_PUPPET || Config.DEFAULT_PUPPET - , head = process.env.WECHATY_HEAD || Config.DEFAULT_HEAD - , port = process.env.WECHATY_PORT || 0 // 0 for disable port - , 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 + 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()')