Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESM support #12

Merged
merged 5 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
21 changes: 14 additions & 7 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: [12]
node:
- 16

runs-on: ${{ matrix.os }}
steps:
Expand All @@ -17,6 +18,8 @@ jobs:
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: package.json

- name: Install Dependencies
run: npm install
Expand All @@ -32,13 +35,15 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 12
node-version: 16
cache: 'npm'
cache-dependency-path: package.json

- name: Install Dependencies
run: npm install

- name: Generate Version
run: ./scripts/generate-version.sh
- name: Generate Package JSON
run: ./scripts/generate-package-json.sh

- name: Pack Testing
run: ./scripts/npm-pack-testing.sh
Expand All @@ -54,14 +59,16 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 12
node-version: 16
registry-url: https://registry.npmjs.org/
cache: 'npm'
cache-dependency-path: package.json

- name: Install Dependencies
run: npm install

- name: Generate Version
run: ./scripts/generate-version.sh
- name: Generate Package JSON
run: ./scripts/generate-package-json.sh

- name: Set Publish Config
run: ./scripts/package-publish-config-tag.sh
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ Learn how to run Wechaty bot from <https://github.com/wechaty/wechaty-getting-st

## HISTORY

### master
### master v0.3

1. ES Modules support

### v0.2 (Feb 11, 2021)

