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

Enbale ESM #56

Merged
merged 1 commit into from
Sep 12, 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.
30 changes: 19 additions & 11 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ jobs:
name: Build
strategy:
matrix:
os: [ubuntu-latest]
node: [12]
os:
- ubuntu-latest
node:
- 16

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
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 @@ -30,15 +34,17 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- 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 @@ -52,13 +58,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- 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 Package JSON
run: ./scripts/generate-package-json.sh

- name: Generate Version
run: ./scripts/generate-version.sh
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const config: RoomInviterConfig = {
wechaty.use(RoomInviter(config))
```

### 9 EventHotHandler
### ~~9 EventHotHandler~~ DEPRECATED after v0.15

Hot reloading event handler module files.

Expand Down Expand Up @@ -325,6 +325,8 @@ We are listing those powerful Wechaty Plugins outside the contrib as in the foll
### master v0.15

1. Add `SourceToTargetRoomConnector` to connect a source room to a target room by forward messages to target room.
1. Support ES Modules
1. Deprecated `EventHotHandler` due to ESM

### v0.14 master

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env ts-node
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm

import test from 'tstest'
import { test } from 'tstest'

import { Wechaty } from 'wechaty'
import { PuppetMock } from 'wechaty-puppet-mock'

import { EventHotHandler } from './event-hot-handler'
import { EventHotHandler } from './event-hot-handler.js'

test('EventHotHandler perfect restart testing', async t => {
const wechaty = new Wechaty({ puppet: new PuppetMock() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
log,
} from 'wechaty'

import { WechatyEventName } from 'wechaty/dist/src/events/wechaty-events'
import type { WechatyEventName } from 'wechaty/dist/src/events/wechaty-events'

import {
callerResolve,
Expand Down Expand Up @@ -84,13 +84,13 @@ async function addEventHandler (
log.error('WechatyPluginContrib', 'EventHotHandler EventHotHandlerPlugin(%s) listener(%s) exception%s',
wechaty, eventName, e,
)
wechaty.emit('error', e)
wechaty.emit('error', e as Error)
}
})
} catch (e) {
log.error('WechatyPluginContrib', 'EventHotHandler EventHotHandlerPlugin() eventName(%s) hotImport(%s) rejection: %s',
eventName, absoluteFilename, e,
)
wechaty.emit('error', e)
wechaty.emit('error', e as Error)
}
}
2 changes: 1 addition & 1 deletion examples/ding-dong-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
DingDong,
EventLogger,
QRCodeTerminal,
} from '../src/mod' // from 'wechaty-plugin-contrib'
} from '../src/mod.js' // from 'wechaty-plugin-contrib'

const bot = new Wechaty({
name : 'ding-dong-bot',
Expand Down
2 changes: 1 addition & 1 deletion examples/message-awaiter-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
EventLogger,
QRCodeTerminal,
MessageAwaiter,
} from '../src/mod' // from 'wechaty-plugin-contrib'
} from '../src/mod.js' // from 'wechaty-plugin-contrib'

const bot = new Wechaty({
name: 'message-awaiter-bot',
Expand Down
44 changes: 31 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
"name": "wechaty-plugin-contrib",
"version": "0.15.5",
"description": "Wechaty Plugin Ecosystem Contrib Package",
"main": "dist/src/mod.js",
"typings": "dist/src/mod.d.ts",
"type": "module",
"exports": {
".": {
"import": "./dist/esm/src/mod.js",
"require": "./dist/cjs/src/mod.js"
}
},
"typings": "./dist/esm/src/mod.d.ts",
"engines": {
"node": ">= 12",
"wechaty": ">=0.56"
"wechaty": ">=0.69",
"wechaty-puppet": ">=0.43",
"node": ">=14"
},
"directories": {
"test": "tests"
Expand All @@ -17,14 +24,16 @@
"qrcode-terminal": "^0.12.0"
},
"devDependencies": {
"@chatie/eslint-config": "^0.12.4",
"@chatie/eslint-config": "^0.14.1",
"@chatie/git-scripts": "^0.6.2",
"@chatie/semver": "^0.4.7",
"@chatie/tsconfig": "^0.16.2",
"@chatie/tsconfig": "^0.20.2",
"@types/mustache": "^4.1.2",
"npm-run-all": "^4.1.5",
"pkg-jq": "^0.2.11",
"shx": "^0.3.3",
"tstest": "^0.4.10",
"tstest": "^0.5.16",
"typescript": "^4.4.3",
"wechaty": "^0.65.6",
"wechaty-mocker": "^0.7.3",
"wechaty-puppet-mock": "^0.29.10"
Expand All @@ -33,17 +42,18 @@
"wechaty": "*"
},
"scripts": {
"build": "tsc && tsc -p tsconfig.cjs.json",
"clean": "shx rm -fr dist/*",
"dist": "npm run clean && tsc",
"pack": "npm pack",
"dist": "npm-run-all clean build dist:commonjs",
"dist:commonjs": "jq -n \"{ type: \\\"commonjs\\\" }\" > dist/cjs/package.json",
"lint": "npm run lint:es && npm run lint:ts && npm run lint:md",
"lint:md": "markdownlint README.md",
"lint:ts": "tsc --noEmit",
"example": "ts-node examples/ding-dong-bot.ts",
"lint:ts": "tsc --isolatedModules --noEmit",
"example": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" examples/ding-dong-bot.ts",
"start": "npm run example",
"test": "npm run lint && npm run test:unit",
"test": "npm-run-all lint test:unit",
"test:pack": "bash -x scripts/npm-pack-testing.sh",
"test:unit": "blue-tape -r ts-node/register \"src/**/*.spec.ts\" \"src/*.spec.ts\" \"tests/*.spec.ts\" \"tests/**/*.spec.ts\"",
"test:unit": "tap --node-arg=--loader=ts-node/esm --node-arg=--no-warnings \"src/**/*.spec.ts\" \"src/*.spec.ts\" \"tests/*.spec.ts\" \"tests/**/*.spec.ts\"",
"lint:es": "eslint --ignore-pattern tests/fixtures/ '{bin,examples,scripts,src,tests}/**/*.ts'"
},
"repository": {
Expand All @@ -66,6 +76,14 @@
"pre-push": "npx git-scripts-pre-push"
}
},
"files": [
"bin/",
"dist/",
"src/"
],
"tap": {
"check-coverage": false
},
"publishConfig": {
"tag": "next"
}
Expand Down
20 changes: 20 additions & 0 deletions scripts/generate-package-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/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
}

VERSION=$(npx pkg-jq -r .version)

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.

43 changes: 39 additions & 4 deletions scripts/npm-pack-testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,67 @@ set -e

VERSION=$(npx pkg-jq -r .version)

if npx --package @chatie/semver semver-is-prod $VERSION; then
if npx --package @chatie/semver semver-is-prod "$VERSION"; then
NPM_TAG=latest
else
NPM_TAG=next
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 \
npm install --production *-*.*.*.tgz \
@types/node \
@chatie/tsconfig@$NPM_TAG \
pkg-jq \
"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
#
npx pkg-jq -i '.type="module"'


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

echo
echo "ES Module: pack testing..."
node smoke-testing.js
12 changes: 10 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
export { VERSION } from './version'
export { log } from 'wechaty'
/// <reference path="./typings.d.ts" />
import { log } from 'wechaty'
import { packageJson } from './package-json.js'

const VERSION = packageJson.version || '0.0.0'

export {
log,
VERSION,
}
2 changes: 1 addition & 1 deletion src/contrib/chatops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import {
MessageMatcherOptions,
messageMatcher,
} from '../matchers/mod'
} from '../matchers/mod.js'

export interface ChatOpsConfig {
/**
Expand Down
8 changes: 4 additions & 4 deletions src/contrib/ding-dong/ding-dong.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env ts-node
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm

import test from 'tstest'
import { test } from 'tstest'
// import sinon from 'sinon'

import {
import type {
// Wechaty,
Message,
} from 'wechaty'
Expand All @@ -14,7 +14,7 @@ import {
import {
isMatchConfig,
DingDongConfigObject,
} from './ding-dong'
} from './ding-dong.js'

test('isMatchConfig {mention: true}', async t => {
for await (const fixture of createFixture()) {
Expand Down
9 changes: 6 additions & 3 deletions src/contrib/ding-dong/ding-dong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
Wechaty,
WechatyPlugin,
Message,
log,
} from 'wechaty'

import * as matchers from '../../matchers/mod'
import * as talkers from '../../talkers/mod'
import {
log,
} from '../../config.js'

import * as matchers from '../../matchers/mod.js'
import * as talkers from '../../talkers/mod.js'

export interface DingDongConfigObject {
/**
Expand Down
Loading