@eggjs/watcher 4.0.4 → 4.0.5-beta.2

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.
Files changed (102) hide show
  1. package/README.md +1 -5
  2. package/dist/agent.d.ts +2 -0
  3. package/dist/agent.js +7 -0
  4. package/dist/app.d.ts +2 -0
  5. package/dist/app.js +7 -0
  6. package/dist/config/config.default.d.ts +13 -0
  7. package/dist/config/config.default.js +14 -0
  8. package/dist/config/config.local.d.ts +8 -0
  9. package/dist/config/config.local.js +5 -0
  10. package/dist/config/config.unittest.d.ts +8 -0
  11. package/dist/config/config.unittest.js +5 -0
  12. package/dist/index.d.ts +6 -0
  13. package/dist/index.js +7 -0
  14. package/dist/lib/boot.d.ts +10 -0
  15. package/dist/lib/boot.js +19 -0
  16. package/dist/lib/event-sources/base.d.ts +9 -0
  17. package/dist/lib/event-sources/base.js +7 -0
  18. package/dist/lib/event-sources/default.d.ts +10 -0
  19. package/dist/lib/event-sources/default.js +19 -0
  20. package/dist/lib/event-sources/development.d.ts +11 -0
  21. package/dist/lib/event-sources/development.js +66 -0
  22. package/dist/lib/event-sources/index.d.ts +4 -0
  23. package/dist/lib/event-sources/index.js +5 -0
  24. package/{src/lib/types.ts → dist/lib/types.d.ts} +10 -12
  25. package/dist/lib/types.js +1 -0
  26. package/dist/lib/utils.d.ts +5 -0
  27. package/dist/lib/utils.js +15 -0
  28. package/dist/lib/watcher.d.ts +14 -0
  29. package/dist/lib/watcher.js +59 -0
  30. package/package.json +49 -59
  31. package/dist/commonjs/agent.d.ts +0 -2
  32. package/dist/commonjs/agent.js +0 -5
  33. package/dist/commonjs/app.d.ts +0 -2
  34. package/dist/commonjs/app.js +0 -5
  35. package/dist/commonjs/config/config.default.d.ts +0 -10
  36. package/dist/commonjs/config/config.default.js +0 -22
  37. package/dist/commonjs/config/config.local.d.ts +0 -5
  38. package/dist/commonjs/config/config.local.js +0 -8
  39. package/dist/commonjs/config/config.unittest.d.ts +0 -5
  40. package/dist/commonjs/config/config.unittest.js +0 -8
  41. package/dist/commonjs/index.d.ts +0 -3
  42. package/dist/commonjs/index.js +0 -20
  43. package/dist/commonjs/lib/boot.d.ts +0 -6
  44. package/dist/commonjs/lib/boot.js +0 -23
  45. package/dist/commonjs/lib/event-sources/base.d.ts +0 -5
  46. package/dist/commonjs/lib/event-sources/base.js +0 -8
  47. package/dist/commonjs/lib/event-sources/default.d.ts +0 -6
  48. package/dist/commonjs/lib/event-sources/default.js +0 -19
  49. package/dist/commonjs/lib/event-sources/development.d.ts +0 -7
  50. package/dist/commonjs/lib/event-sources/development.js +0 -103
  51. package/dist/commonjs/lib/event-sources/index.d.ts +0 -4
  52. package/dist/commonjs/lib/event-sources/index.js +0 -13
  53. package/dist/commonjs/lib/types.d.ts +0 -31
  54. package/dist/commonjs/lib/types.js +0 -3
  55. package/dist/commonjs/lib/utils.d.ts +0 -2
  56. package/dist/commonjs/lib/utils.js +0 -25
  57. package/dist/commonjs/lib/watcher.d.ts +0 -10
  58. package/dist/commonjs/lib/watcher.js +0 -113
  59. package/dist/commonjs/package.json +0 -3
  60. package/dist/esm/agent.d.ts +0 -2
  61. package/dist/esm/agent.js +0 -3
  62. package/dist/esm/app.d.ts +0 -2
  63. package/dist/esm/app.js +0 -3
  64. package/dist/esm/config/config.default.d.ts +0 -10
  65. package/dist/esm/config/config.default.js +0 -17
  66. package/dist/esm/config/config.local.d.ts +0 -5
  67. package/dist/esm/config/config.local.js +0 -6
  68. package/dist/esm/config/config.unittest.d.ts +0 -5
  69. package/dist/esm/config/config.unittest.js +0 -6
  70. package/dist/esm/index.d.ts +0 -3
  71. package/dist/esm/index.js +0 -4
  72. package/dist/esm/lib/boot.d.ts +0 -6
  73. package/dist/esm/lib/boot.js +0 -19
  74. package/dist/esm/lib/event-sources/base.d.ts +0 -5
  75. package/dist/esm/lib/event-sources/base.js +0 -4
  76. package/dist/esm/lib/event-sources/default.d.ts +0 -6
  77. package/dist/esm/lib/event-sources/default.js +0 -16
  78. package/dist/esm/lib/event-sources/development.d.ts +0 -7
  79. package/dist/esm/lib/event-sources/development.js +0 -97
  80. package/dist/esm/lib/event-sources/index.d.ts +0 -4
  81. package/dist/esm/lib/event-sources/index.js +0 -5
  82. package/dist/esm/lib/types.d.ts +0 -31
  83. package/dist/esm/lib/types.js +0 -2
  84. package/dist/esm/lib/utils.d.ts +0 -2
  85. package/dist/esm/lib/utils.js +0 -18
  86. package/dist/esm/lib/watcher.d.ts +0 -10
  87. package/dist/esm/lib/watcher.js +0 -106
  88. package/dist/esm/package.json +0 -3
  89. package/dist/package.json +0 -4
  90. package/src/agent.ts +0 -3
  91. package/src/app.ts +0 -3
  92. package/src/config/config.default.ts +0 -18
  93. package/src/config/config.local.ts +0 -7
  94. package/src/config/config.unittest.ts +0 -7
  95. package/src/index.ts +0 -3
  96. package/src/lib/boot.ts +0 -22
  97. package/src/lib/event-sources/base.ts +0 -6
  98. package/src/lib/event-sources/default.ts +0 -18
  99. package/src/lib/event-sources/development.ts +0 -101
  100. package/src/lib/event-sources/index.ts +0 -9
  101. package/src/lib/utils.ts +0 -20
  102. package/src/lib/watcher.ts +0 -120
