From 51625dd4b828a60a093ec6e8af56e9d4ab25132d Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Thu, 1 Sep 2016 06:40:37 +0000 Subject: [PATCH] move process.env variables to Config module --- package.json | 7 ++++--- src/config.js | 11 +++++++++-- src/config.spec.js | 21 ++++++++++++++++----- src/io.js | 5 +++-- src/wechaty.js | 12 ++++++------ 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index ee7b544a..1aebb07a 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 11444ac3..d827a19a 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 7d4b4748..a96bb9db 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 a1c43a9a..dbcb3ac0 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 d76542d2..264c62b8 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()') -- GitLab