From 66641a4c1b3d3db8151d6fefa8158b1bcf037459 Mon Sep 17 00:00:00 2001 From: Huan Date: Tue, 31 Aug 2021 18:11:22 +0800 Subject: [PATCH] clean ESM support --- .eslintrc.js => .eslintrc.cjs | 0 .github/workflows/node.js.yml | 9 ++-- README.md | 4 +- examples/ding-dong-bot.js | 9 ++-- examples/ding-dong-bot.ts | 13 ++++-- package.json | 26 ++++++----- tests/smoke-testing.ts | 66 --------------------------- tests/wechaty-puppet-mock.spec.ts | 32 +++++++++++++ tests/wechaty-puppet-service.spec.ts | 28 ++++++++++++ tests/wechaty-puppet-wechat.spec.ts | 30 ++++++++++++ tests/wechaty-puppet-wechat4u.spec.ts | 21 +++++++++ tsconfig.json | 2 +- 12 files changed, 149 insertions(+), 91 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) delete mode 100644 tests/smoke-testing.ts create mode 100755 tests/wechaty-puppet-mock.spec.ts create mode 100755 tests/wechaty-puppet-service.spec.ts create mode 100755 tests/wechaty-puppet-wechat.spec.ts create mode 100755 tests/wechaty-puppet-wechat4u.spec.ts diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 100% rename from .eslintrc.js rename to .eslintrc.cjs diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index fa1d1df..2991e03 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -15,9 +15,12 @@ jobs: build: strategy: matrix: - os: [macos-latest, windows-latest, ubuntu-latest] - # os: [macos-latest, ubuntu-latest] - node-version: [14] + os: + - macos-latest + - windows-latest + - ubuntu-latest + node-version: + - 16 runs-on: ${{ matrix.os }} steps: diff --git a/README.md b/README.md index 77e4cd6..f46856a 100644 --- a/README.md +++ b/README.md @@ -235,7 +235,9 @@ Learn more about Wechaty Puppet from our documentation at [Wechaty Puppet](https ## History -### master +### master v0.13 + +1. Enable ESM (ES Module) support ([chatie/tsconfig#16](https://github.com/Chatie/tsconfig/issues/16)) ### v0.8 (Feb 20, 2021) diff --git a/examples/ding-dong-bot.js b/examples/ding-dong-bot.js index c9b5d0e..13c3398 100644 --- a/examples/ding-dong-bot.js +++ b/examples/ding-dong-bot.js @@ -2,15 +2,18 @@ * Wechaty - Conversational RPA SDK for Chatbot Makers. * - https://github.com/wechaty/wechaty */ -const { +import { Wechaty, ScanStatus, log, -} = require('wechaty') +} from 'wechaty' + +import qrcodeTerminal from 'qrcode-terminal' +import 'dotenv/config.js' function onScan (qrcode, status) { if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) { - require('qrcode-terminal').generate(qrcode, { small: true }) // show qrcode on console + qrcodeTerminal.generate(qrcode, { small: true }) // show qrcode on console const qrcodeImageUrl = [ 'https://wechaty.js.org/qrcode/', diff --git a/examples/ding-dong-bot.ts b/examples/ding-dong-bot.ts index 19d15de..285c025 100644 --- a/examples/ding-dong-bot.ts +++ b/examples/ding-dong-bot.ts @@ -1,3 +1,4 @@ +#!/usr/bin/env node --no-warnings --loader ts-node/esm /** * Wechaty - Conversational RPA SDK for Chatbot Makers. * - https://github.com/wechaty/wechaty @@ -10,9 +11,11 @@ import { log, } from 'wechaty' -import { generate } from 'qrcode-terminal' +import qrcodeTerminal from 'qrcode-terminal' -require('dotenv').config() +// https://stackoverflow.com/a/42817956/1123955 +// https://github.com/motdotla/dotenv/issues/89#issuecomment-587753552 +import 'dotenv/config.js' function onLogout (user: Contact) { log.info('StarterBot', '%s logout', user) @@ -20,14 +23,14 @@ function onLogout (user: Contact) { function onScan (qrcode: string, status: ScanStatus) { if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) { - generate(qrcode, { small: true }) // show qrcode on console - const qrcodeImageUrl = [ 'https://wechaty.js.org/qrcode/', encodeURIComponent(qrcode), ].join('') - log.info('StarterBot', 'onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl) + + qrcodeTerminal.generate(qrcode, { small: true }) // show qrcode on console + } else { log.info('StarterBot', 'onScan: %s(%s)', ScanStatus[status], status) } diff --git a/package.json b/package.json index e3a3c14..e1991eb 100644 --- a/package.json +++ b/package.json @@ -1,22 +1,22 @@ { "name": "wechaty-getting-started", - "version": "0.11.9", + "version": "0.13.0", "description": "Wechaty conversational SDK getting start template & examples", - "main": "examples/ding-dong-bot.js", + "type": "module", "engines": { - "node": ">= 12" + "node": ">= 14" }, "scripts": { - "lint": "eslint \"examples/*.ts\"", - "start": "cross-env NODE_OPTIONS='--unhandled-rejections=strict' ts-node examples/ding-dong-bot.ts", + "lint": "eslint \"examples/*.ts\" \"tests/*.ts\"", + "start": "cross-env NODE_OPTIONS='--no-warnings --loader=ts-node/esm' node examples/ding-dong-bot.ts", "start:js": "node examples/ding-dong-bot.js", "start:service": "cross-env WECHATY_LOG=verbose WECHATY_PUPPET=wechaty-puppet-service npm start", "start:wechat:web": "cross-env WECHATY_LOG=verbose WECHATY_PUPPET=wechaty-puppet-wechat npm start", "start:wechat:padlocal": "cross-env WECHATY_LOG=verbose WECHATY_PUPPET=wechaty-puppet-padlocal npm start", "start:whatsapp:web": "cross-env WECHATY_LOG=verbose WECHATY_PUPPET=wechaty-puppet-whatsapp npm start", - "test": "npm run lint && npm run test:version && npm run test:smoke", - "test:smoke": "ts-node tests/smoke-testing.ts", - "test:version": "check-node-version --node \">= 12\"" + "test": "npm-run-all lint test:version test:smoke", + "test:smoke": "tap --node-arg=--loader=ts-node/esm --node-arg=--no-warnings \"tests/**/*.spec.ts\"", + "test:version": "check-node-version --node \">= 14\"" }, "repository": { "type": "git", @@ -32,18 +32,20 @@ "dependencies": { "dotenv": "^10.0.0", "qrcode-terminal": "^0.12.0", - "wechaty": "^0.67.10", + "wechaty": "^0.69.13", "wechaty-plugin-contrib": "^0.14.23" }, "devDependencies": { "@chatie/eslint-config": "^0.12.4", "@chatie/git-scripts": "^0.6.2", - "@chatie/tsconfig": "^0.17.1", + "@chatie/tsconfig": "^0.19.6", "check-node-version": "^4.1.0", "cross-env": "^7.0.3", "is-pr": "^2.0.0", - "wechaty-puppet-mock": "^0.29.8", - "wechaty-puppet-wechat": "^0.28.1", + "npm-run-all": "^4.1.5", + "tstest": "^0.5.16", + "wechaty-puppet-mock": "^0.31.3", + "wechaty-puppet-wechat": "^0.28.4", "wechaty-puppet-wechat4u": "^0.18", "wechaty-puppet-whatsapp": "^0.2.4" }, diff --git a/tests/smoke-testing.ts b/tests/smoke-testing.ts deleted file mode 100644 index 771813a..0000000 --- a/tests/smoke-testing.ts +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env node -r ts-node/register - -import { Wechaty } from 'wechaty' -const isPr = require('is-pr') - -import { - PuppetMock, - mock, -} from 'wechaty-puppet-mock' - -function getBotList () { - const mocker = new mock.Mocker() - mocker.use(mock.SimpleEnvironment()) - const puppetMock = new PuppetMock({ mocker }) - - const optionsWeChat = { - puppet: 'wechaty-puppet-wechat' as const, - puppetOptions: { - launchOptions: { - ignoreDefaultArgs: ['--disable-extensions'], - }, - }, - } - - const botList = [ - new Wechaty({ puppet: puppetMock }), - new Wechaty({ puppet: 'wechaty-puppet-service' }), - new Wechaty(optionsWeChat), - new Wechaty({ puppet: 'wechaty-puppet-wechat4u' }), - ] - - return botList -} - -async function main () { - // Timeout after 2 minutes - const timer = setTimeout(() => { - console.error('Smoke testing timeout after 2 minutes.') - process.exit(1) - }, 120 * 1000) - - if (isPr) { - console.info('This CI test was activated from Pull Request.') - } else { - console.info('This CI test was activated from Master Branch.') - } - - for (const bot of getBotList()) { - const future = new Promise(resolve => bot.once('scan', resolve)) - await bot.start() - await future - await bot.stop() - console.info(`Puppet ${bot.puppet} v${bot.puppet.version()} smoke testing passed.`) - } - - clearTimeout(timer) - console.info(`Wechaty v${Wechaty.VERSION} smoke testing passed.`) - return 0 -} - -main() - .then(process.exit) - .catch(e => { - console.error(e) - process.exit(1) - }) diff --git a/tests/wechaty-puppet-mock.spec.ts b/tests/wechaty-puppet-mock.spec.ts new file mode 100755 index 0000000..941794b --- /dev/null +++ b/tests/wechaty-puppet-mock.spec.ts @@ -0,0 +1,32 @@ +#!/usr/bin/env node --no-warnings --loader ts-node/esm + +import { test } from 'tstest' +import { Wechaty } from 'wechaty' + +import { + mock, + PuppetMock, +} from 'wechaty-puppet-mock' + +test('wechaty-puppet-service', async t => { + const mocker = new mock.Mocker() + mocker.use(mock.SimpleEnvironment()) + const puppetMock = new PuppetMock({ mocker }) + + const bot = new Wechaty({ + puppet: puppetMock, + }) + + const timer = setTimeout(() => { + console.error('Smoke testing timeout after 2 minutes.') + process.exit(1) + }, 120 * 1000) + + const future = new Promise(resolve => bot.once('scan', resolve)) + await bot.start() + await future + await bot.stop() + + clearTimeout(timer) + t.pass(`Puppet ${bot.puppet} v${bot.puppet.version()} smoke testing passed.`) +}) diff --git a/tests/wechaty-puppet-service.spec.ts b/tests/wechaty-puppet-service.spec.ts new file mode 100755 index 0000000..ac35b1e --- /dev/null +++ b/tests/wechaty-puppet-service.spec.ts @@ -0,0 +1,28 @@ +#!/usr/bin/env node --no-warnings --loader ts-node/esm + +import { test } from 'tstest' +import { Wechaty } from 'wechaty' + +test('wechaty-puppet-service', async t => { + const bot = new Wechaty({ + puppet: 'wechaty-puppet-service', + /** + * Huan(202108): our puppet service token is no-TLS for now. + * FIXME: enable TLS in the future + */ + puppetOptions: { tls: { disable: true } }, + }) + + const timer = setTimeout(() => { + console.error('Smoke testing timeout after 2 minutes.') + process.exit(1) + }, 120 * 1000) + + const future = new Promise(resolve => bot.once('scan', resolve)) + await bot.start() + await future + await bot.stop() + + clearTimeout(timer) + t.pass(`Puppet ${bot.puppet} v${bot.puppet.version()} smoke testing passed.`) +}) diff --git a/tests/wechaty-puppet-wechat.spec.ts b/tests/wechaty-puppet-wechat.spec.ts new file mode 100755 index 0000000..d0951ce --- /dev/null +++ b/tests/wechaty-puppet-wechat.spec.ts @@ -0,0 +1,30 @@ +#!/usr/bin/env node --no-warnings --loader ts-node/esm + +import { test } from 'tstest' +import { Wechaty } from 'wechaty' + +test('wechaty-puppet-wechat', async t => { + const optionsWeChat = { + puppet: 'wechaty-puppet-wechat' as const, + puppetOptions: { + launchOptions: { + ignoreDefaultArgs: ['--disable-extensions'], + }, + }, + } + + const bot = new Wechaty(optionsWeChat) + + const timer = setTimeout(() => { + console.error('Smoke testing timeout after 2 minutes.') + process.exit(1) + }, 120 * 1000) + + const future = new Promise(resolve => bot.once('scan', resolve)) + await bot.start() + await future + await bot.stop() + + clearTimeout(timer) + t.pass(`Puppet ${bot.puppet} v${bot.puppet.version()} smoke testing passed.`) +}) diff --git a/tests/wechaty-puppet-wechat4u.spec.ts b/tests/wechaty-puppet-wechat4u.spec.ts new file mode 100755 index 0000000..3cb6c35 --- /dev/null +++ b/tests/wechaty-puppet-wechat4u.spec.ts @@ -0,0 +1,21 @@ +#!/usr/bin/env node --no-warnings --loader ts-node/esm + +import { test } from 'tstest' +import { Wechaty } from 'wechaty' + +test('wechaty-puppet-wechat4u', async t => { + const bot = new Wechaty({ puppet: 'wechaty-puppet-wechat4u' }) + + const timer = setTimeout(() => { + console.error('Smoke testing timeout after 2 minutes.') + process.exit(1) + }, 120 * 1000) + + const future = new Promise(resolve => bot.once('scan', resolve)) + await bot.start() + await future + await bot.stop() + + clearTimeout(timer) + t.pass(`Puppet ${bot.puppet} v${bot.puppet.version()} smoke testing passed.`) +}) diff --git a/tsconfig.json b/tsconfig.json index f2f6205..2000d3c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,7 +15,7 @@ "examples/**/*.ts", "scripts/**/*.ts", "src/**/*.ts", - "tests/**/*.spec.ts" + "tests/**/*.ts" ] }