Repository navigation
Expand file tree
/
Copy pathcheck-css-cascade.ts
More file actions
70 lines (58 loc) · 2.12 KB
/
Copy pathcheck-css-cascade.ts
File metadata and controls
70 lines (58 loc) · 2.12 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
import { readFileSync } from "node:fs";
import { catalogNames, catalogPolicy } from "./component-policy.ts";
const indexCss = readFileSync("src/index.css", "utf8");
const preflightCss = readFileSync("src/preflight.css", "utf8");
const failures: string[] = [];
const expectedOrder =
"@layer base, angularcss.tokens, angularcss.components, components, utilities;";
if (!indexCss.startsWith(expectedOrder)) {
failures.push(`src/index.css must begin with ${expectedOrder}`);
}
const catalogImportPattern =
/^@import "\.\/(foundations|elements|patterns|components|recipes)\/([^/]+)\/([^";]+\.css)"[^;]*;/gm;
const catalogImports = [...indexCss.matchAll(catalogImportPattern)];
for (const match of catalogImports) {
if (!match[0].trimEnd().endsWith("layer(angularcss.components);")) {
failures.push(
`catalog import is outside angularcss.components: ${match[0]}`,
);
}
}
const actualCatalogImports = catalogImports.map(
([, category, directory, file]) => `${category}/${directory}/${file}`,
);
const expectedCatalogImports = catalogNames.map((name) => {
const { category } = catalogPolicy[name];
return `${category}/${name}/${name}.css`;
});
for (const expected of expectedCatalogImports) {
const count = actualCatalogImports.filter((path) => path === expected).length;
if (count !== 1) {
failures.push(
`src/index.css must import ${expected} exactly once; found ${count}`,
);
}
}
for (const actual of actualCatalogImports) {
if (!expectedCatalogImports.includes(actual)) {
failures.push(
`src/index.css imports unregistered catalog stylesheet ${actual}`,
);
}
}
const tokenImport =
'@import "./styles/generated/tokens.css" layer(angularcss.tokens);';
if (indexCss.split(tokenImport).length !== 2) {
failures.push(
"src/index.css must import generated customization tokens exactly once",
);
}
if (!preflightCss.startsWith("@layer base {")) {
failures.push(
"src/preflight.css must define the documentation reset in base",
);
}
if (failures.length > 0) {
throw new Error(`CSS cascade contract failed:\n${failures.join("\n")}`);
}
console.log("CSS cascade contract passed.");