1 import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"
2 import { createRule } from "../../create-rule"
3 import { isIndexFile } from "../../util/filename"
4
5 type MessageIds = "missingDefaultExport"
6
7 export const rule = createRule<[], MessageIds>({
8 name: "scheduled-job-default-export-required",
9 meta: {
10 type: "problem",
11 docs: {
12 description: "Scheduled job files must default-export the job function.",
13 },
14 messages: {
15 missingDefaultExport:
16 "Scheduled job files must have a default export. Default-export the async job function so Medusa registers and runs it.",
17 },
18 schema: [],
19 },
20 defaultOptions: [],
21 create(context) {
22 // `index.<ext>` files in a jobs directory are barrels/re-exports, not job
23 // definitions, so they have no default-exported job function.
24 if (isIndexFile(context.filename)) {
25 return {}
26 }
27
28 let hasDefaultExport = false
29
30 return {
31 ExportDefaultDeclaration() {
32 hasDefaultExport = true
33 },
34 ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration) {
35 for (const spec of node.specifiers) {
36 if (
37 spec.exported.type === AST_NODE_TYPES.Identifier &&
38 spec.exported.name === "default"
39 ) {
40 hasDefaultExport = true
41 return
42 }
43 }
44 },
45 "Program:exit"(node: TSESTree.Program) {
46 if (hasDefaultExport) {
47 return
48 }
49 context.report({
50 node,
51 messageId: "missingDefaultExport",
52 })
53 },
54 }
55 },
56 })
57
58 export default rule