@entur/typography 2.0.0-beta.1 → 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.1",
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",
@@ -58,7 +58,6 @@
58
58
  "migrate": "./scripts/migrate-typography.js"
59
59
  },
60
60
  "peerDependencies": {
61
- "glob": "^11.0.0",
62
61
  "react": ">=16.8.0",
63
62
  "react-dom": ">=16.8.0"
64
63
  },
@@ -69,6 +68,9 @@
69
68
  "classnames": "^2.5.1",
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": "4ca471ab86d5b40be5a87eb41b352bf37b476db2"
87
+ "gitHead": "c975e3a16aa66dd5413cc2ac10a3ab96a8211cf1"
86
88
  }
@@ -65,31 +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 first:');
79
- console.error(' npm install glob');
80
- console.error(' yarn add glob');
81
- console.error(' pnpm add glob');
82
- console.error('');
83
- console.error('Then run the migration:');
84
- console.error(' npx @entur/typography@latest migrate');
85
- console.error(' yarn dlx @entur/typography@latest migrate');
86
- console.error('');
87
- console.error('📚 For more information, see:');
88
- console.error(
89
- ' https://linje.entur.no/komponenter/ressurser/typography-migration',
90
- );
91
- console.error('');
92
- 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
+ }
93
85
  }
94
86
 
95
87
  // Configuration
@@ -944,15 +936,23 @@ function updateComponents(content) {
944
936
  /**
945
937
  * Find files matching the given pattern in allowed directories
946
938
  *
947
- * This function uses efficient glob patterns and data structures:
948
- * - Single glob call with brace expansion instead of multiple calls
949
- * - Set-based extension filtering for O(1) lookups
950
- * - No array concatenation in loops
939
+ * Uses glob if available for better pattern matching, falls back to Node.js built-ins
951
940
  *
952
- * @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}')
953
942
  * @returns {string[]} Array of matching file paths
954
943
  */
955
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) {
956
956
  const allFiles = [];
957
957
 
958
958
  // Process directory patterns
@@ -1000,6 +1000,88 @@ function findFiles(pattern) {
1000
1000
  return uniqueFiles;
1001
1001
  }
1002
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
+
1003
1085
  function updateImportsAndComponents(content) {
1004
1086
  let updatedContent = content;
1005
1087
  let changes = 0;
@@ -1288,6 +1370,9 @@ function showNextSteps() {
1288
1370
  }
1289
1371
 
1290
1372
  async function main() {
1373
+ // Initialize glob detection
1374
+ await initializeGlob();
1375
+
1291
1376
  // Show help if requested
1292
1377
  if (process.argv.includes('--help') || process.argv.includes('-h')) {
1293
1378
  console.log('🎨 Typography Migration Script');
@@ -1298,18 +1383,12 @@ async function main() {
1298
1383
  console.log(' npx @entur/typography@latest migrate [options]');
1299
1384
  console.log(' yarn dlx @entur/typography@latest migrate [options]');
1300
1385
  console.log('');
1301
- console.log(' # Direct execution (requires glob package)');
1386
+ console.log(' # Direct execution (uses Node.js built-ins, glob optional)');
1302
1387
  console.log(' node scripts/migrate-typography.js [options]');
1303
1388
  console.log('');
1304
1389
  console.log(' # Local development');
1305
1390
  console.log(' npm run migrate');
1306
1391
  console.log('');
1307
- console.log('📋 Prerequisites:');
1308
- console.log(' - Install glob package: npm install glob');
1309
- console.log(
1310
- ' - Or use npx/yarn dlx which handles dependencies automatically',
1311
- );
1312
- console.log('');
1313
1392
  console.log('Options:');
1314
1393
  console.log(
1315
1394
  ' --dry-run Show what would be changed without modifying files',