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

clean puppet manager code

上级 b0e0e808
......@@ -3,6 +3,7 @@
*/
export const PUPPET_DEPENDENCIES = {
'default' : '0.0.0', // will be replaced with PUPPET_DEFAULT
'mock' : '0.0.0', // compatible with v0.18, will be replaced with wechaty-puppet-padchat
'padchat' : '0.0.0', // compatible with v0.18, will be replaced with wechaty-puppet-padchat
/////////////////////////////////////
// 'wechaty-puppet-hostie' : '^0.0.1',
......@@ -13,6 +14,6 @@ export const PUPPET_DEPENDENCIES = {
'wechaty-puppet-wechat4u' : '^0.14.1',
}
export const PUPPET_DEFAULT = 'wechaty-puppet-puppeteer'
export type PuppetModuleName = keyof typeof PUPPET_DEPENDENCIES
export const PUPPET_NAME_DEFAULT: PuppetModuleName = 'wechaty-puppet-puppeteer'
#!/usr/bin/env ts-node
/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// tslint:disable:no-shadowed-variable
import test from 'blue-tape'
import {
PuppetManager,
} from './puppet-manager'
test('resolve an unsupported puppet name', async t => {
try {
await PuppetManager.resolve('fasdfsfasfsfdfs' as any)
t.fail('should reject')
} catch (e) {
t.ok('reject when options is a string')
}
try {
await PuppetManager.resolve({ puppet: 'fadfdsafa' as any })
t.fail('should reject')
} catch (e) {
t.ok('reject when options.puppet is unknown')
}
try {
await PuppetManager.resolve({ puppet: 'mock' })
t.pass('should allow "mock" as puppet name')
} catch (e) {
t.fail('should pass "mock" as puppet name')
}
})
......@@ -15,16 +15,16 @@ import {
log,
} from './config'
import {
PUPPET_DEFAULT,
PUPPET_DEPENDENCIES,
PUPPET_NAME_DEFAULT,
PuppetModuleName,
} from './puppet-config'
import {
Wechaty,
// Wechaty,
} from './wechaty'
export interface ResolveOptions {
wechaty : Wechaty,
// wechaty : Wechaty,
puppet : Puppet | PuppetModuleName,
puppetOptions? : PuppetOptions,
}
......@@ -35,19 +35,11 @@ export class PuppetManager {
options: ResolveOptions
): Promise<Puppet> {
log.verbose('PuppetManager', 'resolve({wechaty: %s, puppet: %s, puppetOptions: %s})',
options.wechaty,
// options.wechaty,
options.puppet,
JSON.stringify(options.puppetOptions),
)
if (!options.puppet || options.puppet === 'default') {
options.puppet = PUPPET_DEFAULT
} else if (options.puppet === 'padchat') {
// issue #1496 https://github.com/Chatie/wechaty/issues/1496
// compatible old settings for padchat
options.puppet = 'wechaty-puppet-padchat'
}
let puppetInstance: Puppet
if (options.puppet instanceof Puppet) {
......@@ -74,6 +66,40 @@ export class PuppetManager {
protected static async resolveName (puppetName: PuppetModuleName): Promise<PuppetImplementation> {
log.verbose('PuppetManager', 'resolveName(%s)', puppetName)
if (!puppetName) {
throw new Error('must provide a puppet name')
}
switch (puppetName) {
case 'padchat':
// issue #1496 https://github.com/Chatie/wechaty/issues/1496
// compatible old settings for padchat
puppetName = 'wechaty-puppet-padchat'
break
case 'mock':
puppetName = 'wechaty-puppet-mock'
break
case 'default':
puppetName = PUPPET_NAME_DEFAULT
break
default:
if (!(puppetName in PUPPET_DEPENDENCIES)) {
throw new Error(
[
'',
'puppet npm module not supported: "' + puppetName + '"',
'learn more about supported Wechaty Puppet from our directory at',
'<https://github.com/Chatie/wechaty-puppet/wiki/Directory>',
'',
].join('\n')
)
}
break
}
await this.checkModule(puppetName)
const puppetModule = await import(puppetName)
......@@ -85,18 +111,6 @@ export class PuppetManager {
protected static async checkModule (puppetName: PuppetModuleName): Promise<void> {
log.verbose('PuppetManager', 'checkModule(%s)', puppetName)
if (!(puppetName in PUPPET_DEPENDENCIES)) {
throw new Error(
[
'',
'puppet npm module not supported: "' + puppetName + '"',
'learn more about supported Wechaty Puppet from our directory at',
'<https://github.com/Chatie/wechaty-puppet/wiki/Directory>',
'',
].join('\n')
)
}
const versionRange = PUPPET_DEPENDENCIES[puppetName]
/**
......
......@@ -569,7 +569,7 @@ export class Wechaty extends Accessory implements Sayable {
const puppetInstance = await PuppetManager.resolve({
puppet,
puppetOptions : this.options.puppetOptions,
wechaty : this,
// wechaty : this,
})
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册