UNPKG

color-cleaner

Version:

A CLI tool to clean and consolidate colors in your project files.

88 lines (81 loc) 4.02 kB
import { colord } from 'colord'; import path from 'path'; import {getAllFiles} from './filesService.js'; const cssNamedColors = [ 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'transparent', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen' ]; const COLOR_PATTERNS = [ /rgb\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)/gi, /rgba\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*,\s*[\d.]+\s*\)/gi, /#[0-9a-f]{3,8}/gi, /hsl\(\s*\d+\s*,\s*\d+%\s*,\s*\d+%\s*\)/gi, /hsla\(\s*\d+\s*,\s*\d+%\s*,\s*\d+%\s*,\s*[\d.]+\s*\)/gi, new RegExp(`\\b(${cssNamedColors.join('|')})\\b`, 'gi') ]; const getColorLines = async ( files, config = { framework: 'Angular', version: '19', skipNodeModules: true, thresholdSensitivity: 7, fileTypes: ['.css', '.scss', '.js', '.ts', '.html'] } ) => { const filteredFiles = files.filter(file => { const ext = path.extname(file.path).toLowerCase().replace('.', ''); return config.fileTypes.includes(ext); }); const colorLines = []; for (const file of filteredFiles) { const lines = file.content.split('\n'); for (let i = 0; i < lines.length; i++) { const line = lines[i]; for (const pattern of COLOR_PATTERNS) { const matches = line.match(pattern) || []; for (const match of matches) { try { if (colord(match).isValid() || cssNamedColors.includes(match)) { colorLines.push({ lineNumber: i + 1, currentPath: file.path, color: match, }); } else { console.log(`Color: ${match} is invalid and wont be processed`); } } catch (error) { console.log(`Color: ${match} is invalid and wont be processed`); continue; } } } } } return colorLines; }; export { getColorLines };