Skip to content

Commit

Permalink
ESM wip (#141)
Browse files Browse the repository at this point in the history
* ESM wip

* clean

* 0.43.1

* wip

* 0.43.2

* fix unit tetsts & esm

* 0.43.3

* fix ESM modules

* 0.43.4
  • Loading branch information
huan authored Aug 28, 2021
1 parent 5d7f72f commit 7fc0aa5
Show file tree
Hide file tree
Showing 25 changed files with 269 additions and 204 deletions.
File renamed without changes.
8 changes: 4 additions & 4 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ jobs:
- 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 @@ -60,8 +60,8 @@ jobs:
- 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: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ typings/

# next.js build output
.next
/dist/
dist/
package-lock.json
.DS_Store
t.*

t/
.idea
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ Set the max size for wechaty entities in LRU Cache.

## History

### master
### master v0.43 (Aug 28, 2021)

1. Support ES Modules

### v0.16 (Sep 2019)

Expand Down
49 changes: 30 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
{
"name": "wechaty-puppet",
"version": "0.41.9",
"version": "0.43.4",
"description": "Abstract Puppet for Wechaty",
"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": ">=14",
"wechaty": ">=0.62"
"wechaty": ">=0.68"
},
"scripts": {
"clean": "shx rm -fr dist/*",
"dist": "npm run clean && tsc",
"pack": "npm pack",
"dist": "npm run clean && npm run build && (cd dist/cjs; npm init -y > /dev/null)",
"build": "tsc && tsc -p tsconfig.cjs.json",
"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\"",
"typedoc": "bash scripts/typedoc.sh"
},
"repository": {
Expand All @@ -40,35 +46,40 @@
},
"homepage": "https://github.com/wechaty/wechaty-puppet#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.1",
"@types/normalize-package-data": "^2.4.1",
"@types/read-pkg-up": "^6.0.0",
"pkg-jq": "^0.2.11",
"shx": "^0.3.3",
"tsd": "^0.17.0",
"tstest": "^0.4.10",
"tstest": "^0.5.16",
"type-fest": "^0.8.1",
"typedoc": "^1.0.0-dev.4",
"typescript": "^4.3"
"typescript": "^4.4"
},
"dependencies": {
"@alloc/quick-lru": "^5.2.0",
"brolog": "^1.12.4",
"clone-class": "^0.6.20",
"file-box": "^0.16.8",
"clone-class": "^0.9.3",
"file-box": "^0.17.9",
"hot-import": "^0.2.14",
"memory-card": "^0.12.2",
"normalize-package-data": "^3.0.2",
"quick-lru": "^5",
"read-pkg-up": "^7",
"rx-queue": "^0.12.4",
"rx-queue": "^0.12.6",
"rxjs": "^7.3.0",
"semver": "^7.3.5",
"state-switch": "^0.11.3",
"state-switch": "^0.15.10",
"typed-emitter": "^1.3.1",
"watchdog": "^0.8.17"
},
"files": [
"dist",
"src"
],
"publishConfig": {
"access": "public",
"tag": "next"
},
"git": {
"scripts": {
"pre-push": "npx git-scripts-pre-push"
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 { PackageJson } from 'type-fest'
export const packageJson: PackageJson = $(cat package.json)
_SRC_
18 changes: 0 additions & 18 deletions scripts/generate-version.sh

This file was deleted.

41 changes: 35 additions & 6 deletions scripts/npm-pack-testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,58 @@
set -e

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 \
@types/quick-lru \
@types/node \
@types/normalize-package-data \
typescript \
typescript@latest

#
# 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
6 changes: 4 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import { FileBox } from 'file-box'
import { MemoryCard } from 'memory-card'
import { StateSwitch } from 'state-switch'

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

import * as envVars from './env-vars'
import * as envVars from './env-vars.js'

const logLevel = process.env['WECHATY_LOG']
if (logLevel) {
log.level(logLevel.toLowerCase() as any)
log.silly('Puppet', 'Config: WECHATY_LOG set level to %s', logLevel)
}

const VERSION = packageJson.version || '0.0.0'

export {
log,
envVars,
Expand Down
2 changes: 1 addition & 1 deletion src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
EventScanPayload,
EventReadyPayload,
EventHeartbeatPayload,
} from './schemas/event'
} from './schemas/event.js'

export type PuppetDirtyListener = (payload: EventDirtyPayload) => void
export type PuppetDongListener = (payload: EventDongPayload) => void
Expand Down
28 changes: 14 additions & 14 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export {
ContactType,
ContactPayload,
ContactQueryFilter,
} from './schemas/contact'
} from './schemas/contact.js'
export {
ScanStatus,

Expand All @@ -33,7 +33,7 @@ export {
EventRoomLeavePayload,
EventRoomTopicPayload,
EventScanPayload,
} from './schemas/event'
} from './schemas/event.js'
export {
FriendshipAddOptions,
FriendshipPayload,
Expand All @@ -43,36 +43,36 @@ export {
FriendshipSearchQueryFilter,
FriendshipType,
FriendshipSceneType,
} from './schemas/friendship'
} from './schemas/friendship.js'
export {
ImageType,
} from './schemas/image'
} from './schemas/image.js'
export {
MessagePayload,
MessageType,
MessageQueryFilter,
} from './schemas/message'
} from './schemas/message.js'
export {
RoomPayload,
RoomQueryFilter,
RoomMemberPayload,
RoomMemberQueryFilter,
} from './schemas/room'
} from './schemas/room.js'
export {
RoomInvitationPayload,
} from './schemas/room-invitation'
} from './schemas/room-invitation.js'
export {
UrlLinkPayload,
} from './schemas/url-link'
} from './schemas/url-link.js'
export {
MiniProgramPayload,
} from './schemas/mini-program'
} from './schemas/mini-program.js'

export {
throwUnsupportedError,
} from './throw-unsupported-error'
} from './throw-unsupported-error.js'

export { PayloadType } from './schemas/payload'
export { PayloadType } from './schemas/payload.js'

export {
PuppetOptions,
Expand All @@ -93,13 +93,13 @@ export {
PUPPET_EVENT_DICT,

YOU,
} from './schemas/puppet'
} from './schemas/puppet.js'
export {
VERSION,
log,
} from './config'
} from './config.js'

export {
Puppet,
PuppetImplementation,
} from './puppet'
} from './puppet.js'
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')
})
3 changes: 2 additions & 1 deletion src/version.ts → src/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
* This file will be overwrite when we publish NPM module
* by scripts/generate_version.ts
*/
import { PackageJson } from 'type-fest'

export const VERSION = '0.0.0'
export const packageJson: PackageJson = {}
Loading

0 comments on commit 7fc0aa5

Please sign in to comment.