1 import path from "path"
2 import DocblockGenerator from "../classes/generators/docblock.js"
3 import getMonorepoRoot from "../utils/get-monorepo-root.js"
4 import { GitManager } from "../classes/helpers/git-manager.js"
5 import { CommonCliOptions } from "../types/index.js"
6 import OasGenerator from "../classes/generators/oas.js"
7 import DmlGenerator from "../classes/generators/dml.js"
8 import RouteExamplesGenerator from "../classes/generators/route-examples.js"
9 import EventsGenerator from "../classes/generators/events.js"
10
11 export default async function runGitChanges({
12 type,
13 ...options
14 }: CommonCliOptions) {
15 const monorepoPath = getMonorepoRoot()
16 // retrieve the changed files under `packages` in the monorepo root.
17 const gitManager = new GitManager()
18 let files = await gitManager.getDiffFiles()
19
20 if (!files.length) {
21 console.log(`No file changes detected.`)
22 return
23 }
24
25 console.log(
26 `${files.length} files have changed. Running generator on them...`
27 )
28
29 files = files.map((filePath) => path.resolve(monorepoPath, filePath))
30
31 if (type === "all" || type === "docs") {
32 const docblockGenerator = new DocblockGenerator({
33 paths: files,
34 ...options,
35 })
36
37 await docblockGenerator.run()
38 }
39
40 if (type === "all" || type === "oas") {
41 const oasGenerator = new OasGenerator({
42 paths: files,
43 ...options,
44 })
45
46 await oasGenerator.run()
47 }
48
49 if (type === "all" || type === "dml") {
50 const dmlGenerator = new DmlGenerator({
51 paths: files,
52 ...options,
53 })
54
55 await dmlGenerator.run()
56 }
57
58 if (type === "all" || type === "route-examples") {
59 const routeExamplesGenerator = new RouteExamplesGenerator({
60 paths: files,
61 ...options,
62 })
63
64 await routeExamplesGenerator.run()
65 }
66
67 if (type === "all" || type === "events") {
68 const eventsGenerator = new EventsGenerator({
69 paths: files,
70 ...options,
71 })
72
73 await eventsGenerator.run()
74 }
75
76 console.log(`Finished generating docs for ${files.length} files.`)
77 }