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

clean(code): rename `Config` to `config`, change `wechaty.version()` to static compitibal

上级 fb00a88d
......@@ -20,9 +20,9 @@
import * as os from 'os'
import Config from '../src/config'
import Doctor from '../src/doctor'
import Wechaty from '../src/wechaty'
import { config } from '../src/config'
import { Doctor } from '../src/doctor'
import { Wechaty } from '../src/wechaty'
const wechaty = Wechaty.instance()
const doctor = new Doctor()
......@@ -43,7 +43,7 @@ async function main() {
1. Wechaty version: ${wechaty.version()}
2. ${os.type()} ${os.arch()} version ${os.release()} memory ${Math.floor(os.freemem() / 1024 / 1024)}/${Math.floor(os.totalmem() / 1024 / 1024)} MB
3. Docker: ${Config.dockerMode}
3. Docker: ${config.dockerMode}
4. Node version: ${process.version}
5. Tcp IPC TEST: ${ipcTestResult}
6. Chromedriver: ${chromedriverVersion}
......
......@@ -19,7 +19,7 @@
*/
import {
Config,
config,
log,
} from '../src/config'
......@@ -42,12 +42,12 @@ __________________________________________________
`
let token = Config.token
let token = config.token
if (!token) {
log.error('Client', 'token not found: please set WECHATY_TOKEN in environment before run io-client')
// process.exit(-1)
token = Config.DEFAULT_TOKEN
token = config.DEFAULT_TOKEN
log.warn('Client', `set token to "${token}" for demo purpose`)
}
......
......@@ -41,7 +41,7 @@ import { EventEmitter } from 'events'
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
Config,
config,
Wechaty,
} from '../'
......@@ -56,7 +56,7 @@ import {
const APIAI_API_KEY = '7217d7bce18c4bcfbe04ba7bdfaf9c08'
const brainApiAi = ApiAi(APIAI_API_KEY)
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: config.DEFAULT_PROFILE })
console.log(`
Welcome to api.AI Wechaty Bot.
......
......@@ -28,7 +28,7 @@ const QrcodeTerminal = require('qrcode-terminal')
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
Config,
config,
Contact,
Wechaty,
log,
......@@ -54,7 +54,7 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: config.DEFAULT_PROFILE })
bot
.on('login' , function(this, user) {
......
......@@ -27,7 +27,7 @@ const finis = require('finis')
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
Config,
config,
Wechaty,
log,
MediaMessage,
......@@ -57,7 +57,7 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: config.DEFAULT_PROFILE })
bot
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
......
......@@ -26,10 +26,10 @@ const QrcodeTerminal = require('qrcode-terminal')
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
Wechaty,
config,
Contact,
Config,
log,
Wechaty,
} from '../'
const welcome = `
......@@ -57,7 +57,7 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: config.DEFAULT_PROFILE })
bot
.on('login' , user => log.info('Bot', `${user.name()} logined`))
......
......@@ -23,7 +23,7 @@
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
Config,
config,
Wechaty,
log,
} from '../../'
......@@ -41,7 +41,7 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
Wechaty.instance({ profile: config.DEFAULT_PROFILE })
.on('scan', (url, code) => {
if (!/201|200/.test(String(code))) {
......
......@@ -32,12 +32,12 @@ import * as qrcodeTerminal from 'qrcode-terminal'
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
Config,
config,
Message,
MsgType,
Wechaty,
} from '../'
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: config.DEFAULT_PROFILE })
bot
.on('scan', (url, code) => {
......
......@@ -53,7 +53,7 @@ const qrcodeTerminal = require('qrcode-terminal')
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
Config,
config,
Contact,
Room,
Wechaty,
......@@ -86,7 +86,7 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: config.DEFAULT_PROFILE })
bot
.on('scan', (url, code) => {
......
......@@ -33,13 +33,13 @@ const qrcodeTerminal = require('qrcode-terminal')
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
Config,
config,
MediaMessage,
MsgType,
Wechaty,
} from '../'
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: config.DEFAULT_PROFILE })
bot
.on('scan', (url, code) => {
......
......@@ -36,7 +36,7 @@ const Tuling123 = require('tuling123-client')
* when you are runing with Docker or NPM instead of Git Source.
*/
import {
Config,
config,
Wechaty,
log,
} from '../'
......@@ -53,7 +53,7 @@ import {
const TULING123_API_KEY = '18f25157e0446df58ade098479f74b21'
const tuling = new Tuling123(TULING123_API_KEY)
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
const bot = Wechaty.instance({ profile: config.DEFAULT_PROFILE })
console.log(`
Welcome to Tuling Wechaty Bot.
......
import {
Config,
Sayable,
config,
log,
Sayable,
} from './src/config'
import Contact from './src/contact'
......@@ -23,7 +23,7 @@ import Wechaty from './src/wechaty'
const VERSION = require('./package.json').version
export {
Config,
config,
Contact,
FriendRequest,
IoClient,
......
......@@ -18,24 +18,24 @@
*/
import { test } from 'ava'
import { Config } from './config'
import { config } from './config'
import { Puppet } from './puppet'
test('important variables', t => {
t.true('head' in Config, 'should exist `head` in Config')
t.true('puppet' in Config, 'should exist `puppet` in Config')
t.true('apihost' in Config, 'should exist `apihost` 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.true('head' in config, 'should exist `head` in Config')
t.true('puppet' in config, 'should exist `puppet` in Config')
t.true('apihost' in config, 'should exist `apihost` 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_HEAD , 'should export DEFAULT_HEAD')
t.truthy(Config.DEFAULT_PROTOCOL , 'should export DEFAULT_PROTOCOL')
t.truthy(Config.DEFAULT_APIHOST , 'should export DEFAULT_APIHOST')
t.truthy(Config.CMD_CHROMIUM , 'should export CMD_CHROMIUM')
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_PROTOCOL , 'should export DEFAULT_PROTOCOL')
t.truthy(config.DEFAULT_APIHOST , 'should export DEFAULT_APIHOST')
t.truthy(config.CMD_CHROMIUM , 'should export CMD_CHROMIUM')
})
test('validApiHost()', t => {
......@@ -49,12 +49,12 @@ test('validApiHost()', t => {
]
OK_APIHOSTS.forEach(apihost => {
t.notThrows(() => {
Config.validApiHost(apihost)
config.validApiHost(apihost)
})
}, 'should not row for right apihost')
ERR_APIHOSTS.forEach(apihost => {
t.throws(() => {
Config.validApiHost(apihost)
config.validApiHost(apihost)
})
}, 'should throw for error apihost')
......@@ -62,30 +62,30 @@ test('validApiHost()', t => {
test('puppetInstance()', t => {
t.throws(() => {
Config.puppetInstance()
config.puppetInstance()
}, Error, 'should throw when not initialized')
const EXPECTED = <Puppet>{userId: 'test'}
const mockPuppet = EXPECTED
Config.puppetInstance(mockPuppet)
const instance = Config.puppetInstance()
config.puppetInstance(mockPuppet)
const instance = config.puppetInstance()
t.deepEqual(instance, EXPECTED, 'should equal with initialized data')
Config.puppetInstance(null)
config.puppetInstance(null)
t.throws(() => {
Config.puppetInstance()
config.puppetInstance()
}, Error, 'should throw after set to null')
})
test('dockerMode', t => {
t.true('dockerMode' in Config, 'should identify docker env by `dockerMode`')
t.true('dockerMode' in config, 'should identify docker env by `dockerMode`')
if ('C9_PORT' in process.env) {
t.is(Config.dockerMode, false, 'should not in docker mode in Cloud9 IDE')
t.is(config.dockerMode, false, 'should not in docker mode in Cloud9 IDE')
} else if (require('is-ci')) {
t.is(Config.dockerMode, false, 'should not in docker mode in Continuous Integeration System')
t.is(config.dockerMode, false, 'should not in docker mode in Continuous Integeration System')
} else {
// a custom running envioronment, maybe docker, maybe not
}
......
......@@ -16,7 +16,43 @@
* limitations under the License.
*
*/
import * as os from 'os'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
/**
* Raven.io
*/
import * as Raven from 'raven'
Raven.disableConsoleAlerts()
Raven
.config(
process.env.NODE_ENV === 'production'
&& 'https://f6770399ee65459a82af82650231b22c:d8d11b283deb441e807079b8bb2c45cd@sentry.io/179672',
{
release: require('../package.json').version,
tags: {
git_commit: 'c0deb10c4',
platform: !!process.env('WECHATY_DOCKER')
? 'docker'
: os.platform(),
},
},
)
.install()
/*
try {
doSomething(a[0])
} catch (e) {
Raven.captureException(e)
}
Raven.context(function () {
doSomething(a[0])
})
*/
// const isCi = require('is-ci')
// const isDocker = require('is-docker')
......@@ -78,22 +114,24 @@ export interface ConfigSetting {
puppetInstance(): Puppet
puppetInstance(empty: null): void
puppetInstance(instance: Puppet): void
puppetInstance(instance?: Puppet | null): Puppet | void
puppetInstance(instance?: Puppet | null): Puppet | void,
dockerMode: boolean
gitVersion(): string | null,
npmVersion(): string,
dockerMode: boolean,
}
/* tslint:disable:variable-name */
/* tslint:disable:no-var-requires */
export const Config: ConfigSetting = require('../package.json').wechaty
export const config: ConfigSetting = require('../package.json').wechaty
/**
* 1. ENVIRONMENT VARIABLES + PACKAGES.JSON (default)
*/
Object.assign(Config, {
apihost: process.env['WECHATY_APIHOST'] || Config.DEFAULT_APIHOST,
head: process.env['WECHATY_HEAD'] || Config.DEFAULT_HEAD,
puppet: process.env['WECHATY_PUPPET'] || Config.DEFAULT_PUPPET,
Object.assign(config, {
apihost: process.env['WECHATY_APIHOST'] || config.DEFAULT_APIHOST,
head: process.env['WECHATY_HEAD'] || config.DEFAULT_HEAD,
puppet: process.env['WECHATY_PUPPET'] || config.DEFAULT_PUPPET,
validApiHost,
})
......@@ -103,12 +141,12 @@ function validApiHost(apihost: string): boolean {
}
throw new Error('validApiHost() fail for ' + apihost)
}
validApiHost(Config.apihost)
validApiHost(config.apihost)
/**
* 2. ENVIRONMENT VARIABLES (only)
*/
Object.assign(Config, {
Object.assign(config, {
port: process.env['WECHATY_PORT'] || null, // 0 for disable port
profile: process.env['WECHATY_PROFILE'] || null, // DO NOT set DEFAULT_PROFILE, because sometimes user do not want to save session
token: process.env['WECHATY_TOKEN'] || null, // DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
......@@ -118,15 +156,15 @@ Object.assign(Config, {
/**
* 3. Service Settings
*/
Object.assign(Config, {
Object.assign(config, {
// get PORT form cloud service env, ie: heroku
httpPort: process.env['PORT'] || process.env['WECHATY_PORT'] || Config.DEFAULT_PORT,
httpPort: process.env['PORT'] || process.env['WECHATY_PORT'] || config.DEFAULT_PORT,
})
/**
* 4. Envioronment Identify
*/
Object.assign(Config, {
Object.assign(config, {
dockerMode: !!process.env('WECHATY_DOCKER'),
isGlobal: isWechatyInstalledGlobal(),
})
......@@ -195,7 +233,52 @@ function puppetInstance(instance?: Puppet | null): Puppet | void {
}
Object.assign(Config, {
function gitVersion(): string | null {
const dotGitPath = path.join(__dirname, '..', '.git') // only for ts-node, not for dist
// const gitLogArgs = ['log', '--oneline', '-1']
// TODO: use git rev-parse HEAD ?
const gitArgs = ['rev-parse', 'HEAD']
try {
// Make sure this is a Wechaty repository
fs.statSync(dotGitPath).isDirectory()
const ss = require('child_process')
.spawnSync('git', gitArgs, { cwd: __dirname })
if (ss.status !== 0) {
throw new Error(ss.error)
}
const revision = ss.stdout
.toString()
.trim()
.slice(0, 7)
return revision
} catch (e) { /* fall safe */
/**
* 1. .git not exist
* 2. git log fail
*/
log.silly('Wechaty', 'version() form development environment is not availble: %s', e.message)
return null
}
}
function npmVersion(): string {
try {
return require('../package.json').version
} catch (e) {
log.error('Wechaty', 'npmVersion() exception %s', e.message)
Raven.captureException(e)
return '0.0.0'
}
}
Object.assign(config, {
gitVersion,
npmVersion,
puppetInstance,
})
......@@ -236,38 +319,9 @@ export interface Sleepable {
sleep(millisecond: number): Promise<void>
}
import * as Raven from 'raven'
Raven.disableConsoleAlerts()
Raven
.config(
process.env.NODE_ENV === 'production'
&& 'https://f6770399ee65459a82af82650231b22c:d8d11b283deb441e807079b8bb2c45cd@sentry.io/179672',
{
release: require('../package.json').version,
tags: {
git_commit: 'c0deb10c4',
platform: os.platform(),
},
},
)
.install()
/*
try {
doSomething(a[0])
} catch (e) {
Raven.captureException(e)
}
Raven.context(function () {
doSomething(a[0])
})
*/
export {
log,
Raven,
}
export default Config
export default config
......@@ -17,7 +17,7 @@
*
*/
import {
Config,
config,
Raven,
Sayable,
log,
......@@ -329,9 +329,9 @@ export class Contact implements Sayable {
}
try {
const hostname = await (Config.puppetInstance() as PuppetWeb).browser.hostname()
const hostname = await (config.puppetInstance() as PuppetWeb).browser.hostname()
const avatarUrl = `http://${hostname}${this.obj.avatar}`
const cookies = await (Config.puppetInstance() as PuppetWeb).browser.readCookie()
const cookies = await (config.puppetInstance() as PuppetWeb).browser.readCookie()
log.silly('Contact', 'avatar() url: %s', avatarUrl)
return UtilLib.urlStream(avatarUrl, cookies)
......@@ -388,9 +388,9 @@ export class Contact implements Sayable {
}
if (!contactGetter) {
log.silly('Contact', 'get contact via ' + Config.puppetInstance().constructor.name)
contactGetter = Config.puppetInstance()
.getContact.bind(Config.puppetInstance())
log.silly('Contact', 'get contact via ' + config.puppetInstance().constructor.name)
contactGetter = config.puppetInstance()
.getContact.bind(config.puppetInstance())
}
if (!contactGetter) {
throw new Error('no contatGetter')
......@@ -431,7 +431,7 @@ export class Contact implements Sayable {
* ```
*/
public self(): boolean {
const userId = Config.puppetInstance()
const userId = config.puppetInstance()
.userId
const selfId = this.id
......@@ -522,7 +522,7 @@ export class Contact implements Sayable {
throw new Error('unsupport name type')
}
const contactList = await Config.puppetInstance()
const contactList = await config.puppetInstance()
.contactFind(filterFunction)
.catch(e => {
log.error('Contact', 'findAll() rejected: %s', e.message)
......@@ -627,7 +627,7 @@ export class Contact implements Sayable {
return this.obj && this.obj.alias || null
}
return Config.puppetInstance()
return config.puppetInstance()
.contactAlias(this, newAlias)
.then(ret => {
if (ret) {
......
......@@ -18,7 +18,7 @@
*/
import {
Config,
config,
log,
} from './config'
import Contact from './contact'
......@@ -32,7 +32,7 @@ export abstract class FriendRequest {
constructor() {
log.verbose('FriendRequest', 'constructor()')
if (!Config.puppetInstance()) {
if (!config.puppetInstance()) {
throw new Error('no Config.puppetInstance() instanciated')
}
}
......
......@@ -25,7 +25,7 @@
import { StateSwitch } from 'state-switch'
import {
Config,
config,
log as globalLog,
} from './config'
import { Io } from './io'
......@@ -39,7 +39,7 @@ export class IoClient {
private state = new StateSwitch<'online', 'offline'>('IoClient', 'offline', globalLog)
constructor(
private token: string = Config.token || Config.DEFAULT_TOKEN,
private token: string = config.token || config.DEFAULT_TOKEN,
private log: any = globalLog,
) {
if (!log) {
......@@ -141,7 +141,7 @@ export class IoClient {
return
}
public initWeb(port = Config.httpPort) {
public initWeb(port = config.httpPort) {
// if (process.env.DYNO) {
// }
const app = require('express')()
......
......@@ -21,7 +21,7 @@ import * as WebSocket from 'ws'
import { StateSwitch } from 'state-switch'
import {
Config,
config,
// WechatyEventName
log,
} from './config'
......@@ -71,8 +71,8 @@ export class Io {
throw new Error('Io must has wechaty & token set')
}
setting.apihost = setting.apihost || Config.apihost
setting.protocol = setting.protocol || Config.DEFAULT_PROTOCOL
setting.apihost = setting.apihost || config.apihost
setting.protocol = setting.protocol || config.DEFAULT_PROTOCOL
this.uuid = setting.wechaty.uuid
......
......@@ -19,7 +19,7 @@
import { test } from 'ava'
import {
Config,
config,
log,
} from './config'
import Message from './message'
......@@ -29,7 +29,7 @@ const MOCK_USER_ID = 'TEST-USER-ID'
const puppet = new PuppetWeb()
puppet.userId = MOCK_USER_ID
Config.puppetInstance(puppet)
config.puppetInstance(puppet)
test('constructor()', t => {
/* tslint:disable:max-line-length */
......@@ -93,7 +93,7 @@ test.serial('ready()', async t => {
})
}
Config.puppetInstance()
config.puppetInstance()
.getContact = mockGetContact
const m = new Message(rawData)
......@@ -131,7 +131,7 @@ test('findAll()', async t => {
})
test('self()', t => {
Config.puppetInstance()
config.puppetInstance()
const m = new Message()
m.from(MOCK_USER_ID)
......@@ -183,11 +183,11 @@ test.serial('mentioned()', async t => {
let puppet1
try {
puppet1 = Config.puppetInstance()
puppet1 = config.puppetInstance()
puppet1.getContact = mockContactGetter
} catch (err) {
puppet1 = { getContact: mockContactGetter }
Config.puppetInstance(puppet1)
config.puppetInstance(puppet1)
}
const msg11 = new Message(rawObj11)
const room11 = msg11.room()
......
......@@ -20,7 +20,7 @@ import * as fs from 'fs'
import * as path from 'path'
import {
Config,
config,
Raven,
RecommendInfo,
Sayable,
......@@ -414,7 +414,7 @@ export class Message implements Sayable {
public count() { return this._counter }
public self(): boolean {
const userId = Config.puppetInstance()
const userId = config.puppetInstance()
.userId
const fromId = this.obj.from
......@@ -619,7 +619,7 @@ export class Message implements Sayable {
}
}
return Config.puppetInstance()
return config.puppetInstance()
.send(m)
}
......@@ -651,7 +651,7 @@ export class MediaMessage extends Message {
}
// FIXME: decoupling needed
this.bridge = (Config.puppetInstance() as PuppetWeb)
this.bridge = (config.puppetInstance() as PuppetWeb)
.bridge
}
......@@ -801,7 +801,7 @@ export class MediaMessage extends Message {
try {
await this.ready()
// FIXME: decoupling needed
const cookies = await (Config.puppetInstance() as PuppetWeb).browser.readCookie()
const cookies = await (config.puppetInstance() as PuppetWeb).browser.readCookie()
if (!this.obj.url) {
throw new Error('no url')
}
......
......@@ -27,7 +27,7 @@ import {
} from 'selenium-webdriver'
import {
Config,
config,
HeadName,
log,
} from '../config'
......@@ -113,9 +113,9 @@ export class BrowserDriver {
'--no-sandbox',
], // issue #26 for run inside docker
}
if (Config.dockerMode) {
if (config.dockerMode) {
log.verbose('PuppetWebBrowserDriver', 'getChromeDriver() wechaty in docker confirmed(should not show this in CI)')
options['binary'] = Config.CMD_CHROMIUM
options['binary'] = config.CMD_CHROMIUM
} else {
/**
* https://github.com/Chatie/wechaty/pull/416
......@@ -224,7 +224,7 @@ export class BrowserDriver {
// , '--ssl-client-certificate-file=cert.pem' //
]
if (Config.debug) {
if (config.debug) {
phantomjsArgs.push('--remote-debugger-port=8080') // XXX: be careful when in production env.
phantomjsArgs.push('--webdriver-loglevel=DEBUG')
// phantomjsArgs.push('--webdriver-logfile=webdriver.debug.log')
......
......@@ -26,7 +26,7 @@ import { StateSwitch } from 'state-switch'
const retryPromise = require('retry-promise').default // https://github.com/olalonde/retry-promise
import {
Config,
config,
HeadName,
log,
} from '../config'
......@@ -53,7 +53,7 @@ export class Browser extends EventEmitter {
constructor(
private setting: BrowserSetting = {
head: Config.head,
head: config.head,
sessionFile: '',
},
) {
......
......@@ -18,13 +18,13 @@
*/
import { test } from 'ava'
import Config from '../config'
import config from '../config'
import Contact from '../contact'
import Message from '../message'
import Puppet from '../puppet'
import PuppetWebFriendRequest from './friend-request'
Config.puppetInstance({
config.puppetInstance({
userId: 'xxx',
} as Puppet)
......
......@@ -30,7 +30,7 @@ const retryPromise = require('retry-promise').default
import { Contact } from '../contact'
import {
Config,
config,
RecommendInfo,
log,
} from '../config'
......@@ -97,7 +97,7 @@ export class PuppetWebFriendRequest extends FriendRequest {
this.hello = hello
}
return Config.puppetInstance()
return config.puppetInstance()
.friendRequestSend(contact, hello)
}
......@@ -108,7 +108,7 @@ export class PuppetWebFriendRequest extends FriendRequest {
throw new Error('request is not a `receive` type. it is a ' + this.type + ' type')
}
const ret = await Config.puppetInstance()
const ret = await config.puppetInstance()
.friendRequestAccept(this.contact, this.ticket)
const max = 20
......
......@@ -17,7 +17,7 @@
*
*/
import {
Config,
config,
HeadName,
log,
Raven,
......@@ -75,7 +75,7 @@ export class PuppetWeb extends Puppet {
super()
if (!setting.head) {
setting.head = Config.head
setting.head = config.head
}
this.on('watchdog', Watchdog.onFeed.bind(this))
}
......
......@@ -19,7 +19,7 @@
import { EventEmitter } from 'events'
import {
Config,
config,
Raven,
Sayable,
log,
......@@ -134,8 +134,8 @@ export class Room extends EventEmitter implements Sayable {
}
if (!contactGetter) {
contactGetter = Config.puppetInstance()
.getContact.bind(Config.puppetInstance())
contactGetter = config.puppetInstance()
.getContact.bind(config.puppetInstance())
}
if (!contactGetter) {
throw new Error('no contactGetter')
......@@ -216,7 +216,7 @@ export class Room extends EventEmitter implements Sayable {
m.room(this)
return Config.puppetInstance()
return config.puppetInstance()
.send(m)
}
......@@ -295,7 +295,7 @@ export class Room extends EventEmitter implements Sayable {
throw new Error('contact not found')
}
const n = Config.puppetInstance()
const n = config.puppetInstance()
.roomAdd(this, contact)
return n
}
......@@ -306,7 +306,7 @@ export class Room extends EventEmitter implements Sayable {
if (!contact) {
throw new Error('contact not found')
}
const n = await Config.puppetInstance()
const n = await config.puppetInstance()
.roomDel(this, contact)
.then(_ => this.delLocal(contact))
return n
......@@ -354,7 +354,7 @@ export class Room extends EventEmitter implements Sayable {
if (newTopic) {
log.verbose('Room', 'topic(%s)', newTopic)
Config.puppetInstance()
config.puppetInstance()
.roomTopic(this, newTopic)
.catch(e => {
log.warn('Room', 'topic(newTopic=%s) exception: %s',
......@@ -408,7 +408,7 @@ export class Room extends EventEmitter implements Sayable {
public owner(): Contact | null {
const ownerUin = this.obj && this.obj.ownerUin
const user = Config.puppetInstance()
const user = config.puppetInstance()
.user
if (user && user.get('uin') === ownerUin) {
......@@ -547,7 +547,7 @@ export class Room extends EventEmitter implements Sayable {
throw new Error('contactList not found')
}
return Config.puppetInstance()
return config.puppetInstance()
.roomCreate(contactList, topic)
.catch(e => {
log.error('Room', 'create() exception: %s', e && e.stack || e.message || e)
......@@ -579,7 +579,7 @@ export class Room extends EventEmitter implements Sayable {
throw new Error('unsupport topic type')
}
const roomList = await Config.puppetInstance()
const roomList = await config.puppetInstance()
.roomFind(filterFunction)
.catch(e => {
log.verbose('Room', 'findAll() rejected: %s', e.message)
......
......@@ -17,13 +17,11 @@
*
*/
import { EventEmitter } from 'events'
import * as fs from 'fs'
import * as path from 'path'
import { StateSwitch } from 'state-switch'
import {
Config,
config,
HeadName,
PuppetName,
Raven,
......@@ -103,11 +101,7 @@ export class Wechaty extends EventEmitter implements Sayable {
* @private
*/
private state = new StateSwitch<'standby', 'ready'>('Wechaty', 'standby', log)
/**
* the npmVersion
* @private
*/
private npmVersion: string = require('../package.json').version
/**
* the uuid
* @private
......@@ -134,9 +128,9 @@ export class Wechaty extends EventEmitter implements Sayable {
super()
log.verbose('Wechaty', 'contructor()')
setting.head = setting.head || Config.head
setting.puppet = setting.puppet || Config.puppet
setting.profile = setting.profile || Config.profile
setting.head = setting.head || config.head
setting.puppet = setting.puppet || config.puppet
setting.profile = setting.profile || config.profile
// setting.port = setting.port || Config.port
......@@ -166,39 +160,18 @@ export class Wechaty extends EventEmitter implements Sayable {
* console.log(Wechaty.instance().version(true))
* // '0.7.9'
*/
public version(forceNpm = false): string {
// TODO: use git rev-parse HEAD ?
const dotGitPath = path.join(__dirname, '..', '.git') // only for ts-node, not for dist
// TODO: use git rev-parse HEAD ?
const gitLogCmd = 'git'
const gitLogArgs = ['log', '--oneline', '-1']
public static version(forceNpm = false): string {
if (!forceNpm) {
try {
fs.statSync(dotGitPath).isDirectory()
const ss = require('child_process')
.spawnSync(gitLogCmd, gitLogArgs, { cwd: __dirname })
if (ss.status !== 0) {
throw new Error(ss.error)
}
const revision = ss.stdout
.toString()
.trim()
const revision = config.gitVersion()
if (revision) {
return `#git[${revision}]`
} catch (e) { /* fall safe */
/**
* 1. .git not exist
* 2. git log fail
*/
log.silly('Wechaty', 'version() form development environment is not availble: %s', e.message)
}
}
return config.npmVersion()
}
return this.npmVersion
public version(forceNpm?) {
return Wechaty.version(forceNpm)
}
/**
......@@ -378,7 +351,7 @@ export class Wechaty extends EventEmitter implements Sayable {
this.puppet = <Puppet>puppet // force to use base class Puppet interface for better encapsolation
// set puppet instance to Wechaty Static variable, for using by Contact/Room/Message/FriendRequest etc.
Config.puppetInstance(puppet)
config.puppetInstance(puppet)
await puppet.init()
return puppet
......@@ -405,7 +378,7 @@ export class Wechaty extends EventEmitter implements Sayable {
const puppetBeforeDie = this.puppet
this.puppet = null
Config.puppetInstance(null)
config.puppetInstance(null)
await puppetBeforeDie.quit()
.catch(e => {
......
......@@ -17,11 +17,11 @@
*
*/
import { test } from 'ava'
import Config from '../src/config'
import config from '../src/config'
import Contact from '../src/contact'
import PuppetWeb from '../src/puppet-web'
Config.puppetInstance(new PuppetWeb())
config.puppetInstance(new PuppetWeb())
test('Contact smoke testing', async t => {
/* tslint:disable:variable-name */
......
......@@ -22,20 +22,20 @@ import * as fs from 'fs'
// import { execSync } from 'child_process'
// import * as sinon from 'sinon'
import Config from '../src/config'
import config from '../src/config'
/**
* need keep this !Config.dockerMode because ava need at least one test() inside.
* × No tests found in test\docker.spec.js
*/
if (Config.dockerMode) {
if (config.dockerMode) {
test('Docker smoke testing', function(t) {
// const n = execSync('ps a | grep Xvfb | grep -v grep | wc -l').toString().replace(/\n/, '', 'g')
// t.is(parseInt(n), 1, 'should has Xvfb started')
t.notThrows(() => {
// fs.accessSync(Config.CMD_CHROMIUM, fs['X_OK'])
fs.statSync(Config.CMD_CHROMIUM).isFile()
fs.statSync(config.CMD_CHROMIUM).isFile()
}, 'should exist xvfb-chrome exectable')
})
......
......@@ -20,7 +20,7 @@ import * as fs from 'fs'
import { test } from 'ava'
import {
Config,
config,
log,
} from '../../'
......@@ -31,7 +31,7 @@ import {
const TEST_DOMAIN = 'www.chatie.io'
const TEST_URL = 'https://' + TEST_DOMAIN
const PROFILE = Config.DEFAULT_PROFILE + '-' + process.pid + '-'
const PROFILE = config.DEFAULT_PROFILE + '-' + process.pid + '-'
let profileCounter = 1
test('Cookie smoke testing', async t => {
......@@ -105,7 +105,7 @@ test('Cookie save/load', async t => {
const profileName = PROFILE + (profileCounter++)
let browser = new Browser({
head: Config.head,
head: config.head,
sessionFile: profileName,
})
......@@ -179,7 +179,7 @@ test('Cookie save/load', async t => {
*/
browser = new Browser({
head: Config.head,
head: config.head,
sessionFile: profileName,
})
......
......@@ -19,7 +19,7 @@
import { test } from 'ava'
import {
Config,
config,
log,
} from '../../src/config'
import PuppetWeb from '../../src/puppet-web'
......@@ -34,7 +34,7 @@ test.serial('login/logout events', async t => {
const pw = new PuppetWeb()
t.truthy(pw, 'should instantiated a PuppetWeb')
Config.puppetInstance(pw)
config.puppetInstance(pw)
await pw.init()
t.pass('should be inited')
......@@ -61,7 +61,7 @@ test.serial('server/browser socketio ding', async t => {
const puppet = new PuppetWeb()
t.truthy(puppet, 'should instantiated a PuppetWeb')
Config.puppetInstance(puppet)
config.puppetInstance(puppet)
const EXPECTED_DING_DATA = 'dingdong'
......
......@@ -18,12 +18,12 @@
*/
import { test } from 'ava'
import Config from '../src/config'
import config from '../src/config'
import Contact from '../src/contact'
import PuppetWeb from '../src/puppet-web'
import Room from '../src/room'
Config.puppetInstance(new PuppetWeb())
config.puppetInstance(new PuppetWeb())
// Room.attach(new PuppetWeb())
// test('Room smoke testing', async t => {
......@@ -105,11 +105,11 @@ test('Room smoking test', async t => {
let puppet
try {
puppet = Config.puppetInstance()
puppet = config.puppetInstance()
puppet.getContact = mockContactGetter
} catch (err) {
puppet = { getContact: mockContactGetter }
Config.puppetInstance(puppet)
config.puppetInstance(puppet)
}
await r.ready()
......
......@@ -19,7 +19,7 @@
import { test } from 'ava'
import {
Config,
config,
Contact,
FriendRequest,
IoClient,
......@@ -54,8 +54,8 @@ test('Wechaty Framework', t => {
})
test('Wechaty Config setting', t => {
t.truthy(Config , 'should export Config')
t.truthy(Config.DEFAULT_HEAD , 'should has DEFAULT_HEAD')
t.truthy(Config.DEFAULT_PUPPET , 'should has DEFAULT_PUPPET')
t.truthy(Config.DEFAULT_PORT , 'should has DEFAULT_PORT')
t.truthy(config , 'should export Config')
t.truthy(config.DEFAULT_HEAD , 'should has DEFAULT_HEAD')
t.truthy(config.DEFAULT_PUPPET , 'should has DEFAULT_PUPPET')
t.truthy(config.DEFAULT_PORT , 'should has DEFAULT_PORT')
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册