164 lines
4.9 KiB
JavaScript
164 lines
4.9 KiB
JavaScript
import { readFile, readdir } from 'node:fs/promises';
|
|
import { createHash } from 'node:crypto';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { Linter } from 'eslint';
|
|
|
|
const root = fileURLToPath(new URL('../', import.meta.url));
|
|
const forbidden = [
|
|
/^@radix-ui\//,
|
|
/^@floating-ui\//,
|
|
/^@lobehub\/(?:ui|icons)(?:\/|$)/,
|
|
/^@douyinfe\/semi-(?:ui|icons|foundation)(?:\/|$)/,
|
|
/^@headlessui\//,
|
|
/^@mui\//,
|
|
/^@chakra-ui\//,
|
|
/^(?:leva|antd-style|react-layout-kit)(?:\/|$)/,
|
|
];
|
|
export const isForbiddenUiDependency = (name) =>
|
|
forbidden.some((pattern) => pattern.test(name));
|
|
|
|
export function auditManifest(manifest, lock) {
|
|
const issues = [];
|
|
for (const section of [
|
|
'dependencies',
|
|
'devDependencies',
|
|
'optionalDependencies',
|
|
'peerDependencies',
|
|
]) {
|
|
for (const name of Object.keys(manifest[section] || {})) {
|
|
if (isForbiddenUiDependency(name))
|
|
issues.push(`package.json ${section}: ${name}`);
|
|
}
|
|
}
|
|
for (const location of Object.keys(lock.packages || {})) {
|
|
if (isForbiddenUiDependency(location.split('node_modules/').at(-1))) {
|
|
issues.push(`package-lock.json: ${location}`);
|
|
}
|
|
}
|
|
if (!lock.packages || lock.lockfileVersion < 2)
|
|
issues.push('Expected an npm lockfile with a packages graph.');
|
|
return issues;
|
|
}
|
|
|
|
export function auditSource(source, filename) {
|
|
const linter = new Linter({ configType: 'eslintrc' });
|
|
linter.defineRule('ui-dependencies', {
|
|
create(context) {
|
|
const inspect = (node) => {
|
|
if (
|
|
node?.type === 'Literal' &&
|
|
typeof node.value === 'string' &&
|
|
isForbiddenUiDependency(node.value)
|
|
) {
|
|
context.report({
|
|
node,
|
|
message: `Use Ant Design or the audited local brand assets: ${node.value}`,
|
|
});
|
|
}
|
|
};
|
|
return {
|
|
ImportDeclaration: (node) => inspect(node.source),
|
|
ExportNamedDeclaration: (node) => inspect(node.source),
|
|
ExportAllDeclaration: (node) => inspect(node.source),
|
|
ImportExpression: (node) => inspect(node.source),
|
|
CallExpression: (node) => {
|
|
if (
|
|
node.callee.type === 'Identifier' &&
|
|
node.callee.name === 'require'
|
|
)
|
|
inspect(node.arguments[0]);
|
|
},
|
|
};
|
|
},
|
|
});
|
|
return linter
|
|
.verify(
|
|
source,
|
|
{
|
|
parserOptions: {
|
|
ecmaVersion: 2022,
|
|
sourceType: 'module',
|
|
ecmaFeatures: { jsx: true },
|
|
},
|
|
rules: { 'ui-dependencies': 'error' },
|
|
},
|
|
{ filename, allowInlineConfig: false },
|
|
)
|
|
.map((issue) => `${filename}:${issue.line}: ${issue.message}`);
|
|
}
|
|
|
|
async function sourceFiles(dir) {
|
|
const files = [];
|
|
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
const name = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) files.push(...(await sourceFiles(name)));
|
|
else if (/\.[cm]?[jt]sx?$/.test(entry.name)) files.push(name);
|
|
}
|
|
return files;
|
|
}
|
|
|
|
export async function checkUiDependencies(directory = root) {
|
|
const manifest = JSON.parse(
|
|
await readFile(path.join(directory, 'package.json'), 'utf8'),
|
|
);
|
|
const lock = JSON.parse(
|
|
await readFile(path.join(directory, 'package-lock.json'), 'utf8'),
|
|
);
|
|
const issues = auditManifest(manifest, lock);
|
|
const files = await sourceFiles(path.join(directory, 'src'));
|
|
for (const filename of files) {
|
|
issues.push(
|
|
...auditSource(
|
|
await readFile(filename, 'utf8'),
|
|
path.relative(directory, filename),
|
|
),
|
|
);
|
|
}
|
|
const assets = path.join(directory, 'src/assets/brand-icons');
|
|
const provenance = JSON.parse(
|
|
await readFile(path.join(assets, 'manifest.json'), 'utf8'),
|
|
);
|
|
const generated = (await readdir(assets))
|
|
.filter((name) => name.endsWith('.js'))
|
|
.sort();
|
|
if (
|
|
JSON.stringify(generated) !==
|
|
JSON.stringify(Object.keys(provenance.files).sort())
|
|
)
|
|
issues.push(
|
|
'Brand icon provenance must list every generated JavaScript file.',
|
|
);
|
|
for (const [name, hash] of Object.entries(provenance.files)) {
|
|
if (!/^(?:lobehub|primitives|shared-[A-Z0-9]+)\.js$/.test(name)) {
|
|
issues.push(`Invalid generated icon filename: ${name}`);
|
|
continue;
|
|
}
|
|
const icons = await readFile(path.join(assets, name));
|
|
if (createHash('sha256').update(icons).digest('hex') !== hash)
|
|
issues.push(
|
|
`Generated brand icons differ from their provenance hash: ${name}; regenerate them.`,
|
|
);
|
|
}
|
|
return {
|
|
issues,
|
|
files: files.length,
|
|
packages: Object.keys(lock.packages).length - 1,
|
|
};
|
|
}
|
|
|
|
if (
|
|
process.argv[1] &&
|
|
path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)
|
|
) {
|
|
const result = await checkUiDependencies();
|
|
if (result.issues.length) {
|
|
console.error(result.issues.join('\n'));
|
|
process.exitCode = 1;
|
|
} else {
|
|
console.log(
|
|
`UI dependency audit passed: ${result.files} source files, ${result.packages} locked packages.`,
|
|
);
|
|
}
|
|
}
|