gongxiangp2/scripts/check-dark-variants.mjs

26 lines
988 B
JavaScript

import { globSync, readFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
// Lower this ceiling as part of any cleanup that removes variants.
export const DARK_VARIANT_BASELINE = 1389;
export function countDarkVariants(root) {
const sources = globSync(['app', 'components', 'hooks'].map(dir => dir + '/**/*.{ts,tsx,css}'), { cwd: root });
let count = 0;
for (const file of sources) {
count += (readFileSync(join(root, file), 'utf8').match(/dark:/g) ?? []).length;
}
return count;
}
export function checkDarkVariants(root, baseline = DARK_VARIANT_BASELINE) {
const count = countDarkVariants(root);
if (count > baseline) throw Error('dark: 出现 ' + count + ' 次,超过基线 ' + baseline + ' 次');
return count;
}
if (process.argv[1] && pathToFileURL(resolve(process.argv[1])).href === import.meta.url) {
console.log('dark: ' + checkDarkVariants(process.cwd()) + ' / ' + DARK_VARIANT_BASELINE);
}