Files
videoreg.ru/scripts/design-system/check-fsd.mjs
T

26 lines
4.2 KiB
JavaScript

/** Audit the actually imported UI graph, public APIs, layers and real consumers. */
import ts from 'typescript';
import { readdir,readFile,stat,writeFile } from 'node:fs/promises';
import { resolve,relative,dirname,sep } from 'node:path';
const root=resolve('web-front/src'), fsd=resolve(root,'fsd'), app=resolve(root,'app');
async function files(dir){let all=[];for(const d of await readdir(dir,{withFileTypes:true})){const p=resolve(dir,d.name);if(d.isDirectory())all.push(...await files(p));else if(/\.(ts|tsx)$/.test(p)&&!p.endsWith('.test.ts'))all.push(p);}return all;}
async function exists(p){try{return (await stat(p)).isFile();}catch{return false;}}
async function target(owner,name){let stem;if(name.startsWith('@/'))stem=resolve(root,name.slice(2));else if(name.startsWith('.'))stem=resolve(dirname(owner),name);else return null;for(const p of [stem,...['.ts','.tsx','.json','.css'].map(x=>stem+x),...['index.ts','index.tsx'].map(x=>resolve(stem,x))])if(await exists(p))return p;return null;}
const paths=[...await files(fsd),...await files(app)],info=new Map(),errors=[],edges=[];
const levels={app:6,pages:5,widgets:4,features:3,entities:2,shared:1};
function owner(p){if(p.startsWith(app+sep))return {layer:'app',slice:'app'};const [layer,slice]=relative(fsd,p).split(sep);return {layer,slice};}
for(const path of paths){const code=await readFile(path,'utf8'),ast=ts.createSourceFile(path,code,ts.ScriptTarget.Latest,true),imports=[];
for(const node of ast.statements){if((ts.isImportDeclaration(node)||ts.isExportDeclaration(node))&&node.moduleSpecifier&&ts.isStringLiteral(node.moduleSpecifier)){const name=node.moduleSpecifier.text,to=await target(path,name);imports.push({name,to,names:ts.isImportDeclaration(node)&&node.importClause?.namedBindings&&ts.isNamedImports(node.importClause.namedBindings)?node.importClause.namedBindings.elements.map(x=>x.name.text):[]});}}
const from=owner(path);if((from.layer==='shared'||from.layer==='entities')&&ast.statements.some(n=>ts.isExpressionStatement(n)&&ts.isStringLiteral(n.expression)&&n.expression.text==='use client'))errors.push({kind:'client-boundary-in-shared/entity',file:relative(root,path)});
for(const imp of imports){if(imp.name==='@/content/library')errors.push({kind:'compatibility-cycle-risk',file:relative(root,path)});if(!imp.to)continue;if(imp.to.startsWith(fsd+sep)){const dest=owner(imp.to);if(levels[dest.layer]>levels[from.layer]||(dest.layer===from.layer&&dest.slice!==from.slice&&from.layer!=='app'))errors.push({kind:'upward/lateral-import',from:relative(root,path),to:relative(root,imp.to)});if(imp.name.startsWith('@/fsd/')&&!(dest.layer==='shared'&&imp.to.endsWith('.css'))&&!/^index\.tsx?$/.test(imp.to.split(sep).at(-1)))errors.push({kind:'slice-public-api-bypass',from:relative(root,path),import:imp.name});}edges.push({from:relative(root,path),to:relative(root,imp.to),import:imp.name});}
info.set(path,{imports,ast});
}
const active=new Set();async function visit(p){if(active.has(p))return;active.add(p);for(const imp of info.get(p)?.imports??[])if(imp.to)await visit(imp.to);}
for(const p of await files(app))await visit(p);
const consumers={};for(const [path,data] of info){if(!active.has(path)||path.includes('/pages/ui-kit/'))continue;for(const imp of data.imports)if(imp.name==='@/fsd/shared/ui')for(const name of imp.names)(consumers[name]??=[]).push(relative(root,path));}
const ui=info.get(resolve(fsd,'shared/ui/index.tsx'));const exports=ui.ast.statements.filter(n=>ts.isFunctionDeclaration(n)&&n.modifiers?.some(m=>m.kind===ts.SyntaxKind.ExportKeyword)).map(n=>n.name.text);
for(const name of exports)if(!consumers[name]?.length)errors.push({kind:'shared-ui-without-real-page-consumer',component:name});
if(await exists(resolve(root,'pages/index.tsx')))errors.push({kind:'accidental-next-pages-router'});
const report={scope:'Active technical-library FSD graph; inactive legacy catalog shared->entities debt preserved, no runtime cycle inferred',activeSourceFiles:active.size,FSDModules:info.size,edges,sharedComponentRealConsumers:consumers,errors};
await writeFile('docs/design-system/fsd-graph.json',JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({activeFiles:active.size,modules:info.size,sharedComponents:exports.length,errors}));if(errors.length)process.exitCode=1;