@kingsworld/plugin-cron 3.0.1-next.e1b3c15 → 3.0.2-next.69cca0b

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,65 +1,126 @@
1
1
  import { __name } from '../../chunk-2JTKI4GS.mjs';
2
2
  import { Store } from '@sapphire/pieces';
3
- import { CronJob } from 'cron';
3
+ import { Cron } from 'croner';
4
4
  import { CronTask } from './CronTask.mjs';
5
+ import { normalizePattern } from '../utils/normalizePattern.mjs';
5
6
 
6
7
  var _CronTaskStore = class _CronTaskStore extends Store {
7
8
  constructor() {
8
9
  super(CronTask, { name: "cron-tasks" });
9
10
  }
10
11
  /**
11
- * Loops over all tasks and starts those that are enabled.
12
- * This gets called automatically when the Client is ready.
12
+ * Loops over all tasks and pauses those that are running.
13
+ *
14
+ * @remarks
15
+ * This method will only pause tasks that:
16
+ * - Are enabled
17
+ * - Are currently running
18
+ * - Have not been permanently stopped
19
+ *
13
20
  * @returns CronTaskStore
14
21
  */
15
- startAll() {
22
+ pauseAll() {
16
23
  for (const task of this.values()) {
17
- if (!task.enabled) continue;
18
- task.job.start();
24
+ if (!task.enabled || !task.job.isRunning()) continue;
25
+ task.job.pause();
19
26
  }
20
- Store.logger?.(`[STORE => ${this.name}] [START] Started all cronjob tasks.`);
27
+ Store.logger?.(`[STORE => ${this.name}] [PAUSE] Paused all cronjob tasks.`);
28
+ return this;
29
+ }
30
+ /**
31
+ * Loops over all tasks and resumes those that are paused.
32
+ *
33
+ * @remarks
34
+ * This method will only resume tasks that:
35
+ * - Are enabled
36
+ * - Are not currently running
37
+ * - Have not been permanently stopped
38
+ *
39
+ * @returns CronTaskStore
40
+ */
41
+ resumeAll() {
42
+ for (const task of this.values()) {
43
+ if (!task.enabled || task.job.isRunning() || task.job.isStopped()) continue;
44
+ task.job.resume();
45
+ }
46
+ Store.logger?.(`[STORE => ${this.name}] [RESUME] Resumed all cronjob tasks.`);
21
47
  return this;
22
48
  }
23
49
  /**
24
50
  * Loops over all tasks and stops those that are running.
51
+ *
52
+ * @remarks
53
+ * This method will only stop tasks that:
54
+ * - Are enabled
55
+ * - Have not been permanently stopped
56
+ *
57
+ * ⚠️ Stopping jobs is **permanent** and cannot be resumed afterwards!
58
+ *
25
59
  * @returns CronTaskStore
26
60
  */
27
61
  stopAll() {
28
62
  for (const task of this.values()) {
29
- if (!task.job.running) continue;
63
+ if (!task.enabled || task.job.isStopped()) continue;
30
64
  task.job.stop();
31
65
  }
32
66
  Store.logger?.(`[STORE => ${this.name}] [STOP] Stopped all cronjob tasks.`);
33
67
  return this;
34
68
  }
35
69
  set(key, value) {
36
- const { options } = value;
37
- const { sentry, defaultTimezone } = this.container.cron;
38
- const cronJob = sentry ? sentry.cron.instrumentCron(CronJob, key) : CronJob;
70
+ const { pattern, timezone, protect, ...options } = value.options;
71
+ const { sentry, defaultTimezone } = this.container.cronTasks;
72
+ if (this.has(key)) {
73
+ Store.logger?.(`[STORE => ${this.name}] [SET] Stopping existing cronjob task before creating a new one.`);
74
+ this.get(key)?.job.stop();
75
+ }
76
+ const timeZone = timezone ?? defaultTimezone;
39
77
  try {
40
- value.job = cronJob.from({
41
- ...options,
42
- onTick: /* @__PURE__ */ __name(() => void value.run.bind(value)(), "onTick"),
43
- start: false,
44
- context: value,
45
- timeZone: options.timeZone ?? defaultTimezone
46
- });
78
+ Store.logger?.(
79
+ `[STORE => ${this.name}] [SET] Creating cronjob for ${key} with '${pattern}' as the pattern and '${timeZone}' for the timezone`
80
+ );
81
+ value.job = new Cron(
82
+ pattern,
83
+ {
84
+ name: key,
85
+ timezone: timeZone,
86
+ paused: true,
87
+ // we start the job manually once the client is ready
88
+ protect: value.protect ? (job) => void value.protect(job) : protect,
89
+ catch: /* @__PURE__ */ __name((error) => {
90
+ value.error("Encountered an error while running the cron job", error);
91
+ if (sentry) sentry.captureException(error);
92
+ void value.catch?.(error, value.job);
93
+ }, "catch"),
94
+ ...options
95
+ },
96
+ () => {
97
+ if (sentry && typeof pattern === "string" && !pattern.includes(":")) {
98
+ return sentry.withMonitor(key, () => void value.run.bind(value)(), {
99
+ schedule: { type: "crontab", value: pattern },
100
+ timezone: timeZone ? normalizePattern(timeZone) : void 0
101
+ });
102
+ }
103
+ return value.run.bind(value)();
104
+ }
105
+ );
47
106
  } catch (error) {
48
107
  value.error("Encountered an error while creating the cron job", error);
49
108
  void value.unload();
50
109
  }
51
110
  return super.set(key, value);
52
111
  }
112
+ /**
113
+ * Deletes a task from the store and stops it if it's running.
114
+ */
53
115
  delete(key) {
54
116
  const task = this.get(key);
55
- if (task?.job.running) {
117
+ if (task && !task.job.isStopped()) {
56
118
  task.job.stop();
57
119
  }
58
120
  return super.delete(key);
59
121
  }
60
122
  /**
61
123
  * Stops all running cron jobs and clears the store.
62
- * @returns void
63
124
  */
64
125
  clear() {
65
126
  this.stopAll();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/structures/CronTaskStore.ts"],"names":[],"mappings":";;;;;AAIO,IAAM,cAAA,GAAN,MAAM,cAAA,SAAsB,KAA8B,CAAA;AAAA,EACzD,WAAc,GAAA;AACpB,IAAA,KAAA,CAAM,QAAU,EAAA,EAAE,IAAM,EAAA,YAAA,EAAc,CAAA,CAAA;AAAA,GACvC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,QAAW,GAAA;AACjB,IAAW,KAAA,MAAA,IAAA,IAAQ,IAAK,CAAA,MAAA,EAAU,EAAA;AACjC,MAAI,IAAA,CAAC,KAAK,OAAS,EAAA,SAAA;AACnB,MAAA,IAAA,CAAK,IAAI,KAAM,EAAA,CAAA;AAAA,KAChB;AAEA,IAAA,KAAA,CAAM,MAAS,GAAA,CAAA,UAAA,EAAa,IAAK,CAAA,IAAI,CAAsC,oCAAA,CAAA,CAAA,CAAA;AAC3E,IAAO,OAAA,IAAA,CAAA;AAAA,GACR;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,OAAU,GAAA;AAChB,IAAW,KAAA,MAA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AI,GAAG,CAAA,CAAA;AACzB,IAAI,IAAA,IAAA,EAAM,IAAI,OAAS,EAAA;AACtB,MAAA,IAAA,CAAK,IAAI,IAAK,EAAA,CAAA;AAAA,KACf;AAEA,IAAO,OAAA,KAAA,CAAM,OAAO,GAAG,CAAA,CAAA;AAAA,GACxB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMgB,KAAQ,GAAA;AACvB,IAAA,IAAA,CAAK,OAAQ,EAAA,CAAA;AACb,IAAA,OAAO,MAAM,KAAM,EAAA,CAAA;AAAA,GACpB;AACD,CAAA,CAAA;AAzEiE,MAAA,CAAA,cAAA,EAAA,eAAA,CAAA,CAAA;AAA1D,IAAM,aAAN,GAAA","file":"CronTaskStore.mjs","sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { CronJob } from 'cron';\nimport { CronTask } from './CronTask';\n\nexport class CronTaskStore extends Store<CronTask, 'cron-tasks'> {\n\tpublic constructor() {\n\t\tsuper(CronTask, { name: 'cron-tasks' });\n\t}\n\n\t/**\n\t * Loops over all tasks and starts those that are enabled.\n\t * This gets called automatically when the Client is ready.\n\t * @returns CronTaskStore\n\t */\n\tpublic startAll() {\n\t\tfor (const task of this.values()) {\n\t\t\tif (!task.enabled) continue;\n\t\t\ttask.job.start();\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [START] Started all cronjob tasks.`);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Loops over all tasks and stops those that are running.\n\t * @returns CronTaskStore\n\t */\n\tpublic stopAll() {\n\t\tfor (const task of this.values()) {\n\t\t\tif (!task.job.running) continue;\n\t\t\ttask.job.stop();\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [STOP] Stopped all cronjob tasks.`);\n\t\treturn this;\n\t}\n\n\tpublic override set(key: string, value: CronTask): this {\n\t\tconst { options } = value;\n\n\t\tconst { sentry, defaultTimezone } = this.container.cron;\n\t\tconst cronJob = sentry ? sentry.cron.instrumentCron(CronJob, key) : CronJob;\n\n\t\ttry {\n\t\t\tvalue.job = cronJob.from({\n\t\t\t\t...options,\n\t\t\t\tonTick: () => void value.run.bind(value)(),\n\t\t\t\tstart: false,\n\t\t\t\tcontext: value,\n\t\t\t\ttimeZone: options.timeZone ?? defaultTimezone\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tvalue.error('Encountered an error while creating the cron job', error);\n\t\t\tvoid value.unload();\n\t\t}\n\n\t\treturn super.set(key, value);\n\t}\n\n\tpublic override delete(key: string) {\n\t\tconst task = this.get(key);\n\t\tif (task?.job.running) {\n\t\t\ttask.job.stop();\n\t\t}\n\n\t\treturn super.delete(key);\n\t}\n\n\t/**\n\t * Stops all running cron jobs and clears the store.\n\t * @returns void\n\t */\n\tpublic override clear() {\n\t\tthis.stopAll();\n\t\treturn super.clear();\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/structures/CronTaskStore.ts"],"names":[],"mappings":";;;;;;AAKO,IAAM,cAAA,GAAN,MAAM,cAAA,SAAsB,KAA8B,CAAA;AAAA,EACzD,WAAc,GAAA;AACpB,IAAA,KAAA,CAAM,QAAU,EAAA,EAAE,IAAM,EAAA,YAAA,EAAc,CAAA;AAAA;AACvC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaO,QAAW,GAAA;AACjB,IAAW,KAAA,MAAA,IAAA,IAAQ,IAAK,CAAA,MAAA,EAAU,EAAA;AACjC,MAAA,IAAI,CAAC,IAAK,CAAA,OAAA,IAAW,CAAC,IAAK,CAAA,GAAA,CAAI,WAAa,EAAA;AAC5C,MAAA,IAAA,CAAK,IAAI,KAAM,EAAA;AAAA;AAGhB,IAAA,KAAA,CAAM,MAAS,GAAA,CAAA,UAAA,EAAa,IAAK,CAAA,IAAI,CAAqC,mCAAA,CAAA,CAAA;AAC1E,IAAO,OAAA,IAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA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A,KAAA;AAAA,aAClD,CAAA;AAAA;AAGF,UAAA,OAAO,KAAM,CAAA,GAAA,CAAI,IAAK,CAAA,KAAK,CAAE,EAAA;AAAA;AAC9B,OACD;AAAA,aACQ,KAAO,EAAA;AACf,MAAM,KAAA,CAAA,KAAA,CAAM,oDAAoD,KAAK,CAAA;AACrE,MAAA,KAAK,MAAM,MAAO,EAAA;AAAA;AAGnB,IAAO,OAAA,KAAA,CAAM,GAAI,CAAA,GAAA,EAAK,KAAK,CAAA;AAAA;AAC5B;AAAA;AAAA;AAAA,EAKgB,OAAO,GAAa,EAAA;AACnC,IAAM,MAAA,IAAA,GAAO,IAAK,CAAA,GAAA,CAAI,GAAG,CAAA;AACzB,IAAA,IAAI,IAAQ,IAAA,CAAC,IAAK,CAAA,GAAA,CAAI,WAAa,EAAA;AAClC,MAAA,IAAA,CAAK,IAAI,IAAK,EAAA;AAAA;AAGf,IAAO,OAAA,KAAA,CAAM,OAAO,GAAG,CAAA;AAAA;AACxB;AAAA;AAAA;AAAA,EAKgB,KAAQ,GAAA;AACvB,IAAA,IAAA,CAAK,OAAQ,EAAA;AACb,IAAA,OAAO,MAAM,KAAM,EAAA;AAAA;AAErB,CAAA;AA3IiE,MAAA,CAAA,cAAA,EAAA,eAAA,CAAA;AAA1D,IAAM,aAAN,GAAA","file":"CronTaskStore.mjs","sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { Cron } from 'croner';\nimport { CronTask } from './CronTask';\nimport { normalizePattern } from '../utils/normalizePattern';\n\nexport class CronTaskStore extends Store<CronTask, 'cron-tasks'> {\n\tpublic constructor() {\n\t\tsuper(CronTask, { name: 'cron-tasks' });\n\t}\n\n\t/**\n\t * Loops over all tasks and pauses those that are running.\n\t *\n\t * @remarks\n\t * This method will only pause tasks that:\n\t * - Are enabled\n\t * - Are currently running\n\t * - Have not been permanently stopped\n\t *\n\t * @returns CronTaskStore\n\t */\n\tpublic pauseAll() {\n\t\tfor (const task of this.values()) {\n\t\t\tif (!task.enabled || !task.job.isRunning()) continue;\n\t\t\ttask.job.pause();\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [PAUSE] Paused all cronjob tasks.`);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Loops over all tasks and resumes those that are paused.\n\t *\n\t * @remarks\n\t * This method will only resume tasks that:\n\t * - Are enabled\n\t * - Are not currently running\n\t * - Have not been permanently stopped\n\t *\n\t * @returns CronTaskStore\n\t */\n\tpublic resumeAll() {\n\t\tfor (const task of this.values()) {\n\t\t\tif (!task.enabled || task.job.isRunning() || task.job.isStopped()) continue;\n\t\t\ttask.job.resume();\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [RESUME] Resumed all cronjob tasks.`);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Loops over all tasks and stops those that are running.\n\t *\n\t * @remarks\n\t * This method will only stop tasks that:\n\t * - Are enabled\n\t * - Have not been permanently stopped\n\t *\n\t * ⚠️ Stopping jobs is **permanent** and cannot be resumed afterwards!\n\t *\n\t * @returns CronTaskStore\n\t */\n\tpublic stopAll() {\n\t\tfor (const task of this.values()) {\n\t\t\tif (!task.enabled || task.job.isStopped()) continue;\n\t\t\ttask.job.stop();\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [STOP] Stopped all cronjob tasks.`);\n\t\treturn this;\n\t}\n\n\tpublic override set(key: string, value: CronTask): this {\n\t\tconst { pattern, timezone, protect, ...options } = value.options;\n\t\tconst { sentry, defaultTimezone } = this.container.cronTasks;\n\n\t\t// if a task with the same key already exists, stop it before creating a new one\n\t\tif (this.has(key)) {\n\t\t\tStore.logger?.(`[STORE => ${this.name}] [SET] Stopping existing cronjob task before creating a new one.`);\n\t\t\tthis.get(key)?.job.stop();\n\t\t}\n\n\t\tconst timeZone = timezone ?? defaultTimezone;\n\n\t\ttry {\n\t\t\tStore.logger?.(\n\t\t\t\t`[STORE => ${this.name}] [SET] Creating cronjob for ${key} with '${pattern}' as the pattern and '${timeZone}' for the timezone`\n\t\t\t);\n\n\t\t\tvalue.job = new Cron(\n\t\t\t\tpattern,\n\t\t\t\t{\n\t\t\t\t\tname: key,\n\t\t\t\t\ttimezone: timeZone,\n\t\t\t\t\tpaused: true, // we start the job manually once the client is ready\n\t\t\t\t\tprotect: value.protect ? (job) => void value.protect!(job) : protect,\n\t\t\t\t\tcatch: (error) => {\n\t\t\t\t\t\tvalue.error('Encountered an error while running the cron job', error);\n\t\t\t\t\t\tif (sentry) sentry.captureException(error);\n\t\t\t\t\t\tvoid value.catch?.(error, value.job);\n\t\t\t\t\t},\n\t\t\t\t\t...options\n\t\t\t\t},\n\t\t\t\t() => {\n\t\t\t\t\t// we only want to monitor cron patterns and not single-use tasks that croner supports\n\t\t\t\t\tif (sentry && typeof pattern === 'string' && !pattern.includes(':')) {\n\t\t\t\t\t\treturn sentry.withMonitor(key, () => void value.run.bind(value)(), {\n\t\t\t\t\t\t\tschedule: { type: 'crontab', value: pattern },\n\t\t\t\t\t\t\ttimezone: timeZone ? normalizePattern(timeZone) : undefined\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value.run.bind(value)();\n\t\t\t\t}\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tvalue.error('Encountered an error while creating the cron job', error);\n\t\t\tvoid value.unload();\n\t\t}\n\n\t\treturn super.set(key, value);\n\t}\n\n\t/**\n\t * Deletes a task from the store and stops it if it's running.\n\t */\n\tpublic override delete(key: string) {\n\t\tconst task = this.get(key);\n\t\tif (task && !task.job.isStopped()) {\n\t\t\ttask.job.stop();\n\t\t}\n\n\t\treturn super.delete(key);\n\t}\n\n\t/**\n\t * Stops all running cron jobs and clears the store.\n\t */\n\tpublic override clear() {\n\t\tthis.stopAll();\n\t\treturn super.clear();\n\t}\n}\n"]}
@@ -0,0 +1,42 @@
1
+ import { __name } from '../../chunk-2JTKI4GS.mjs';
2
+
3
+ // src/lib/utils/normalizePattern.ts
4
+ var predefined = {
5
+ "@annually": "0 0 1 1 *",
6
+ "@yearly": "0 0 1 1 *",
7
+ "@monthly": "0 0 1 * *",
8
+ "@weekly": "0 0 * * 0",
9
+ "@daily": "0 0 * * *",
10
+ "@hourly": "0 * * * *"
11
+ };
12
+ var cronTokens = {
13
+ jan: 1,
14
+ feb: 2,
15
+ mar: 3,
16
+ apr: 4,
17
+ may: 5,
18
+ jun: 6,
19
+ jul: 7,
20
+ aug: 8,
21
+ sep: 9,
22
+ oct: 10,
23
+ nov: 11,
24
+ dec: 12,
25
+ sun: 0,
26
+ mon: 1,
27
+ tue: 2,
28
+ wed: 3,
29
+ thu: 4,
30
+ fri: 5,
31
+ sat: 6
32
+ };
33
+ var tokensRegex = new RegExp(Object.keys(cronTokens).join("|"), "g");
34
+ function normalizePattern(pattern) {
35
+ if (Reflect.has(predefined, pattern)) return Reflect.get(predefined, pattern);
36
+ return pattern.replace(tokensRegex, (match) => String(Reflect.get(cronTokens, match)));
37
+ }
38
+ __name(normalizePattern, "normalizePattern");
39
+
40
+ export { normalizePattern };
41
+ //# sourceMappingURL=normalizePattern.mjs.map
42
+ //# sourceMappingURL=normalizePattern.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/lib/utils/normalizePattern.ts"],"names":[],"mappings":";;;AAMA,IAAM,UAAa,GAAA;AAAA,EAClB,WAAa,EAAA,WAAA;AAAA,EACb,SAAW,EAAA,WAAA;AAAA,EACX,UAAY,EAAA,WAAA;AAAA,EACZ,SAAW,EAAA,WAAA;AAAA,EACX,QAAU,EAAA,WAAA;AAAA,EACV,SAAW,EAAA;AACZ,CAAA;AAEA,IAAM,UAAqC,GAAA;AAAA,EAC1C,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,EAAA;AAAA,EACL,GAAK,EAAA,EAAA;AAAA,EACL,GAAK,EAAA,EAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA,CAAA;AAAA,EACL,GAAK,EAAA;AACN,CAAA;AAEA,IAAM,WAAA,GAAc,IAAI,MAAA,CAAO,MAAO,CAAA,IAAA,CAAK,UAAU,CAAE,CAAA,IAAA,CAAK,GAAG,CAAA,EAAG,GAAG,CAAA;AAE9D,SAAS,iBAAiB,OAAyB,EAAA;AACzD,EAAI,IAAA,OAAA,CAAQ,IAAI,UAAY,EAAA,OAAO,GAAU,OAAA,OAAA,CAAQ,GAAI,CAAA,UAAA,EAAY,OAAO,CAAA;AAC5E,EAAO,OAAA,OAAA,CAAQ,OAAQ,CAAA,WAAA,EAAa,CAAC,KAAA,KAAU,MAAO,CAAA,OAAA,CAAQ,GAAI,CAAA,UAAA,EAAY,KAAK,CAAC,CAAC,CAAA;AACtF;AAHgB,MAAA,CAAA,gBAAA,EAAA,kBAAA,CAAA","file":"normalizePattern.mjs","sourcesContent":["/*\n * Credits to Sapphire for this\n * https://github.com/sapphiredev/utilities/blob/main/packages/cron/src/lib/constants.ts#L26-L57\n * https://github.com/sapphiredev/utilities/blob/main/packages/cron/src/lib/Cron.ts#L91\n */\n\nconst predefined = {\n\t'@annually': '0 0 1 1 *',\n\t'@yearly': '0 0 1 1 *',\n\t'@monthly': '0 0 1 * *',\n\t'@weekly': '0 0 * * 0',\n\t'@daily': '0 0 * * *',\n\t'@hourly': '0 * * * *'\n} as const;\n\nconst cronTokens: Record<string, number> = {\n\tjan: 1,\n\tfeb: 2,\n\tmar: 3,\n\tapr: 4,\n\tmay: 5,\n\tjun: 6,\n\tjul: 7,\n\taug: 8,\n\tsep: 9,\n\toct: 10,\n\tnov: 11,\n\tdec: 12,\n\tsun: 0,\n\tmon: 1,\n\ttue: 2,\n\twed: 3,\n\tthu: 4,\n\tfri: 5,\n\tsat: 6\n} as const;\n\nconst tokensRegex = new RegExp(Object.keys(cronTokens).join('|'), 'g');\n\nexport function normalizePattern(pattern: string): string {\n\tif (Reflect.has(predefined, pattern)) return Reflect.get(predefined, pattern);\n\treturn pattern.replace(tokensRegex, (match) => String(Reflect.get(cronTokens, match)));\n}\n"]}
@@ -4,17 +4,17 @@ import { SapphireClient, preGenericsInitialization, postInitialization, preLogin
4
4
 
5
5
  var _CronTaskPlugin = class _CronTaskPlugin extends Plugin {
6
6
  static [preGenericsInitialization](options) {
7
- container.cron = new CronTaskHandler(options.cron);
7
+ container.cronTasks = new CronTaskHandler(options.cronTasks);
8
8
  }
9
9
  static [postInitialization]() {
10
10
  this.stores.register(new CronTaskStore());
11
11
  }
12
12
  static async [preLogin]() {
13
- if (container.cron.disableSentry) return;
14
- container.cron.sentry = await import('@sentry/node').then((mod) => mod.default).catch(() => void 0);
13
+ if (container.cronTasks.disableSentry) return;
14
+ container.cronTasks.sentry = await import('@sentry/node').catch(() => void 0);
15
15
  }
16
16
  static [postLogin]() {
17
- container.cron.startAll();
17
+ this.stores.get("cron-tasks").resumeAll();
18
18
  }
19
19
  };
20
20
  __name(_CronTaskPlugin, "CronTaskPlugin");
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/register.ts"],"names":[],"mappings":";;;;AAMO,IAAM,eAAA,GAAN,MAAM,eAAA,SAAuB,MAAO,CAAA;AAAA,EAC1C,QAAe,yBAAyB,CAAA,CAAwB,OAAwB,EAAA;AACvF,IAAA,SAAA,CAAU,IAAO,GAAA,IAAI,eAAgB,CAAA,OAAA,CAAQ,IAAI,CAAA,CAAA;AAAA,GAClD;AAAA,EAEA,QAAe,kBAAkB,CAAwB,GAAA;AACxD,IAAA,IAAA,CAAK,MAAO,CAAA,QAAA,CAAS,IAAI,aAAA,EAAe,CAAA,CAAA;AAAA,GACzC;AAAA,EAEA,cAAqB,QAAQ,CAAwB,GAAA;AACpD,IAAI,IAAA,SAAA,CAAU,KAAK,aAAe,EAAA,OAAA;AAClC,IAAA,SAAA,CAAU,IAAK,CAAA,MAAA,GAAS,MAAM,OAAO,cAAc,CAAE,CAAA,IAAA,CAAK,CAAC,GAAA,KAAQ,GAAI,CAAA,OAAO,CAAE,CAAA,KAAA,CAAM,MAAM,KAAS,CAAA,CAAA,CAAA;AAAA,GACtG;AAAA,EAEA,QAAe,SAAS,CAAwB,GAAA;AAC/C,IAAA,SAAA,CAAU,KAAK,QAAS,EAAA,CAAA;AAAA,GACzB;AACD,CAAA,CAAA;AAjB2C,MAAA,CAAA,eAAA,EAAA,gBAAA,CAAA,CAAA;AAApC,IAAM,cAAN,GAAA,gBAAA;AAmBP,cAAA,CAAe,OAAQ,CAAA,qCAAA,CAAsC,cAAe,CAAA,yBAAyB,GAAG,qCAAqC,CAAA,CAAA;AAE7I,cAAA,CAAe,OAAQ,CAAA,8BAAA,CAA+B,cAAe,CAAA,kBAAkB,GAAG,8BAA8B,CAAA,CAAA;AAExH,cAAA,CAAe,OAAQ,CAAA,oBAAA,CAAqB,cAAe,CAAA,QAAQ,GAAG,oBAAoB,CAAA,CAAA;AAE1F,cAAA,CAAe,OAAQ,CAAA,qBAAA,CAAsB,cAAe,CAAA,SAAS,GAAG,qBAAqB,CAAA","file":"register.mjs","sourcesContent":["import './index';\n\nimport { container, Plugin, postInitialization, postLogin, preGenericsInitialization, preLogin, SapphireClient } from '@sapphire/framework';\nimport type { ClientOptions } from 'discord.js';\nimport { CronTaskHandler, CronTaskStore } from './index';\n\nexport class CronTaskPlugin extends Plugin {\n\tpublic static [preGenericsInitialization](this: SapphireClient, options: ClientOptions) {\n\t\tcontainer.cron = new CronTaskHandler(options.cron);\n\t}\n\n\tpublic static [postInitialization](this: SapphireClient) {\n\t\tthis.stores.register(new CronTaskStore());\n\t}\n\n\tpublic static async [preLogin](this: SapphireClient) {\n\t\tif (container.cron.disableSentry) return;\n\t\tcontainer.cron.sentry = await import('@sentry/node').then((mod) => mod.default).catch(() => undefined);\n\t}\n\n\tpublic static [postLogin](this: SapphireClient) {\n\t\tcontainer.cron.startAll();\n\t}\n}\n\nSapphireClient.plugins.registerPreGenericsInitializationHook(CronTaskPlugin[preGenericsInitialization], 'Cron-Task-PreGenericsInitialization');\n\nSapphireClient.plugins.registerPostInitializationHook(CronTaskPlugin[postInitialization], 'Cron-Task-PostInitialization');\n\nSapphireClient.plugins.registerPreLoginHook(CronTaskPlugin[preLogin], 'Cron-Task-PreLogin');\n\nSapphireClient.plugins.registerPostLoginHook(CronTaskPlugin[postLogin], 'Cron-Task-PostLogin');\n"]}
1
+ {"version":3,"sources":["../../src/register.ts"],"names":[],"mappings":";;;;AAMO,IAAM,eAAA,GAAN,MAAM,eAAA,SAAuB,MAAO,CAAA;AAAA,EAC1C,QAAwB,yBAAyB,CAAA,CAAwB,OAAwB,EAAA;AAChG,IAAA,SAAA,CAAU,SAAY,GAAA,IAAI,eAAgB,CAAA,OAAA,CAAQ,SAAS,CAAA;AAAA;AAC5D,EAEA,QAAwB,kBAAkB,CAAwB,GAAA;AACjE,IAAA,IAAA,CAAK,MAAO,CAAA,QAAA,CAAS,IAAI,aAAA,EAAe,CAAA;AAAA;AACzC,EAEA,cAA8B,QAAQ,CAAwB,GAAA;AAC7D,IAAI,IAAA,SAAA,CAAU,UAAU,aAAe,EAAA;AACvC,IAAU,SAAA,CAAA,SAAA,CAAU,SAAS,MAAM,OAAO,cAAc,CAAE,CAAA,KAAA,CAAM,MAAM,MAAS,CAAA;AAAA;AAChF,EAEA,QAAwB,SAAS,CAAwB,GAAA;AACxD,IAAA,IAAA,CAAK,MAAO,CAAA,GAAA,CAAI,YAAY,CAAA,CAAE,SAAU,EAAA;AAAA;AAE1C,CAAA;AAjB2C,MAAA,CAAA,eAAA,EAAA,gBAAA,CAAA;AAApC,IAAM,cAAN,GAAA;AAmBP,cAAA,CAAe,OAAQ,CAAA,qCAAA,CAAsC,cAAe,CAAA,yBAAyB,GAAG,qCAAqC,CAAA;AAE7I,cAAA,CAAe,OAAQ,CAAA,8BAAA,CAA+B,cAAe,CAAA,kBAAkB,GAAG,8BAA8B,CAAA;AAExH,cAAA,CAAe,OAAQ,CAAA,oBAAA,CAAqB,cAAe,CAAA,QAAQ,GAAG,oBAAoB,CAAA;AAE1F,cAAA,CAAe,OAAQ,CAAA,qBAAA,CAAsB,cAAe,CAAA,SAAS,GAAG,qBAAqB,CAAA","file":"register.mjs","sourcesContent":["import './index';\n\nimport { container, Plugin, postInitialization, postLogin, preGenericsInitialization, preLogin, SapphireClient } from '@sapphire/framework';\nimport type { ClientOptions } from 'discord.js';\nimport { CronTaskHandler, CronTaskStore } from './index';\n\nexport class CronTaskPlugin extends Plugin {\n\tpublic static override [preGenericsInitialization](this: SapphireClient, options: ClientOptions) {\n\t\tcontainer.cronTasks = new CronTaskHandler(options.cronTasks);\n\t}\n\n\tpublic static override [postInitialization](this: SapphireClient) {\n\t\tthis.stores.register(new CronTaskStore());\n\t}\n\n\tpublic static override async [preLogin](this: SapphireClient) {\n\t\tif (container.cronTasks.disableSentry) return;\n\t\tcontainer.cronTasks.sentry = await import('@sentry/node').catch(() => undefined);\n\t}\n\n\tpublic static override [postLogin](this: SapphireClient) {\n\t\tthis.stores.get('cron-tasks').resumeAll();\n\t}\n}\n\nSapphireClient.plugins.registerPreGenericsInitializationHook(CronTaskPlugin[preGenericsInitialization], 'Cron-Task-PreGenericsInitialization');\n\nSapphireClient.plugins.registerPostInitializationHook(CronTaskPlugin[postInitialization], 'Cron-Task-PostInitialization');\n\nSapphireClient.plugins.registerPreLoginHook(CronTaskPlugin[preLogin], 'Cron-Task-PreLogin');\n\nSapphireClient.plugins.registerPostLoginHook(CronTaskPlugin[postLogin], 'Cron-Task-PostLogin');\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kingsworld/plugin-cron",
3
3
  "description": "A simple @sapphire/framework plugin that aims to make use of the cron package and allow users to make cron jobs within their Sapphire discord bot.",
4
- "version": "3.0.1-next.e1b3c15",
4
+ "version": "3.0.2-next.69cca0b",
5
5
  "author": "Seren_Modz 21 <seren@kings-world.net>",
6
6
  "license": "MIT",
7
7
  "main": "dist/cjs/index.cjs",
@@ -36,7 +36,7 @@
36
36
  "scripts": {
37
37
  "build": "tsup && yarn build:types && yarn build:rename-cjs-register",
38
38
  "build:types": "concurrently \"yarn:build:types:*\"",
39
- "build:types:cjs": "rollup-type-bundler -d dist/cjs -ot .cts",
39
+ "build:types:cjs": "rollup-type-bundler -d dist/cjs --output-typings-file-extension .cts",
40
40
  "build:types:esm": "rollup-type-bundler -d dist/esm -t .mts",
41
41
  "build:types:cleanup": "tsx ../../scripts/clean-register-imports.mts",
42
42
  "build:rename-cjs-register": "tsx ../../scripts/rename-cjs-register.mts",
@@ -46,16 +46,16 @@
46
46
  "check-update": "cliff-jumper --dry-run"
47
47
  },
48
48
  "dependencies": {
49
- "cron": "^3.1.7"
49
+ "croner": "^9.0.0"
50
50
  },
51
51
  "devDependencies": {
52
- "@favware/cliff-jumper": "^4.1.0",
53
- "@favware/rollup-type-bundler": "^3.3.0",
54
- "@sentry/node": "^8.28.0",
55
- "@types/node": "^20.16.5",
56
- "concurrently": "^8.2.2",
57
- "tsup": "^8.2.4",
58
- "tsx": "^4.19.0",
52
+ "@favware/cliff-jumper": "^6.0.0",
53
+ "@favware/rollup-type-bundler": "^4.0.0",
54
+ "@sentry/node": "^9.11.0",
55
+ "@types/node": "^22.14.0",
56
+ "concurrently": "^9.1.2",
57
+ "tsup": "^8.4.0",
58
+ "tsx": "^4.19.3",
59
59
  "typescript": "~5.4.5"
60
60
  },
61
61
  "homepage": "https://github.com/Kings-World/sapphire-plugins",