From 21c327db336f9b655e1aa59a1d47c07ed7071f2d Mon Sep 17 00:00:00 2001 From: Huan LI Date: Thu, 5 Jul 2018 14:40:35 +0800 Subject: [PATCH] fix dynamic load puppet (#1437) --- package.json | 6 +++++- src/accessory.ts | 11 ++++++----- src/puppet-config.ts | 29 +++++++++++++++++++++++------ src/wechaty.ts | 2 +- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 408f84f2..ce56de1f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/accessory.ts b/src/accessory.ts index e252839f..1cc78b8a 100644 --- a/src/accessory.ts +++ b/src/accessory.ts @@ -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('')) } /** diff --git a/src/puppet-config.ts b/src/puppet-config.ts index 46434455..5f9a7165 100644 --- a/src/puppet-config.ts +++ b/src/puppet-config.ts @@ -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> { +export async function puppetResolver (puppet: PuppetName): Promise> { + 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 + 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 } diff --git a/src/wechaty.ts b/src/wechaty.ts index 020e4f68..1b7018f5 100644 --- a/src/wechaty.ts +++ b/src/wechaty.ts @@ -469,7 +469,7 @@ export class Wechaty extends Accessory implements Sayable { } private async initPuppet(): Promise { - log.verbose('Wechaty', 'initPuppet(%s)', this.options.puppet) + log.verbose('Wechaty', 'initPuppet() %s', this.options.puppet || '') let inited = false try { -- GitLab