@entur/typography 2.0.0-beta.2 → 2.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@entur/typography",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.3",
4
4
  "license": "SEE LICENSE IN README.md",
5
5
  "type": "module",
6
6
  "main": "./dist/typography.cjs.js",
@@ -66,9 +66,11 @@
66
66
  "@entur/tokens": "^3.19.3",
67
67
  "@entur/utils": "^0.12.5",
68
68
  "classnames": "^2.5.1",
69
- "glob": "^11.0.0",
70
69
  "modern-normalize": "^3.0.1"
71
70
  },
71
+ "optionalDependencies": {
72
+ "glob": "^11.0.3"
73
+ },
72
74
  "devDependencies": {
73
75
  "@testing-library/jest-dom": "^5.17.0",
74
76
  "@testing-library/react": "^10.4.9",
@@ -82,5 +84,5 @@
82
84
  "vite": "^7.1.3",
83
85
  "vite-plugin-dts": "^4.5.4"
84
86
  },
85
- "gitHead": "4b2446e281aa4e8b7b7f78ca9191baf74fb250cb"
87
+ "gitHead": "c975e3a16aa66dd5413cc2ac10a3ab96a8211cf1"
86
88
  }
@@ -65,24 +65,23 @@ import { fileURLToPath } from 'url';
65
65
  const __filename = fileURLToPath(import.meta.url);
66
66
  const __dirname = path.dirname(__filename);
67
67
 
68
- // Check if glob is available
68
+ // Try to use glob if available, fallback to Node.js built-ins
69
69
  let glob;
