forked from catppuccin/userstyles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.ts
More file actions
135 lines (117 loc) Β· 3.43 KB
/
Copy pathdata.ts
File metadata and controls
135 lines (117 loc) Β· 3.43 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
import type { SetRequired } from "type-fest/source/set-required.d.ts";
import type { CategoriesSchema, UserstylesSchema } from "../types/mod.ts";
import path from "node:path";
import { sprintf } from "@std/fmt/printf";
import Ajv, { type Schema } from "ajv";
import yaml, { YAMLParseError } from "yaml";
import {
CATEGORIES_SCHEMA,
REPO_ROOT,
STYLES_ROOT,
USERSTYLES_SCHEMA,
} from "../constants.ts";
import { readDirSync, readTextFileSync } from "./fs.ts";
import { log } from "./logger.ts";
type Userstyles = SetRequired<
UserstylesSchema.UserstylesSchema,
"userstyles" | "collaborators"
>;
/**
* @param content A string of YAML content
* @param schema A JSON schema
* @returns A promise that resolves to the parsed YAML content, verified against the schema. Rejects if the content is invalid.
*/
export function validateYaml<T>(
content: string,
schema: Schema,
file: string,
options?: Ajv.Options,
): T {
const ajv = new Ajv.default(options);
const validate = ajv.compile<T>(schema);
const data = yaml.parse(content);
if (!validate(data)) {
log.error(
validate
.errors!.map((err) =>
sprintf(
"%s %s%s",
err.instancePath.slice(1).replaceAll("/", "."),
err.message,
err.params.allowedValues
? ` (${JSON.stringify(err.params.allowedValues, undefined)})`
: "",
),
)
.join(" and "),
{
file,
},
);
process.exit(1);
}
return data as T;
}
/**
* Utility function that calls {@link validateYaml} on the userstyles.yml file.
* Requires the `userstyles` and `collaborators` fields.
*/
export function getUserstylesData(): Userstyles {
const content = readTextFileSync(
path.join(REPO_ROOT, "scripts/userstyles.yml"),
);
try {
const data = validateYaml<UserstylesSchema.UserstylesSchema>(
content,
USERSTYLES_SCHEMA,
"scripts/userstyles.yml",
{ schemas: [CATEGORIES_SCHEMA] },
);
for (const field of ["userstyles", "collaborators"] as const) {
if (data[field] === undefined) {
log.error(`Missing required field \`${field}\``, {
file: "scripts/userstyles.yml",
});
process.exit(1);
}
}
return data as Userstyles;
} catch (err) {
if (err instanceof YAMLParseError) {
const groups =
/(?<message>.*) at line (?<line>\d+), column (?<column>\d+):[\S\s]*/.exec(
err.message,
)?.groups;
log.error(groups!.message, {
file: "scripts/userstyles.yml",
startLine: Number(groups!.line),
content: content,
});
} else {
throw err;
}
process.exit(1);
}
}
/**
* Utility function that calls {@link validateYaml} on the categories.yml file.
*/
export async function getCategoriesData(): Promise<CategoriesSchema.CategoryDefinitions> {
const content = await fetch(
"https://raw.githubusercontent.com/catppuccin/catppuccin/de9d2cd963059753c8fd66fbb6f807be95c6cc1e/resources/categories.yml",
).then((res) => res.text());
const data = validateYaml<CategoriesSchema.CategoryDefinitions>(
content,
CATEGORIES_SCHEMA,
"categories.yml",
);
return data;
}
export function getUserstylesFiles(): string[] {
const files: string[] = [];
for (const dir of readDirSync(STYLES_ROOT)) {
if (!dir.isDirectory()) continue;
files.push(path.join(STYLES_ROOT, dir.name, "catppuccin.user.less"));
}
return files;
}