@eggjs/watcher 5.0.0-beta.15 → 5.0.0-beta.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,16 @@
1
- import { WatcherConfig } from "../lib/types.js";
2
-
3
1
  //#region src/config/config.default.d.ts
2
+ interface WatcherConfig {
3
+ /**
4
+ * event source type, default is `default`
5
+ * can be `default` or `development`
6
+ */
7
+ type: string;
8
+ /**
9
+ * event sources
10
+ * key is event source type, value is event source module path
11
+ */
12
+ eventSources: Record<string, string>;
13
+ }
4
14
  declare const _default: {
5
15
  /**
6
16
  * watcher options
@@ -10,4 +20,4 @@ declare const _default: {
10
20
  watcher: WatcherConfig;
11
21
  };
12
22
  //#endregion
13
- export { _default as default };
23
+ export { WatcherConfig, _default as default };
@@ -1,12 +1,11 @@
1
- import { getSourceDirname } from "../lib/utils.js";
2
1
  import path from "node:path";
3
2
 
4
3
  //#region src/config/config.default.ts
5
4
  var config_default_default = { watcher: {
6
5
  type: "default",
7
6
  eventSources: {
8
- default: path.join(getSourceDirname(), "lib", "event-sources", "default"),
9
- development: path.join(getSourceDirname(), "lib", "event-sources", "development")
7
+ default: path.join(import.meta.dirname, "../lib/event-sources/default"),
8
+ development: path.join(import.meta.dirname, "../lib/event-sources/development")
10
9
  }
11
10
  } };
12
11
 
@@ -1,8 +1,6 @@
1
- import { WatcherConfig } from "../lib/types.js";
1
+ import { PartialEggConfig } from "egg";
2
2
 
3
3
  //#region src/config/config.local.d.ts
4
- declare const _default: {
5
- watcher: WatcherConfig;
6
- };
4
+ declare const _default: PartialEggConfig;
7
5
  //#endregion
8
6
  export { _default as default };
@@ -1,8 +1,6 @@
1
- import { WatcherConfig } from "../lib/types.js";
1
+ import { PartialEggConfig } from "egg";
2
2
 
3
3
  //#region src/config/config.unittest.d.ts
4
- declare const _default: {
5
- watcher: WatcherConfig;
6
- };
4
+ declare const _default: PartialEggConfig;
7
5
  //#endregion
8
6
  export { _default as default };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { WatchListener, Watcher } from "./lib/watcher.js";
2
- import { ChangeInfo, WatcherConfig } from "./lib/types.js";
1
+ import { ChangeInfo, WatchListener, Watcher } from "./lib/watcher.js";
3
2
  import { BaseEventSource } from "./lib/event-sources/base.js";
4
3
  import DefaultEventSource from "./lib/event-sources/default.js";
5
4
  import DevelopmentEventSource from "./lib/event-sources/development.js";
6
- export { BaseEventSource, ChangeInfo, DefaultEventSource, DevelopmentEventSource, WatchListener, Watcher, WatcherConfig };
5
+ export { BaseEventSource, ChangeInfo, DefaultEventSource, DevelopmentEventSource, WatchListener, Watcher };
package/dist/lib/boot.js CHANGED
@@ -1,16 +1,23 @@
1
1
  import { Watcher } from "./watcher.js";
2
+ import { debuglog } from "node:util";
2
3
 
3
4
  //#region src/lib/boot.ts
5
+ const debug = debuglog("egg/watcher/lib/boot");
4
6
  var Boot = class {
5
7
  #app;
6
- #watcher;
7
8
  constructor(appOrAgent) {
8
9
  this.#app = appOrAgent;
9
- this.#watcher = this.#app.watcher = this.#app.cluster(Watcher, {}).delegate("watch", "subscribe").create(appOrAgent.config);
10
- this.#watcher.on("info", (msg, ...args) => this.#app.coreLogger.info(msg, ...args)).on("warn", (msg, ...args) => this.#app.coreLogger.warn(msg, ...args)).on("error", (msg, ...args) => this.#app.coreLogger.error(msg, ...args));
10
+ if (this.#app.options.mode === "all-in-one-process") {
11
+ debug("init watcher in all-in-one-process mode");
12
+ this.#app.watcher = new Watcher(appOrAgent.config);
13
+ } else {
14
+ debug("init watcher in cluster mode");
15
+ this.#app.watcher = this.#app.clusterWrapper(Watcher, {}).delegate("watch", "subscribe").create(appOrAgent.config);
16
+ }
17
+ this.#app.watcher.on("info", (msg, ...args) => this.#app.coreLogger.info(msg, ...args)).on("warn", (msg, ...args) => this.#app.coreLogger.warn(msg, ...args)).on("error", (msg, ...args) => this.#app.coreLogger.error(msg, ...args));
11
18
  }
12
19
  async didLoad() {
13
- await this.#watcher.ready();
20
+ await this.#app.watcher.ready();
14
21
  this.#app.coreLogger.info("[@eggjs/watcher:%s] watcher start success", this.#app.type);
15
22
  }
16
23
  };
@@ -1,5 +1,4 @@
1
1
  //#region src/lib/utils.d.ts
2
2
  declare function isEqualOrParentPath(parent: string, child: string): boolean;
3
- declare function getSourceDirname(): string;
4
3
  //#endregion
5
- export { getSourceDirname, isEqualOrParentPath };
4
+ export { isEqualOrParentPath };
package/dist/lib/utils.js CHANGED
@@ -1,15 +1,9 @@
1
1
  import path from "node:path";
2
- import { fileURLToPath } from "node:url";
3
2
 
4
3
  //#region src/lib/utils.ts
5
4
  function isEqualOrParentPath(parent, child) {
6
5
  return !path.relative(parent, child).startsWith("..");
7
6
  }
8
- function getSourceDirname() {
9
- if (typeof __dirname === "string") return path.dirname(__dirname);
10
- const __filename = fileURLToPath(import.meta.url);
11
- return path.dirname(path.dirname(__filename));
12
- }
13
7
 
14
8
  //#endregion
15
- export { getSourceDirname, isEqualOrParentPath };
9
+ export { isEqualOrParentPath };
@@ -1,8 +1,17 @@
1
- import { ChangeInfo } from "./types.js";
2
1
  import { Base } from "sdk-base";
2
+ import { Stats, WatchEventType } from "node:fs";
3
3
  import { EggAppConfig } from "egg";
4
4
 
5
5
  //#region src/lib/watcher.d.ts
6
+ interface ChangeInfo extends Record<string, any> {
7
+ event: WatchEventType;
8
+ /**
9
+ * file stat if path exists
10
+ */
11
+ stat?: Stats;
12
+ path: string;
13
+ isDirectory?: boolean;
14
+ }
6
15
  type WatchListener = (info: ChangeInfo) => void;
