Skip to content

Commit a6617e8

Browse files
committed
dry
1 parent 17ffe48 commit a6617e8

File tree

3 files changed

+88
-108
lines changed

3 files changed

+88
-108
lines changed

scripts/add-platform-exports.js

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,11 @@
1616
const fs = require('fs');
1717
const path = require('path');
1818
const ts = require('typescript');
19-
const { minimatch } = require('minimatch');
20-
const { extractPlatformsFromAST, getValidPlatforms } = require('./platform-utils');
19+
const { extractPlatformsFromFile, findSourceFiles } = require('./platform-utils');
2120

2221
const WORKSPACE_ROOT = path.join(__dirname, '..');
2322
const 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-
3924
function 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) {
284260
function 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-
400335
function 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;

scripts/platform-utils.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,45 @@
2727
const fs = require('fs');
2828
const path = require('path');
2929
const ts = require('typescript');
30+
const { minimatch } = require('minimatch');
3031

3132
// Cache for valid platforms
3233
let validPlatformsCache = null;
3334

3435
// Cache for file platforms
3536
const filePlatformCache = new Map();
3637

38+
// Cache for config
39+
let configCache = null;
40+
41+
/**
42+
* Load platform isolation configuration
43+
*
44+
* @returns {Object} Configuration object with include/exclude patterns
45+
*/
46+
function loadConfig() {
47+
if (configCache) {
48+
return configCache;
49+
}
50+
51+
const workspaceRoot = path.join(__dirname, '..');
52+
const configPath = path.join(workspaceRoot, '.platform-isolation.config.js');
53+
54+
configCache = fs.existsSync(configPath)
55+
? require(configPath)
56+
: {
57+
include: ['lib/**/*.ts', 'lib/**/*.js'],
58+
exclude: [
59+
'**/*.spec.ts', '**/*.test.ts', '**/*.tests.ts',
60+
'**/*.test.js', '**/*.spec.js', '**/*.tests.js',
61+
'**/*.umdtests.js', '**/*.test-d.ts', '**/*.gen.ts',
62+
'**/*.d.ts', '**/__mocks__/**', '**/tests/**'
63+
]
64+
};
65+
66+
return configCache;
67+
}
68+
3769
/**
3870
* Extract valid platform values from Platform type definition in platform_support.ts
3971
* Parses: type Platform = 'browser' | 'node' | 'react_native' | '__universal__';
@@ -267,8 +299,56 @@ function extractPlatformsFromFile(absolutePath) {
267299
return result;
268300
}
269301

302+
/**
303+
* Find all source files matching include/exclude patterns from config
304+
*
305+
* @returns {string[]} Array of absolute file paths
306+
*/
307+
function findSourceFiles() {
308+
const workspaceRoot = path.join(__dirname, '..');
309+
const config = loadConfig();
310+
311+
/**
312+
* Check if file matches any pattern using minimatch
313+
*/
314+
function matchesPattern(filePath, patterns, options = {}) {
315+
const relativePath = path.relative(workspaceRoot, filePath).replace(/\\/g, '/');
316+
return patterns.some(pattern => minimatch(relativePath, pattern, options));
317+
}
318+
319+
/**
320+
* Recursively find all files matching include patterns and not matching exclude patterns
321+
*/
322+
function findFilesRecursive(dir, files = []) {
323+
const entries = fs.readdirSync(dir, { withFileTypes: true });
324+
325+
for (const entry of entries) {
326+
const fullPath = path.join(dir, entry.name);
327+
328+
if (entry.isDirectory()) {
329+
// Check if this directory path could potentially contain files matching include patterns
330+
if (matchesPattern(fullPath, config.include, { partial: true })) {
331+
findFilesRecursive(fullPath, files);
332+
}
333+
} else if (entry.isFile()) {
334+
// Check if file matches include patterns and is NOT excluded
335+
if (matchesPattern(fullPath, config.include)
336+
&& !matchesPattern(fullPath, config.exclude)) {
337+
files.push(fullPath);
338+
}
339+
}
340+
}
341+
342+
return files;
343+
}
344+
345+
return findFilesRecursive(workspaceRoot);
346+
}
347+
270348
module.exports = {
271349
getValidPlatforms,
272350
extractPlatformsFromAST,
273351
extractPlatformsFromFile,
352+
findSourceFiles,
353+
loadConfig,
274354
};

scripts/validate-platform-isolation-ts.js

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const fs = require('fs');
4242
const path = require('path');
4343
const ts = require('typescript');
4444
const { minimatch } = require('minimatch');
45-
const { getValidPlatforms, extractPlatformsFromFile } = require('./platform-utils');
45+
const { getValidPlatforms, extractPlatformsFromFile, findSourceFiles, loadConfig } = require('./platform-utils');
4646

4747
const WORKSPACE_ROOT = path.join(__dirname, '..');
4848

@@ -56,18 +56,8 @@ const compilerOptions = ts.convertCompilerOptionsFromJson(
5656
).options;
5757

5858
// Load configuration
59+
const config = loadConfig();
5960
const configPath = path.join(WORKSPACE_ROOT, '.platform-isolation.config.js');
60-
const config = fs.existsSync(configPath)
61-
? require(configPath)
62-
: {
63-
include: ['lib/**/*.ts', 'lib/**/*.js'],
64-
exclude: [
65-
'**/*.spec.ts', '**/*.test.ts', '**/*.tests.ts',
66-
'**/*.test.js', '**/*.spec.js', '**/*.tests.js',
67-
'**/*.umdtests.js', '**/*.test-d.ts', '**/*.gen.ts',
68-
'**/*.d.ts', '**/__mocks__/**', '**/tests/**'
69-
]
70-
};
7161

7262
// Track files with errrors in __platforms export
7363
const fileErrors = new Map();
@@ -371,31 +361,7 @@ function reportPlatformErrors(errorsByType, validPlatforms) {
371361
return hasErrors;
372362
}
373363

374-
/**
375-
* Recursively find all files matching include patterns and not matching exclude patterns
376-
*/
377-
function findSourceFiles(dir, files = []) {
378-
const entries = fs.readdirSync(dir, { withFileTypes: true });
379-
380-
for (const entry of entries) {
381-
const fullPath = path.join(dir, entry.name);
382-
383-
if (entry.isDirectory()) {
384-
// Check if this directory path could potentially contain files matching include patterns
385-
if (matchesPattern(fullPath, config.include, { partial: true })) {
386-
findSourceFiles(fullPath, files);
387-
}
388-
} else if (entry.isFile()) {
389-
// Check if file matches include patterns and is NOT excluded
390-
if (matchesPattern(fullPath, config.include)
391-
&& !matchesPattern(fullPath, config.exclude)) {
392-
files.push(fullPath);
393-
}
394-
}
395-
}
396-
397-
return files;
398-
}
364+
399365

400366
/**
401367
* Main validation function
@@ -404,7 +370,7 @@ function main() {
404370
console.log('🔍 Validating platform isolation...\n');
405371
console.log(`📋 Configuration: ${path.relative(WORKSPACE_ROOT, configPath) || '.platform-isolation.config.js'}\n`);
406372

407-
const files = findSourceFiles(WORKSPACE_ROOT);
373+
const files = findSourceFiles();
408374

409375
// Load valid platforms first
410376
const validPlatforms = getValidPlatforms();

0 commit comments

Comments
 (0)