From 0471d6362ca992f8275aa1170b9e22b748b5139e Mon Sep 17 00:00:00 2001 From: Huan Date: Sun, 12 Sep 2021 17:12:33 +0800 Subject: [PATCH 1/2] enable ESM --- .eslintrc.js => .eslintrc.cjs | 0 .github/workflows/npm.yml | 30 ++++++++----- README.md | 6 ++- examples/ding-dong-bot.ts | 2 +- examples/redux-ducks-bot/ha-environment.ts | 6 +-- examples/redux-ducks-bot/redux-ducks-bot.ts | 6 +-- examples/redux/main.ts | 8 ++-- examples/redux/root-action.ts | 2 +- examples/redux/root-epic.ts | 2 +- examples/redux/root-reducer.ts | 2 +- examples/remote-devtools.ts | 6 +-- package.json | 43 +++++++++++++------ scripts/generate-package-json.sh | 20 +++++++++ scripts/generate-version.sh | 18 -------- scripts/npm-pack-testing.sh | 43 +++++++++++++++++-- scripts/update-license.ts | 2 +- src/config.ts | 10 ++++- src/configure-ha.ts | 10 ++--- src/duck/actions.ts | 2 +- src/duck/ducks.ts | 4 +- src/duck/epics/ding.epic.ts | 4 +- src/duck/epics/dong.epic.ts | 8 ++-- src/duck/epics/failure-ha.epic.ts | 6 +-- src/duck/epics/failure-wechaty.epic.ts | 4 +- src/duck/epics/main.epic.ts | 4 +- src/duck/epics/mod.ts | 12 +++--- src/duck/epics/operators/mod.ts | 4 +- src/duck/epics/operators/take-until-dong.ts | 4 +- .../epics/operators/take-until-loginout.ts | 2 +- .../epics/pipes/ding-emitter-per-wechaty.ts | 10 ++--- src/duck/epics/pipes/mod.ts | 10 ++--- src/duck/epics/pipes/recover-ha.ts | 6 +-- src/duck/epics/pipes/recover-wechaty.ts | 11 ++--- .../epics/pipes/reset-emitter-per-wechaty.ts | 8 ++-- src/duck/epics/pipes/wechaty-message.ts | 6 +-- src/duck/epics/recover.epic.ts | 6 +-- src/duck/mod.ts | 16 +++---- src/duck/operations.ts | 8 ++-- src/duck/reducers.ts | 4 +- src/duck/selectors.spec.ts | 6 +-- src/duck/selectors.ts | 4 +- src/duck/tests.spec.ts | 6 ++- src/duck/utils.spec.ts | 4 +- src/duck/utils.ts | 8 ++-- src/get-wechaty-options-from-env.spec.ts | 6 +-- src/global-instance-manager.ts | 2 +- src/ha-wechaty.spec.ts | 8 ++-- src/ha-wechaty.ts | 14 +++--- src/heartbeat$.spec.ts | 6 ++- src/index.spec.ts | 8 ++-- src/mod.ts | 10 ++--- src/package-json.spec.ts | 10 +++++ src/package-json.ts | 11 +++++ src/version.spec.ts | 27 ------------ src/version.ts | 21 --------- tests/fixtures/smoke-testing.ts | 2 +- tests/integration.spec.ts | 8 ++-- tsconfig.cjs.json | 7 +++ tsconfig.json | 2 +- 59 files changed, 293 insertions(+), 232 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) create mode 100755 scripts/generate-package-json.sh delete mode 100755 scripts/generate-version.sh create mode 100755 src/package-json.spec.ts create mode 100644 src/package-json.ts delete mode 100644 src/version.spec.ts delete mode 100644 src/version.ts create mode 100644 tsconfig.cjs.json diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 100% rename from .eslintrc.js rename to .eslintrc.cjs diff --git a/.github/workflows/npm.yml b/.github/workflows/npm.yml index 2fbadad..1efea6a 100644 --- a/.github/workflows/npm.yml +++ b/.github/workflows/npm.yml @@ -7,16 +7,20 @@ jobs: name: Build strategy: matrix: - os: [ubuntu-latest] - node: [14] + 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 @@ -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: 14 + 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 @@ -52,16 +58,18 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v2 with: - node-version: 14 + 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 diff --git a/README.md b/README.md index a93ade3..2537a62 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,11 @@ See issue: Backoff straitegy [#2](/~https://github.com/wechaty/ha-wechaty/issues/2 ## History -### master (v0.7) +### master v0.9 (Sep 12, 2021) + +1. ES Modules supported + +### v0.7 (Jun 4, 2021) #### BREAKING CHANGE diff --git a/examples/ding-dong-bot.ts b/examples/ding-dong-bot.ts index 8f6e27b..7c01f13 100644 --- a/examples/ding-dong-bot.ts +++ b/examples/ding-dong-bot.ts @@ -24,7 +24,7 @@ import { } from 'wechaty-plugin-contrib' import { log } from 'wechaty' -import { configureHa } from '../src/mod' +import { configureHa } from '../src/mod.js' import dotenv from 'dotenv' dotenv.config() diff --git a/examples/redux-ducks-bot/ha-environment.ts b/examples/redux-ducks-bot/ha-environment.ts index 28ddf6e..7a834b0 100644 --- a/examples/redux-ducks-bot/ha-environment.ts +++ b/examples/redux-ducks-bot/ha-environment.ts @@ -1,14 +1,14 @@ import { Message, } from 'wechaty' -import { +import type { mock, } from 'wechaty-puppet-mock' -import { ContactMock } from 'wechaty-puppet-mock/dist/src/mock/mod' +import type { ContactMock } from 'wechaty-puppet-mock/dist/src/mock/mod' import { CHATIE_OA_ID, -} from '../../src/config' +} from '../../src/config.js' const HaEnvironment = (): mock.EnvironmentMock => { diff --git a/examples/redux-ducks-bot/redux-ducks-bot.ts b/examples/redux-ducks-bot/redux-ducks-bot.ts index 583ecf6..822fe87 100644 --- a/examples/redux-ducks-bot/redux-ducks-bot.ts +++ b/examples/redux-ducks-bot/redux-ducks-bot.ts @@ -43,10 +43,10 @@ import { Counter as CounterDuck } from 'wechaty-ducks-contrib' import { HAWechaty, Duck as HaDuck, -} from '../../src/mod' +} from '../../src/mod.js' -import { HaEnvironment } from './ha-environment' -// import { CHATIE_OA_ID } from '../../src/config' +import { HaEnvironment } from './ha-environment.js' +// import { CHATIE_OA_ID } from '../../src/config.js' const ducks = new Ducks({ counter : CounterDuck, diff --git a/examples/redux/main.ts b/examples/redux/main.ts index e819bb7..9a128f8 100644 --- a/examples/redux/main.ts +++ b/examples/redux/main.ts @@ -31,7 +31,7 @@ import { createEpicMiddleware, Epic, } from 'redux-observable' -import { +import type { // ActionType, // StateType, RootState, @@ -39,9 +39,9 @@ import { // createAsyncAction, } from 'typesafe-actions' -// import rootAction from './root-action' -import rootReducer from './root-reducer' -import rootEpic from './root-epic' +// import rootAction from './root-action.js' +import rootReducer from './root-reducer.js' +import rootEpic from './root-epic.js' export interface Dependency {} const dependencies = {} as Dependency diff --git a/examples/redux/root-action.ts b/examples/redux/root-action.ts index 9287d7e..1338c20 100644 --- a/examples/redux/root-action.ts +++ b/examples/redux/root-action.ts @@ -20,7 +20,7 @@ import { Duck as WechatyDuck } from 'wechaty-redux' import { Counter as CounterDuck } from 'wechaty-ducks-contrib' -import * as HaDuck from '../../src/duck/mod' +import * as HaDuck from '../../src/duck/mod.js' export default { counter : CounterDuck.actions, diff --git a/examples/redux/root-epic.ts b/examples/redux/root-epic.ts index 3d188bd..5148dab 100644 --- a/examples/redux/root-epic.ts +++ b/examples/redux/root-epic.ts @@ -22,7 +22,7 @@ import { combineEpics } from 'redux-observable' import { Duck as WechatyDuck } from 'wechaty-redux' import { Counter as CounterDuck } from 'wechaty-ducks-contrib' -import * as HaDuck from '../../src/duck/mod' +import * as HaDuck from '../../src/duck/mod.js' export default combineEpics( ...Object.values(HaDuck.epics), diff --git a/examples/redux/root-reducer.ts b/examples/redux/root-reducer.ts index 5c67948..e72c51b 100644 --- a/examples/redux/root-reducer.ts +++ b/examples/redux/root-reducer.ts @@ -22,7 +22,7 @@ import { combineReducers } from 'redux' import { Duck as WechatyDuck } from 'wechaty-redux' import { Counter as CounterDuck } from 'wechaty-ducks-contrib' -import * as HaDuck from '../../src/duck/mod' +import * as HaDuck from '../../src/duck/mod.js' export default combineReducers({ counter : CounterDuck.default, diff --git a/examples/remote-devtools.ts b/examples/remote-devtools.ts index 1ef8f4a..040fe9e 100644 --- a/examples/remote-devtools.ts +++ b/examples/remote-devtools.ts @@ -39,10 +39,10 @@ import { Counter as CounterDuck } from 'wechaty-ducks-contrib' import { Duck as HaDuck, configureHa, -} from '../src/mod' +} from '../src/mod.js' -import { HaEnvironment } from './redux-ducks-bot/ha-environment' -// import { CHATIE_OA_ID } from '../../src/config' +import { HaEnvironment } from './redux-ducks-bot/ha-environment.js' +// import { CHATIE_OA_ID } from '../../src/config.js' const ducks = new Ducks({ counter : CounterDuck, diff --git a/package.json b/package.json index 95508d4..0d5ed41 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,18 @@ { "name": "ha-wechaty", - "version": "0.7.6", + "version": "0.9.0", "description": "HAWechaty is a Load Balance for providing High Availability for Wechaty Chatbot by spreading requests across multiple WeChat individual accounts.", - "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": { + "wechaty": ">=0.69", + "wechaty-puppet": ">=0.43", "node": ">=14" }, "directories": { @@ -13,19 +21,20 @@ "test": "tests" }, "scripts": { + "build": "tsc && tsc -p tsconfig.cjs.json", "clean": "shx rm -fr dist/*", - "dist": "npm run clean && tsc", + "dist": "npm-run-all clean build dist:commonjs", + "dist:commonjs": "jq -n \"{ type: \\\"commonjs\\\" }\" > dist/cjs/package.json", "example": "ts-node examples/ding-dong-bot.ts", - "pack": "npm pack", "lint": "npm run lint:es && npm run lint:ts && npm run lint:md", "lint:md": "markdownlint README.md", - "lint:ts": "tsc --noEmit", + "lint:ts": "tsc --isolatedModules --noEmit", "redux-devtools": "redux-devtools --hostname=localhost --port=8000", - "start": "ts-node --files examples/ding-dong-bot.ts", - "redux": "ts-node --files examples/redux-ducks-bot/redux-ducks-bot.ts", + "start": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" node examples/ding-dong-bot.ts", + "redux": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" node examples/redux-ducks-bot/redux-ducks-bot.ts", "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\" \"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 fixtures/ 'src/**/*.ts' 'tests/**/*.ts'" }, "dependencies": { @@ -41,22 +50,24 @@ "wechaty": ">=0.61.9" }, "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.17.1", + "@chatie/tsconfig": "^0.20.2", "@types/cuid": "^1.3.1", "@types/glob": "^7.1.3", "@types/redux-mock-store": "^1.0.2", "@types/remote-redux-devtools": "^0.5.4", "dotenv": "^10.0.0", "glob": "^7.1.7", + "npm-run-all": "^4.1.5", "pkg-jq": "^0.2.11", "redux-devtools-cli": "1.0.0-4", "redux-mock-store": "^1.5.4", "remote-redux-devtools": "^0.5.16", "shx": "^0.3.3", - "tstest": "^0.4.10", + "tstest": "^0.5.16", + "typescript": "^4.4.3", "utility-types": "^3.10.0", "wechaty": "^0.61.9", "wechaty-ducks-contrib": "^0.2.2", @@ -84,6 +95,14 @@ "access": "public", "tag": "next" }, + "files": [ + "bin/", + "dist/", + "src/" + ], + "tap": { + "check-coverage": false + }, "git": { "scripts": { "pre-push": "npx git-scripts-pre-push" diff --git a/scripts/generate-package-json.sh b/scripts/generate-package-json.sh new file mode 100755 index 0000000..790f924 --- /dev/null +++ b/scripts/generate-package-json.sh @@ -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_ diff --git a/scripts/generate-version.sh b/scripts/generate-version.sh deleted file mode 100755 index e8ef7f9..0000000 --- a/scripts/generate-version.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash -set -e - -SRC_VERSION_TS_FILE='src/version.ts' - -[ -f ${SRC_VERSION_TS_FILE} ] || { - echo ${SRC_VERSION_TS_FILE}" not found" - exit 1 -} - -VERSION=$(npx pkg-jq -r .version) - -cat <<_SRC_ > ${SRC_VERSION_TS_FILE} -/** - * This file was auto generated from scripts/generate-version.sh - */ -export const VERSION: string = '${VERSION}' -_SRC_ diff --git a/scripts/npm-pack-testing.sh b/scripts/npm-pack-testing.sh index 7449ff3..0a85b58 100755 --- a/scripts/npm-pack-testing.sh +++ b/scripts/npm-pack-testing.sh @@ -3,14 +3,14 @@ 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" @@ -18,17 +18,52 @@ 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 diff --git a/scripts/update-license.ts b/scripts/update-license.ts index 6607368..54cd416 100644 --- a/scripts/update-license.ts +++ b/scripts/update-license.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - /~https://github.com/wechaty diff --git a/src/config.ts b/src/config.ts index bd53fc1..0c10efe 100644 --- a/src/config.ts +++ b/src/config.ts @@ -17,10 +17,16 @@ * limitations under the License. * */ -export { log } from 'wechaty' +import { packageJson } from './package-json.js' + +const VERSION = packageJson.version || '0.0.0' -export { VERSION } from './version' +export { log } from 'wechaty' export const CHATIE_OA_ID = 'gh_051c89260e5d' // chatieio official account export const DING = 'ding' export const DONG = 'dong' + +export { + VERSION, +} diff --git a/src/configure-ha.ts b/src/configure-ha.ts index e86060f..09f95a2 100644 --- a/src/configure-ha.ts +++ b/src/configure-ha.ts @@ -21,10 +21,10 @@ import { createStore, compose, } from 'redux' -import { RemoteReduxDevToolsOptions } from 'remote-redux-devtools' +import type { RemoteReduxDevToolsOptions } from 'remote-redux-devtools' import { Ducks } from 'ducks' -import { DucksMapObject } from 'ducks/dist/src/duck' +import type { DucksMapObject } from 'ducks/dist/src/duck' import { Duck as WechatyDuck } from 'wechaty-redux' import { log, @@ -32,9 +32,9 @@ import { Wechaty, } from 'wechaty' -import * as HaDuck from './duck/mod' -import { HAWechaty } from './ha-wechaty' -import { getWechatyOptionsListFromEnv } from './get-wechaty-options-from-env' +import * as HaDuck from './duck/mod.js' +import { HAWechaty } from './ha-wechaty.js' +import { getWechatyOptionsListFromEnv } from './get-wechaty-options-from-env.js' let initialized = false diff --git a/src/duck/actions.ts b/src/duck/actions.ts index e65b96f..57f1e9c 100644 --- a/src/duck/actions.ts +++ b/src/duck/actions.ts @@ -19,7 +19,7 @@ */ import { createAction } from 'typesafe-actions' -import * as types from './types' +import * as types from './types.js' const prepareHa = (haId: string) => ({ haId }) const prepareWechaty = (wechatyId: string) => ({ wechatyId }) diff --git a/src/duck/ducks.ts b/src/duck/ducks.ts index 19474f7..3972d74 100644 --- a/src/duck/ducks.ts +++ b/src/duck/ducks.ts @@ -1,9 +1,9 @@ -import { +import type { Bundle, Ducks, } from 'ducks' -import * as haDuck from './mod' +import * as haDuck from './mod.js' let instance: Ducks const getBundle = () => instance.ducksify(haDuck) as Bundle diff --git a/src/duck/epics/ding.epic.ts b/src/duck/epics/ding.epic.ts index 5c8a147..84e8c76 100644 --- a/src/duck/epics/ding.epic.ts +++ b/src/duck/epics/ding.epic.ts @@ -30,13 +30,13 @@ import { filter, mergeMap, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' import { getWechaty, Duck as WechatyDuck, } from 'wechaty-redux' -import * as actions from '../actions' +import * as actions from '../actions.js' const DING = 'ding' diff --git a/src/duck/epics/dong.epic.ts b/src/duck/epics/dong.epic.ts index 8500bcc..e8a1d2d 100644 --- a/src/duck/epics/dong.epic.ts +++ b/src/duck/epics/dong.epic.ts @@ -17,7 +17,7 @@ * limitations under the License. * */ -import { Message } from 'wechaty' +import type { Message } from 'wechaty' import { isActionOf } from 'typesafe-actions' import { filter, @@ -25,7 +25,7 @@ import { mergeMap, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' import { Duck as WechatyDuck, @@ -33,9 +33,9 @@ import { import { DONG, -} from '../../config' +} from '../../config.js' -import * as actions from '../actions' +import * as actions from '../actions.js' const messageToDong = (message: Message) => actions.dongHa(message.wechaty.id, message.id) diff --git a/src/duck/epics/failure-ha.epic.ts b/src/duck/epics/failure-ha.epic.ts index e9d26eb..82ad884 100644 --- a/src/duck/epics/failure-ha.epic.ts +++ b/src/duck/epics/failure-ha.epic.ts @@ -25,11 +25,11 @@ import { map, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' -import * as actions from '../actions' +import * as actions from '../actions.js' -import { getBundle } from '../ducks' +import { getBundle } from '../ducks.js' type FailureWechatyAction = ReturnType diff --git a/src/duck/epics/failure-wechaty.epic.ts b/src/duck/epics/failure-wechaty.epic.ts index 5628d42..510ab82 100644 --- a/src/duck/epics/failure-wechaty.epic.ts +++ b/src/duck/epics/failure-wechaty.epic.ts @@ -23,11 +23,11 @@ import { filter, map, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' import { Duck as WechatyDuck } from 'wechaty-redux' -import * as actions from '../actions' +import * as actions from '../actions.js' /** * diff --git a/src/duck/epics/main.epic.ts b/src/duck/epics/main.epic.ts index 0448a53..c8a7053 100644 --- a/src/duck/epics/main.epic.ts +++ b/src/duck/epics/main.epic.ts @@ -24,13 +24,13 @@ import { mergeMap, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' import { wechatyMessage$$, dingEmitterPerWechaty$, resetEmitterPerWechaty$, -} from './pipes/mod' +} from './pipes/mod.js' /** * Main Epic at here: diff --git a/src/duck/epics/mod.ts b/src/duck/epics/mod.ts index a9b9cd6..477c964 100644 --- a/src/duck/epics/mod.ts +++ b/src/duck/epics/mod.ts @@ -17,12 +17,12 @@ * limitations under the License. * */ -import { dingEpic } from './ding.epic' -import { dongEpic } from './dong.epic' -import { failureHaEpic } from './failure-ha.epic' -import { failureWechatyEpic } from './failure-wechaty.epic' -import { mainEpic } from './main.epic' -import { recoverEpic } from './recover.epic' +import { dingEpic } from './ding.epic.js' +import { dongEpic } from './dong.epic.js' +import { failureHaEpic } from './failure-ha.epic.js' +import { failureWechatyEpic } from './failure-wechaty.epic.js' +import { mainEpic } from './main.epic.js' +import { recoverEpic } from './recover.epic.js' export { dingEpic, diff --git a/src/duck/epics/operators/mod.ts b/src/duck/epics/operators/mod.ts index 8e7d7b8..5a5e45b 100644 --- a/src/duck/epics/operators/mod.ts +++ b/src/duck/epics/operators/mod.ts @@ -17,8 +17,8 @@ * limitations under the License. * */ -import { takeUntilDong } from './take-until-dong' -import { takeUntilLoginout } from './take-until-loginout' +import { takeUntilDong } from './take-until-dong.js' +import { takeUntilLoginout } from './take-until-loginout.js' export { takeUntilDong, diff --git a/src/duck/epics/operators/take-until-dong.ts b/src/duck/epics/operators/take-until-dong.ts index ecbee4d..50c09b9 100644 --- a/src/duck/epics/operators/take-until-dong.ts +++ b/src/duck/epics/operators/take-until-dong.ts @@ -24,9 +24,9 @@ import { filter, takeUntil, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' -import * as HaDuck from '../../mod' +import * as HaDuck from '../../mod.js' /** * Huan(202004): diff --git a/src/duck/epics/operators/take-until-loginout.ts b/src/duck/epics/operators/take-until-loginout.ts index 9656c2f..4d10603 100644 --- a/src/duck/epics/operators/take-until-loginout.ts +++ b/src/duck/epics/operators/take-until-loginout.ts @@ -24,7 +24,7 @@ import { filter, takeUntil, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' import { Duck as WechatyDuck, diff --git a/src/duck/epics/pipes/ding-emitter-per-wechaty.ts b/src/duck/epics/pipes/ding-emitter-per-wechaty.ts index fe3f249..1647351 100644 --- a/src/duck/epics/pipes/ding-emitter-per-wechaty.ts +++ b/src/duck/epics/pipes/ding-emitter-per-wechaty.ts @@ -29,25 +29,25 @@ import { switchMap, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' import { CHATIE_OA_ID, -} from '../../../config' +} from '../../../config.js' import { dingHa, -} from '../../actions' +} from '../../actions.js' import { takeUntilDong, takeUntilLoginout, -} from '../operators/mod' +} from '../operators/mod.js' import { GroupedMessageByWechaty, DING_WAIT_MILLISECONDS, -} from './wechaty-message' +} from './wechaty-message.js' /** * In: wechatyMessage diff --git a/src/duck/epics/pipes/mod.ts b/src/duck/epics/pipes/mod.ts index 0e18e55..31d3e73 100644 --- a/src/duck/epics/pipes/mod.ts +++ b/src/duck/epics/pipes/mod.ts @@ -17,11 +17,11 @@ * limitations under the License. * */ -import { dingEmitterPerWechaty$ } from './ding-emitter-per-wechaty' -import { recoverHa$ } from './recover-ha' -import { recoverWechaty$ } from './recover-wechaty' -import { resetEmitterPerWechaty$ } from './reset-emitter-per-wechaty' -import { wechatyMessage$$ } from './wechaty-message' +import { dingEmitterPerWechaty$ } from './ding-emitter-per-wechaty.js' +import { recoverHa$ } from './recover-ha.js' +import { recoverWechaty$ } from './recover-wechaty.js' +import { resetEmitterPerWechaty$ } from './reset-emitter-per-wechaty.js' +import { wechatyMessage$$ } from './wechaty-message.js' export { dingEmitterPerWechaty$, diff --git a/src/duck/epics/pipes/recover-ha.ts b/src/duck/epics/pipes/recover-ha.ts index 5289c2c..7b46237 100644 --- a/src/duck/epics/pipes/recover-ha.ts +++ b/src/duck/epics/pipes/recover-ha.ts @@ -22,10 +22,10 @@ import { EMPTY, } from 'rxjs' -import { Duck as WechatyDuck } from 'wechaty-redux' +import type { Duck as WechatyDuck } from 'wechaty-redux' -import { getBundle } from '../../ducks' -import * as actions from '../../actions' +import { getBundle } from '../../ducks.js' +import * as actions from '../../actions.js' type RecoverAction = ReturnType | ReturnType diff --git a/src/duck/epics/pipes/recover-wechaty.ts b/src/duck/epics/pipes/recover-wechaty.ts index 239ef1f..a97dc20 100644 --- a/src/duck/epics/pipes/recover-wechaty.ts +++ b/src/duck/epics/pipes/recover-wechaty.ts @@ -19,12 +19,13 @@ */ import { of, - empty, + // empty, + EMPTY, } from 'rxjs' -import { Duck as WechatyDuck } from 'wechaty-redux' +import type { Duck as WechatyDuck } from 'wechaty-redux' -import { getBundle } from '../../ducks' -import * as actions from '../../actions' +import { getBundle } from '../../ducks.js' +import * as actions from '../../actions.js' type RecoverAction = ReturnType | ReturnType @@ -33,7 +34,7 @@ const recoverWechaty$ = (action: RecoverAction) => { * Need not recovery because it's available */ if (getBundle().selectors.isWechatyAvailable(action.payload.wechatyId)) { - return empty() + return EMPTY // empty() } /** diff --git a/src/duck/epics/pipes/reset-emitter-per-wechaty.ts b/src/duck/epics/pipes/reset-emitter-per-wechaty.ts index b08a868..facd7bf 100644 --- a/src/duck/epics/pipes/reset-emitter-per-wechaty.ts +++ b/src/duck/epics/pipes/reset-emitter-per-wechaty.ts @@ -29,7 +29,7 @@ import { switchMap, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' import { Duck as WechatyDuck, @@ -37,17 +37,17 @@ import { import { failureWechaty, -} from '../../actions' +} from '../../actions.js' import { takeUntilDong, takeUntilLoginout, -} from '../operators/mod' +} from '../operators/mod.js' import { GroupedMessageByWechaty, RESET_WAIT_MILLISECONDS, -} from './wechaty-message' +} from './wechaty-message.js' /** * In: wechatyMessage$ diff --git a/src/duck/epics/pipes/wechaty-message.ts b/src/duck/epics/pipes/wechaty-message.ts index 92002f8..8e714b9 100644 --- a/src/duck/epics/pipes/wechaty-message.ts +++ b/src/duck/epics/pipes/wechaty-message.ts @@ -28,7 +28,7 @@ import { merge, of, } from 'rxjs' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' import { Duck as WechatyDuck, @@ -36,11 +36,11 @@ import { import { milliAroundSeconds, -} from '../../utils' +} from '../../utils.js' import { takeUntilLoginout, -} from '../operators/mod' +} from '../operators/mod.js' const DING_WAIT_MILLISECONDS = () => milliAroundSeconds(60) const RESET_WAIT_MILLISECONDS = () => milliAroundSeconds(120) diff --git a/src/duck/epics/recover.epic.ts b/src/duck/epics/recover.epic.ts index 59fe0e5..d1a4e09 100644 --- a/src/duck/epics/recover.epic.ts +++ b/src/duck/epics/recover.epic.ts @@ -25,16 +25,16 @@ import { mergeMap, filter, } from 'rxjs/operators' -import { Epic } from 'redux-observable' +import type { Epic } from 'redux-observable' import { Duck as WechatyDuck } from 'wechaty-redux' -import * as actions from '../actions' +import * as actions from '../actions.js' import { recoverWechaty$, recoverHa$, -} from './pipes/mod' +} from './pipes/mod.js' /** * In: actions.dongHA diff --git a/src/duck/mod.ts b/src/duck/mod.ts index a7e7a8b..ccb1c2f 100644 --- a/src/duck/mod.ts +++ b/src/duck/mod.ts @@ -17,16 +17,16 @@ * limitations under the License. * */ -import reducer from './reducers' +import reducer from './reducers.js' -import * as actions from './actions' -import * as epics from './epics/mod' -import * as operations from './operations' -import * as selectors from './selectors' -import * as types from './types' -import * as utils from './utils' +import * as actions from './actions.js' +import * as epics from './epics/mod.js' +import * as operations from './operations.js' +import * as selectors from './selectors.js' +import * as types from './types.js' +import * as utils from './utils.js' -import { setDucks } from './ducks' +import { setDucks } from './ducks.js' export { actions, diff --git a/src/duck/operations.ts b/src/duck/operations.ts index 4eed172..0d40476 100644 --- a/src/duck/operations.ts +++ b/src/duck/operations.ts @@ -17,12 +17,12 @@ * limitations under the License. * */ -import { Dispatch } from 'redux' -import { Wechaty } from 'wechaty' +import type { Dispatch } from 'redux' +import type { Wechaty } from 'wechaty' -import { HAWechaty } from '../ha-wechaty' +import type { HAWechaty } from '../ha-wechaty.js' -import * as actions from './actions' +import * as actions from './actions.js' const ding = (dispatch: Dispatch) => (haId: string, data: string) => { return dispatch(actions.dingHa(haId, data)) diff --git a/src/duck/reducers.ts b/src/duck/reducers.ts index 5cc481e..b11b5e4 100644 --- a/src/duck/reducers.ts +++ b/src/duck/reducers.ts @@ -21,9 +21,9 @@ import { ActionType, createReducer, } from 'typesafe-actions' -import { DeepReadonly } from 'utility-types' +import type { DeepReadonly } from 'utility-types' -import * as actions from './actions' +import * as actions from './actions.js' const initialState: DeepReadonly<{ availability: { diff --git a/src/duck/selectors.spec.ts b/src/duck/selectors.spec.ts index ddc95e3..8a76449 100644 --- a/src/duck/selectors.spec.ts +++ b/src/duck/selectors.spec.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - /~https://github.com/wechaty @@ -21,8 +21,8 @@ */ import { test } from 'tstest' -import { State } from './reducers' -import * as selectors from './selectors' +import type { State } from './reducers.js' +import * as selectors from './selectors.js' test('isHaAvailable() for empty state', async t => { const state: State = { diff --git a/src/duck/selectors.ts b/src/duck/selectors.ts index ec3190d..2911390 100644 --- a/src/duck/selectors.ts +++ b/src/duck/selectors.ts @@ -19,9 +19,9 @@ */ import { Wechaty } from 'wechaty' -import { HAWechaty } from '../mod' +import { HAWechaty } from '../mod.js' -import { State } from './reducers' +import type { State } from './reducers.js' const isWechatyAvailable = (state: State) => (wechatyOrId: string | Wechaty): boolean => { if (wechatyOrId instanceof Wechaty) { diff --git a/src/duck/tests.spec.ts b/src/duck/tests.spec.ts index f587e3d..c3aee1d 100644 --- a/src/duck/tests.spec.ts +++ b/src/duck/tests.spec.ts @@ -27,8 +27,8 @@ // import rxjs from 'rxjs' // import operators from 'rxjs/operators' -// import actions from './actions' -// import epics from './epics' +// import actions from './actions.js' +// import epics from './epics.js' // // void TestScheduler // // void Wechaty @@ -148,3 +148,5 @@ // t.deepEqual(actions, [expectedPayload], 'actions should be expected') // }) // }) + +export {} diff --git a/src/duck/utils.spec.ts b/src/duck/utils.spec.ts index 9cd7ddd..c523f91 100644 --- a/src/duck/utils.spec.ts +++ b/src/duck/utils.spec.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - /~https://github.com/wechaty @@ -23,7 +23,7 @@ import { test } from 'tstest' import { milliAroundSeconds, -} from './utils' +} from './utils.js' test('aroundSeconds()', async t => { const SECONDS = 60 diff --git a/src/duck/utils.ts b/src/duck/utils.ts index e358efc..66a054e 100644 --- a/src/duck/utils.ts +++ b/src/duck/utils.ts @@ -22,7 +22,7 @@ import { Message, } from 'wechaty' -import { +import type { MessageType, } from 'wechaty-puppet' import { @@ -32,16 +32,16 @@ import { import { getHa, -} from '../mod' +} from '../mod.js' import { CHATIE_OA_ID, DONG, log, -} from '../config' +} from '../config.js' // import { // PayloadMessageId, -// } from './schema' +// } from './schema.js' interface PayloadMessageId { wechatyId: string, messageId: string } interface PayloadHaId { haId: string } diff --git a/src/get-wechaty-options-from-env.spec.ts b/src/get-wechaty-options-from-env.spec.ts index e4e274a..cc6e84c 100755 --- a/src/get-wechaty-options-from-env.spec.ts +++ b/src/get-wechaty-options-from-env.spec.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - /~https://github.com/wechaty * @@ -18,9 +18,9 @@ * limitations under the License. * */ -import test from 'tstest' +import { test } from 'tstest' -import { getWechatyOptionsListFromEnv } from './get-wechaty-options-from-env' +import { getWechatyOptionsListFromEnv } from './get-wechaty-options-from-env.js' test('envWechaty() smoke testing', async t => { const ENV = { diff --git a/src/global-instance-manager.ts b/src/global-instance-manager.ts index 3355b0f..c1a398d 100644 --- a/src/global-instance-manager.ts +++ b/src/global-instance-manager.ts @@ -17,7 +17,7 @@ * limitations under the License. * */ -import { HAWechaty } from './ha-wechaty' +import type { HAWechaty } from './ha-wechaty.js' const instances = new Map() diff --git a/src/ha-wechaty.spec.ts b/src/ha-wechaty.spec.ts index c7d48a4..7c54f18 100644 --- a/src/ha-wechaty.spec.ts +++ b/src/ha-wechaty.spec.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - /~https://github.com/wechaty @@ -19,13 +19,13 @@ * limitations under the License. * */ -import test from 'tstest' +import { test } from 'tstest' import { HAWechaty, -} from './ha-wechaty' +} from './ha-wechaty.js' -test('tbw', async (t) => { +test('tbw', async t => { const ha = new HAWechaty({ ducks: { ducksify: (..._: any[]) => ({} as any), diff --git a/src/ha-wechaty.ts b/src/ha-wechaty.ts index 817af52..268eb79 100644 --- a/src/ha-wechaty.ts +++ b/src/ha-wechaty.ts @@ -18,17 +18,17 @@ * */ import { EventEmitter } from 'events' -import { +import type { MemoryCard, Room, Wechaty, WechatyPlugin, Contact, } from 'wechaty' -import { WechatyEventName } from 'wechaty/dist/src/events/wechaty-events' +import type { WechatyEventName } from 'wechaty/dist/src/events/wechaty-events' import { StateSwitch } from 'state-switch' import cuid from 'cuid' -import { +import type { Bundle, Ducks, } from 'ducks' @@ -38,10 +38,10 @@ import { WechatyRedux } from 'wechaty-redux' import { VERSION, log, -} from './config' -import * as haDuck from './duck/mod' -import { addHa } from './global-instance-manager' -import { DucksMapObject } from 'ducks/dist/src/duck' +} from './config.js' +import * as haDuck from './duck/mod.js' +import { addHa } from './global-instance-manager.js' +import type { DucksMapObject } from 'ducks/dist/src/duck' export interface HAWechatyOptions { name? : string, diff --git a/src/heartbeat$.spec.ts b/src/heartbeat$.spec.ts index 07c5875..842135d 100644 --- a/src/heartbeat$.spec.ts +++ b/src/heartbeat$.spec.ts @@ -17,7 +17,7 @@ * limitations under the License. * */ -// #!/usr/bin/env ts-node +// #!/usr/bin/env -S node --no-warnings --loader ts-node/esm // import { // test, @@ -43,7 +43,7 @@ // switchSuccess, // isChatieOA, // message$, -// } from './heartbeat$' +// } from './heartbeat$.js' // test('switchSuccess()', async t => { // const TRUE = true @@ -104,3 +104,5 @@ // m.expectObservable(result$).toBe(expected, values) // }) // }) + +export {} diff --git a/src/index.spec.ts b/src/index.spec.ts index e538ff7..d868022 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - /~https://github.com/wechaty @@ -21,8 +21,8 @@ */ import { test } from 'tstest' -// import * as index from './index' +// import * as index from './index.js' -test('tbw', async (t) => { - t.skip('tbw') +test('tbw', async t => { + await t.skip('tbw') }) diff --git a/src/mod.ts b/src/mod.ts index 6956c72..adf9ff4 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -17,11 +17,11 @@ * limitations under the License. * */ -import * as Duck from './duck/mod' -import { VERSION } from './config' -import { HAWechaty } from './ha-wechaty' -import { configureHa } from './configure-ha' -import { getHa } from './global-instance-manager' +import * as Duck from './duck/mod.js' +import { VERSION } from './config.js' +import { HAWechaty } from './ha-wechaty.js' +import { configureHa } from './configure-ha.js' +import { getHa } from './global-instance-manager.js' export { configureHa, diff --git a/src/package-json.spec.ts b/src/package-json.spec.ts new file mode 100755 index 0000000..5d9cac5 --- /dev/null +++ b/src/package-json.spec.ts @@ -0,0 +1,10 @@ +#!/usr/bin/env -S 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') +}) diff --git a/src/package-json.ts b/src/package-json.ts new file mode 100644 index 0000000..0a9806f --- /dev/null +++ b/src/package-json.ts @@ -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 = {} diff --git a/src/version.spec.ts b/src/version.spec.ts deleted file mode 100644 index 91fba2e..0000000 --- a/src/version.spec.ts +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env ts-node -/** - * Wechaty Open Source Software - /~https://github.com/wechaty - * - * @copyright 2016 Huan LI (李卓桓) , and - * Wechaty Contributors . - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -import test from 'tstest' - -import { VERSION } from './version' - -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') -}) diff --git a/src/version.ts b/src/version.ts deleted file mode 100644 index f2da2ca..0000000 --- a/src/version.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * Wechaty Open Source Software - /~https://github.com/wechaty - * - * @copyright 2016 Huan LI (李卓桓) , and - * Wechaty Contributors . - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -export const VERSION = '0.0.0' diff --git a/tests/fixtures/smoke-testing.ts b/tests/fixtures/smoke-testing.ts index b0ea414..4feabc9 100644 --- a/tests/fixtures/smoke-testing.ts +++ b/tests/fixtures/smoke-testing.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - /~https://github.com/wechaty * diff --git a/tests/integration.spec.ts b/tests/integration.spec.ts index a688bdd..924ed15 100644 --- a/tests/integration.spec.ts +++ b/tests/integration.spec.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env ts-node +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Open Source Software - /~https://github.com/wechaty @@ -27,9 +27,9 @@ // import { // HAWechaty, // // HAWechatyOptions, -// } from '../src/' +// } from '../src/.js' -// test('integration testing', async (t) => { +// test('integration testing', async t => { // const TOKEN = 'test_token' // const ENDPOINT = '0.0.0.0:8788' // const DING = 'ding_data' @@ -90,3 +90,5 @@ // await puppetService.stop() // await serviceServer.stop() // }) + +export {} diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..8693cd0 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "CommonJS", + "outDir": "dist/cjs", + }, +} diff --git a/tsconfig.json b/tsconfig.json index 728a055..bbf1a08 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "@chatie/tsconfig", "compilerOptions": { - "outDir": "dist", + "outDir": "dist/esm", }, "exclude": [ "node_modules/", From 1538663495dc10f7325224ef78abef0957320b5c Mon Sep 17 00:00:00 2001 From: Huan Date: Sun, 12 Sep 2021 17:15:50 +0800 Subject: [PATCH 2/2] 0.9.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b66934a..12ef85e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ha-wechaty", - "version": "0.9.0", + "version": "0.9.1", "description": "HAWechaty is a Load Balance for providing High Availability for Wechaty Chatbot by spreading requests across multiple WeChat individual accounts.", "type": "module", "exports": {