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

ES Module support #8

Merged
merged 22 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 21 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
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false

# 4 tab indentation
[Makefile]
indent_style = tab
indent_size = 4
File renamed without changes.
99 changes: 99 additions & 0 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: NPM

on: [push, pull_request]

jobs:
build:
name: Build
strategy:
matrix:
os:
- ubuntu-latest
node-version:
- 16

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: npm
cache-dependency-path: package.json

- name: Install Dependencies
run: npm install

- name: Test
run: npm test

pack:
name: Pack
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
cache: npm
cache-dependency-path: package.json

- name: Install Dependencies
run: npm install

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

- name: Pack Testing
run: ./scripts/npm-pack-testing.sh

publish:
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/v'))
name: Publish
needs:
- build
- pack
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
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: Set Publish Config
run: ./scripts/package-publish-config-tag.sh

- name: Build Dist
run: npm run dist

- name: Check Branch
id: check-branch
run: |
if [[ ${{ github.ref }} =~ ^refs/heads/(main|v[0-9]+\.[0-9]+.*)$ ]]; then
echo ::set-output name=match::true
fi # See: https://stackoverflow.com/a/58869470/1123955
- name: Is A Publish Branch
if: steps.check-branch.outputs.match == 'true'
run: |
NAME=$(npx pkg-jq -r .name)
VERSION=$(npx pkg-jq -r .version)
if npx version-exists "$NAME" "$VERSION"
then echo "$NAME@$VERSION exists on NPM, skipped."
else npm publish
fi
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Is Not A Publish Branch
if: steps.check-branch.outputs.match != 'true'
run: echo 'Not A Publish Branch'
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

# WECHATY-CHATOPERA

