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

fix dynamic load puppet (#1437)

上级 16235905
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
"wechaty": { "wechaty": {
"DEFAULT_HEAD": 0, "DEFAULT_HEAD": 0,
"DEFAULT_PORT": 8080, "DEFAULT_PORT": 8080,
"DEFAULT_PUPPET": "puppeteer", "DEFAULT_PUPPET": "wechat4u",
"DEFAULT_PROFILE": "default", "DEFAULT_PROFILE": "default",
"DEFAULT_PROTOCOL": "io|0.0.1", "DEFAULT_PROTOCOL": "io|0.0.1",
"DEFAULT_TOKEN": "WECHATY_IO_TOKEN", "DEFAULT_TOKEN": "WECHATY_IO_TOKEN",
...@@ -95,6 +95,7 @@ ...@@ -95,6 +95,7 @@
"memory-card": "^0.4.4", "memory-card": "^0.4.4",
"mime": "^2.2.0", "mime": "^2.2.0",
"npm-programmatic": "0.0.10", "npm-programmatic": "0.0.10",
"pkg-dir": "^3.0.0",
"promise-retry": "^1.1.1", "promise-retry": "^1.1.1",
"qr-image": "^3.2.0", "qr-image": "^3.2.0",
"raven": "^2.6.2", "raven": "^2.6.2",
...@@ -104,6 +105,8 @@ ...@@ -104,6 +105,8 @@
"state-switch": "^0.6.2", "state-switch": "^0.6.2",
"watchdog": "^0.8.1", "watchdog": "^0.8.1",
"wechaty-puppet": "^0.3.3", "wechaty-puppet": "^0.3.3",
"wechaty-puppet-padchat": "^0.2.11",
"wechaty-puppet-wechat4u": "^0.2.15",
"ws": "^5.2.0", "ws": "^5.2.0",
"xml2json": "^0.11.2" "xml2json": "^0.11.2"
}, },
...@@ -121,6 +124,7 @@ ...@@ -121,6 +124,7 @@
"@types/mime": "^2.0.0", "@types/mime": "^2.0.0",
"@types/node": "^10.3.0", "@types/node": "^10.3.0",
"@types/normalize-package-data": "^2.4.0", "@types/normalize-package-data": "^2.4.0",
"@types/pkg-dir": "^2.0.0",
"@types/promise-retry": "^1.1.1", "@types/promise-retry": "^1.1.1",
"@types/puppeteer": "^1.3.4", "@types/puppeteer": "^1.3.4",
"@types/qr-image": "^3.2.1", "@types/qr-image": "^3.2.1",
......
...@@ -57,11 +57,12 @@ export abstract class Accessory extends EventEmitter { ...@@ -57,11 +57,12 @@ export abstract class Accessory extends EventEmitter {
return this._puppet return this._puppet
} }
log.error('Accessory', 'static get puppet() please see issue #1217: https://github.com/Chatie/wechaty/issues/1217') throw new Error([
'static puppet not found for ',
throw new Error('static puppet not found for ' this.name,
+ this.name, ', ',
) 'please see issue #1217: https://github.com/Chatie/wechaty/issues/1217',
].join(''))
} }
/** /**
......
...@@ -2,11 +2,16 @@ import { ...@@ -2,11 +2,16 @@ import {
Constructor, Constructor,
} from 'clone-class' } from 'clone-class'
import npm from 'npm-programmatic' import npm from 'npm-programmatic'
import pkgDir from 'pkg-dir'
import { import {
Puppet, Puppet,
} from 'wechaty-puppet' } from 'wechaty-puppet'
import {
log,
} from './config'
export interface PuppetConfig { export interface PuppetConfig {
npm: string, npm: string,
} }
...@@ -47,21 +52,33 @@ export type PuppetName = keyof typeof PUPPET_DICT ...@@ -47,21 +52,33 @@ export type PuppetName = keyof typeof PUPPET_DICT
// | 'web' // | 'web'
// | 'win32' // | 'win32'
export async function puppetResolver (puppet: PuppetName): Promise<typeof Puppet & Constructor<typeof Puppet>> { export async function puppetResolver (puppet: PuppetName): Promise<typeof Puppet & Constructor<Puppet>> {
log.verbose('PuppetConfig', 'puppetResolver(%s)', puppet)
const config = PUPPET_DICT[puppet] const config = PUPPET_DICT[puppet]
if (!config) { if (!config) {
throw new Error('no such puppet: ' + puppet) throw new Error('no such puppet: ' + puppet)
} }
// tslint:disable-next-line:variable-name // tslint:disable-next-line:variable-name
let MyPuppet: typeof Puppet & Constructor<typeof Puppet> let puppetModule
try { try {
MyPuppet = await import(config.npm) puppetModule = await import(config.npm)
} catch (e) { } catch (e) {
await npm.install(config.npm) log.info('PuppetConfig', 'puppetResolver(%s) not installed, prepare to install it ...', puppet)
MyPuppet = await import(config.npm) await npm.install(
config.npm,
{
cwd : await pkgDir(__dirname),
save : false,
},
)
log.info('PuppetConfig', 'puppetResolver(%s) install success', puppet)
puppetModule = await import(config.npm)
} }
return MyPuppet log.silly('PuppetConfig', 'puppetResolver(%s) import success.', puppet)
return puppetModule.default as typeof Puppet & Constructor<typeof Puppet>
} }
...@@ -469,7 +469,7 @@ export class Wechaty extends Accessory implements Sayable { ...@@ -469,7 +469,7 @@ export class Wechaty extends Accessory implements Sayable {
} }
private async initPuppet(): Promise<void> { private async initPuppet(): Promise<void> {
log.verbose('Wechaty', 'initPuppet(%s)', this.options.puppet) log.verbose('Wechaty', 'initPuppet() %s', this.options.puppet || '')
let inited = false let inited = false
try { try {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册