Skip to content

Commit 0baade6

Browse files
authored
enable ESM (#20)
* enable ESM * generate package json * 0.1.1
1 parent 27abbdd commit 0baade6

19 files changed

+195
-103
lines changed
File renamed without changes.

.github/workflows/npm.yml

+14-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ jobs:
77
name: Build
88
strategy:
99
matrix:
10-
os: [ubuntu-latest]
11-
node: [16]
10+
os:
11+
- ubuntu-latest
12+
node:
13+
- 16
1214

1315
runs-on: ${{ matrix.os }}
1416
steps:
@@ -17,6 +19,8 @@ jobs:
1719
uses: actions/setup-node@v2
1820
with:
1921
node-version: ${{ matrix.node-version }}
22+
cache: npm
23+
cache-dependency-path: package.json
2024

2125
- name: Install Dependencies
2226
run: npm install
@@ -33,12 +37,14 @@ jobs:
3337
- uses: actions/setup-node@v2
3438
with:
3539
node-version: 16
40+
cache: npm
41+
cache-dependency-path: package.json
3642

3743
- name: Install Dependencies
3844
run: npm install
3945

40-
- name: Generate Version
41-
run: ./scripts/generate-version.sh
46+
- name: Generate Package JSON
47+
run: ./scripts/generate-package-json.sh
4248

4349
- name: Pack Testing
4450
run: ./scripts/npm-pack-testing.sh
@@ -56,12 +62,14 @@ jobs:
5662
with:
5763
node-version: 16
5864
registry-url: https://registry.npmjs.org/
65+
cache: npm
66+
cache-dependency-path: package.json
5967

6068
- name: Install Dependencies
6169
run: npm install
6270

63-
- name: Generate Version
64-
run: ./scripts/generate-version.sh
71+
- name: Generate Package JSON
72+
run: ./scripts/generate-package-json.sh
6573

6674
- name: Set Publish Config
6775
run: ./scripts/package-publish-config-tag.sh

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ To test the app in external terminal, run `npm run debug` then start debugging w
5050

5151
## History
5252

53-
### master
53+
### master v0.1 (Sep 12, 2021)
54+
55+
1. ESM support
5456

5557
### v0.0.1 (Jul 3, 2021)
5658

bin/cli.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
#!/usr/bin/env node
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
22

3-
import path from 'path'
43
import { ArgumentParser } from 'argparse'
54
import updateNotifier from 'update-notifier'
65

76
import {
87
log,
9-
MODULE_ROOT,
108
VERSION,
11-
} from '../src/config'
12-
13-
import { startBot } from '../src/index'
9+
} from '../src/config.js'
10+
import { packageJson } from '../src/package-json.js'
11+
import { startBot } from '../src/index.js'
1412

