1616const fs = require ( 'fs' ) ;
1717const path = require ( 'path' ) ;
1818const ts = require ( 'typescript' ) ;
19- const { minimatch } = require ( 'minimatch' ) ;
20- const { extractPlatformsFromAST, getValidPlatforms } = require ( './platform-utils' ) ;
19+ const { extractPlatformsFromFile, findSourceFiles } = require ( './platform-utils' ) ;
2120
2221const WORKSPACE_ROOT = path . join ( __dirname , '..' ) ;
2322const PLATFORMS = [ 'browser' , 'node' , 'react_native' ] ;
2423
25- // Load configuration
26- const configPath = path . join ( WORKSPACE_ROOT , '.platform-isolation.config.js' ) ;
27- const config = fs . existsSync ( configPath )
28- ? require ( configPath )
29- : {
30- include : [ 'lib/**/*.ts' , 'lib/**/*.js' ] ,
31- exclude : [
32- '**/*.spec.ts' , '**/*.test.ts' , '**/*.tests.ts' ,
33- '**/*.test.js' , '**/*.spec.js' , '**/*.tests.js' ,
34- '**/*.umdtests.js' , '**/*.test-d.ts' , '**/*.gen.ts' ,
35- '**/*.d.ts' , '**/__mocks__/**' , '**/tests/**'
36- ]
37- } ;
38-
3924function getPlatformFromFilename ( filename ) {
4025 const platforms = [ ] ;
4126 for ( const platform of PLATFORMS ) {
@@ -46,15 +31,6 @@ function getPlatformFromFilename(filename) {
4631 return platforms . length > 0 ? platforms : null ;
4732}
4833
49- /**
50- * Check if file matches any pattern using minimatch
51- */
52- function matchesPattern ( filePath , patterns ) {
53- const relativePath = path . relative ( WORKSPACE_ROOT , filePath ) . replace ( / \\ / g, '/' ) ;
54-
55- return patterns . some ( pattern => minimatch ( relativePath , pattern ) ) ;
56- }
57-
5834/**
5935 * Calculate relative import path for Platform type
6036 */
@@ -284,15 +260,8 @@ function addPlatformExport(content, platforms) {
284260function processFile ( filePath ) {
285261 let content = fs . readFileSync ( filePath , 'utf-8' ) ;
286262
287- // Get valid platforms for validation
288- const validPlatforms = getValidPlatforms ( ) ;
289-
290- // Use TypeScript parser to check for existing __platforms
291- const result = extractPlatformsFromAST (
292- ts . createSourceFile ( filePath , content , ts . ScriptTarget . Latest , true ) ,
293- filePath ,
294- validPlatforms // Validate platform values
295- ) ;
263+ // Use extractPlatformsFromFile which validates platform values
264+ const result = extractPlatformsFromFile ( filePath ) ;
296265
297266 // Extract platforms and error info from result
298267 const existingPlatforms = result . success ? result . platforms : null ;
@@ -363,45 +332,10 @@ function processFile(filePath) {
363332 return { skipped : true , reason : 'no changes needed' } ;
364333}
365334
366- /**
367- * Recursively find all files matching include patterns and not matching exclude patterns
368- */
369- function findSourceFiles ( dir , files = [ ] ) {
370- const entries = fs . readdirSync ( dir , { withFileTypes : true } ) ;
371-
372- for ( const entry of entries ) {
373- const fullPath = path . join ( dir , entry . name ) ;
374-
375- if ( entry . isDirectory ( ) ) {
376- // Check if this directory path could potentially contain files matching include patterns
377- // Use minimatch with partial mode to test if pattern could match files under this directory
378- const relativePath = path . relative ( WORKSPACE_ROOT , fullPath ) . replace ( / \\ / g, '/' ) ;
379- const couldMatch = config . include . some ( pattern => {
380- return minimatch ( relativePath , pattern , { partial : true } ) ;
381- } ) ;
382-
383- if ( couldMatch ) {
384- findSourceFiles ( fullPath , files ) ;
385- }
386- } else if ( entry . isFile ( ) ) {
387- // Check if file matches include patterns
388- if ( matchesPattern ( fullPath , config . include ) ) {
389- // Check if file is NOT excluded
390- if ( ! matchesPattern ( fullPath , config . exclude ) ) {
391- files . push ( fullPath ) ;
392- }
393- }
394- }
395- }
396-
397- return files ;
398- }
399-
400335function main ( ) {
401336 console . log ( '🔧 Processing __platforms exports...\n' ) ;
402- console . log ( `📋 Configuration: ${ path . relative ( WORKSPACE_ROOT , configPath ) || '.platform-isolation.config.js' } \n` ) ;
403337
404- const files = findSourceFiles ( WORKSPACE_ROOT ) ;
338+ const files = findSourceFiles ( ) ;
405339 let added = 0 ;
406340 let moved = 0 ;
407341 let fixed = 0 ;
0 commit comments