@sigil-dev/plugin-jobs 0.9.29 → 0.9.30

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/manifest.ts ADDED
@@ -0,0 +1,96 @@
1
+ import {
2
+ existsSync,
3
+ mkdirSync,
4
+ readdirSync,
5
+ rmSync,
6
+ writeFileSync,
7
+ } from "node:fs";
8
+ import { join, relative } from "node:path";
9
+ import type { Job } from "./define.ts";
10
+
11
+ /**
12
+ * The jobs folder, compiled to a list.
13
+ *
14
+ * Like routes: the files are found when the app is built, and written out as
15
+ * plain imports (`.grimoire/jobs.ts`) that the server loads. Nothing scans the
16
+ * folder while serving, and every job is an import the bundler and the
17
+ * editor can follow.
18
+ *
19
+ * A job's name is its path under the folder without the extension:
20
+ * `src/jobs/send-welcome.ts` is "send-welcome", `src/jobs/mail/digest.ts` is
21
+ * "mail/digest". Tests (`*.test.ts`) and files starting with `_` (helpers the
22
+ * jobs share) are not jobs.
23
+ */
24
+ export function jobFiles(dir: string): { name: string; file: string }[] {
25
+ if (!existsSync(dir)) return [];
26
+ const out: { name: string; file: string }[] = [];
27
+ for (const entry of readdirSync(dir, {
28
+ recursive: true,
29
+ withFileTypes: true,
30
+ })) {
31
+ if (!entry.isFile()) continue;
32
+ if (!/\.[cm]?[jt]s$/.test(entry.name)) continue;
33
+ if (
34
+ /\.(test|spec)\.[cm]?[jt]s$/.test(entry.name) ||
35
+ entry.name.startsWith("_")
36
+ )
37
+ continue;
38
+ const file = join(entry.parentPath, entry.name);
39
+ const name = relative(dir, file)
40
+ .replace(/\\/g, "/")
41
+ .replace(/\.[cm]?[jt]s$/, "");
42
+ out.push({ name, file });
43
+ }
44
+ return out.sort((a, b) => a.name.localeCompare(b.name));
45
+ }
46
+
47
+ export function manifestPath(root: string): string {
48
+ return join(root, ".grimoire", "jobs.ts");
49
+ }
50
+
51
+ /** Write `.grimoire/jobs.ts` for the folder; remove it when there are no jobs. */
52
+ export function writeManifest(root: string, dir: string): string | undefined {
53
+ const files = jobFiles(dir);
54
+ const out = manifestPath(root);
55
+ if (files.length === 0) {
56
+ rmSync(out, { force: true });
57
+ return undefined;
58
+ }
59
+ const from = join(root, ".grimoire");
60
+ const spec = (file: string) => {
61
+ const rel = relative(from, file).replace(/\\/g, "/");
62
+ return rel.startsWith(".") ? rel : `./${rel}`;
63
+ };
64
+ const lines = [
65
+ "// Auto-generated by @sigil-dev/plugin-jobs from the jobs folder — do not edit",
66
+ ...files.map(
67
+ (f, i) => `import j${i} from ${JSON.stringify(spec(f.file))};`,
68
+ ),
69
+ "",
70
+ "export default {",
71
+ ...files.map((f, i) => `\t${JSON.stringify(f.name)}: j${i},`),
72
+ "};",
73
+ "",
74
+ ];
75
+ mkdirSync(from, { recursive: true });
76
+ writeFileSync(out, lines.join("\n"));
77
+ return out;
78
+ }
79
+
80
+ /** The jobs in the folder, compiled fresh. A file whose default export is not a job is an error naming it. */
81
+ export async function loadManifest(
82
+ root: string,
83
+ dir: string,
84
+ ): Promise<Record<string, Job<any>>> {
85
+ const out = writeManifest(root, dir);
86
+ if (!out) return {};
87
+ const mod = (await import(out)) as { default: Record<string, unknown> };
88
+ for (const [name, job] of Object.entries(mod.default)) {
89
+ if (!job || typeof (job as Job<any>).handler !== "function") {
90
+ throw new Error(
91
+ `plugin-jobs: ${relative(root, dir).replace(/\\/g, "/")}/${name} does not export a job. Its default export must be defineJob({ ... }).`,
92
+ );
93
+ }
94
+ }
95
+ return mod.default as Record<string, Job<any>>;
96
+ }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "module": "index.ts",
12
12
  "type": "module",
13
13
  "private": false,
14
- "version": "0.9.29",
14
+ "version": "0.9.30",
15
15
  "keywords": [
16
16
  "grimoire",
17
17
  "sigil",
@@ -28,7 +28,8 @@
28
28
  },
29
29
  "peerDependencies": {
30
30
  "typescript": "^5",
31
- "@sigil-dev/grimoire": "0.9.29",
32
- "@sigil-dev/plugin-sql": "0.9.29"
31
+ "@sigil-dev/grimoire": "0.9.30",
32
+ "@sigil-dev/plugin-sql": "0.9.30",
33
+ "@sigil-dev/tome": "0.9.30"
33
34
  }
34
35
  }