Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 18, 2024
1 parent 10b5137 commit 47472ef
Show file tree
Hide file tree
Showing 46 changed files with 646 additions and 428 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest'
version: '18, 20, 22'
version: '18.19.0, 18, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Any Commit
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Build
run: npm run prepublishOnly --if-present

- run: npx pkg-pr-new publish
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ Say we want to build a custom event source plugin (package name: `egg-watcher-cu

Firstly define our custom event source like this:

```js
```ts
// {plugin_root}/lib/custom_event_source.js
const Base = require('sdk-base');
import { Base } from 'sdk-base';

class CustomEventSource extends Base {
export default class CustomEventSource extends Base {
// `opts` comes from app.config[${eventSourceName}]
// `eventSourceName` will be registered later in
// `config.watcher.eventSources` as the key shown below
Expand All @@ -94,8 +94,6 @@ class CustomEventSource extends Base {
}
}
}

module.exports = CustomEventSource;
```

Event source implementations varies according to your running environment. When working with vagrant, docker, samba or such other non-standard way of development, you should use a different watch API specific to what you are working with.
Expand All @@ -104,9 +102,13 @@ Then add your custom event source to config:

```js
// config/config.default.js
exports.watcher = {
eventSources: {
custom: require('../lib/custom_event_source'),
import CustomEventSource from '../lib/custom_event_source';

export default {
watcher: {
eventSources: {
custom: CustomEventSource,
},
},
};
```
Expand All @@ -115,13 +117,16 @@ Choose to use your custom watching mode in your desired env.

```js
// config/config.${env}.js
exports.watcher = {
type: 'custom',
};

// this will pass to your CustomEventSource constructor as opts
exports.watcherCustom = {
// foo: 'bar',
export default {
watcher: {
type: 'custom',
},

// this will pass to your CustomEventSource constructor as opts
watcherCustom: {
// foo: 'bar',
},
};
```

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"node": ">= 18.19.0"
},
"dependencies": {
"@eggjs/utils": "^4.0.3",
"camelcase": "^5.0.0",
"sdk-base": "^4.2.1",
"wt": "^1.2.0"
"sdk-base": "^5.0.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.1",
Expand Down Expand Up @@ -78,5 +78,6 @@
"src"
],
"types": "./dist/commonjs/index.d.ts",
"main": "./dist/commonjs/index.js"
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js"
}
1 change: 0 additions & 1 deletion src/agent.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Boot } from './lib/boot.js';

export default Boot;
3 changes: 0 additions & 3 deletions src/app.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Boot } from './lib/boot.js';

export default Boot;
16 changes: 0 additions & 16 deletions src/config/config.default.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'node:path';
import { getSourceDirname } from '../lib/utils.js';

export default {
/**
* watcher options
* @member Config#watcher
* @property {string} type - event source type
*/
watcher: {
type: 'default', // default event source
eventSources: {
default: path.join(getSourceDirname(), 'lib', 'event-sources', 'default'),
development: path.join(getSourceDirname(), 'lib', 'event-sources', 'development'),
},
},
};
5 changes: 0 additions & 5 deletions src/config/config.local.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/config/config.local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
watcher: {
type: 'development',
},
};
5 changes: 0 additions & 5 deletions src/config/config.unittest.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/config/config.unittest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
watcher: {
type: 'development',
},
};
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './lib/types.js';
export * from './lib/watcher.js';
export * from './lib/event-sources/index.js';
22 changes: 22 additions & 0 deletions src/lib/boot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Application, Agent, ILifecycleBoot } from 'egg';
import { Watcher } from './watcher.js';

export class Boot implements ILifecycleBoot {
#app: Application | Agent;
#watcher: Watcher;

constructor(appOrAgent: Application | Agent) {
this.#app = appOrAgent;
this.#watcher = this.#app.watcher = this.#app.cluster(Watcher, {})
.delegate('watch', 'subscribe')
.create(appOrAgent.config)
.on('info', (msg: string, ...args: any[]) => this.#app.coreLogger.info(msg, ...args))
.on('warn', (msg: string, ...args: any[]) => this.#app.coreLogger.warn(msg, ...args))
.on('error', (msg: string, ...args: any[]) => this.#app.coreLogger.error(msg, ...args));
}

async didLoad(): Promise<void> {
await this.#watcher.ready();
this.#app.coreLogger.info('[@eggjs/watcher:%s] watcher start success', this.#app.type);
}
}
6 changes: 6 additions & 0 deletions src/lib/event-sources/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Base } from 'sdk-base';

export abstract class BaseEventSource extends Base {
abstract watch(file: string): void;
abstract unwatch(file: string): void;
}
24 changes: 0 additions & 24 deletions src/lib/event-sources/default.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/lib/event-sources/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BaseEventSource } from './base.js';

export default class DefaultEventSource extends BaseEventSource {
constructor() {
super();
// delay emit so that can be listened
setImmediate(() => this.emit('info', '[@eggjs/watcher] defaultEventSource watcher will NOT take effect'));
this.ready(true);
}

watch() {
this.emit('info', '[@eggjs/watcher] using defaultEventSource watcher.watch() does NOTHING');
}

unwatch() {
this.emit('info', '[@eggjs/watcher] using defaultEventSource watcher.unwatch() does NOTHING');
}
}
89 changes: 0 additions & 89 deletions src/lib/event-sources/development.js

This file was deleted.

Loading

0 comments on commit 47472ef

Please sign in to comment.