@@ -3,39 +3,51 @@ import { dirname } from "node:path";
33
44import { CWD } from "../constants" ;
55
6- type ProjectFileStructure = {
7- root : string ;
8- metadata : string ;
9- files : string [ ] ;
6+ export type ScannerOptions = {
7+ /**
8+ * A list of folders to ignore
9+ *
10+ * @default [".git", "node_modules", "dist", "out"]
11+ */
12+ excludeFolders ?: string [ ] ;
13+ /**
14+ * A list of files to include (following glob matcher)
15+ *
16+ * @default **\/*\/!(test|stories|*.test|*.stories).?(m){j,t}s?(x)
17+ */
18+ includeFiles ?: string [ ] ;
1019} ;
1120
12- export const scan = ( ) => {
13- const projects = getProjects ( ) ;
21+ export const scan = ( options : ScannerOptions = { } ) => {
22+ const excludedFolders = options . excludeFolders ?? DEFAULT_EXCLUDED_FOLDERS ;
23+ const includedFiles = options . includeFiles ?? DEFAULT_INCLUDED_FILES ;
1424
15- const output : ProjectFileStructure [ ] = projects . map ( ( project ) => {
16- return { ...project , files : getFiles ( project . root ) } ;
17- } ) ;
18-
19- return output ;
20- } ;
21-
22- const getProjects = ( ) : Pick < ProjectFileStructure , "metadata" | "root" > [ ] => {
23- return new fdir ( )
25+ const projects = new fdir ( )
2426 . withBasePath ( )
2527 . glob ( "**/package.json" )
28+ . exclude ( ( dirName ) => excludedFolders . includes ( dirName ) )
2629 . crawl ( CWD )
2730 . sync ( )
2831 . map ( ( path ) => {
29- return { root : dirname ( path ) , metadata : path } ;
32+ return { metadata : path , folder : dirname ( path ) } ;
3033 } ) ;
31- } ;
3234
33- const getFiles = ( projectPath : string ) => {
34- return new fdir ( )
35- . withBasePath ( )
36- . glob ( "**/*.?(m){j,t}s?(x)" ) // js|jsx|ts|tsx|cjs|mjs|cjsx|mjsx
37- . crawl ( projectPath )
38- . sync ( ) ;
35+ return projects . map ( ( project ) => {
36+ const files : string [ ] = new fdir ( )
37+ . withBasePath ( )
38+ . glob ( ...includedFiles )
39+ . exclude ( ( dirName ) => excludedFolders . includes ( dirName ) )
40+ . crawl ( project . folder )
41+ . sync ( ) ;
42+
43+ return { ...project , files } ;
44+ } ) ;
3945} ;
4046
47+ const DEFAULT_EXCLUDED_FOLDERS = [ ".git" , "node_modules" , "dist" , "out" ] ;
48+
49+ const DEFAULT_INCLUDED_FILES = [
50+ "**/*/!(test|*.test|stories|*.stories).?(m){j,t}s?(x)" , // js|jsx|ts|tsx|cjs|mjs|cjsx|mjsx excluding test-like and story-like files
51+ ] ;
52+
4153console . log ( scan ( ) ) ;
0 commit comments