Expand Down
4 changes: 2 additions & 2 deletions examples/ding-dong-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*
*/
import {
import type {
EventLogoutPayload,
EventLoginPayload,
EventScanPayload,
Expand Down Expand Up @@ -110,7 +110,7 @@ function onError (payload: EventErrorPayload) {
*/
async function onMessage (payload: EventMessagePayload) {
const msgPayload = await puppet.messagePayload(payload.messageId)
if ((/ding/.test(msgPayload.text || '') )) {
if ((/ding/.test(msgPayload.text || ''))) {
await puppet.messageSendText(msgPayload.fromId!, 'dong')
}
}
Expand Down
50 changes: 33 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
{
"name": "wechaty-puppet-whatsapp",
"version": "0.2.4",
"version": "0.3.2",
"description": "Wechaty Puppet for WhatsApp",
"directories": {
"test": "tests"
"type": "module",
"exports": {
".": {
"import": "./dist/esm/src/mod.js",
"require": "./dist/cjs/src/mod.js"
}
},
"main": "dist/src/mod.js",
"typings": "dist/src/mod.d.ts",
"typings": "./dist/esm/src/mod.d.ts",
"engines": {
"wechaty": ">=0.50"
"wechaty": ">=0.69",
"wechaty-puppet": ">=0.43"
},
"scripts": {
"clean": "shx rm -fr dist/*",
"dist": "npm run clean && tsc",
"pack": "npm pack",
"start": "ts-node examples/ding-dong-bot.ts",
"dist": "npm-run-all clean build fixup",
"build": "tsc && tsc -p tsconfig.cjs.json",
"fixup": "shx echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json",
"start": "cross-env NODE_OPTIONS='--no-warnings --loader=ts-node/esm' node examples/ding-dong-bot.ts",
"lint": "npm run lint:es && npm run lint:ts && npm run lint:md",
"lint:md": "markdownlint README.md",
"lint:ts": "tsc --noEmit",
"lint:es": "eslint \"src/**/*.ts\" \"tests/**/*.spec.ts\" --ignore-pattern tests/fixtures/",
"test": "npm run lint && npm run test:unit",
"test:pack": "bash -x scripts/npm-pack-testing.sh",
"test:unit": "blue-tape -r ts-node/register 'src/**/*.spec.ts' 'tests/**/*.spec.ts'"
"test:unit": "tap --node-arg=--loader=ts-node/esm --node-arg=--no-warnings 'src/**/*.spec.ts' 'tests/**/*.spec.ts'"
},
"repository": {
"type": "git",
Expand All @@ -44,24 +49,35 @@
},
"homepage": "https://github.com/wechaty/wechaty-puppet-whatsapp#readme",
"devDependencies": {
"@chatie/eslint-config": "^0.12.4",
"@chatie/eslint-config": "^0.13.5",
"@chatie/git-scripts": "^0.6.2",
"@chatie/semver": "^0.4.7",
"@chatie/tsconfig": "^0.17.2",
"@chatie/tsconfig": "^0.19.6",
"@types/cuid": "^2.0.1",
"npm-run-all": "^4.1.5",
"pkg-jq": "^0.2.11",
"qrcode-terminal": "^0.12.0",
"shx": "^0.3.3",
"ts-node": "^10.0.0",
"tstest": "^0.4.10",
"wechaty": "0.63.3",
"wechaty-puppet": "^0.41.4"
"ts-node": "^10.2.1",
"tstest": "^0.5.16",
"type-fest": "^0.20.2",
"typescript": "^4.4.2",
"wechaty": "0.68.1",
"wechaty-puppet": "^0.43.10"
},
"peerDependencies": {
"wechaty-puppet": "*"
},
"dependencies": {
"whatsapp-web.js": "^1.12.6"
"whatsapp-web.js": "^1.14.1"
},
"files": [
"bin/",
"dist/",
"src/"
],
"tap": {
"check-coverage": false
},
"publishConfig": {
"access": "public",
Expand Down
17 changes: 17 additions & 0 deletions scripts/generate-package-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -e

SRC_PACKAGE_JSON_TS_FILE='src/package-json.ts'

[ -f ${SRC_PACKAGE_JSON_TS_FILE} ] || {
echo ${SRC_PACKAGE_JSON_TS_FILE}" not found"
exit 1
}

cat <<_SRC_ > ${SRC_PACKAGE_JSON_TS_FILE}
/**
* This file was auto generated from scripts/generate-version.sh
*/
import type { PackageJson } from 'type-fest'
export const packageJson: PackageJson = $(cat package.json) as any
_SRC_
18 changes: 0 additions & 18 deletions scripts/generate-version.sh

This file was deleted.

45 changes: 37 additions & 8 deletions scripts/npm-pack-testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,60 @@ else
fi

npm run dist
npm run pack
npm pack

TMPDIR="/tmp/npm-pack-testing.$$"
mkdir "$TMPDIR"
mv ./*-*.*.*.tgz "$TMPDIR"
cp tests/fixtures/smoke-testing.ts "$TMPDIR"

cd $TMPDIR

npm init -y
npm install ./*-*.*.*.tgz \
@chatie/tsconfig \
@types/quick-lru \
@types/normalize-package-data \
\
npm install --production *-*.*.*.tgz \
@types/node \
@chatie/tsconfig@$NPM_TAG \
"wechaty-puppet@$NPM_TAG" \
"wechaty@$NPM_TAG" \

#
# CommonJS
#
./node_modules/.bin/tsc \
--target es6 \
--module CommonJS \
\
--moduleResolution node \
--esModuleInterop \
--lib esnext \
--noEmitOnError \
--noImplicitAny \
--skipLibCheck \
smoke-testing.ts

echo
echo "CommonJS: pack testing..."
node smoke-testing.js

#
# ES Modules
#

# https://stackoverflow.com/a/59203952/1123955
echo "`jq '.type="module"' package.json`" > package.json

./node_modules/.bin/tsc \
--target es2020 \
--module es2020 \
\
--moduleResolution node \
--esModuleInterop \
--lib esnext \
--noEmitOnError \
--noImplicitAny \
--target es6 \
--module commonjs \
--skipLibCheck \
smoke-testing.ts

echo
echo "ES Module: pack testing..."
node smoke-testing.js
18 changes: 15 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import {
FileBox,
} from 'wechaty-puppet'
export const CHATIE_OFFICIAL_ACCOUNT_QRCODE = 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
import { packageJson } from './package-json.js'

export function qrCodeForChatie (): FileBox {
const VERSION = packageJson.version || '0.0.0'

const CHATIE_OFFICIAL_ACCOUNT_QRCODE = 'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'

function qrCodeForChatie (): FileBox {
return FileBox.fromQRCode(CHATIE_OFFICIAL_ACCOUNT_QRCODE)
}

export const MEMORY_SLOT = 'PUPPET_WHATSAPP'
const MEMORY_SLOT = 'PUPPET_WHATSAPP'

export {
CHATIE_OFFICIAL_ACCOUNT_QRCODE,
FileBox,
MEMORY_SLOT,
qrCodeForChatie,
VERSION,
}
4 changes: 2 additions & 2 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VERSION } from './version'
import { PuppetWhatsapp } from './puppet-whatsapp'
import { VERSION } from './config.js'
import { PuppetWhatsapp } from './puppet-whatsapp.js'

export {
VERSION,
Expand Down
10 changes: 10 additions & 0 deletions src/package-json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node --no-warnings --loader ts-node/esm

import { test } from 'tstest'

import { packageJson } from './package-json.js'

test('Make sure the packageJson is fresh in source code', async t => {
const keyNum = Object.keys(packageJson).length
t.equal(keyNum, 0, 'packageJson should be empty in source code, only updated before publish to NPM')
})
11 changes: 11 additions & 0 deletions src/package-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* This file will be overwrite when we publish NPM module
* by scripts/generate_version.ts
*/
import type { PackageJson } from 'type-fest'

/**
* Huan(202108):
* The below default values is only for unit testing
*/
export const packageJson: PackageJson = {}
14 changes: 7 additions & 7 deletions src/puppet-whatsapp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#!/usr/bin/env ts-node
#!/usr/bin/env node --no-warnings --loader ts-node/esm

import test from 'blue-tape'
import { test } from 'tstest'

import { PuppetWhatsapp } from './puppet-whatsapp'
import { PuppetWhatsapp } from './puppet-whatsapp.js'

class PuppetWhatsAppTest extends PuppetWhatsapp {
}

test('PuppetWhatsapp perfect restart testing', async (t) => {
test('PuppetWhatsapp perfect restart testing', async t => {
const puppet = new PuppetWhatsAppTest()
try {

for (let i = 0; i < 3; i++) {
await puppet.start()
t.true(puppet.state.on(), 'should be turned on after start()')
t.ok(puppet.state.on(), 'should be turned on after start()')

await puppet.stop()
t.true(puppet.state.off(), 'should be turned off after stop()')
t.ok(puppet.state.off(), 'should be turned off after stop()')

t.pass('start/stop-ed at #' + i)
}
Expand All @@ -25,6 +25,6 @@ test('PuppetWhatsapp perfect restart testing', async (t) => {

} catch (e) {
console.error(e)
t.fail(e)
t.fail(e as any)
}
})
Loading