@@ -1,120 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import { Base } from 'sdk-base';
3
- import camelcase from 'camelcase';
4
- import { importModule } from '@eggjs/utils';
5
- import type { EggAppConfig } from '@eggjs/core';
6
- import { BaseEventSource } from './event-sources/base.js';
7
- import { isEqualOrParentPath } from './utils.js';
8
- import type { ChangeInfo } from './types.js';
9
-
10
- const debug = debuglog('@eggjs/watcher/lib/watcher');
11
-
12
- export type WatchListener = (info: ChangeInfo) => void;
13
-
14
- export class Watcher extends Base {
15
- #config: EggAppConfig;
16
- #eventSource: BaseEventSource;
17
-
18
- constructor(config: EggAppConfig) {
19
- super({
20
- initMethod: '_init',
21
- });
22
- this.#config = config;
23
- }
24
-
25
- protected async _init() {
26
- const watcherType = this.#config.watcher.type;
27
- let EventSource = this.#config.watcher.eventSources[watcherType] as unknown as typeof BaseEventSource;
28
- if (typeof EventSource === 'string') {
29
- EventSource = await importModule(EventSource, {
30
- importDefaultOnly: true,
31
- });
32
- }
33
-
34
- // chokidar => watcherChokidar
35
- // custom => watcherCustom
36
- //
37
- // e.g:
38
- // config => { watcher: { type: 'custom' }, watcherCustom: { ... } }
39
- const key = camelcase([ 'watcher', watcherType ]);
40
- const eventSourceOptions = this.#config[key] ?? {};
41
- this.#eventSource = Reflect.construct(EventSource, [ eventSourceOptions ]);
42
- this.#eventSource.on('change', this.#onChange.bind(this))
43
- .on('fuzzy-change', this.#onFuzzyChange.bind(this))
44
- .on('info', (...args) => this.emit('info', ...args))
45
- .on('warn', (...args) => this.emit('warn', ...args))
46
- .on('error', (...args) => this.emit('error', ...args));
47
- await this.#eventSource.ready();
48
- }
49
-
50
- watch(path: string | string[], listener: WatchListener) {
51
- this.emit('info', '[@eggjs/watcher] Start watching: %j', path);
52
- if (!path) return;
53
-
54
- // support array
55
- if (Array.isArray(path)) {
56
- path.forEach(p => this.watch(p, listener));
57
- return;
58
- }
59
-
60
- // one file only watch once
61
- if (!this.listenerCount(path)) {
62
- this.#eventSource.watch(path);
63
- }
64
- this.on(path, listener);
65
- }
66
-
67
- /*
68
- // TODO wait unsubscribe implementation of cluster-client
69
- unwatch(path, callback) {
70
- if (!path) return;
71
-
72
- // support array
73
- if (Array.isArray(path)) {
74
- path.forEach(p => this.unwatch(p, callback));
75
- return;
76
- }
77
-
78
- if (callback) {
79
- this.removeListener(path, callback);
80
- // stop watching when no listener bound to the path
81
- if (this.listenerCount(path) === 0) {
82
- this._eventSource.unwatch(path);
83
- }
84
- return;
85
- }
86
-
87
- this.removeAllListeners(path);
88
- this._eventSource.unwatch(path);
89
- }
90
- */
91
-
92
- #onChange(info: ChangeInfo) {
93
- debug('onChange %o', info);
94
- this.emit('info', '[@eggjs/watcher] Received a change event from eventSource: %j', info);
95
- const path = info.path;
96
-
97
- for (const p of this.eventNames()) {
98
- if (typeof p !== 'string') continue;
99
- // if it is a sub path, emit a `change` event
100
- if (isEqualOrParentPath(p, path)) {
101
- this.emit(p, info);
102
- }
103
- }
104
- }
105
-
106
- #onFuzzyChange(info: ChangeInfo) {
107
- debug('onFuzzyChange %o', info);
108
- this.emit('info', '[@eggjs/watcher] Received a fuzzy-change event from eventSource: %j', info);
109
- const path = info.path;
110
-
111
- for (const p of this.eventNames()) {
112
- if (typeof p !== 'string') continue;
113
- // if it is a parent path, emit a `change` event
114
- // just the opposite to `_onChange`
115
- if (isEqualOrParentPath(path, p)) {
116
- this.emit(p, info);
117
- }
118
- }
119
- }
120
- }