|
| 1 | +/* |
| 2 | + * Helper script that lists all CSS classes used in ReCodEx. |
| 3 | + */ |
| 4 | + |
| 5 | +/* eslint no-console: "off" */ |
| 6 | +import fs from 'node:fs'; |
| 7 | +import fsp from 'node:fs/promises'; |
| 8 | +import path from 'node:path'; |
| 9 | +import { glob } from 'glob'; |
| 10 | +import 'colors'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Fix import path. |
| 14 | + * @param {string} path |
| 15 | + * @param {string} basePath to which the path is relative |
| 16 | + * @returns {string} fixed path |
| 17 | + */ |
| 18 | +function patchImportPath(path, basePath) { |
| 19 | + if (!path.startsWith('.')) { |
| 20 | + return path; // our paths (to be fixed) begins with . or .. |
| 21 | + } |
| 22 | + |
| 23 | + const absPath = basePath + '/' + path; |
| 24 | + |
| 25 | + if (fixMissingExtension && !fs.existsSync(absPath)) { |
| 26 | + const missingExt = '.js'; |
| 27 | + if ( |
| 28 | + !absPath.endsWith(missingExt) && |
| 29 | + fs.existsSync(absPath + missingExt) && |
| 30 | + !fs.lstatSync(absPath + missingExt).isDirectory() |
| 31 | + ) { |
| 32 | + return path + missingExt; |
| 33 | + } else { |
| 34 | + console.log(`${path} ${basePath}`.red); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (addIndex && fs.lstatSync(absPath).isDirectory()) { |
| 39 | + const indexJs = '/index.js'; |
| 40 | + if (fs.existsSync(absPath + indexJs) && !fs.lstatSync(absPath + indexJs).isDirectory()) { |
| 41 | + return path + indexJs; |
| 42 | + } else { |
| 43 | + console.log(`${path} dir does not have 'index.js'`.red); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return path; |
| 48 | +} |
| 49 | + |
| 50 | +function getClassNameContent(content, fromIndex) { |
| 51 | + let index = fromIndex; |
| 52 | + let level = 1; |
| 53 | + while (index < content.length) { |
| 54 | + if (content[index] === '{') { |
| 55 | + ++level; |
| 56 | + } else if (content[index] === '}') { |
| 57 | + --level; |
| 58 | + } |
| 59 | + if (level === 0) { |
| 60 | + return content.substring(fromIndex, index); |
| 61 | + } |
| 62 | + ++index; |
| 63 | + } |
| 64 | + return null; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Process a JS file and fix all import and direct export statements. |
| 69 | + * @param {string} file path to the file |
| 70 | + * @returns {Array} tuple [ imports/exports count, modified paths count ] |
| 71 | + */ |
| 72 | +async function processFile(file) { |
| 73 | + const content = await fsp.readFile(file, 'utf8'); |
| 74 | + const res = []; |
| 75 | + |
| 76 | + // regular string classes |
| 77 | + const regex = /className="([^"]+)"/g; |
| 78 | + let match; |
| 79 | + while ((match = regex.exec(content)) !== null) { |
| 80 | + match[1] |
| 81 | + .split(/\s+/) |
| 82 | + .filter(cls => cls) |
| 83 | + .forEach(cls => res.push([cls, `${file}:${match.index}`])); |
| 84 | + } |
| 85 | + |
| 86 | + const regex2 = /className={/g; |
| 87 | + while ((match = regex2.exec(content)) !== null) { |
| 88 | + let classes = getClassNameContent(content, regex2.lastIndex); |
| 89 | + if (classes.startsWith('classnames(')) { |
| 90 | + // Try to extract as many names from classnames() dict keys |
| 91 | + const subrex = /'([^']+)':/g; |
| 92 | + let submatch; |
| 93 | + while ((submatch = subrex.exec(classes)) !== null) { |
| 94 | + if (/^[-_a-zA-Z0-9]+$/.test(submatch[1])) { |
| 95 | + res.push([submatch[1], `${file}:${match.index}`]); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + const subrex2 = /\[`([^`]+)`\]:/g; |
| 100 | + while ((submatch = subrex2.exec(classes)) !== null) { |
| 101 | + const cls = submatch[1].replaceAll(/\$\{[^}]+\}/g, '*'); |
| 102 | + if (/^[-_a-zA-Z0-9*]+$/.test(cls)) { |
| 103 | + res.push([cls, `${file}:${match.index}`]); |
| 104 | + } |
| 105 | + } |
| 106 | + } else if (classes.startsWith('`') && classes.endsWith('`')) { |
| 107 | + // handle `` strings, converting ${} into * as a wildcard replacement |
| 108 | + classes = classes.substring(1, classes.length - 1); |
| 109 | + classes = classes.replaceAll(/\$\{[^}]+\}/g, '*'); |
| 110 | + classes |
| 111 | + .split(/\s+/) |
| 112 | + .filter(cls => cls && cls !== '*') |
| 113 | + .forEach(cls => res.push([cls, `${file}:${match.index}`])); |
| 114 | + } else { |
| 115 | + // handle all internal strings in '' as possible class names |
| 116 | + const subrex = /'\s*([^']+)\s*'/g; |
| 117 | + let submatch; |
| 118 | + while ((submatch = subrex.exec(classes)) !== null) { |
| 119 | + submatch[1] |
| 120 | + .split(/\s+/) |
| 121 | + .filter(cls => /^[-_a-zA-Z0-9]+$/.test(cls)) |
| 122 | + .forEach(cls => res.push([cls, `${file}:${match.index}`])); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return res; |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Scan all JS files in given top-level directory. |
| 132 | + * @param {String} dir |
| 133 | + */ |
| 134 | +async function processDir(dir, res) { |
| 135 | + const searchPattern = `${dir}/**/*.js`; |
| 136 | + const files = await glob(searchPattern, { posix: true }); |
| 137 | + const promises = {}; |
| 138 | + |
| 139 | + for (const file of files) { |
| 140 | + promises[file] = await processFile(file); |
| 141 | + } |
| 142 | + |
| 143 | + for (const file in promises) { |
| 144 | + const classes = await promises[file]; |
| 145 | + classes.forEach(([cls, location]) => (res[cls] = [...(res[cls] || []), location])); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Main function that do all the stuff. |
| 151 | + */ |
| 152 | +async function main(args) { |
| 153 | + const classes = {}; |
| 154 | + for (const arg of args) { |
| 155 | + await processDir(arg, classes); |
| 156 | + } |
| 157 | + |
| 158 | + // const classNames = Object.keys(classes); |
| 159 | + // classNames.sort(); |
| 160 | + |
| 161 | + console.log(JSON.stringify(classes)); |
| 162 | +} |
| 163 | + |
| 164 | +const args = [...process.argv]; |
| 165 | +args.shift(); // discard node |
| 166 | +args.shift(); // discard script name |
| 167 | +main(args).catch(e => console.error(e)); |
0 commit comments