From 6bec7433ff7abfcc53e4a874d6e09965764dd998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Huan=20=28=E6=9D=8E=E5=8D=93=E6=A1=93=29?= Date: Wed, 27 Oct 2021 12:08:08 +0800 Subject: [PATCH] enable ESM --- .eslintrc.js => .eslintrc.cjs | 0 .vscode/settings.json | 8 +- README.md | 5 + examples/npm-like-logger.js | 2 +- package.json | 66 ++++----- scripts/npm-pack-testing.sh | 37 ++++- src/brolog.spec.ts | 129 +++++++++--------- src/brolog.ts | 56 +++++--- src/config.ts | 17 ++- src/mod.ts | 19 +++ src/typings.d.ts | 8 +- src/version.spec.ts | 10 +- tests/integration.spec.ts | 25 ++++ ...protractor.conf.ts => protractor.conf.cjs} | 16 ++- tests/protractor.sh | 5 +- tsconfig.cjs.json | 7 + tsconfig.json | 6 +- 17 files changed, 269 insertions(+), 147 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) create mode 100644 src/mod.ts create mode 100644 tests/integration.spec.ts rename tests/{protractor.conf.ts => protractor.conf.cjs} (72%) 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/.vscode/settings.json b/.vscode/settings.json index ec6ed22..9906b63 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,9 +1,9 @@ // Place your settings in this file to overwrite default and user settings. { - "typescript.tsdk": "node_modules/typescript/lib" - , - "window.zoomLevel": 1, "editor.minimap.enabled": true - , "files.exclude": { + "typescript.tsdk": "node_modules/typescript/lib", + "window.zoomLevel": 0, + "editor.minimap.enabled": true, + "files.exclude": { "node_modules/": true , "dist": true }, diff --git a/README.md b/README.md index 916bfd2..1d707c9 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,11 @@ P.S. running E2E test is based on *brolog demo project*: [git repository](https: ## CHANGELOG +### main v1.14 (Oct 27, 2021) + +1. ES Module support +1. export `Loggable` interface + ### v1.12 (Jun 18, 2020) 1. Update to use Chatie DevOps toolsets. diff --git a/examples/npm-like-logger.js b/examples/npm-like-logger.js index c4555e8..823347c 100644 --- a/examples/npm-like-logger.js +++ b/examples/npm-like-logger.js @@ -1,4 +1,4 @@ -const { Brolog } = require('brolog') +import { Brolog } from 'brolog' const log = new Brolog() diff --git a/package.json b/package.json index 162f370..90797e5 100644 --- a/package.json +++ b/package.json @@ -1,21 +1,30 @@ { "name": "brolog", - "version": "1.12.4", + "version": "1.14.0", "description": "Npmlog like logger for Browser", - "main": "bundles/brolog.umd.js", - "typings": "dist/brolog.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": ">=16", + "npm": ">=7" + }, "scripts": { - "clean": "rm -fr dist/* bundles/*", - "dist": "npm run clean && npm run build && npm run rollup && npm run dist:es6to5", - "dist:es6to5": "tsc --out ./bundles/brolog.umd.js --target es5 --allowJs bundles/brolog.es6.umd.js --lib es6,dom", - "build": "tsc --module es6", - "lint": "npm run lint:es && npm run lint:ts", - "rollup": "rollup -c", - "lint:ts": "tsc --noEmit", - "test": "npm run lint && npm run dist && npm run test:unit && npm run test:e2e", + "build": "tsc && tsc -p tsconfig.cjs.json", + "clean": "shx rm -fr dist/*", + "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:ts": "tsc --isolatedModules --noEmit", + "test": "npm-run-all lint dist test:unit test:e2e", "test:e2e": "bash -x tests/protractor.sh", "test:pack": "bash -x scripts/npm-pack-testing.sh", - "test:unit": "tap -R tap \"src/**/*.spec.ts\"", + "test:unit": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" tap -R tap \"src/**/*.spec.ts\"", "lint:es": "eslint --ignore-pattern tests/fixtures/ '{bin,examples,scripts,src,tests}/**/*.ts'" }, "repository": { @@ -38,33 +47,28 @@ "homepage": "https://github.com/huan/brolog#readme", "dependencies": {}, "devDependencies": { - "@chatie/eslint-config": "^0.8.1", + "@chatie/eslint-config": "^1.0.2", "@chatie/git-scripts": "^0.6.2", "@chatie/semver": "^0.4.7", - "@chatie/tsconfig": "^0.10.1", + "@chatie/tsconfig": "^1.0.2", "@types/chokidar": "^2.1.3", - "@types/jasmine": "^3.5.10", - "@types/puppeteer": "^3.0.0", - "@types/rollup-plugin-json": "^3.0.0", - "chokidar": "^3.4.0", - "jasmine-spec-reporter": "^5.0.2", - "magic-string": "^0.25.0", - "pkg-jq": "^0.2.4", + "@types/jasmine": "^3.10.1", + "@types/puppeteer": "^5.4.4", + "@types/rollup-plugin-json": "^3.0.3", + "chokidar": "^3.5.2", + "jasmine-spec-reporter": "^7.0.0", + "magic-string": "^0.25.7", + "pkg-jq": "^0.2.11", "protractor": "^7.0.0", - "puppeteer": "^4.0.0", - "rollup": "^2.13.1", + "puppeteer": "^10.4.0", + "rollup": "^2.58.3", "rollup-plugin-json": "^4.0.0", - "source-map": "^0.7.2", - "tap": "^14.10.7", + "source-map": "^0.7.3", + "tap": "^15.0.10", "tslint": "^6.1.2", - "tstest": "^0.4.10" + "tstest": "^1.0.1" }, "files": [ - "package.json", - "README.md", - "CHANGELOG.md", - "LICENSE", - "bundles/", "src/", "dist/" ], diff --git a/scripts/npm-pack-testing.sh b/scripts/npm-pack-testing.sh index 2533727..e42dfcd 100755 --- a/scripts/npm-pack-testing.sh +++ b/scripts/npm-pack-testing.sh @@ -10,15 +10,48 @@ mv *-*.*.*.tgz "$TMPDIR" cp tests/fixtures/smoke-testing.ts "$TMPDIR" cd $TMPDIR + npm init -y npm install *-*.*.*.tgz \ - typescript + @types/node \ + typescript \ + +# +# CommonJS +# +./node_modules/.bin/tsc \ + --esModuleInterop \ + --lib esnext \ + --noEmitOnError \ + --noImplicitAny \ + --skipLibCheck \ + --target es5 \ + --module CommonJS \ + --moduleResolution node \ + 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 \ - --lib esnext,dom \ + --esModuleInterop \ + --lib esnext \ --noEmitOnError \ --noImplicitAny \ --skipLibCheck \ + --target es2020 \ + --module es2020 \ + --moduleResolution node \ smoke-testing.ts +echo +echo "ES Module: pack testing..." node smoke-testing.js diff --git a/src/brolog.spec.ts b/src/brolog.spec.ts index 2d79089..0c3b584 100755 --- a/src/brolog.spec.ts +++ b/src/brolog.spec.ts @@ -1,25 +1,23 @@ -#!/usr/bin/env ts-node -/* eslint-disable comma-spacing */ -/* eslint-disable no-console */ - -import * as sinon from 'sinon' +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm +/* eslint no-console: off */ +/* eslint comma-spacing: off */ +import { + test, + sinon, +} from 'tstest' import { Brolog, VERSION, Loggable, -} from './brolog' - -const t = require('tap') // tslint:disable:no-shadowed-variable - -const sinonTest = require('sinon-test')(sinon) as (func: (this: any, ...args: any[]) => void | Promise) => Function +} from './brolog.js' -t.test('VERSION', (t: any) => { +test('VERSION', t => { t.ok(/^\d+\.\d+\.\d+$/.test(VERSION), 'should get semver VERSION') t.end() }) -t.test('Brolog static/instance construct test', (t: any) => { +test('Brolog static/instance construct test', async t => { const EXPECTED_LEVEL = 'silly' @@ -58,11 +56,9 @@ t.test('Brolog static/instance construct test', (t: any) => { log1.level(LEVEL_SILENT) ll = log1.level() t.equal(ll, LEVEL_SILENT, 'should has current level as LEVEL_SILENT after function init') - - t.end() }) -t.test('Brolog global log level test', (t: any) => { +test('Brolog global log level test', async t => { const log = Brolog let l // level @@ -89,7 +85,6 @@ t.test('Brolog global log level test', (t: any) => { l = log.level() t.equal(l, 'silly', 'should be silly after level set to silly') - t.end() }) /** @@ -98,63 +93,71 @@ t.test('Brolog global log level test', (t: any) => { * because monkey patch is not recover when it finish * */ -t.test('Brolog global instance level filter test', (t: any) => { +test('Brolog global instance level filter test', async t => { const logFuncList = [ 'error', 'warn', 'info', 'log', - ] + ] as const let log2: Brolog log2 = Brolog.instance('silent') - sinonTest(function () { - logFuncList.forEach(logFunc => this.stub(console, logFunc).callThrough()) + + await t.test('no error for silent', async t => { + const sandbox = sinon.createSandbox() + logFuncList.forEach(logFunc => sandbox.stub(console, logFunc).callThrough()) doLog(log2) t.ok((console.error as any)['notCalled'], 'should not call error with level SILENT ##############') - }).apply(log2) + sandbox.restore() + }) log2 = Brolog.instance('silly') - sinonTest(function () { - logFuncList.forEach(logFunc => this.stub(console, logFunc).callThrough()) + await t.test('verbose + silly for silly', async t => { + const sandbox = sinon.createSandbox() + logFuncList.forEach(logFunc => sandbox.stub(console, logFunc).callThrough()) doLog(log2) t.equal((console.log as any)['callCount'], 2, 'should call log2(verbose + silly) 2 time with level SILLY') - }).apply(log2) + sandbox.restore() + }) log2 = Brolog.instance() log2.level('silent') - sinonTest(function () { - logFuncList.forEach(logFunc => this.stub(console, logFunc).callThrough()) + await t.test('no log for silent', async t => { + const sandbox = sinon.createSandbox() + logFuncList.forEach(logFunc => sandbox.stub(console, logFunc).callThrough()) doLog(log2) t.equal((console.error as any)['callCount'] , 0, 'should call error 0 time with level SILENT') t.equal((console.warn as any)['callCount'] , 0, 'should call warn 0 time with level SILENT') t.equal((console.info as any)['callCount'] , 0, 'should call info 0 time with level SILENT') t.equal((console.log as any)['callCount'] , 0, 'should call log2(verbose + silly) 0 time with level SILENT') - }).apply(log2) + sandbox.restore() + }) log2.level('error') - sinonTest(function () { - logFuncList.forEach(logFunc => this.stub(console, logFunc).callThrough()) + await t.test('only error for error', async t => { + const sandbox = sinon.createSandbox() + logFuncList.forEach(logFunc => sandbox.stub(console, logFunc).callThrough()) doLog(log2) t.equal((console.error as any)['callCount'] , 1, 'should call error 1 time with level ERR') t.equal((console.warn as any)['callCount'] , 0, 'should call warn 0 time with level ERR') t.equal((console.info as any)['callCount'] , 0, 'should call info 0 time with level ERR') t.equal((console.log as any)['callCount'] , 0, 'should call log2(verbose + silly) 0 time with level ERR') - }).apply(log2) + sandbox.restore() + }) log2.level('verbose') - - sinonTest(function () { - logFuncList.forEach(logFunc => this.stub(console, logFunc).callThrough()) + await t.test('for verbose', async t => { + const sandbox = sinon.createSandbox() + logFuncList.forEach(logFunc => sandbox.stub(console, logFunc).callThrough()) doLog(log2) t.equal((console.error as any)['callCount'] , 1, 'should call error 1 time with level VERBOSE') t.equal((console.warn as any)['callCount'] , 1, 'should call warn 1 time with level VERBOSE') t.equal((console.info as any)['callCount'] , 1, 'should call info 1 time with level VERBOSE') t.equal((console.log as any)['callCount'] , 1, 'should call log2(verbose + silly) 1 time with level VERBOSE') - }).apply(log2) - - t.end() + sandbox.restore() + }) /** */ @@ -167,58 +170,64 @@ t.test('Brolog global instance level filter test', (t: any) => { } }) -t.test('Brolog global instance prefix filter test', (t: any) => { +test('Brolog global instance prefix filter test', async t => { const logFuncList = [ 'error', 'warn', 'info', 'log', - ] + ] as const // filter log by prefix match /Show/ const log = Brolog.instance('info', /Show/) - sinonTest(function () { - logFuncList.forEach(logFunc => this.stub(console, logFunc).callThrough()) + await t.test('doLogHide /Show/', async t => { + const sandbox = sinon.createSandbox() + logFuncList.forEach(logFunc => sandbox.stub(console, logFunc).callThrough()) doLogHide(log) t.equal((console.error as any)['callCount'] , 0, 'should call error 0 time with prefix Hide') t.equal((console.warn as any)['callCount'] , 0, 'should call warn 0 time with prefix Hide') t.equal((console.info as any)['callCount'] , 0, 'should call info 0 time with prefix Hide') t.equal((console.log as any)['callCount'] , 0, 'should call log2(verbose + silly) 0 time with prefix Hide') - }).apply(log) + sandbox.restore() + }) - sinonTest(function () { - logFuncList.forEach(logFunc => this.stub(console, logFunc).callThrough()) + await t.test('doLogShow /Show/', async t => { + const sandbox = sinon.createSandbox() + logFuncList.forEach(logFunc => sandbox.stub(console, logFunc).callThrough()) doLogShow(log) t.equal((console.error as any)['callCount'] , 1, 'should call error 1 time with prefix Show') t.equal((console.warn as any)['callCount'] , 1, 'should call warn 1 time with prefix Show') t.equal((console.info as any)['callCount'] , 1, 'should call info 1 time with prefix Show') t.equal((console.log as any)['callCount'] , 0, 'should call log2(verbose + silly) 1 time with prefix Show') - }).apply(log) + sandbox.restore() + }) log.level('silent') - sinonTest(function () { - logFuncList.forEach(logFunc => this.stub(console, logFunc).callThrough()) + await t.test('doLogShow for silent', async t => { + const sandbox = sinon.createSandbox() + logFuncList.forEach(logFunc => sandbox.stub(console, logFunc).callThrough()) doLogShow(log) t.equal((console.error as any)['callCount'] , 0, 'should call error 0 time with prefix Show with level silent') t.equal((console.warn as any)['callCount'] , 0, 'should call warn 0 time with prefix Show with level silent') t.equal((console.info as any)['callCount'] , 0, 'should call info 0 time with prefix Show with level silent') t.equal((console.log as any)['callCount'] , 0, 'should call log2(verbose + silly) 0 time with prefix Show with level silent') - }).apply(log) + sandbox.restore() + }) log.level('silly') - sinonTest(function () { - logFuncList.forEach(logFunc => this.stub(console, logFunc).callThrough()) + await t.test('doLogShow for silly', async t => { + const sandbox = sinon.createSandbox() + logFuncList.forEach(logFunc => sandbox.stub(console, logFunc).callThrough()) doLogShow(log) t.equal((console.error as any)['callCount'] , 1, 'should call error 1 time with prefix Show with level silly') t.equal((console.warn as any)['callCount'] , 1, 'should call warn 1 time with prefix Show with level silly') t.equal((console.info as any)['callCount'] , 1, 'should call info 1 time with prefix Show with level silly') t.equal((console.log as any)['callCount'] , 2, 'should call log2(verbose + silly) 2 time with prefix Show with level silly') - }).apply(log) - - t.end() + sandbox.restore() + }) /** */ @@ -239,7 +248,7 @@ t.test('Brolog global instance prefix filter test', (t: any) => { } }) -t.test('Brolog individual instance prefix filter test', async (t: any) => { +test('Brolog individual instance prefix filter test', async t => { // important: reset all to default: 'info', /.*/ const log = Brolog.instance('info', /.*/) @@ -264,13 +273,13 @@ t.test('Brolog individual instance prefix filter test', async (t: any) => { t.end() }) -t.test('Brolog enableLogger()', sinonTest(async function (t: any) { +test('Brolog enableLogger()', async t => { const sandbox = sinon.createSandbox() const spy = sandbox.spy() const stub = sandbox.stub(Brolog.prototype, 'defaultTextPrinter') - t.test('enableLogging(false/true)', async (t: any) => { + await t.test('enableLogging(false/true)', async t => { spy.resetHistory() stub.resetHistory() @@ -285,7 +294,7 @@ t.test('Brolog enableLogger()', sinonTest(async function (t: any) { t.ok(stub.calledOnce, 'should log after enableLogging(true)') }) - t.test('enableLogging(log function)', async (t: any) => { + await t.test('enableLogging(log function)', async t => { spy.resetHistory() stub.resetHistory() @@ -298,10 +307,9 @@ t.test('Brolog enableLogger()', sinonTest(async function (t: any) { // XXX why need t.end() inside async function(which will return a promise?) sandbox.restore() - t.end() -})) +}) -t.test('Timestamp()', (t: any) => { +test('Timestamp()', async t => { const log = new Brolog() t.ok(log.timestamp(), 'should enable timestamp by default') @@ -311,10 +319,9 @@ t.test('Timestamp()', (t: any) => { t.equal(log.timestamp(true), undefined, 'should return void when set timestamp to true') t.ok(log.timestamp(), 'should return timestamp string when timestamp is enabled') - t.end() }) -t.test('version()', (t: any) => { +test('version()', t => { const log = new Brolog() t.ok(log.version(), 'should get version') t.end() diff --git a/src/brolog.ts b/src/brolog.ts index a94db19..023f51c 100644 --- a/src/brolog.ts +++ b/src/brolog.ts @@ -13,17 +13,18 @@ import { VERSION, BROLOG_LEVEL, BROLOG_PREFIX, -} from './config' +} from './config.js' -export type LogLevelTitle = 'ERR' - | 'WARN' - | 'INFO' - | 'VERB' - | 'SILL' +type LogLevelTitle = + | 'ERR' + | 'WARN' + | 'INFO' + | 'VERB' + | 'SILL' -export type TextPrinterFunction = (title: string, text?: string) => void +type TextPrinterFunction = (title: string, text?: string) => void -export enum LogLevel { +enum LogLevel { silent = 0, error = 1, warn = 2, @@ -32,9 +33,9 @@ export enum LogLevel { silly = 5, } -export type LogLevelName = keyof typeof LogLevel +type LogLevelName = keyof typeof LogLevel -export interface Loggable { +interface Loggable { error (moduleName: string, message: string, ...args: any[]): void warn (moduleName: string, message: string, ...args: any[]): void info (moduleName: string, message: string, ...args: any[]): void @@ -42,18 +43,15 @@ export interface Loggable { silly (moduleName: string, message: string, ...args: any[]): void } -// declare the `log` variable first -export let log: Brolog +class Brolog implements Loggable { -export class Brolog implements Loggable { - - private static globalInstance : Brolog + private static globalInstance? : Brolog private static globalLogLevelName: LogLevelName = 'info' private static globalPrefix : string | RegExp = /.*/ // Match all by default private enableTimestamp = true private logLevel: LogLevel - private prefixFilter: RegExp + private prefixFilter?: RegExp public textPrinter: (levelTitle: LogLevelTitle, text: string) => void @@ -164,7 +162,7 @@ export class Brolog implements Loggable { public static prefix (filter?: string | RegExp): void | RegExp { if (filter) { this.globalPrefix = filter - this.globalInstance.prefix(filter) + this.globalInstance?.prefix(filter) } else { return this.instance().prefix() } @@ -198,7 +196,7 @@ export class Brolog implements Loggable { // console.log('levelName: ' + levelName) // http://stackoverflow.com/a/21294925/1123955 // XXX: fix the any here? - let logLevel = LogLevel[levelName.toLowerCase() as any] as any as LogLevel + let logLevel = LogLevel[levelName.toLowerCase() as any] as any as (undefined | LogLevel) if (logLevel === undefined) { // be aware of number 0 here log.error('Brolog', 'level(%s) not exist, set to silly.', levelName) logLevel = LogLevel.silly @@ -213,7 +211,7 @@ export class Brolog implements Loggable { return // skip message not match prefix filter } - const args = Array.prototype.slice.call(arguments, 3) || [] + const args = Array.prototype.slice.call(arguments, 3) args.unshift(this.timestamp() + levelTitle + ' ' + prefix + ' ' + (message || '')) // const args = Array.from(arguments) || [] // args[0] = this.timestamp() + args[0] @@ -242,6 +240,7 @@ export class Brolog implements Loggable { console.info(text) break + // eslint-disable-next-line default-case-last default: case 'VERB': case 'SILL': @@ -251,11 +250,13 @@ export class Brolog implements Loggable { } } + public static error (prefix: string, ...args: any[]): void { const instance = Brolog.instance() // return instance.error.apply(instance, arguments) return Reflect.apply(instance.error, instance, ([] as any).concat(prefix, args)) } + public error (prefix: string, ...args: any[]): void { if (this.logLevel < LogLevel.error) { return @@ -269,6 +270,7 @@ export class Brolog implements Loggable { const instance = Brolog.instance() return Reflect.apply(instance.warn, instance, ([] as any).concat(prefix, args)) } + public warn (prefix: string, ...args: any[]): void { if (this.logLevel < LogLevel.warn) { return @@ -282,6 +284,7 @@ export class Brolog implements Loggable { const instance = Brolog.instance() return Reflect.apply(instance.info, instance, ([] as any).concat(prefix, args)) } + public info (prefix: string, ...args: any[]): void { if (this.logLevel < LogLevel.info) { return @@ -295,6 +298,7 @@ export class Brolog implements Loggable { const instance = Brolog.instance() return Reflect.apply(instance.verbose, instance, ([] as any).concat(prefix, args)) } + public verbose (prefix: string, ...args: any[]): void { if (this.logLevel < LogLevel.verbose) { return @@ -309,6 +313,7 @@ export class Brolog implements Loggable { const instance = Brolog.instance() return Reflect.apply(instance.silly, instance, ([] as any).concat(prefix, args)) } + public silly (prefix: string, ...args: any[]): void { if (this.logLevel < LogLevel.silly) { return @@ -380,7 +385,7 @@ export { VERSION, } -log = Brolog.instance() +const log = Brolog.instance() if (BROLOG_LEVEL) { /** @@ -399,4 +404,13 @@ if (BROLOG_PREFIX && BROLOG_PREFIX !== '*') { log.prefix(BROLOG_PREFIX) } -export default Brolog +export type { + LogLevelTitle, + Loggable, + LogLevelName, +} +export { + LogLevel, + Brolog, + log, +} diff --git a/src/config.ts b/src/config.ts index 56671d9..ceede76 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,4 @@ -export { VERSION } from './version' +export { VERSION } from './version.js' /** * BROLOG_LEVEL @@ -12,11 +12,13 @@ let level : undefined | string let debugModule: undefined | string /** + * * Sometimes there's a `process` in browser (ionic3 & angular5) * Sometimes there's a window in Node.js (browserify) + * */ - -if (typeof process !== 'undefined' && process.env) { +// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition +if (typeof process !== 'undefined' && process['env']) { /** * Node.js */ @@ -28,7 +30,8 @@ if (typeof process !== 'undefined' && process.env) { } } -if (typeof window !== 'undefined' && window.location && typeof window.location.search === 'string') { +// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition +if (typeof window !== 'undefined' && typeof window?.location?.search === 'string') { /** * Browser */ @@ -45,8 +48,10 @@ function getJsonFromUrl () { const query = location.search.substr(1) const result = {} as { [idx: string]: string } query.split('&').forEach(function (part) { - const item = part.split('=') - result[item[0]] = decodeURIComponent(item[1]) + const [key, val] = part.split('=') + if (typeof key !== 'undefined' && typeof val !== 'undefined') { + result[key] = decodeURIComponent(val) + } }) return result } diff --git a/src/mod.ts b/src/mod.ts new file mode 100644 index 0000000..1d67dc8 --- /dev/null +++ b/src/mod.ts @@ -0,0 +1,19 @@ +import { + VERSION, +} from './config.js' +import { + Brolog, + log, +} from './brolog.js' +import type { + Loggable, +} from './brolog.js' + +export type { + Loggable, +} +export { + VERSION, + Brolog, + log, +} diff --git a/src/typings.d.ts b/src/typings.d.ts index d483c3c..20a53b0 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -1,4 +1,4 @@ -declare module '*/package.json' { - export const version: string - // export default version -} +// declare module '*/package.json' { +// export const version: string +// // export default version +// } diff --git a/src/version.spec.ts b/src/version.spec.ts index 773262e..f763814 100644 --- a/src/version.spec.ts +++ b/src/version.spec.ts @@ -1,5 +1,4 @@ -#!/usr/bin/env ts-node - +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm /** * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty * @@ -21,11 +20,10 @@ */ import { VERSION, -} from './version' +} from './version.js' -const t = require('tap') // tslint:disable:no-shadowed-variable +import { test } from 'tstest' -t.test('Make sure the VERSION is fresh in source code', (t: any) => { +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') - t.end() }) diff --git a/tests/integration.spec.ts b/tests/integration.spec.ts new file mode 100644 index 0000000..b1adf64 --- /dev/null +++ b/tests/integration.spec.ts @@ -0,0 +1,25 @@ +#!/usr/bin/env -S node --no-warnings --loader ts-node/esm +/** + * Wechaty Chatbot SDK - https://github.com/wechaty/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' + +test('tbw', async t => { + t.pass('tbw') +}) diff --git a/tests/protractor.conf.ts b/tests/protractor.conf.cjs similarity index 72% rename from tests/protractor.conf.ts rename to tests/protractor.conf.cjs index 3556895..46d3479 100644 --- a/tests/protractor.conf.ts +++ b/tests/protractor.conf.cjs @@ -2,15 +2,15 @@ // Protractor configuration file, see link for more information // https://github.com/angular/protractor/blob/master/docs/referenceConf.js -import { SpecReporter } from 'jasmine-spec-reporter' -import puppeteer from 'puppeteer' +const { SpecReporter } = require('jasmine-spec-reporter') +const puppeteer = require('puppeteer') -import { Config } from 'protractor' +// import { Config } from 'protractor' /* global jasmine */ -declare const jasmine: any +// declare const jasmine: any -export const config: Config = { +const config = { allScriptsTimeout: 11000, specs: [ 'e2e/**/*.e2e-spec.js', @@ -28,6 +28,8 @@ export const config: Config = { '--no-sandbox', '--disable-dev-shm-usage', ], + // Huan(202110) "as any as ...": https://github.com/puppeteer/puppeteer/issues/6899#issuecomment-878415393 + // binary: (puppeteer as any as puppeteer.PuppeteerNode).executablePath(), binary: puppeteer.executablePath(), }, }, @@ -50,3 +52,7 @@ export const config: Config = { } console.info(JSON.stringify(exports.config, null, ' ')) + +module.exports = { + config, +} diff --git a/tests/protractor.sh b/tests/protractor.sh index 119a13a..2c65283 100755 --- a/tests/protractor.sh +++ b/tests/protractor.sh @@ -8,7 +8,7 @@ npm run dist npm pack # https://github.com/angular/webdriver-manager/issues/307#issuecomment-377684918 -webdriver-manager update --gecko=false +npx webdriver-manager update --gecko=false git clone "$GIT_URL" "$E2E_TESTING_DIR" @@ -21,7 +21,8 @@ npm start cd - # http://stackoverflow.com/a/3474556/1123955 -ts-node ./node_modules/.bin/protractor tests/protractor.conf.ts +# npx cross-env NODE_OPTIONS="--no-warnings --loader=ts-node/esm" protractor tests/protractor.conf.ts +npx protractor tests/protractor.conf.cjs cd "$E2E_TESTING_DIR" npm stop 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 6a71fb0..00dc463 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,8 @@ { "extends": "@chatie/tsconfig", "compilerOptions": { - "outDir": "dist", - "resolveJsonModule": false, + "outDir": "dist/esm", + "lib": ["DOM", "ESNext"], }, "exclude": [ "node_modules/", @@ -10,9 +10,7 @@ "tests/fixtures/", ], "include": [ - "app/**/*.ts", "bin/*.ts", - "bot/**/*.ts", "examples/**/*.ts", "scripts/**/*.ts", "src/**/*.ts",