Skip to content

Commit f990f6c

Browse files
committed
build(package): make sure src file is propertly sorted
1 parent 06ffc7e commit f990f6c

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

.husky/pre-commit

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
npm run sort-colors
12
npm test

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"semantic-release": "semantic-release",
3333
"build-history": "node scripts/tools/history.js > dist/history.json",
3434
"history": "node scripts/tools/history.js > dist/history.json",
35-
"prepare": "husky"
35+
"prepare": "husky",
36+
"sort-colors": "node scripts/sortSrc.js"
3637
},
3738
"repository": {
3839
"type": "git",

scripts/sortSrc.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)