forked from cognetwork-dev/Metallic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
themes.js
93 lines (83 loc) · 2.87 KB
/
themes.js
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
import test from 'ava';
import { z } from "zod";
import fs from 'fs';
test('Themes file exists', (t) => {
t.true(fs.existsSync('src/themes.json'), "The file /src/themes.json file does not exist.");
});
let themes = fs.readFileSync("src/themes.json", "utf-8");
test('Themes file contains JSON', (t) => {
try {
themes = JSON.parse(themes)
t.pass();
} catch {
t.fail("The themes file does not contain valid JSON.");
}
});
test('Themes file contains an array', (t) => {
t.true(Array.isArray(themes), "The themes file does not contain a valid array.");
});
const ThemeProperties = z.object({
background: z.string({
required_error: "Background is required",
invalid_type_error: "Background must be a string",
}),
secondary: z.string({
required_error: "Secondary is required",
invalid_type_error: "Secondary must be a string",
}),
primary: z.string({
required_error: "Name is required",
invalid_type_error: "Name must be a string",
}),
text: z.string({
required_error: "Text is required",
invalid_type_error: "Text must be a string",
}),
textInverse: z.string({
required_error: "Text inverse is required",
invalid_type_error: "Text inverse must be a string",
}),
font: z.string({
required_error: "Font is required",
invalid_type_error: "Font must be a string",
}),
})
const CustomThemes = z.record(z.string({
required_error: "Custom property selector is required",
invalid_type_error: "Custom property selectors value must be a string",
}), z.record(z.string({
required_error: "Custom property key is required",
invalid_type_error: "Custom property keys value must be a string",
}), z.string({
required_error: "Custom property value is required",
invalid_type_error: "Custom property values value must be a string",
}))).optional()
const Theme = z.object({
name: z.string({
required_error: "Name is required",
invalid_type_error: "Name must be a string",
}),
id: z.string({
required_error: "ID is required",
invalid_type_error: "ID must be a string",
}),
theme: ThemeProperties,
custom: CustomThemes
})
test('Themes are valid', (t) => {
for (let theme of themes) {
if (Theme.safeParse(theme).success) {
t.pass();
} else {
console.log("Failed theme: ")
console.log(theme)
t.fail("A theme is missing a property or a property is an incorrect type.");
}
}
});
test('Default theme exists', (t) => {
t.true(themes.filter((theme) => theme.id == "default").length > 0, "No default theme was found")
});
test('No duplicate themes exist', (t) => {
t.true(themes.filter((theme) => themes.filter((x) => x.id == theme.id).length > 1).length == 0, "Duplicate themes exist with the same ID.")
});