1513
function checkUpdate () {
16-
const pkgFile = path.join(MODULE_ROOT, 'package.json')
17-
const pkg = require(pkgFile)
14+
// const pkgFile = path.join(MODULE_ROOT, 'package.json')
15+
// const pkg = require(pkgFile)
1816
const notifier = updateNotifier({
19-
pkg,
17+
pkg: packageJson as any,
2018
updateCheckInterval: 1000 * 60 * 60 * 24 * 7, // 1 week
2119
})
2220
notifier.notify()

package.json

+30-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
{
22
"name": "wechaty-cli",
3-
"version": "0.0.8",
3+
"version": "0.1.1",
44
"description": "Command-line Client for Wechaty, powered by blessed",
5-
"main": "dist/src/mod.js",
6-
"typings": "dist/src/mod.d.ts",
5+
"type": "module",
6+
"exports": {
7+
".": {
8+
"import": "./dist/esm/src/mod.js",
9+
"require": "./dist/cjs/src/mod.js"
10+
}
11+
},
12+
"typings": "./dist/esm/src/mod.d.ts",
713
"engines": {
8-
"wechaty": ">=0.60"
14+
"wechaty": ">=0.69"
915
},
1016
"bin": {
11-
"wechaty-cli": "dist/bin/cli.js"
17+
"wechaty-cli": "dist/esm/bin/cli.js"
1218
},
1319
"scripts": {
1420
"clean": "shx rm -fr dist/*",
15-
"dist": "npm run clean && tsc",
21+
"dist": "npm-run-all clean build dist:commonjs",
22+
"build": "tsc && tsc -p tsconfig.cjs.json",
23+
"dist:commonjs": "jq -n \"{ type: \\\"commonjs\\\" }\" > dist/cjs/package.json",
1624
"debug": "node --nolazy --inspect-brk=9229 -r ts-node/register bin/cli.ts",
17-
"pack": "npm pack",
18-
"start": "ts-node bin/cli.ts",
19-
"lint": "npm run lint:es && npm run lint:ts && npm run lint:md",
25+
"start": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" node bin/cli.ts",
26+
"lint": "npm-run-all lint:es lint:ts lint:md",
2027
"lint:md": "markdownlint README.md",
21-
"lint:ts": "tsc --noEmit",
28+
"lint:ts": "tsc --isolatedModules --noEmit",
2229
"lint:es": "eslint \"src/**/*.ts\" \"tests/**/*.spec.ts\" --ignore-pattern tests/fixtures/",
2330
"test": "npm run lint && npm run test:unit",
2431
"test:pack": "bash -x scripts/npm-pack-testing.sh",
25-
"test:unit": "blue-tape -r ts-node/register 'src/**/*.spec.ts' 'tests/**/*.spec.ts'"
32+
"test:unit": "tap --node-arg=--loader=ts-node/esm --node-arg=--no-warnings \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\""
2633
},
2734
"files": [
2835
"bin/",
@@ -51,28 +58,33 @@
5158
"blessed-contrib": "github:chinggg/blessed-contrib",
5259
"qrcode-terminal": "^0.12.0",
5360
"update-notifier": "^5.1.0",
54-
"wechaty": "^0.62.3"
61+
"wechaty": "^0.69.38"
5562
},
5663
"devDependencies": {
57-
"@chatie/eslint-config": "^0.12.4",
64+
"@chatie/eslint-config": "^0.14.1",
5865
"@chatie/git-scripts": "^0.6.2",
5966
"@chatie/semver": "^0.4.7",
60-
"@chatie/tsconfig": "^0.16.2",
67+
"@chatie/tsconfig": "^0.20.2",
6168
"@grpc/grpc-js": "^1.3.6",
6269
"@types/argparse": "^2.0.10",
6370
"@types/update-notifier": "^5.1.0",
6471
"@types/uuid": "^8.3.1",
72+
"cross-env": "^7.0.3",
73+
"npm-run-all": "^4.1.5",
6574
"pkg-jq": "^0.2.11",
6675
"shx": "^0.3.3",
67-
"tsconfig-paths": "^3.10.1",
68-
"tstest": "^0.4.10",
69-
"wechaty-puppet-wechat": "^0.28.3",
70-
"wechaty-puppet-wechat4u": "^0.18.2"
76+
"tstest": "^0.5.16",
77+
"type-fest": "^2.3.2",
78+
"typescript": "^4.4.2",
79+
"wechaty-puppet-wechat": "^0.29.7"
7180
},
7281
"publishConfig": {
7382
"access": "public",
7483
"tag": "next"
7584
},
85+
"tap": {
86+
"check-coverage": false
87+
},
7688
"git": {
7789
"scripts": {
7890
"pre-push": "npx git-scripts-pre-push"

scripts/generate-package-json.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
SRC_PACKAGE_JSON_TS_FILE='src/package-json.ts'
5+
6+
[ -f ${SRC_PACKAGE_JSON_TS_FILE} ] || {
7+
echo ${SRC_PACKAGE_JSON_TS_FILE}" not found"
8+
exit 1
9+
}
10+
11+
cat <<_SRC_ > ${SRC_PACKAGE_JSON_TS_FILE}
12+
/**
13+
* This file was auto generated from scripts/generate-version.sh
14+
*/
15+
import type { PackageJson } from 'type-fest'
16+
export const packageJson: PackageJson = $(cat package.json) as any
17+
_SRC_

scripts/generate-version.sh

-18
This file was deleted.

scripts/npm-pack-testing.sh

+39-6
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,60 @@ else
1010
fi
1111

1212
npm run dist
13-
npm run pack
13+
npm pack
1414

1515
TMPDIR="/tmp/npm-pack-testing.$$"
1616
mkdir "$TMPDIR"
1717
mv ./*-*.*.*.tgz "$TMPDIR"
1818
cp tests/fixtures/smoke-testing.ts "$TMPDIR"
1919

2020
cd $TMPDIR
21+
2122
npm init -y
22-
npm install ./*-*.*.*.tgz \
23-
@chatie/tsconfig \
24-
@grpc/grpc-js \
23+
npm install --production *-*.*.*.tgz \
24+
@types/node \
25+
@chatie/tsconfig@$NPM_TAG \
26+
pkg-jq \
27+
"wechaty@$NPM_TAG" \
2528

29+
#
30+
# CommonJS
31+
#
2632
./node_modules/.bin/tsc \
33+
--target es6 \
34+
--module CommonJS \
35+
\
36+
--moduleResolution node \
2737
--esModuleInterop \
2838
--lib esnext \
39+
--noEmitOnError \
40+
--noImplicitAny \
2941
--skipLibCheck \
42+
smoke-testing.ts
43+
44+
echo
45+
echo "CommonJS: pack testing..."
46+
node smoke-testing.js
47+
48+
#
49+
# ES Modules
50+
#
51+
52+
# https://stackoverflow.com/a/59203952/1123955
53+
npx pkg-jq -i '.type="module"'
54+
55+
./node_modules/.bin/tsc \
56+
--target es2020 \
57+
--module es2020 \
58+
\
59+
--moduleResolution node \
60+
--esModuleInterop \
61+
--lib esnext \
3062
--noEmitOnError \
3163
--noImplicitAny \
32-
--target es6 \
33-
--module commonjs \
64+
--skipLibCheck \
3465
smoke-testing.ts
3566

67+
echo
68+
echo "ES Module: pack testing..."
3669
node smoke-testing.js

src/config.ts

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
import path from 'path'
1+
// import path from 'path'
22

3-
export { log } from 'brolog'
3+
import { log } from 'brolog'
44

5-
export function parentDirectory (): string { // export for test
6-
const parentDir = __dirname.split(path.sep) // [... 'node_modules', 'facenet', 'dist', 'src']
7-
.slice(-2, -1)[0] // 'dist'
8-
return parentDir
9-
}
5+
import { packageJson } from './package-json.js'
6+
7+
// export function parentDirectory (): string { // export for test
8+
// const parentDir = __dirname.split(path.sep) // [... 'node_modules', 'facenet', 'dist', 'src']
9+
// .slice(-2, -1)[0] // 'dist'
10+
// return parentDir
11+
// }
12+
13+
// export const MODULE_ROOT = parentDirectory() === 'dist'
14+
// ? path.join(__dirname, '/../..')
15+
// : path.join(__dirname, '/..')
16+
17+
// const packageFile = path.join(MODULE_ROOT, 'package.json')
18+
// export const VERSION = require(packageFile).version
1019

11-
export const MODULE_ROOT = parentDirectory() === 'dist'
12-
? path.join(__dirname, '/../..')
13-
: path.join(__dirname, '/..')
20+
const VERSION = packageJson.version || '0.0.0'
1421

15-
const packageFile = path.join(MODULE_ROOT, 'package.json')
16-
export const VERSION = require(packageFile).version
22+
/* eslint-disable no-use-before-define */
1723

18-
export interface TreeNode {
24+
type TreeChildren = Record<string, TreeNode>
25+
26+
interface TreeNode {
1927
name?: string,
2028
children?: TreeChildren | ((node: TreeNode) => TreeChildren | Promise<TreeChildren>),
2129
childrenContent?: TreeChildren,
@@ -24,4 +32,11 @@ export interface TreeNode {
2432
[custom: string]: any
2533
}
2634

27-
export type TreeChildren = Record<string, TreeNode>
35+
export type {
36+
TreeNode,
37+
TreeChildren,
38+
}
39+
export {
40+
log,
41+
VERSION,
42+
}

src/index.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env node --no-warnings --loader ts-node/esm
2+
23
import { join } from 'path'
34
import { mkdirSync } from 'fs'
4-
import { Contact, Message, Room, ScanStatus, Wechaty } from 'wechaty'
5-
import { generate } from 'qrcode-terminal'
6-
import { screen, msgConsole, leftPanel, rightPanel, textArea } from './main'
7-
import { TreeNode, TreeChildren } from './config'
5+
6+
import { Contact, Message, PuppetModuleName, Room, ScanStatus, Wechaty } from 'wechaty'
7+
import qrTerminal from 'qrcode-terminal'
8+
9+
import { screen, msgConsole, leftPanel, rightPanel, textArea } from './main.js'
10+
import type { TreeNode, TreeChildren } from './config.js'
811

912
let bot: Wechaty
1013
const filePath = join('data', 'files')
@@ -51,7 +54,7 @@ function onLogout (user: Contact) {
5154

5255
function onScan (qrcode: string, status: ScanStatus) {
5356
if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
54-
generate(qrcode, { small: true }, (asciiart: string) => msgConsole.add(asciiart))
57+
qrTerminal.generate(qrcode, { small: true }, (asciiart: string) => msgConsole.add(asciiart))
5558
msgConsole.log('Scan QR Code to login, status:' + ScanStatus[status])
5659
const qrcodeImageUrl = [
5760
'https://wechaty.js.org/qrcode/',
@@ -125,7 +128,13 @@ async function onMessage (message: Message) {
125128

126129
export function startBot (args: any) {
127130
msgConsole.log('Initing...')
128-
bot = new Wechaty({ name: args.name })
131+
132+
const puppet = process.env['WECHATY_PUPPET'] || 'wechaty-puppet-wechat'
133+
134+
bot = new Wechaty({
135+
name: args.name,
136+
puppet: puppet as PuppetModuleName,
137+
})
129138
.on('logout', user => onLogout(user))
130139
.on('scan', (qrcode, status) => onScan(qrcode, status))
131140
.on('login', user => onLogin(user))
@@ -152,7 +161,7 @@ export function startBot (args: any) {
152161
}
153162

154163
leftPanel.on('select', async (node: TreeNode) => {
155-
const real = node.real
164+
const real = node['real']
156165
curChat = real
157166
msgConsole.setLabel(`与 ${node.name} 的对话`)
158167
msgConsole.setContent('')

0 commit comments

Comments
 (0)