33 * Vérifie que les imports respectent les règles de l'architecture
44 */
55
6- const fs = require ( 'fs' ) ;
7- const path = require ( ' path' ) ;
6+ const fs = require ( "fs" ) ;
7+ const path = require ( " path" ) ;
88
9- const LAYERS = [ ' app' , ' pages' , ' widgets' , ' features' , ' entities' , ' shared' ] ;
9+ const LAYERS = [ " app" , " pages" , " widgets" , " features" , " entities" , " shared" ] ;
1010const LAYER_HIERARCHY = {
11- ' shared' : [ ] ,
12- ' entities' : [ ' shared' ] ,
13- ' features' : [ ' entities' , ' shared' ] ,
14- ' widgets' : [ ' features' , ' entities' , ' shared' ] ,
15- ' pages' : [ ' widgets' , ' features' , ' entities' , ' shared' ] ,
16- ' app' : [ ' pages' , ' widgets' , ' features' , ' entities' , ' shared' ]
11+ shared : [ ] ,
12+ entities : [ " shared" ] ,
13+ features : [ " entities" , " shared" ] ,
14+ widgets : [ " features" , " entities" , " shared" ] ,
15+ pages : [ " widgets" , " features" , " entities" , " shared" ] ,
16+ app : [ " pages" , " widgets" , " features" , " entities" , " shared" ] ,
1717} ;
1818
1919function getLayerFromPath ( filePath ) {
20- const relativePath = path . relative ( path . join ( __dirname , ' src' ) , filePath ) ;
20+ const relativePath = path . relative ( path . join ( __dirname , " src" ) , filePath ) ;
2121 const firstDir = relativePath . split ( path . sep ) [ 0 ] ;
2222 return LAYERS . includes ( firstDir ) ? firstDir : null ;
2323}
@@ -33,12 +33,15 @@ function checkImports(filePath, fileContent) {
3333 while ( ( match = importRegex . exec ( fileContent ) ) !== null ) {
3434 const importedLayer = match [ 1 ] ;
3535
36- if ( LAYERS . includes ( importedLayer ) && ! LAYER_HIERARCHY [ layer ] . includes ( importedLayer ) ) {
36+ if (
37+ LAYERS . includes ( importedLayer ) &&
38+ ! LAYER_HIERARCHY [ layer ] . includes ( importedLayer )
39+ ) {
3740 violations . push ( {
3841 file : filePath ,
3942 layer,
4043 importedLayer,
41- line : fileContent . substring ( 0 , match . index ) . split ( '\n' ) . length
44+ line : fileContent . substring ( 0 , match . index ) . split ( "\n" ) . length ,
4245 } ) ;
4346 }
4447 }
@@ -54,10 +57,17 @@ function scanDirectory(dir) {
5457 const filePath = path . join ( dir , file ) ;
5558 const stat = fs . statSync ( filePath ) ;
5659
57- if ( stat . isDirectory ( ) && ! file . startsWith ( '.' ) && file !== 'node_modules' ) {
60+ if (
61+ stat . isDirectory ( ) &&
62+ ! file . startsWith ( "." ) &&
63+ file !== "node_modules"
64+ ) {
5865 violations . push ( ...scanDirectory ( filePath ) ) ;
59- } else if ( stat . isFile ( ) && ( file . endsWith ( '.ts' ) || file . endsWith ( '.tsx' ) ) ) {
60- const content = fs . readFileSync ( filePath , 'utf8' ) ;
66+ } else if (
67+ stat . isFile ( ) &&
68+ ( file . endsWith ( ".ts" ) || file . endsWith ( ".tsx" ) )
69+ ) {
70+ const content = fs . readFileSync ( filePath , "utf8" ) ;
6171 violations . push ( ...checkImports ( filePath , content ) ) ;
6272 }
6373 }
@@ -66,21 +76,27 @@ function scanDirectory(dir) {
6676}
6777
6878// Exécution
69- console . log ( ' 🔍 Vérification de l\ 'architecture FSD...\n' ) ;
79+ console . log ( " 🔍 Vérification de l'architecture FSD...\n" ) ;
7080
71- const srcDir = path . join ( __dirname , ' ../src' ) ;
81+ const srcDir = path . join ( __dirname , " ../src" ) ;
7282const violations = scanDirectory ( srcDir ) ;
7383
7484if ( violations . length === 0 ) {
75- console . log ( '✅ Aucune violation détectée ! L\'architecture FSD est respectée.' ) ;
85+ console . log (
86+ "✅ Aucune violation détectée ! L'architecture FSD est respectée." ,
87+ ) ;
7688} else {
7789 console . log ( `❌ ${ violations . length } violation(s) détectée(s):\n` ) ;
7890
7991 violations . forEach ( ( { file, layer, importedLayer, line } ) => {
8092 const relativeFile = path . relative ( __dirname , file ) ;
8193 console . log ( ` ${ relativeFile } :${ line } ` ) ;
82- console . log ( ` ↳ La couche "${ layer } " ne peut pas importer depuis "${ importedLayer } "` ) ;
83- console . log ( ` ↳ Imports autorisés: ${ LAYER_HIERARCHY [ layer ] . join ( ', ' ) || 'aucun' } \n` ) ;
94+ console . log (
95+ ` ↳ La couche "${ layer } " ne peut pas importer depuis "${ importedLayer } "` ,
96+ ) ;
97+ console . log (
98+ ` ↳ Imports autorisés: ${ LAYER_HIERARCHY [ layer ] . join ( ", " ) || "aucun" } \n` ,
99+ ) ;
84100 } ) ;
85101
86102 process . exit ( 1 ) ;
0 commit comments