70
- try {
71
- const globModule = await import('glob');
72
- glob = globModule.default || globModule;
73
- } catch (error) {
74
- console.error(
75
- '❌ Error: The "glob" package is required to run this migration script.',
76
- );
77
- console.error('');
78
- console.error('Please install it:');
79
- console.error(' npm install glob');
80
- console.error(' yarn add glob');
81
- console.error('');
82
- console.error('Or use npx which will handle dependencies automatically:');
83
- console.error(' npx @entur/typography@latest migrate');
84
- console.error('');
85
- process.exit(1);
70
+ let useGlob = false;
71
+
72
+ // Initialize glob detection
73
+ async function initializeGlob() {
74
+ try {
75
+ const globModule = await import('glob');
76
+ glob = globModule.default || globModule;
77
+ useGlob = true;
78
+ console.log('📦 Using glob package for pattern matching');
79
+ } catch (error) {
80
+ console.log('📁 Using Node.js built-ins for file discovery');
81
+ console.log(
82
+ ' (Install glob for better pattern matching: npm install glob or yarn add glob)',
83
+ );
84
+ }
86
85
  }
87
86
 
88
87
  // Configuration
@@ -937,15 +936,23 @@ function updateComponents(content) {
937
936
  /**
938
937
  * Find files matching the given pattern in allowed directories
939
938
  *
940
- * This function uses efficient glob patterns and data structures:
941
- * - Single glob call with brace expansion instead of multiple calls
942
- * - Set-based extension filtering for O(1) lookups
943
- * - No array concatenation in loops
939
+ * Uses glob if available for better pattern matching, falls back to Node.js built-ins
944
940
  *
945
- * @param {string} pattern - Glob pattern to match (e.g., '*.{ts,tsx,js,jsx}')
941
+ * @param {string} pattern - File pattern to match (e.g., '*.{ts,tsx,js,jsx}')
946
942
  * @returns {string[]} Array of matching file paths
947
943
  */
948
944
  function findFiles(pattern) {
945
+ if (useGlob) {
946
+ return findFilesWithGlob(pattern);
947
+ } else {
948
+ return findFilesWithNode(pattern);
949
+ }
950
+ }
951
+
952
+ /**
953
+ * Find files using glob package (preferred method)
954
+ */
955
+ function findFilesWithGlob(pattern) {
949
956
  const allFiles = [];
950
957
 
951
958
  // Process directory patterns
@@ -993,6 +1000,88 @@ function findFiles(pattern) {
993
1000
  return uniqueFiles;
994
1001
  }
995
1002
 
1003
+ /**
1004
+ * Find files using Node.js built-ins (fallback method)
1005
+ */
1006
+ function findFilesWithNode(pattern) {
1007
+ const allFiles = [];
1008
+
1009
+ // Parse the pattern to extract extensions
1010
+ const extensions = pattern.match(/\*\.\{([^}]+)\}/);
1011
+ const fileExtensions = extensions
1012
+ ? extensions[1].split(',').map(ext => `.${ext.trim()}`)
1013
+ : ['.ts', '.tsx', '.js', '.jsx', '.scss', '.css'];
1014
+
1015
+ const extensionSet = new Set(fileExtensions);
1016
+
1017
+ // Helper function to check if a path should be ignored
1018
+ function shouldIgnore(filePath) {
1019
+ return BLOCKED_DIRECTORIES.some(blockedPattern => {
1020
+ // Simple pattern matching for common blocked directories
1021
+ if (blockedPattern.includes('**')) {
1022
+ const basePattern = blockedPattern.replace('/**', '');
1023
+ return filePath.includes(basePattern);
1024
+ }
1025
+ return filePath.includes(blockedPattern);
1026
+ });
1027
+ }
1028
+
1029
+ // Helper function to recursively find files
1030
+ function findFilesRecursive(dir, relativePath = '') {
1031
+ try {
1032
+ const items = fs.readdirSync(dir, { withFileTypes: true });
1033
+
1034
+ for (const item of items) {
1035
+ const itemPath = path.join(dir, item.name);
1036
+ const relativeItemPath = path.join(relativePath, item.name);
1037
+
1038
+ // Skip if this path should be ignored
1039
+ if (shouldIgnore(relativeItemPath)) {
1040
+ continue;
1041
+ }
1042
+
1043
+ if (item.isDirectory()) {
1044
+ // Recursively search subdirectories
1045
+ findFilesRecursive(itemPath, relativeItemPath);
1046
+ } else if (item.isFile()) {
1047
+ // Check if file has matching extension
1048
+ const ext = path.extname(item.name).toLowerCase();
1049
+ if (extensionSet.has(ext)) {
1050
+ allFiles.push(relativeItemPath);
1051
+ }
1052
+ }
1053
+ }
1054
+ } catch (error) {
1055
+ // Skip directories that can't be read (permissions, etc.)
1056
+ console.warn(
1057
+ `Warning: Could not read directory ${dir}: ${error.message}`,
1058
+ );
1059
+ }
1060
+ }
1061
+
1062
+ // Process each allowed directory
1063
+ for (const allowedDir of ALLOWED_DIRECTORIES) {
1064
+ if (allowedDir.includes('**')) {
1065
+ // Directory pattern (e.g., src/**)
1066
+ const baseDir = allowedDir.replace('/**', '');
1067
+ const fullPath = path.resolve(baseDir);
1068
+
1069
+ if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
1070
+ findFilesRecursive(fullPath, baseDir);
1071
+ }
1072
+ } else {
1073
+ // File pattern (e.g., *.jsx) - not supported in this implementation
1074
+ // as it would require more complex pattern matching
1075
+ console.warn(
1076
+ `Warning: File pattern "${allowedDir}" not supported in Node.js-only mode. Use directory patterns like "src/**" instead.`,
1077
+ );
1078
+ }
1079
+ }
1080
+
1081
+ // Remove duplicates and return
1082
+ return [...new Set(allFiles)];
1083
+ }
1084
+
996
1085
  function updateImportsAndComponents(content) {
997
1086
  let updatedContent = content;
998
1087
  let changes = 0;
@@ -1281,6 +1370,9 @@ function showNextSteps() {
1281
1370
  }
1282
1371
 
1283
1372
  async function main() {
1373
+ // Initialize glob detection
1374
+ await initializeGlob();
1375
+
1284
1376
  // Show help if requested
1285
1377
  if (process.argv.includes('--help') || process.argv.includes('-h')) {
1286
1378
  console.log('🎨 Typography Migration Script');
@@ -1291,7 +1383,7 @@ async function main() {
1291
1383
  console.log(' npx @entur/typography@latest migrate [options]');
1292
1384
  console.log(' yarn dlx @entur/typography@latest migrate [options]');
1293
1385
  console.log('');
1294
- console.log(' # Direct execution (requires glob package)');
1386
+ console.log(' # Direct execution (uses Node.js built-ins, glob optional)');
1295
1387
  console.log(' node scripts/migrate-typography.js [options]');
1296
1388
  console.log('');
1297
1389
  console.log(' # Local development');