-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmanifest.ts
More file actions
162 lines (131 loc) · 4.27 KB
/
manifest.ts
File metadata and controls
162 lines (131 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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(),
})
),
})
),
});
type RulesManifestData = z.infer<typeof RulesManifestDataSchema>;
export type RulesManifestVersionOption = {
name: string;
title: string;
label: string;
contents: string;
tokens: number;
client: string | undefined;
installStrategy: RulesFileInstallStrategy;
applyTo: string | undefined;
};
export type ManifestVersion = {
version: string;
options: Array<RulesManifestVersionOption>;
};
export class RulesManifest {
constructor(
private readonly manifest: RulesManifestData,
private readonly loader: RulesManifestLoader
) {}
get name() {
return this.manifest.name;
}
get description() {
return this.manifest.description;
}
get currentVersion() {
return this.manifest.currentVersion;
}
async getCurrentVersion(): Promise<ManifestVersion> {
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) as Array<ManifestVersion["options"][number]>,
};
}
get versions() {
return this.manifest.versions;
}
}
export async function loadRulesManifest(loader: RulesManifestLoader): Promise<RulesManifest> {
const content = await loader.loadManifestContent();
return new RulesManifest(RulesManifestDataSchema.parse(JSON.parse(content)), loader);
}
export interface RulesManifestLoader {
loadManifestContent(): Promise<string>;
loadRulesFile(relativePath: string): Promise<string>;
}
export class GithubRulesManifestLoader implements RulesManifestLoader {
constructor(private readonly branch: string = "main") {}
async loadManifestContent(): Promise<string> {
const response = await fetch(
`https://raw.githubusercontent.com/triggerdotdev/trigger.dev/refs/heads/${this.branch}/rules/manifest.json`
);
if (!response.ok) {
throw new Error(`Failed to load rules manifest: ${response.status} ${response.statusText}`);
}
return response.text();
}
async loadRulesFile(relativePath: string): Promise<string> {
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 implements RulesManifestLoader {
constructor(private readonly path: string) {}
async loadManifestContent(): Promise<string> {
try {
return await readFile(this.path, "utf8");
} catch (error) {
throw new Error(`Failed to load rules manifest: ${this.path} - ${error}`);
}
}
async loadRulesFile(relativePath: string): Promise<string> {
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}`);
}
}
}