UNPKG

trigger.dev

Version:

A Command-Line Interface for Trigger.dev projects

112 lines 3.88 kB
import { readFile } from "fs/promises"; import { dirname, join } from "path"; import { z } from "zod"; import { RulesFileInstallStrategy } from "./types.js"; const RulesManifestDataSchema = z.object({ name: z.string(), description: z.string(), currentVersion: z.string(), versions: z.record(z.string(), z.object({ options: z.array(z.object({ name: z.string(), title: z.string(), label: z.string(), path: z.string(), tokens: z.number(), client: z.string().optional(), installStrategy: z.string().optional(), applyTo: z.string().optional(), })), })), }); export class RulesManifest { manifest; loader; constructor(manifest, loader) { this.manifest = manifest; this.loader = loader; } get name() { return this.manifest.name; } get description() { return this.manifest.description; } get currentVersion() { return this.manifest.currentVersion; } async getCurrentVersion() { const version = this.versions[this.manifest.currentVersion]; if (!version) { throw new Error(`Version ${this.manifest.currentVersion} not found in manifest`); } const options = await Promise.all(version.options.map(async (option) => { const contents = await this.loader.loadRulesFile(option.path); // Omit path const { path, installStrategy, ...rest } = option; const $installStrategy = RulesFileInstallStrategy.safeParse(installStrategy ?? "default"); // Skip variants with invalid install strategies if (!$installStrategy.success) { return; } return { ...rest, contents, installStrategy: $installStrategy.data }; })); return { version: this.manifest.currentVersion, options: options.filter(Boolean), }; } get versions() { return this.manifest.versions; } } export async function loadRulesManifest(loader) { const content = await loader.loadManifestContent(); return new RulesManifest(RulesManifestDataSchema.parse(JSON.parse(content)), loader); } export class GithubRulesManifestLoader { branch; constructor(branch = "main") { this.branch = branch; } async loadManifestContent() { const response = await fetch(`https://raw.githubusercontent.com/triggerdotdev/trigger.dev/refs/heads/${this.branch}/rules/manifest.json`, { signal: AbortSignal.timeout(5000), }); if (!response.ok) { throw new Error(`Failed to load rules manifest: ${response.status} ${response.statusText}`); } return response.text(); } async loadRulesFile(relativePath) { const response = await fetch(`https://raw.githubusercontent.com/triggerdotdev/trigger.dev/refs/heads/${this.branch}/rules/${relativePath}`); if (!response.ok) { throw new Error(`Failed to load rules file: ${relativePath} - ${response.status} ${response.statusText}`); } return response.text(); } } export class LocalRulesManifestLoader { path; constructor(path) { this.path = path; } async loadManifestContent() { try { return await readFile(this.path, "utf8"); } catch (error) { throw new Error(`Failed to load rules manifest: ${this.path} - ${error}`); } } async loadRulesFile(relativePath) { const path = join(dirname(this.path), relativePath); try { return await readFile(path, "utf8"); } catch (error) { throw new Error(`Failed to load rules file: ${relativePath} - ${error}`); } } } //# sourceMappingURL=manifest.js.map