@enshou/cron 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Copyright 2026 Ivan Popov <iiivanpopov999@gmail.com>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4
+
5
+ THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@@ -0,0 +1,22 @@
1
+ import { Plugin } from "@enshou/core";
2
+
3
+ //#region ../../shared/src/types.d.ts
4
+ type AnyFunction = (...args: any[]) => any;
5
+ type Class<T> = new (...args: any[]) => T;
6
+ //#endregion
7
+ //#region src/decorators.d.ts
8
+ type CronDecorator = {
9
+ (_value: AnyFunction, context: ClassMethodDecoratorContext<object, AnyFunction>): void;
10
+ (_value: undefined, context: ClassFieldDecoratorContext<object, AnyFunction>): (initialValue: AnyFunction) => AnyFunction;
11
+ };
12
+ declare function Cron(pattern: Bun.CronWithAutocomplete): CronDecorator;
13
+ //#endregion
14
+ //#region src/plugin.d.ts
15
+ interface CronPluginOptions {
16
+ jobs: Class<any>[];
17
+ }
18
+ declare function CronPlugin({
19
+ jobs
20
+ }: CronPluginOptions): Plugin;
21
+ //#endregion
22
+ export { Cron, CronPlugin, CronPluginOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,39 @@
1
+ import { createToken } from "@enshou/di";
2
+ //#region ../../shared/src/polyfill.ts
3
+ if (!Symbol.metadata) Symbol.metadata = Symbol.for("Symbol.metadata");
4
+ //#endregion
5
+ //#region src/metadata.ts
6
+ function asCronMetadata(metadata) {
7
+ metadata.jobs ??= /* @__PURE__ */ new Map();
8
+ return metadata;
9
+ }
10
+ //#endregion
11
+ //#region src/decorators.ts
12
+ function Cron(pattern) {
13
+ function decorator(_value, context) {
14
+ const controllerMetadata = asCronMetadata(context.metadata);
15
+ const methodName = String(context.name);
16
+ controllerMetadata.jobs.set(methodName, pattern);
17
+ if (context.kind === "method") return;
18
+ return (initialValue) => initialValue;
19
+ }
20
+ return decorator;
21
+ }
22
+ //#endregion
23
+ //#region src/plugin.ts
24
+ function CronPlugin({ jobs }) {
25
+ return { onApplicationInit: async ({ options: { container } }) => {
26
+ for (const job of jobs) {
27
+ const token = createToken(job.name);
28
+ container.registerClass(token, job);
29
+ const instance = await container.resolveAsync(token);
30
+ const metadata = asCronMetadata(instance[Symbol.metadata]);
31
+ for (const [methodName, cronPattern] of metadata.jobs) {
32
+ const handler = instance[methodName].bind(instance);
33
+ Bun.cron(cronPattern, handler);
34
+ }
35
+ }
36
+ } };
37
+ }
38
+ //#endregion
39
+ export { Cron, CronPlugin };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@enshou/cron",
3
+ "version": "0.0.2",
4
+ "private": false,
5
+ "license": "ISC",
6
+ "author": "Ivan Popov <iiivanpopov999@gmail.com>",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/enshoujs/enshou.git"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "README.md",
14
+ "LICENSE"
15
+ ],
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.mts",
20
+ "import": "./dist/index.mjs"
21
+ }
22
+ },
23
+ "devDependencies": {
24
+ "@types/bun": "^1.3.14",
25
+ "tsdown": "^0.22.3",
26
+ "typescript": "^7.0.2",
27
+ "@enshou/core": "0.12.4",
28
+ "@enshou/di": "0.8.3"
29
+ },
30
+ "peerDependencies": {
31
+ "@enshou/core": "0.12.4",
32
+ "@enshou/di": "0.8.3"
33
+ },
34
+ "scripts": {
35
+ "build": "tsdown"
36
+ }
37
+ }