This commit is contained in:
+103
-6
@@ -3,6 +3,97 @@ import { fileURLToPath } from 'node:url';
|
||||
import path from 'node:path';
|
||||
import { parse } from 'svelte/compiler';
|
||||
|
||||
const sourceRoot = fileURLToPath(new URL('../src/', import.meta.url));
|
||||
const tokens = await readFile(path.join(sourceRoot, 'lib/ui/styles/tokens.css'), 'utf8');
|
||||
const sharedBreakpointWidths = new Set(
|
||||
[...tokens.matchAll(/--breakpoint-[\w-]+:\s*([\d.]+px)\s*;/g)].map((match) => match[1])
|
||||
);
|
||||
|
||||
// Existing layouts awaiting migration (Refs #130). Remove widths as each file
|
||||
// moves to the shared scale; a listed file cannot introduce other legacy widths.
|
||||
/** @type {Record<string, number[]>} */
|
||||
const legacyBreakpointWidths = {
|
||||
'src/lib/ui/components/ArtifactDetail.svelte': [720, 1000],
|
||||
'src/lib/ui/components/BetaPlanBuilder.svelte': [480, 850],
|
||||
'src/lib/ui/components/BetaTestProgress.svelte': [720],
|
||||
'src/lib/ui/components/BlueprintBuilder.svelte': [620],
|
||||
'src/lib/ui/components/ConfigEditor.svelte': [600],
|
||||
'src/lib/ui/components/ConfigInspector.svelte': [600],
|
||||
'src/lib/ui/components/ConfigLibrary.svelte': [600],
|
||||
'src/lib/ui/components/ConfigTextEditor.svelte': [600],
|
||||
'src/lib/ui/components/DeployCollectionDialog.svelte': [440, 600],
|
||||
'src/lib/ui/components/DeployServerDialog.svelte': [440],
|
||||
'src/lib/ui/components/ProvisioningActivity.svelte': [600],
|
||||
'src/lib/ui/components/ProvisioningProgress.svelte': [600],
|
||||
'src/lib/ui/components/SettingsEditor.svelte': [720],
|
||||
'src/lib/ui/components/capacity/RebalancePanel.svelte': [600],
|
||||
'src/lib/ui/components/capacity/RebalanceRunView.svelte': [600],
|
||||
'src/lib/ui/components/metrics/MetricsExplore.svelte': [540],
|
||||
'src/lib/ui/components/metrics/MetricsSummary.svelte': [760],
|
||||
'src/lib/ui/components/primitives/Button.svelte': [720],
|
||||
'src/lib/ui/components/primitives/Card.svelte': [480],
|
||||
'src/lib/ui/components/primitives/Dialog.svelte': [600],
|
||||
'src/lib/ui/styles/components.css': [600],
|
||||
'src/routes/account/+page.svelte': [540],
|
||||
'src/routes/autoscaling/+page.svelte': [500],
|
||||
'src/routes/betas/+page.svelte': [720],
|
||||
'src/routes/betas/[id]/+page.svelte': [720],
|
||||
'src/routes/betas/new/+page.svelte': [700],
|
||||
'src/routes/campaigns/+page.svelte': [720],
|
||||
'src/routes/campaigns/[id]/+page.svelte': [720],
|
||||
'src/routes/capacity/+page.svelte': [600],
|
||||
'src/routes/collections/launches/[id]/+page.svelte': [600],
|
||||
'src/routes/hosts/operations/+page.svelte': [600],
|
||||
'src/routes/leaderboard/+page.svelte': [800, 1000],
|
||||
'src/routes/login/+page.svelte': [540],
|
||||
'src/routes/matches/[id]/+page.svelte': [480, 800],
|
||||
'src/routes/metrics/+page.svelte': [540],
|
||||
'src/routes/plugins/+page.svelte': [720],
|
||||
'src/routes/provisioning/+page.svelte': [720],
|
||||
'src/routes/provisioning/[id]/+page.svelte': [600, 1000],
|
||||
'src/routes/setup/+page.svelte': [540]
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} source
|
||||
* @param {string} filename
|
||||
*/
|
||||
export function checkMediaWidths(source, filename) {
|
||||
// Parse styles so comments, script strings and CSS declaration values cannot
|
||||
// be mistaken for media queries. Wrap standalone CSS without adding a line.
|
||||
const cssSource = filename.endsWith('.css') ? `<style>${source}</style>` : source;
|
||||
const stylesheet = parse(cssSource, { modern: true }).css;
|
||||
const legacyWidths = legacyBreakpointWidths[filename.replaceAll('\\', '/')] ?? [];
|
||||
const allowed = new Set([...sharedBreakpointWidths, ...legacyWidths.map((n) => `${n}px`)]);
|
||||
/** @type {string[]} */
|
||||
const failures = [];
|
||||
/** @param {import('svelte/compiler').AST.CSS.StyleSheet | import('svelte/compiler').AST.CSS.Block | null} block */
|
||||
function visit(block) {
|
||||
for (const node of block?.children ?? []) {
|
||||
if (node.type === 'Atrule' && node.name.toLowerCase() === 'media') {
|
||||
const query = node.prelude.replace(/\/\*[\s\S]*?\*\//g, ' ');
|
||||
// Check both sides independently to cover chained range syntax.
|
||||
const widths = [
|
||||
...query.matchAll(
|
||||
/(?<![\w-])(?:min-|max-)?width\s*(?::|[<>]=?|=)\s*([^\s)]+)/gi
|
||||
),
|
||||
...query.matchAll(/([\d.]+(?:[a-z%]+)?)\s*(?:[<>]=?|=)\s*width\b/gi)
|
||||
];
|
||||
for (const match of widths) {
|
||||
if (allowed.has(match[1].toLowerCase())) continue;
|
||||
const line = cssSource.slice(0, node.start).split('\n').length;
|
||||
failures.push(
|
||||
`${filename}:${line}: use a shared media width (${[...sharedBreakpointWidths].join(', ')}): ${match[1]}`
|
||||
);
|
||||
}
|
||||
}
|
||||
if ('block' in node) visit(node.block);
|
||||
}
|
||||
}
|
||||
visit(stylesheet);
|
||||
return failures;
|
||||
}
|
||||
|
||||
const nativeInputTypes = new Set([
|
||||
'checkbox',
|
||||
'radio',
|
||||
@@ -69,7 +160,6 @@ export function checkFormControls(source, filename) {
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const root = fileURLToPath(new URL('../src/', import.meta.url));
|
||||
/** @type {string[]} */
|
||||
const failures = [];
|
||||
/** @param {string} directory */
|
||||
@@ -77,9 +167,14 @@ async function main() {
|
||||
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
||||
const file = path.join(directory, entry.name);
|
||||
if (entry.isDirectory()) await visit(file);
|
||||
else if (/\.(svelte|css)$/.test(entry.name) && entry.name !== 'tokens.css') {
|
||||
else if (/\.(svelte|css)$/.test(entry.name)) {
|
||||
const source = await readFile(file, 'utf8');
|
||||
const filename = path.relative(process.cwd(), file);
|
||||
const filename = path
|
||||
.relative(path.dirname(sourceRoot), file)
|
||||
.split(path.sep)
|
||||
.join('/');
|
||||
failures.push(...checkMediaWidths(source, filename));
|
||||
if (file === path.join(sourceRoot, 'lib/ui/styles/tokens.css')) continue;
|
||||
failures.push(...checkFormControls(source, filename));
|
||||
for (const match of source.matchAll(/font-size\s*:\s*[\d.]+px\b/g)) {
|
||||
const line = source.slice(0, match.index).split('\n').length;
|
||||
@@ -90,8 +185,7 @@ async function main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
await visit(root);
|
||||
const tokens = await readFile(path.join(root, 'lib/ui/styles/tokens.css'), 'utf8');
|
||||
await visit(sourceRoot);
|
||||
for (const match of tokens.matchAll(
|
||||
/--text-(?:xs|sm|base|md|lg|xl|2xl|3xl|input):\s*([\d.]+)px/g
|
||||
)) {
|
||||
@@ -100,7 +194,10 @@ async function main() {
|
||||
if (failures.length) {
|
||||
console.error(failures.join('\n'));
|
||||
process.exitCode = 1;
|
||||
} else console.log('UI styles: shared form controls, font sizes and 12 px minimum verified.');
|
||||
} else
|
||||
console.log(
|
||||
'UI styles: shared form controls, media widths, font sizes and 12 px minimum verified.'
|
||||
);
|
||||
}
|
||||
|
||||
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url))
|
||||
|
||||
Reference in New Issue
Block a user