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

fix dynamic load puppet (#1437)

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