7
16
  declare class Watcher extends Base {
8
17
  #private;
@@ -11,4 +20,4 @@ declare class Watcher extends Base {
11
20
  watch(path: string | string[], listener: WatchListener): void;
12
21
  }
13
22
  //#endregion
14
- export { WatchListener, Watcher };
23
+ export { ChangeInfo, WatchListener, Watcher };
@@ -6,7 +6,7 @@ import camelcase from "camelcase";
6
6
  import { importModule } from "@eggjs/utils";
7
7
 
8
8
  //#region src/lib/watcher.ts
9
- const debug = debuglog("egg-watcher/lib/watcher");
9
+ const debug = debuglog("egg/watcher/lib/watcher");
10
10
  var Watcher = class extends Base {
11
11
  #config;
12
12
  #eventSource;
@@ -16,7 +16,11 @@ var Watcher = class extends Base {
16
16
  }
17
17
  async _init() {
18
18
  const watcherType = this.#config.watcher?.type;
19
- if (!watcherType) return;
19
+ debug("init with watcherType %o", watcherType);
20
+ if (!watcherType) {
21
+ debug("watcherType is not defined, skip initialization");
22
+ return;
23
+ }
20
24
  let EventSource = this.#config.watcher?.eventSources[watcherType];
21
25
  if (typeof EventSource === "string") EventSource = await importModule(EventSource, { importDefaultOnly: true });
22
26
  const key = camelcase(["watcher", watcherType]);
@@ -26,6 +30,7 @@ var Watcher = class extends Base {
26
30
  await this.#eventSource.ready();
27
31
  }
28
32
  watch(path, listener) {
33
+ debug("watch %o", path);
29
34
  this.emit("info", "[@eggjs/watcher] Start watching: %j", path);
30
35
  if (!path) return;
31
36
  if (Array.isArray(path)) {
@@ -0,0 +1,12 @@
1
+ import { WatcherConfig } from "./config/config.default.js";
2
+ import { Watcher } from "./lib/watcher.js";
3
+
4
+ //#region src/types.d.ts
5
+ declare module 'egg' {
6
+ interface EggApplicationCore {
7
+ watcher: Watcher;
8
+ }
9
+ interface EggAppConfig {
10
+ watcher?: WatcherConfig;
11
+ }
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eggjs/watcher",
3
- "version": "5.0.0-beta.15",
3
+ "version": "5.0.0-beta.18",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -26,18 +26,18 @@
26
26
  "dependencies": {
27
27
  "camelcase": "^8.0.0",
28
28
  "sdk-base": "^5.0.1",
29
- "@eggjs/utils": "5.0.0-beta.15"
29
+ "@eggjs/utils": "5.0.0-beta.18"
30
30
  },
31
31
  "peerDependencies": {
32
- "egg": "4.1.0-beta.15"
32
+ "egg": "4.1.0-beta.18"
33
33
  },
34
34
  "devDependencies": {
35
35
  "tsdown": "^0.15.4",
36
36
  "typescript": "5.9.2",
37
37
  "vitest": "4.0.0-beta.13",
38
- "@eggjs/tsconfig": "3.1.0-beta.15",
39
- "@eggjs/mock": "7.0.0-beta.15",
40
- "@eggjs/utils": "5.0.0-beta.15"
38
+ "@eggjs/mock": "7.0.0-beta.18",
39
+ "@eggjs/tsconfig": "3.1.0-beta.18",
40
+ "@eggjs/utils": "5.0.0-beta.18"
41
41
  },
42
42
  "type": "module",
43
43
  "exports": {
@@ -52,9 +52,9 @@
52
52
  "./lib/event-sources/base": "./dist/lib/event-sources/base.js",
53
53
  "./lib/event-sources/default": "./dist/lib/event-sources/default.js",
54
54
  "./lib/event-sources/development": "./dist/lib/event-sources/development.js",
55
- "./lib/types": "./dist/lib/types.js",
56
55
  "./lib/utils": "./dist/lib/utils.js",
57
56
  "./lib/watcher": "./dist/lib/watcher.js",
57
+ "./types": "./dist/types.js",
58
58
  "./package.json": "./package.json"
59
59
  },
60
60
  "files": [
@@ -1,35 +0,0 @@
1
- import { Watcher } from "./watcher.js";
2
- import { Stats, WatchEventType } from "node:fs";
3
-
4
- //#region src/lib/types.d.ts
5
- interface WatcherConfig {
6
- /**
7
- * event source type, default is `default`
8
- * can be `default` or `development`
9
- */
10
- type: string;
11
- /**
12
- * event sources
13
- * key is event source type, value is event source module path
14
- */
15
- eventSources: Record<string, string>;
16
- }
17
- interface ChangeInfo extends Record<string, any> {
18
- event: WatchEventType;
19
- /**
20
- * file stat if path exists
21
- */
22
- stat?: Stats;
23
- path: string;
24
- isDirectory?: boolean;
25
- }
26
- declare module 'egg' {
27
- interface EggApplicationCore {
28
- watcher: Watcher;
29
- }
30
- interface EggAppConfig {
31
- watcher?: WatcherConfig;
32
- }
33
- }
34
- //#endregion
35
- export { ChangeInfo, WatcherConfig };
File without changes