Skip to content

Commit

Permalink
clean ESM support
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Aug 31, 2021
1 parent 0aed6da commit 66641a4
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 91 deletions.
File renamed without changes.
9 changes: 6 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 6 additions & 3 deletions examples/ding-dong-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down
13 changes: 8 additions & 5 deletions examples/ding-dong-bot.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,24 +11,26 @@ 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)
}

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)
}
Expand Down
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down
66 changes: 0 additions & 66 deletions tests/smoke-testing.ts

This file was deleted.

32 changes: 32 additions & 0 deletions tests/wechaty-puppet-mock.spec.ts
Original file line number Diff line number Diff line change
@@ -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.`)
})
28 changes: 28 additions & 0 deletions tests/wechaty-puppet-service.spec.ts
Original file line number Diff line number Diff line change
@@ -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.`)
})
30 changes: 30 additions & 0 deletions tests/wechaty-puppet-wechat.spec.ts
Original file line number Diff line number Diff line change
@@ -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.`)
})
21 changes: 21 additions & 0 deletions tests/wechaty-puppet-wechat4u.spec.ts
Original file line number Diff line number Diff line change
@@ -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.`)
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"examples/**/*.ts",
"scripts/**/*.ts",
"src/**/*.ts",
"tests/**/*.spec.ts"
"tests/**/*.ts"
]
}

0 comments on commit 66641a4

Please sign in to comment.