cron 3.0.0 → 3.1.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,34 @@
1
+ ## [3.1.1](https://github.com/kelektiv/node-cron/compare/v3.1.0...v3.1.1) (2023-10-12)
2
+
3
+
4
+ ### 🐛 Bug Fixes
5
+
6
+ * fix lastDate() value for intervals > 25 days ([#711](https://github.com/kelektiv/node-cron/issues/711)) ([141aa00](https://github.com/kelektiv/node-cron/commit/141aa00f55fa105d89df7e257d82c94ad2bb2b3a))
7
+ * fix object constructor typings & make OC generic type optional ([#712](https://github.com/kelektiv/node-cron/issues/712)) ([6536084](https://github.com/kelektiv/node-cron/commit/653608451caecd51f50884d83563c03c6c27bc54))
8
+
9
+
10
+ ### 📚 Documentation
11
+
12
+ * **readme:** update nextDates documentation ([#702](https://github.com/kelektiv/node-cron/issues/702)) ([1ad2e22](https://github.com/kelektiv/node-cron/commit/1ad2e228f75bcaab3eb97a27665c06b7892678c5))
13
+
14
+ ## [3.1.0](https://github.com/kelektiv/node-cron/compare/v3.0.0...v3.1.0) (2023-10-09)
15
+
16
+
17
+ ### ✨ Features
18
+
19
+ * improve context, onTick & onComplete typings ([#705](https://github.com/kelektiv/node-cron/issues/705)) ([82c78d7](https://github.com/kelektiv/node-cron/commit/82c78d79594c2d1c1e36baa67ecd76c033a171e4))
20
+
21
+
22
+ ### 🚨 Tests
23
+
24
+ * check at runtime that all tests call expect ([#706](https://github.com/kelektiv/node-cron/issues/706)) [skip ci] ([cc4e62f](https://github.com/kelektiv/node-cron/commit/cc4e62fd1fd713d4c8a324ba36c0caa8b0c364e9))
25
+
26
+
27
+ ### ♻️ Chores
28
+
29
+ * **deps:** update semantic-release related packages ([#709](https://github.com/kelektiv/node-cron/issues/709)) [skip ci] ([b94a48a](https://github.com/kelektiv/node-cron/commit/b94a48a6b24e20c1f3a9c5109158ff359d048bac))
30
+ * reflect repository label changes ([#708](https://github.com/kelektiv/node-cron/issues/708)) ([85c9e18](https://github.com/kelektiv/node-cron/commit/85c9e18023aea907c10ca77bc92db7a8086f1b6b))
31
+
1
32
  ## [3.0.0](https://github.com/kelektiv/node-cron/compare/v2.4.4...v3.0.0) (2023-09-30)
2
33
 
3
34
 
package/README.md CHANGED
@@ -44,6 +44,7 @@ For day-of-week indexing, we only added support for `7` as Sunday, so you don't
44
44
 
45
45
  - **constructor no longer accepts an object as its first and only params. Use `CronJob.from(argsObject)` instead.**
46
46
  - **callbacks are now called in the order they were registered.**
47
+ - **`nextDates(count?: number)` now always returns an array (empty if no argument is provided). Use `nextDate()` instead for a single date.**
47
48
 
48
49
  ### Removed methods
49
50
 
@@ -118,7 +119,7 @@ Parameter Based
118
119
  - `constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout)`
119
120
  - `cronTime` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
120
121
  - `onTick` - [REQUIRED] - The function to fire at the specified time. If an `onComplete` callback was provided, `onTick` will receive it as an argument. `onTick` may call `onComplete` when it has finished its work.
121
- - `onComplete` - [OPTIONAL] - A function that will fire when the job is stopped with `job.stop()`, and may also be called by `onTick` at the end of each run.
122
+ - `onComplete` - [OPTIONAL] - A function that will fire when the job is stopped with `job.stop()`, and may also be called by `onTick` at the end of each run. **Note for TS users**: This should either be an arrow function, or a regular function cast to `() => void` (bug with generic type inference).
122
123
  - `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). This does not immediately fire your `onTick` function, it just gives you more control over the behavior of your jobs.
123
124
  - `timeZone` - [OPTIONAL] - Specify the time zone for the execution. This will modify the actual time relative to your time zone. If the time zone is invalid, an error is thrown. By default (if this is omitted) the local time zone will be used. You can check the various time zones format accepted in the [Luxon documentation](https://github.com/moment/luxon/blob/master/docs/zones.md#specifying-a-zone). Note: This parameter supports minutes offsets, e.g. `UTC+5:30`. **Note**: Cannot be used together with `utcOffset`.
124
125
  - `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object.
@@ -131,7 +132,8 @@ Parameter Based
131
132
  - `setTime` - Stops and changes the time for the `CronJob`. Param must be a `CronTime`.
132
133
  - `lastDate` - Tells you the last execution date.
133
134
  - `nextDate` - Provides the next date that will trigger an `onTick`.
134
- - `nextDates` - Provides an array of the next set of dates that will trigger an `onTick`.
135
+ - `nextDates(count)` - Provides an array of the next set of dates that will trigger an `onTick`.
136
+ - `count` - [OPTIONAL] - The number of next dates to return. Defaults to 0, returning an empty array.
135
137
  - `fireOnTick` - Allows you to override the `onTick` calling behavior. This matters so only do this if you have a really good reason to do so.
136
138
  - `addCallback` - Allows you to add `onTick` callbacks. Callbacks are run in the order they are registered.
137
139
  - `CronTime`
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { DateTime } from 'luxon';
2
2
  export { CronJob } from './job';
3
3
  export { CronTime } from './time';
4
- export { CronCommand, CronJobParams, Ranges, TimeUnit } from './types/cron.types';
4
+ export { CronCallback, CronCommand, CronContext, CronJobParams, CronOnCompleteCallback, CronOnCompleteCommand, Ranges, TimeUnit } from './types/cron.types';
5
5
  export declare const sendAt: (cronTime: string | Date | DateTime) => DateTime;
6
6
  export declare const timeout: (cronTime: string | Date | DateTime) => number;
package/dist/job.d.ts CHANGED
@@ -1,20 +1,20 @@
1
1
  import { CronTime } from './time';
2
- import { CronJobParams } from './types/cron.types';
3
- export declare class CronJob {
2
+ import { CronCallback, CronContext, CronJobParams, CronOnCompleteCallback, CronOnCompleteCommand, WithOnComplete } from './types/cron.types';
3
+ export declare class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
4
4
  cronTime: CronTime;
5
5
  running: boolean;
6
6
  unrefTimeout: boolean;
7
7
  lastExecution: Date | null;
8
8
  runOnce: boolean;
9
- context: unknown;
10
- onComplete?: (...args: any) => void;
9
+ context: CronContext<C>;
10
+ onComplete?: WithOnComplete<OC> extends true ? CronOnCompleteCallback : undefined;
11
11
  private _timeout?;
12
12
  private _callbacks;
13
- constructor(cronTime: CronJobParams['cronTime'], onTick: CronJobParams['onTick'], onComplete?: CronJobParams['onComplete'], start?: CronJobParams['start'], timeZone?: CronJobParams['timeZone'], context?: CronJobParams['context'], runOnInit?: CronJobParams['runOnInit'], utcOffset?: null, unrefTimeout?: CronJobParams['unrefTimeout']);
14
- constructor(cronTime: CronJobParams['cronTime'], onTick: CronJobParams['onTick'], onComplete?: CronJobParams['onComplete'], start?: CronJobParams['start'], timeZone?: null, context?: CronJobParams['context'], runOnInit?: CronJobParams['runOnInit'], utcOffset?: CronJobParams['utcOffset'], unrefTimeout?: CronJobParams['unrefTimeout']);
15
- static from(params: CronJobParams): CronJob;
13
+ constructor(cronTime: CronJobParams<OC, C>['cronTime'], onTick: CronJobParams<OC, C>['onTick'], onComplete?: CronJobParams<OC, C>['onComplete'], start?: CronJobParams<OC, C>['start'], timeZone?: CronJobParams<OC, C>['timeZone'], context?: CronJobParams<OC, C>['context'], runOnInit?: CronJobParams<OC, C>['runOnInit'], utcOffset?: null, unrefTimeout?: CronJobParams<OC, C>['unrefTimeout']);
14
+ constructor(cronTime: CronJobParams<OC, C>['cronTime'], onTick: CronJobParams<OC, C>['onTick'], onComplete?: CronJobParams<OC, C>['onComplete'], start?: CronJobParams<OC, C>['start'], timeZone?: null, context?: CronJobParams<OC, C>['context'], runOnInit?: CronJobParams<OC, C>['runOnInit'], utcOffset?: CronJobParams<OC, C>['utcOffset'], unrefTimeout?: CronJobParams<OC, C>['unrefTimeout']);
15
+ static from<OC extends CronOnCompleteCommand | null = null, C = null>(params: CronJobParams<OC, C>): CronJob<OC, C>;
16
16
  private _fnWrap;
17
- addCallback(callback: (...args: any) => void): void;
17
+ addCallback(callback: CronCallback<C, WithOnComplete<OC>>): void;
18
18
  setTime(time: CronTime): void;
19
19
  nextDate(): import("luxon").DateTime;
20
20
  fireOnTick(): void;
package/dist/job.js CHANGED
@@ -11,7 +11,7 @@ class CronJob {
11
11
  this.lastExecution = null;
12
12
  this.runOnce = false;
13
13
  this._callbacks = [];
14
- this.context = context || this;
14
+ this.context = (context !== null && context !== void 0 ? context : this);
15
15
  if (timeZone != null && utcOffset != null) {
16
16
  throw new errors_1.ExclusiveParametersError('timeZone', 'utcOffset');
17
17
  }
@@ -63,7 +63,7 @@ class CronJob {
63
63
  }
64
64
  case 'string': {
65
65
  const [command, ...args] = cmd.split(' ');
66
- return child_process_1.spawn.bind(undefined, command !== null && command !== void 0 ? command : cmd, args);
66
+ return child_process_1.spawn.bind(undefined, command !== null && command !== void 0 ? command : cmd, args, {});
67
67
  }
68
68
  case 'object': {
69
69
  return child_process_1.spawn.bind(undefined, cmd.command, (_a = cmd.args) !== null && _a !== void 0 ? _a : [], (_b = cmd.options) !== null && _b !== void 0 ? _b : {});
@@ -120,7 +120,6 @@ class CronJob {
120
120
  }
121
121
  remaining += newTimeout;
122
122
  }
123
- this.lastExecution = new Date();
124
123
  if (remaining) {
125
124
  if (remaining > MAXDELAY) {
126
125
  remaining -= MAXDELAY;
@@ -133,6 +132,7 @@ class CronJob {
133
132
  setCronTimeout(timeout);
134
133
  }
135
134
  else {
135
+ this.lastExecution = new Date();
136
136
  this.running = false;
137
137
  if (!this.runOnce) {
138
138
  this.start();
@@ -160,7 +160,7 @@ class CronJob {
160
160
  clearTimeout(this._timeout);
161
161
  this.running = false;
162
162
  if (typeof this.onComplete === 'function') {
163
- this.onComplete();
163
+ this.onComplete.call(this.context);
164
164
  }
165
165
  }
166
166
  }
@@ -4,27 +4,33 @@ import { DateTime } from 'luxon';
4
4
  import { CONSTRAINTS, TIME_UNITS_MAP } from '../constants';
5
5
  import { CronJob } from '../job';
6
6
  import { IntRange } from './utils';
7
- interface BaseCronJobParams {
7
+ interface BaseCronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> {
8
8
  cronTime: string | Date | DateTime;
9
- onTick: CronCommand;
10
- onComplete?: CronCommand | null;
9
+ onTick: CronCommand<C, WithOnComplete<OC>>;
10
+ onComplete?: OC;
11
11
  start?: boolean | null;
12
- context?: unknown | null;
12
+ context?: C;
13
13
  runOnInit?: boolean | null;
14
14
  unrefTimeout?: boolean | null;
15
15
  }
16
- export type CronJobParams = BaseCronJobParams & ({
16
+ export type CronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> = BaseCronJobParams<OC, C> & ({
17
17
  timeZone?: string | null;
18
18
  utcOffset?: never;
19
19
  } | {
20
20
  timeZone?: never;
21
21
  utcOffset?: number | null;
22
22
  });
23
- export type CronCommand = ((this: CronJob | any) => void) | string | {
23
+ export type CronContext<C> = C extends null ? CronJob : NonNullable<C>;
24
+ export type CronCallback<C, WithOnCompleteBool extends boolean = false> = (this: CronContext<C>, onComplete: WithOnCompleteBool extends true ? CronOnCompleteCallback : never) => void;
25
+ export type CronOnCompleteCallback = () => void;
26
+ export type CronSystemCommand = string | {
24
27
  command: string;
25
28
  args?: readonly string[] | null;
26
29
  options?: SpawnOptions | null;
27
30
  };
31
+ export type CronCommand<C, WithOnCompleteBool extends boolean = false> = CronCallback<C, WithOnCompleteBool> | CronSystemCommand;
32
+ export type CronOnCompleteCommand = CronOnCompleteCallback | CronSystemCommand;
33
+ export type WithOnComplete<OC> = OC extends null ? false : true;
28
34
  export type TimeUnit = (typeof TIME_UNITS_MAP)[keyof typeof TIME_UNITS_MAP];
29
35
  export type TimeUnitField<T extends TimeUnit> = Partial<Record<Ranges[T], boolean>>;
30
36
  export interface Ranges {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cron",
3
3
  "description": "Cron jobs for your node",
4
- "version": "3.0.0",
4
+ "version": "3.1.1",
5
5
  "author": "Nick Campbell <nicholas.j.campbell@gmail.com> (https://github.com/ncb000gt)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/kelektiv/node-cron/issues"
@@ -27,15 +27,15 @@
27
27
  "luxon": "~3.3.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@commitlint/cli": "~17.6.6",
31
- "@insurgentlab/commitlint-config": "^18.1.0",
32
- "@insurgentlab/conventional-changelog-preset": "~6.0.3",
33
- "@semantic-release/changelog": "~6.0.x",
34
- "@semantic-release/commit-analyzer": "~9.0.x",
35
- "@semantic-release/git": "~10.0.x",
36
- "@semantic-release/github": "~8.1.x",
37
- "@semantic-release/npm": "~10.0.x",
38
- "@semantic-release/release-notes-generator": "~11.0.x",
30
+ "@commitlint/cli": "17.7.2",
31
+ "@insurgentlab/commitlint-config": "18.1.3",
32
+ "@insurgentlab/conventional-changelog-preset": "7.0.0",
33
+ "@semantic-release/changelog": "6.0.3",
34
+ "@semantic-release/commit-analyzer": "11.0.0",
35
+ "@semantic-release/git": "10.0.1",
36
+ "@semantic-release/github": "9.2.1",
37
+ "@semantic-release/npm": "11.0.0",
38
+ "@semantic-release/release-notes-generator": "12.0.0",
39
39
  "@types/jest": "^29.5.5",
40
40
  "@types/node": "^20.6.0",
41
41
  "@types/sinon": "^10.0.16",
@@ -48,7 +48,7 @@
48
48
  "husky": "^8.0.3",
49
49
  "jest": "~29.5.x",
50
50
  "prettier": "~2.8.x",
51
- "semantic-release": "~21.0.x",
51
+ "semantic-release": "22.0.5",
52
52
  "sinon": "^15.0.x",
53
53
  "ts-jest": "^29.1.1",
54
54
  "typescript": "~5.1.6"