|
| 1 | +import { Framework, ProjectInstallOptions } from ".."; |
| 2 | +import { InstallPackage } from "../../utils/addDependencies"; |
| 3 | +import { pathExists, someFileExists } from "../../utils/fileSystem"; |
| 4 | +import { PackageManager } from "../../utils/getUserPkgManager"; |
| 5 | +import pathModule from "path"; |
| 6 | +import { getPathAlias } from "../../utils/pathAlias"; |
| 7 | +import { createFileFromTemplate } from "../../utils/createFileFromTemplate"; |
| 8 | +import { templatesPath } from "../../paths"; |
| 9 | +import { logger } from "../../utils/logger"; |
| 10 | +import { readPackageJson } from "../../utils/readPackageJson"; |
| 11 | +import { standardWatchFilePaths } from "../watchConfig"; |
| 12 | + |
| 13 | +export class Astro implements Framework { |
| 14 | + id = "astro"; |
| 15 | + name = "Astro"; |
| 16 | + |
| 17 | + async isMatch(path: string, packageManager: PackageManager): Promise<boolean> { |
| 18 | + const configFilenames = [ |
| 19 | + "astro.config.js", |
| 20 | + "astro.config.mjs", |
| 21 | + "astro.config.cjs", |
| 22 | + "astro.config.ts", |
| 23 | + ]; |
| 24 | + //check for astro.config.mjs |
| 25 | + const hasConfigFile = await someFileExists(path, configFilenames); |
| 26 | + if (hasConfigFile) { |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + //check for the astro package |
| 31 | + const packageJsonContent = await readPackageJson(path); |
| 32 | + if (packageJsonContent?.dependencies?.astro) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + async dependencies(): Promise<InstallPackage[]> { |
| 40 | + return [ |
| 41 | + { name: "@trigger.dev/sdk", tag: "latest" }, |
| 42 | + { name: "@trigger.dev/astro", tag: "latest" }, |
| 43 | + { name: "@trigger.dev/react", tag: "latest" }, |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + possibleEnvFilenames(): string[] { |
| 48 | + return [".env", ".env.development"]; |
| 49 | + } |
| 50 | + |
| 51 | + async install(path: string, { typescript, endpointSlug }: ProjectInstallOptions): Promise<void> { |
| 52 | + const pathAlias = await getPathAlias({ |
| 53 | + projectPath: path, |
| 54 | + isTypescriptProject: typescript, |
| 55 | + extraDirectories: ["src"], |
| 56 | + }); |
| 57 | + const templatesDir = pathModule.join(templatesPath(), "astro"); |
| 58 | + const srcFolder = pathModule.join(path, "src"); |
| 59 | + const fileExtension = typescript ? ".ts" : ".js"; |
| 60 | + |
| 61 | + //create src/pages/api/trigger.js |
| 62 | + const apiRoutePath = pathModule.join(srcFolder, "pages", "api", `trigger${fileExtension}`); |
| 63 | + const apiRouteResult = await createFileFromTemplate({ |
| 64 | + templatePath: pathModule.join(templatesDir, "apiRoute.js"), |
| 65 | + replacements: { |
| 66 | + routePathPrefix: pathAlias ? pathAlias + "/" : "../../", |
| 67 | + }, |
| 68 | + outputPath: apiRoutePath, |
| 69 | + }); |
| 70 | + if (!apiRouteResult.success) { |
| 71 | + throw new Error("Failed to create API route file"); |
| 72 | + } |
| 73 | + logger.success(`✔ Created API route at ${apiRoutePath}`); |
| 74 | + |
| 75 | + //src/trigger.js |
| 76 | + const triggerFilePath = pathModule.join(srcFolder, `trigger${fileExtension}`); |
| 77 | + const triggerResult = await createFileFromTemplate({ |
| 78 | + templatePath: pathModule.join(templatesDir, "trigger.js"), |
| 79 | + replacements: { |
| 80 | + endpointSlug, |
| 81 | + }, |
| 82 | + outputPath: triggerFilePath, |
| 83 | + }); |
| 84 | + if (!triggerResult.success) { |
| 85 | + throw new Error("Failed to create trigger file"); |
| 86 | + } |
| 87 | + logger.success(`✔ Created Trigger client at ${triggerFilePath}`); |
| 88 | + |
| 89 | + //src/jobs/example.js |
| 90 | + const exampleJobFilePath = pathModule.join(srcFolder, "jobs", `example${fileExtension}`); |
| 91 | + const exampleJobResult = await createFileFromTemplate({ |
| 92 | + templatePath: pathModule.join(templatesDir, "exampleJob.js"), |
| 93 | + replacements: { |
| 94 | + jobsPathPrefix: pathAlias ? pathAlias + "/" : "../", |
| 95 | + }, |
| 96 | + outputPath: exampleJobFilePath, |
| 97 | + }); |
| 98 | + if (!exampleJobResult.success) { |
| 99 | + throw new Error("Failed to create example job file"); |
| 100 | + } |
| 101 | + logger.success(`✔ Created example job at ${exampleJobFilePath}`); |
| 102 | + |
| 103 | + //src/jobs/index.js |
| 104 | + const jobsIndexFilePath = pathModule.join(srcFolder, "jobs", `index${fileExtension}`); |
| 105 | + const jobsIndexResult = await createFileFromTemplate({ |
| 106 | + templatePath: pathModule.join(templatesDir, "jobsIndex.js"), |
| 107 | + replacements: { |
| 108 | + jobsPathPrefix: pathAlias ? pathAlias + "/" : "../", |
| 109 | + }, |
| 110 | + outputPath: jobsIndexFilePath, |
| 111 | + }); |
| 112 | + if (!jobsIndexResult.success) { |
| 113 | + throw new Error("Failed to create jobs index file"); |
| 114 | + } |
| 115 | + logger.success(`✔ Created jobs index at ${jobsIndexFilePath}`); |
| 116 | + } |
| 117 | + |
| 118 | + async postInstall(path: string, options: ProjectInstallOptions): Promise<void> { |
| 119 | + logger.warn( |
| 120 | + `⚠︎ Ensure your astro.config output is "server" or "hybrid":\nhttps://docs.astro.build/en/guides/server-side-rendering/#enabling-ssr-in-your-project` |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + defaultHostnames = ["localhost", "[::]"]; |
| 125 | + defaultPorts = [4321, 4322, 4323, 4324]; |
| 126 | + watchFilePaths = standardWatchFilePaths; |
| 127 | + watchIgnoreRegex = /(node_modules)/; |
| 128 | +} |
0 commit comments