[![ES Modules](https://img.shields.io/badge/ES-Modules-brightgreen)](https://github.com/Chatie/tsconfig/issues/16)

## INTRODUCTION

Bring your bots into Wechat with Wechaty Chatopera Plugin.
Expand All @@ -24,24 +26,24 @@ Bring your bots into Wechat with Wechaty Chatopera Plugin.
To use the plugin:

```ts
import { WechatyChatopera } from "wechaty-chatopera";
import { WechatyChatopera } from 'wechaty-chatopera'

const config = {
/**
* Chatopera Service ID
*/
clientId: "YOUR_CLIENTID",
secret: "YOUR_SECRET",
personalAccessToken: "YOUR_PERSONAL_ACCESS_TOKEN",
clientId: 'YOUR_CLIENTID',
secret: 'YOUR_SECRET',
personalAccessToken: 'YOUR_PERSONAL_ACCESS_TOKEN',
faqBestReplyThreshold: 0.8,
faqSuggReplyThreshold: 0.2
repoConfig: {}
};
}

const ChatoperaPlugin = WechatyChatopera(config);
const ChatoperaPlugin = WechatyChatopera(config)

const wechaty = new Wechaty();
wechaty.use(ChatoperaPlugin);
const wechaty = new Wechaty()
wechaty.use(ChatoperaPlugin)
```

In `config`, either {`personalAccessToken`, `repoConfig`} or {`clientId`, `secret`} must be present.
Expand Down
4 changes: 2 additions & 2 deletions examples/chatopera-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
log,
} from 'wechaty'

import { WechatyChatoperaConfig, WechatyChatopera } from '../src/mod'
import { WechatyChatoperaConfig, WechatyChatopera } from '../src/mod.js'

function onLogin (user: Contact) {
log.info('Login %s', user)
Expand Down Expand Up @@ -46,7 +46,7 @@ const bot = new Wechaty({
* https://github.com/wechaty/wechaty-puppet/wiki/Directory
*/

puppet: 'wechaty-puppet-hostie',
puppet: 'wechaty-puppet-service',
})

const chatoperaConfig: WechatyChatoperaConfig = {
Expand Down
63 changes: 37 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,54 @@
"name": "wechaty-chatopera",
"version": "0.4.1",
"description": "Chatopera Plugin for Wechaty, deliver chatbots in low-code way. <https://bot.chatopera.com>",
"directories": {
"doc": "docs",
"example": "examples",
"test": "tests"
"type": "module",
"exports": {
".": {
"import": "./dist/esm/src/mod.js",
"require": "./dist/cjs/src/mod.js"
}
},
"typings": "./dist/esm/src/mod.d.ts",
"engines": {
"node": ">=16",
"npm": ">=7",
"wechaty": ">=0.77"
},
"dependencies": {
"@chatopera/sdk": "^2.8.0",
"language-monitor": "^1.0.3",
"wechaty-plugin-contrib": "^0.14.2"
"wechaty-plugin-contrib": "^0.17.2"
},
"devDependencies": {
"@chatie/eslint-config": "^0.12.1",
"@chatie/eslint-config": "^0.16.2",
"@chatie/git-scripts": "^0.6.2",
"@chatie/semver": "^0.4.7",
"@chatie/tsconfig": "^0.10.1",
"@types/body-parser": "^1.19.0",
"@types/express": "^4.17.6",
"pkg-jq": "^0.2.4",
"shx": "^0.3.2",
"tstest": "^0.4.10",
"wechaty": "^0.50.5",
"wechaty-puppet-mock": "^0.28.2",
"wechaty-vorpal": "^0.6.11"
"@chatie/tsconfig": "^0.20.6",
"@types/body-parser": "^1.19.1",
"@types/express": "^4.17.13",
"cross-env": "^7.0.3",
"npm-run-all": "^4.1.5",
"pkg-jq": "^0.2.11",
"shx": "^0.3.3",
"tstest": "^0.7.3",
"typescript": "^4.4.4",
"wechaty": "^0.77",
"wechaty-puppet-mock": "^0.35"
},
"main": "dist/src/mod.js",
"typings": "dist/src/mod.d.ts",
"scripts": {
"build": "tsc && tsc -p tsconfig.cjs.json",
"clean": "shx rm -fr dist/*",
"dist": "npm run clean && tsc",
"pack": "npm pack",
"lint": "npm run lint:es && npm run lint:ts && npm run lint:md",
"dist": "npm-run-all clean build dist:commonjs",
"dist:commonjs": "jq -n \"{ type: \\\"commonjs\\\" }\" > dist/cjs/package.json",
"lint": "npm-run-all lint:es lint:ts 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\" node 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\"",
"lint:es": "eslint --fix --ignore-pattern tests/fixtures/ \"{bin,examples,scripts,src,tests}/**/*.ts\"",
"prepublish": "npm run dist"
"test:unit": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" tap \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\"",
"lint:es": "eslint --fix --ignore-pattern tests/fixtures/ \"{bin,examples,scripts,src,tests}/**/*.ts\""
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -77,6 +85,9 @@
"dist/",
"src/"
],
"tap": {
"check-coverage": false
},
"publishConfig": {
"tag": "next"
},
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.

44 changes: 39 additions & 5 deletions scripts/npm-pack-testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,66 @@ 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"
trap "rm -fr '$TMPDIR'" EXIT

mv ./*-*.*.*.tgz "$TMPDIR"
cp tests/fixtures/smoke-testing.ts "$TMPDIR"

cd $TMPDIR

npm init -y
npm install *-*.*.*.tgz \
@chatie/tsconfig \
npm install --production ./*-*.*.*.tgz \
@chatie/tsconfig@$NPM_TAG \
pkg-jq \
"wechaty@$NPM_TAG" \

./node_modules/.bin/tsc \
#
# CommonJS
#
npx 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"'

npx 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
8 changes: 5 additions & 3 deletions src/asker.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 { asker } from './asker'
// import { normalizeConfig } from './normalize-config'
// import { asker } from './asker.js'
// import { normalizeConfig } from './normalize-config.js'

// test('asker()', async t => {
// // use our normalizeConfig() helper function to get the config:
Expand All @@ -17,3 +17,5 @@
// answers = await ask('中文', 'test-user-id')
// t.equal(answers.length, 0, 'should get no answer for 中文')
// })

export {}
Loading