Skip to content

Commit

Permalink
Merge pull request #33 from wechaty/esm
Browse files Browse the repository at this point in the history
Enable ESM with Wechaty v0.77
  • Loading branch information
huan authored Oct 18, 2021
2 parents 3148579 + b77b08c commit 983706d
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 82 deletions.
File renamed without changes.
20 changes: 14 additions & 6 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ jobs:
name: Build
strategy:
matrix:
os: [ubuntu-latest]
node: [12]
os:
- ubuntu-latest
node-version:
- 16

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

- name: Install Dependencies
run: npm install
Expand All @@ -34,9 +38,11 @@ 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
Expand All @@ -56,10 +62,12 @@ 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
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![NPM Version](https://img.shields.io/npm/v/wechaty-qnamaker?color=brightgreen)](https://www.npmjs.com/package/wechaty-qnamaker)
[![NPM](https://github.com/wechaty/wechaty-qnamaker/workflows/NPM/badge.svg)](https://github.com/wechaty/wechaty-qnamaker/actions?query=workflow%3ANPM)
[![ES Modules](https://img.shields.io/badge/ES-Modules-brightgreen)](https://github.com/Chatie/tsconfig/issues/16)

[QnA Maker](https://qnamaker.ai) is a cloud-based Natural Language Processing (NLP) service that easily creates a natural conversational layer over your data.

Expand Down Expand Up @@ -136,7 +137,9 @@ Our Friday BOT are using `wechaty-qnamaker` to connect our WeChat conversations

## HISTORY

### master
### master v0.7 (Oct 2022)

1. Enable ESM module

### v0.6 (Aut 5, 2020)

Expand Down
68 changes: 38 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
{
"name": "wechaty-qnamaker",
"version": "0.6.2",
"version": "0.7.1",
"description": "QnAMaker.ai Plugin for Wechaty",
"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"
},
"dependencies": {
"@azure/cognitiveservices-qnamaker-runtime": "^1.0.0",
"@azure/ms-rest-js": "^2.0.7",
"@azure/cognitiveservices-qnamaker-runtime": "^1.0.1",
"@azure/ms-rest-js": "^2.6.0",
"language-monitor": "^1.0.3",
"wechaty-plugin-contrib": "^0.14.2"
},
"peerDependencies": {
"wechaty-vorpal": "*"
"wechaty-plugin-contrib": "^0.19.4"
},
"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.60.9",
"wechaty-vorpal": "^0.6.11",
"wechaty-puppet-mock": "^0.28.2"
"@chatie/tsconfig": "^0.20.6",
"@types/body-parser": "^1.19.1",
"@types/express": "^4.17.13",
"pkg-jq": "^0.2.11",
"shx": "^0.3.3",
"tstest": "^0.7.3",
"wechaty": "^0.77.2",
"wechaty-puppet-mock": "^0.35.8",
"wechaty-vorpal": "^0.9.2"
},
"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\"",
"test:unit": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" tap \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\"",
"lint:es": "eslint --ignore-pattern tests/fixtures/ '{bin,examples,scripts,src,tests}/**/*.ts'"
},
"repository": {
Expand All @@ -67,6 +70,11 @@
"pre-push": "npx git-scripts-pre-push"
}
},
"files": [
"bin/",
"dist/",
"src/"
],
"publishConfig": {
"tag": "next"
},
Expand Down
42 changes: 38 additions & 4 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"
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@$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
10 changes: 5 additions & 5 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 { 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 @@ -16,7 +16,7 @@ test('asker()', async t => {
const ask = asker(config)

let answers = await ask('wechaty')
t.true(answers[0].answer, 'should get answer back: ' + answers[0].answer)
t.ok(answers[0]!.answer, 'should get answer back: ' + answers[0]!.answer)

answers = await ask('中文')
t.equal(answers.length, 0, 'should get no answer for 中文')
Expand Down
7 changes: 4 additions & 3 deletions src/asker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import * as runtime from '@azure/cognitiveservices-qnamaker-runtime'

import { log } from 'wechaty'

import { QnAMakerOptions } from './qnamaker'
import { QueryDTO } from '@azure/cognitiveservices-qnamaker-runtime/esm/models'
import type { QnAMakerOptions } from './qnamaker.js'
// eslint-disable-next-line import/extensions
import type { QueryDTO } from '@azure/cognitiveservices-qnamaker-runtime/esm/models'

function asker (options: QnAMakerOptions) {
log.verbose('WechatyQnAMaker', 'asker(%s)', JSON.stringify(options))
Expand Down Expand Up @@ -41,7 +42,7 @@ function asker (options: QnAMakerOptions) {
const requestQuery = await runtimeClient.runtime.generateAnswer(
options.knowledgeBaseId,
normalizedQueryDto,
{ customHeaders }
{ customHeaders },
)
// console.info(JSON.stringify(requestQuery))

Expand Down
10 changes: 6 additions & 4 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export { VERSION } from './version'
export { VERSION } from './version.js'
export type {
WechatyQnAMakerConfig,
} from './plugin.js'
export {
WechatyQnAMaker,
WechatyQnAMakerConfig,
} from './plugin'
} from './plugin.js'
export {
Faq,
} from './vorpal'
} from './vorpal.js'
4 changes: 2 additions & 2 deletions src/normalize-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { WechatyQnAMakerConfig } from './plugin'
import type { WechatyQnAMakerConfig } from './plugin.js'
import {
QnAMakerOptions,
DEFAULT_SCORE_THRESHOLD,
} from './qnamaker'
} from './qnamaker.js'

function normalizeConfig (config: WechatyQnAMakerConfig): QnAMakerOptions {
const WECHATY_PLUGIN_QNAMAKER_ENDPOINT_KEY = 'WECHATY_PLUGIN_QNAMAKER_ENDPOINT_KEY'
Expand Down
10 changes: 5 additions & 5 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
matchers,
} from 'wechaty-plugin-contrib'

import { asker } from './asker'
import { normalizeConfig } from './normalize-config'
import { mentionMatcher } from './mention-matcher'
import { asker } from './asker.js'
import { normalizeConfig } from './normalize-config.js'
import { mentionMatcher } from './mention-matcher.js'

import { QnAMakerOptions } from './qnamaker'
import type { QnAMakerOptions } from './qnamaker.js'

interface WechatyQnAMakerConfigMatcher {
contact? : matchers.ContactMatcherOptions,
Expand Down Expand Up @@ -111,7 +111,7 @@ function WechatyQnAMaker (config: WechatyQnAMakerConfig): WechatyPlugin {
if (!text) { return }

const answers = await ask(text)
if (answers.length <= 0) { return }
if (answers.length <= 0 || !answers[0]) { return }

const answer = answers[0].answer
if (!answer) {
Expand Down
2 changes: 1 addition & 1 deletion src/qnamaker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { matchers } from 'wechaty-plugin-contrib'
import type { matchers } from 'wechaty-plugin-contrib'

export interface QnAMakerOptions {
endpointKey : string
Expand Down
6 changes: 3 additions & 3 deletions src/version.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/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 { VERSION } from './version'
import { VERSION } from './version.js'

test('Make sure the VERSION is fresh in source code', async t => {
t.equal(VERSION, '0.0.0', 'version should be 0.0.0 in source code, only updated before publish to NPM')
Expand Down
Loading

0 comments on commit 983706d

Please sign in to comment.