From 0c3b68370540f1a6e1987be15ff4aed2fe216c6f Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 4 Jan 2025 15:39:23 +0800 Subject: [PATCH] fix: export watcher type on eggjs/core namespace --- package.json | 19 ++++++++++++------- src/config/config.default.ts | 3 ++- src/config/config.local.ts | 4 +++- src/config/config.unittest.ts | 4 +++- src/lib/boot.ts | 7 +++---- src/lib/event-sources/index.ts | 12 +++++++++--- src/lib/types.ts | 30 +++++++++++++++++++----------- src/lib/watcher.ts | 9 +++++---- test/development.test.ts | 2 +- test/development_cluster.test.ts | 2 +- test/index.test.ts | 13 +++++++++++++ test/watcher.test.ts | 6 +++--- 12 files changed, 74 insertions(+), 37 deletions(-) create mode 100644 test/index.test.ts diff --git a/package.json b/package.json index e17ab83..fc6a935 100644 --- a/package.json +++ b/package.json @@ -9,16 +9,19 @@ "name": "watcher", "exports": { "import": "./dist/esm", - "require": "./dist/commonjs" + "require": "./dist/commonjs", + "typescript": "./src" } }, "scripts": { "lint": "eslint --cache src test --ext .ts", - "pretest": "npm run lint -- --fix && npm run prepublishOnly", + "pretest": "npm run clean && npm run lint -- --fix", "test": "egg-bin test", - "preci": "npm run lint && npm run prepublishOnly && attw --pack", + "preci": "npm run clean && npm run lint", "ci": "egg-bin cov", - "prepublishOnly": "tshy && tshy-after" + "postci": "npm run prepublishOnly && attw --pack && npm run clean", + "prepublishOnly": "tshy && tshy-after", + "clean": "rimraf dist" }, "homepage": "https://github.com/eggjs/watcher", "repository": { @@ -35,7 +38,8 @@ "node": ">= 18.19.0" }, "dependencies": { - "@eggjs/utils": "^4.0.3", + "@eggjs/core": "^6.2.13", + "@eggjs/utils": "^4.2.3", "camelcase": "^5.0.0", "sdk-base": "^5.0.0" }, @@ -45,8 +49,9 @@ "@types/node": "22", "@types/mocha": "10", "egg": "beta", - "egg-bin": "beta", - "egg-mock": "beta", + "@eggjs/bin": "7", + "@eggjs/mock": "6", + "rimraf": "6", "eslint": "8", "eslint-config-egg": "14", "tshy": "3", diff --git a/src/config/config.default.ts b/src/config/config.default.ts index 5a8a47a..a78ca3d 100644 --- a/src/config/config.default.ts +++ b/src/config/config.default.ts @@ -1,5 +1,6 @@ import path from 'node:path'; import { getSourceDirname } from '../lib/utils.js'; +import type { WatcherConfig } from '../lib/types.js'; export default { /** @@ -13,5 +14,5 @@ export default { default: path.join(getSourceDirname(), 'lib', 'event-sources', 'default'), development: path.join(getSourceDirname(), 'lib', 'event-sources', 'development'), }, - }, + } as WatcherConfig, }; diff --git a/src/config/config.local.ts b/src/config/config.local.ts index 1116b6a..fab825e 100644 --- a/src/config/config.local.ts +++ b/src/config/config.local.ts @@ -1,5 +1,7 @@ +import type { WatcherConfig } from '../lib/types.js'; + export default { watcher: { type: 'development', - }, + } as WatcherConfig, }; diff --git a/src/config/config.unittest.ts b/src/config/config.unittest.ts index 1116b6a..fab825e 100644 --- a/src/config/config.unittest.ts +++ b/src/config/config.unittest.ts @@ -1,5 +1,7 @@ +import type { WatcherConfig } from '../lib/types.js'; + export default { watcher: { type: 'development', - }, + } as WatcherConfig, }; diff --git a/src/lib/boot.ts b/src/lib/boot.ts index 2def2bb..ff4bc19 100644 --- a/src/lib/boot.ts +++ b/src/lib/boot.ts @@ -1,12 +1,11 @@ -import type { ILifecycleBoot } from 'egg'; -import { EggWatcherApplicationCore } from './types.js'; +import type { ILifecycleBoot, EggApplicationCore } from 'egg'; import { Watcher } from './watcher.js'; export class Boot implements ILifecycleBoot { - #app: EggWatcherApplicationCore; + #app: EggApplicationCore; #watcher: Watcher; - constructor(appOrAgent: EggWatcherApplicationCore) { + constructor(appOrAgent: EggApplicationCore) { this.#app = appOrAgent; this.#watcher = this.#app.watcher = this.#app.cluster(Watcher, {}) .delegate('watch', 'subscribe') diff --git a/src/lib/event-sources/index.ts b/src/lib/event-sources/index.ts index 7e89c71..da9ffa7 100644 --- a/src/lib/event-sources/index.ts +++ b/src/lib/event-sources/index.ts @@ -1,3 +1,9 @@ -export * from './base.js'; -export * from './default.js'; -export * from './development.js'; +import { BaseEventSource } from './base.js'; +import DefaultEventSource from './default.js'; +import DevelopmentEventSource from './development.js'; + +export { + BaseEventSource, + DefaultEventSource, + DevelopmentEventSource, +}; diff --git a/src/lib/types.ts b/src/lib/types.ts index 9609a44..9da6b19 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,16 +1,20 @@ import type { WatchEventType, Stats } from 'node:fs'; -import type { EggApplicationCore, EggAppConfig } from 'egg'; import type { Watcher } from './watcher.js'; export interface WatcherConfig { - watcher: { - type: string; - eventSources: Record; - }; - [key: string]: Record; + /** + * event source type, default is `default` + * can be `default` or `development` + */ + type: string; + /** + * event sources + * key is event source type, value is event source module path + */ + eventSources: Record; } -export interface ChangeInfo { +export interface ChangeInfo extends Record { event: WatchEventType; /** * file stat if path exists @@ -19,8 +23,12 @@ export interface ChangeInfo { path: string; } -export interface EggWatcherApplicationCore extends EggApplicationCore { - watcher: Watcher; -} +declare module '@eggjs/core' { + interface EggCore { + watcher: Watcher; + } -export type EggWatcherAppConfig = EggAppConfig & WatcherConfig; + interface EggAppConfig { + watcher: WatcherConfig; + } +} diff --git a/src/lib/watcher.ts b/src/lib/watcher.ts index 92ac2b1..dfc327e 100644 --- a/src/lib/watcher.ts +++ b/src/lib/watcher.ts @@ -2,19 +2,20 @@ import { debuglog } from 'node:util'; import { Base } from 'sdk-base'; import camelcase from 'camelcase'; import { importModule } from '@eggjs/utils'; +import type { EggAppConfig } from '@eggjs/core'; import { BaseEventSource } from './event-sources/base.js'; import { isEqualOrParentPath } from './utils.js'; -import type { ChangeInfo, WatcherConfig } from './types.js'; +import type { ChangeInfo } from './types.js'; const debug = debuglog('@eggjs/watcher/lib/watcher'); export type WatchListener = (info: ChangeInfo) => void; export class Watcher extends Base { - #config: WatcherConfig; + #config: EggAppConfig; #eventSource: BaseEventSource; - constructor(config: WatcherConfig) { + constructor(config: EggAppConfig) { super({ initMethod: '_init', }); @@ -23,7 +24,7 @@ export class Watcher extends Base { protected async _init() { const watcherType = this.#config.watcher.type; - let EventSource: typeof BaseEventSource = this.#config.watcher.eventSources[watcherType] as any; + let EventSource = this.#config.watcher.eventSources[watcherType] as unknown as typeof BaseEventSource; if (typeof EventSource === 'string') { EventSource = await importModule(EventSource, { importDefaultOnly: true, diff --git a/test/development.test.ts b/test/development.test.ts index 509119d..1dd8f5e 100644 --- a/test/development.test.ts +++ b/test/development.test.ts @@ -1,7 +1,7 @@ import fs from 'node:fs'; import { strict as assert } from 'node:assert'; import { scheduler } from 'node:timers/promises'; -import { mm, MockApplication } from 'egg-mock'; +import { mm, MockApplication } from '@eggjs/mock'; import { getFilePath } from './utils.js'; const file_path1 = getFilePath('apps/watcher-development-app/tmp.txt'); diff --git a/test/development_cluster.test.ts b/test/development_cluster.test.ts index 8bb4fb3..91ea5f7 100644 --- a/test/development_cluster.test.ts +++ b/test/development_cluster.test.ts @@ -1,7 +1,7 @@ import fs from 'node:fs'; import { strict as assert } from 'node:assert'; import { scheduler } from 'node:timers/promises'; -import { mm, MockApplication } from 'egg-mock'; +import { mm, MockApplication } from '@eggjs/mock'; import { getFilePath } from './utils.js'; const file_path1 = getFilePath('apps/watcher-development-app/tmp.txt'); diff --git a/test/index.test.ts b/test/index.test.ts new file mode 100644 index 0000000..3c2c612 --- /dev/null +++ b/test/index.test.ts @@ -0,0 +1,13 @@ +import { strict as assert } from 'node:assert'; +import * as watcher from '../src/index.js'; + +describe('test/index.test.ts', () => { + it('should exports work', async () => { + assert.deepEqual(Object.keys(watcher), [ + 'BaseEventSource', + 'DefaultEventSource', + 'DevelopmentEventSource', + 'Watcher', + ]); + }); +}); diff --git a/test/watcher.test.ts b/test/watcher.test.ts index 8fcd95b..e8e1b7b 100644 --- a/test/watcher.test.ts +++ b/test/watcher.test.ts @@ -1,6 +1,6 @@ import fs from 'node:fs'; import { strict as assert } from 'node:assert'; -import { mm, MockApplication } from 'egg-mock'; +import { mm, MockApplication } from '@eggjs/mock'; import { getFilePath } from './utils.js'; import { ChangeInfo } from '../src/index.js'; @@ -27,7 +27,7 @@ describe('test/watcher.test.ts', () => { await app.ready(); const p1 = new Promise(resolve => { - app.watcher.watch('xxxx', (info: ChangeInfo & { foo: string }) => { + app.watcher.watch('xxxx', (info: ChangeInfo) => { assert.equal(info.path, 'xxxx'); // ensure use config.custom assert.equal(info.foo, 'bar'); @@ -58,7 +58,7 @@ describe('test/watcher.test.ts', () => { await app.ready(); await new Promise(resolve => { - app.watcher.watch([ '/home/admin/xxx' ], (info: ChangeInfo & { foo: string }) => { + app.watcher.watch([ '/home/admin/xxx' ], (info: ChangeInfo) => { assert.equal(info.path, '/home/admin'); // ensure use config.custom