Skip to content

Commit

Permalink
Fix UMD build. Update docs. Add migration guide for 9.x.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Oct 12, 2024
1 parent 77148c8 commit 9eabbc6
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
14 changes: 9 additions & 5 deletions build/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import esbuild from "esbuild";
import { readFile, rm, rmdir, writeFile, cp } from "@cross/fs";
import { cp, readFile, rm, rmdir, writeFile } from "@cross/fs";
import { expandGlob } from "@std/fs";
import { dtsPlugin } from "esbuild-plugin-d.ts";

Expand Down Expand Up @@ -100,11 +100,15 @@ if (Deno.args[1] === "clean") {
outdir: resolvedDistPath,
platform: "node",
format: "cjs",
outExtension: { ".js": ".cjs" }
outExtension: { ".js": ".cjs" },
},
{
outdir: resolvedDistPath,
outExtension: { ".js": ".umd.js" },
entryPoints: [],
stdin: {
contents: 'import { Cron } from "./croner.ts";module.exports = Cron;',
resolveDir: "./src/",
},
outfile: resolve(resolvedDistPath, "croner.umd.js"),
platform: "browser",
format: "iife",
globalName: "Cron",
Expand All @@ -120,7 +124,7 @@ if (Deno.args[1] === "clean") {
declaration: true,
emitDeclarationOnly: true,
allowImportingTsExtensions: true,
lib: ["es6", "dom"]
lib: ["es6", "dom"],
},
},
})],
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hexagon/croner",
"version": "9.0.0-dev.10",
"version": "9.0.0-dev.11",
"exports": "./src/croner.ts",
"lint": {
"include": ["src", "build"]
Expand Down
2 changes: 1 addition & 1 deletion docs/src/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ Make sure to replace `$CRONER_VERSION` with the latest version.

To use Croner in a webpage, you can load it as a UMD module from a CDN:

`<script src="https://cdn.jsdelivr.net/npm/croner@6/dist/croner.umd.min.js"></script>`
`<script src="https://cdn.jsdelivr.net/npm/croner@9/dist/croner.umd.js"></script>`
18 changes: 18 additions & 0 deletions docs/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ Version `7.x` introduces significant changes, including the introduction of the

Version `8.x` introduces no significant changes in the API for Croner. However, a major change is the discontinuation of support for Node.js versions prior to `18.0`. It is crucial to ensure that your environment is running Node.js `18.0` or higher before upgrading to `8.x`. If you rely on Node <= `16` you should stick with `7.x`.

### Upgrading from 8.x to 9.x

Version 9.x brings several changes to Croner to fix existing issues and ensure consistency in code:

### Changes to the API

* `new` is no longer optional. Previously, it was possible to instantiate Croner using `Cron(/*...*/)` now, you need to use `new Cron(/* ... */)`.

* The default export is removed. Previously, you could `import Cron from`, now you need to `import { Cron } from`. The same goes for require.

### Changes to the file structure

* Files in the `/dist` directory are always minified, no need to be explicit with that through the file names. As a result, `croner.min.js` is now `croner.js`.

* Typings are moved from `/types` to `/dist`.

* Only the `/src` directory is exposed in the JSR module.

## Switching from Cron

If you're currently using the cron package and want to migrate to Croner, the following steps can guide you:
Expand Down
4 changes: 2 additions & 2 deletions docs/src/usage/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ nav_order: 1

---

Croner uses the function `Cron()` which takes in three arguments:
Croner uses the function `new Cron()` which takes in three arguments:

```ts
const job = Cron(
const job = new Cron(
/* The pattern */
"* * * * * *",
/* Options (optional) */
Expand Down
30 changes: 15 additions & 15 deletions docs/src/usage/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ nav_order: 3

```ts
// Find next month
const nextMonth = Cron("@monthly").nextRun(),
nextSunday = Cron("@weekly").nextRun(),
nextSat29feb = Cron("0 0 0 29 2 6", { legacyMode: false }).nextRun(),
nextSunLastOfMonth = Cron("0 0 0 L * 7", { legacyMode: false }).nextRun(),
nextLastSundayOfMonth = Cron("0 0 0 * * L7").nextRun();
const nextMonth = new Cron("@monthly").nextRun(),
nextSunday = new Cron("@weekly").nextRun(),
nextSat29feb = new Cron("0 0 0 29 2 6", { legacyMode: false }).nextRun(),
nextSunLastOfMonth = new Cron("0 0 0 L * 7", { legacyMode: false }).nextRun(),
nextLastSundayOfMonth = new Cron("0 0 0 * * L7").nextRun();

console.log("First day of next month: " + nextMonth.toLocaleDateString());
console.log("Next sunday: " + nextSunday.toLocaleDateString());
Expand All @@ -31,23 +31,23 @@ console.log("Next last sunday of month: " + nextLastSundayOfMonth.toLocaleDateS

### Job controls
```ts
const job = Cron('* * * * * *', (self) => {
const job = new Cron('* * * * * *', (self) => {
console.log('This will run every second. Pause on second 10. Resume on 15. And quit on 20.');
console.log('Current second: ', new Date().getSeconds());
console.log('Previous run: ' + self.previousRun());
console.log('Next run: ' + self.nextRun());
});

Cron('10 * * * * *', {maxRuns: 1}, () => job.pause());
Cron('15 * * * * *', {maxRuns: 1}, () => job.resume());
Cron('20 * * * * *', {maxRuns: 1}, () => job.stop());
new Cron('10 * * * * *', {maxRuns: 1}, () => job.pause());
new Cron('15 * * * * *', {maxRuns: 1}, () => job.resume());
new Cron('20 * * * * *', {maxRuns: 1}, () => job.stop());
```

### Options
```ts
import { Cron } from "./dist/croner.js";

const job = Cron(
const job = new Cron(
'* * * * *',
{
startAt: "2023-11-01T00:00:00",
Expand Down Expand Up @@ -76,11 +76,11 @@ const data = {
what: "stuff"
};

Cron('* * * * * *', { context: data }, (_self, context) => {
new Cron('* * * * * *', { context: data }, (_self, context) => {
console.log('This will print stuff: ' + context.what);
});

Cron('*/5 * * * * *', { context: data }, (self, context) => {
new Cron('*/5 * * * * *', { context: data }, (self, context) => {
console.log('After this, other stuff will be printed instead');
context.what = "other stuff";
self.stop();
Expand All @@ -91,7 +91,7 @@ Cron('*/5 * * * * *', { context: data }, (self, context) => {
```ts
// A javascript date, or a ISO 8601 local time string can be passed, to fire a function once.
// Always specify which timezone the ISO 8601 time string has with the timezone option.
let job = Cron("2025-01-01T23:00:00",{timezone: "Europe/Stockholm"},() => {
let job = new Cron("2025-01-01T23:00:00",{timezone: "Europe/Stockholm"},() => {
console.log('This will run at 2025-01-01 23:00:00 in timezone Europe/Stockholm');
});

Expand All @@ -104,7 +104,7 @@ if (job.nextRun() === null) {

### Time zone
```ts
let job = Cron("0 0 14 * * *", { timezone: "Europe/Stockholm" }, () => {
let job = new Cron("0 0 14 * * *", { timezone: "Europe/Stockholm" }, () => {
console.log('This will every day at 14:00 in time zone Europe/Stockholm');
});

Expand All @@ -131,7 +131,7 @@ If a job is stopped using `.stop()`, it will be removed from the scheduledJobs a
(() => {

// As we specify a name for the job, a reference will be kept in `scheduledJobs`
const job = Cron("* * * * * *", { name: "Job1" }, function () {
const job = new Cron("* * * * * *", { name: "Job1" }, function () {
console.log("This will run every second");
});

Expand Down
4 changes: 2 additions & 2 deletions docs/src/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ nav_order: 3
The most basic usage of Croner for scheduling is:

```ts
Cron("0 12 * * *, () => {
new Cron("0 12 * * *, () => {
console.log("This will run every day at 12:00");
});
```

And the most basic usage of Croner for getting next execution time of a pattern is:

```ts
console.log(Cron("0 12 * * *).next());
console.log(new Cron("0 12 * * *).next());
// 2023-07-08T12:00:00
```

0 comments on commit 9eabbc6

Please sign in to comment.