|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Script to sort the colornames.csv file alphabetically by name |
| 5 | + * This helps maintain order when new colors are added to the list |
| 6 | + */ |
| 7 | + |
| 8 | +import fs from 'fs'; |
| 9 | +import path from 'path'; |
| 10 | +import { fileURLToPath } from 'url'; |
| 11 | + |
| 12 | +// Get the directory name using ES modules approach |
| 13 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 14 | + |
| 15 | +// Path to the CSV file |
| 16 | +const csvPath = path.join(__dirname, '..', 'src', 'colornames.csv'); |
| 17 | + |
| 18 | +// Read the CSV file |
| 19 | +const readAndSortCSV = () => { |
| 20 | + try { |
| 21 | + // Read the file |
| 22 | + const data = fs.readFileSync(csvPath, 'utf8'); |
| 23 | + |
| 24 | + // Split the data into lines |
| 25 | + const lines = data.trim().split('\n'); |
| 26 | + |
| 27 | + // The header should be kept as the first line |
| 28 | + const header = lines[0]; |
| 29 | + |
| 30 | + // Remove the header from the array of lines |
| 31 | + const colorLines = lines.slice(1); |
| 32 | + |
| 33 | + // Sort the color lines alphabetically by name (case-insensitive) |
| 34 | + const sortedColorLines = colorLines.sort((a, b) => { |
| 35 | + // Extract the name from each line (first column before the comma) |
| 36 | + const nameA = a.split(',')[0].toLowerCase(); |
| 37 | + const nameB = b.split(',')[0].toLowerCase(); |
| 38 | + return nameA.localeCompare(nameB); |
| 39 | + }); |
| 40 | + |
| 41 | + // Combine the header and sorted lines |
| 42 | + const sortedData = [header, ...sortedColorLines].join('\n'); |
| 43 | + |
| 44 | + // Write the sorted data back to the file |
| 45 | + fs.writeFileSync(csvPath, sortedData, 'utf8'); |
| 46 | + |
| 47 | + console.log(`✅ Successfully sorted ${sortedColorLines.length} colors alphabetically by name`); |
| 48 | + console.log(`📝 File saved: ${csvPath}`); |
| 49 | + |
| 50 | + } catch (error) { |
| 51 | + console.error('❌ Error sorting the CSV file:', error); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +// Execute the function |
| 57 | +readAndSortCSV(); |
0 commit comments