rob-revisited/ide/ui/js/bootstrap-detect.js

60 lines
4.4 KiB
JavaScript

// Does this page, this project use Bootstrap, and which one (no DOM: node --test runs it). A page says it by its links and
// data attributes; a Twig child template says nothing (its layout does): the project's package.json, importmap.php and
// base template are read too. The version is '5' or '4'; a project that uses Bootstrap without saying which gets '5'.
const MARKERS = /(?:^|[\s"'])(?:btn|container|container-fluid|row|navbar|card|form-control|modal|alert|badge|nav-link|list-group|dropdown-menu|col(?:-(?:sm|md|lg|xl|xxl)(?:-\d+)?|-\d+)?)(?=[\s"'])/g;
/** {used, version, reason} of a text: version null when it does not say */
export function detectInText(text) {
const t = String(text || '');
let m;
if ((m = /bootstrap(?:@|\/|-)v?(\d)(?:\.\d+)*/i.exec(t.replace(/\{\{[\s\S]*?\}\}/g, ' ')))) return { used: true, version: m[1] === '4' ? '4' : '5', reason: 'link' };
if (/\bdata-bs-(?:toggle|target|dismiss|ride|slide|parent)\b/.test(t)) return { used: true, version: '5', reason: 'data-bs' };
if (/\bdata-(?:toggle|dismiss)="(?:collapse|modal|dropdown|tab|pill|tooltip|popover|alert|button|buttons)"/.test(t)) return { used: true, version: '4', reason: 'data-toggle' };
const classes = [...t.matchAll(/\bclass\s*=\s*"([^"]*)"/g)].map((x) => x[1]).join(' ');
if (/\b(?:ms|me|ps|pe)-(?:[0-5]|auto)\b|\bform-select\b|\btext-bg-|\bbtn-close\b|\boffcanvas\b|\bfloat-(?:start|end)\b/.test(classes)) return { used: true, version: '5', reason: 'classes' };
if (/\b(?:ml|mr|pl|pr)-(?:[0-5]|auto)\b|\bcustom-select\b|\bform-group\b|\bbadge-(?:primary|secondary|success|danger|warning|info|light|dark)\b|\bsr-only\b|\bfloat-(?:left|right)\b/.test(classes)) return { used: true, version: '4', reason: 'classes' };
const found = (` ${classes} `.replace(/\s+/g, ' ')).match(MARKERS) || [];
if (new Set(found.map((s) => s.trim())).size >= 2) return { used: true, version: null, reason: 'classes' };
return { used: false, version: null, reason: '' };
}
/** the project's Bootstrap: package.json, importmap.php, composer.json (symfonycasts / twbs), the base templates. readText(path) -> text | null */
export async function detectInProject(readText) {
const read = async (p) => { try { const x = await readText(p); return typeof x === 'string' ? x : null; } catch { return null; } };
const pkg = await read('package.json');
if (pkg) {
try {
const j = JSON.parse(pkg);
const v = { ...(j.dependencies || {}), ...(j.devDependencies || {}) }.bootstrap;
if (v) return { used: true, version: /(\d+)/.exec(String(v))?.[1] === '4' ? '4' : '5', reason: 'package.json' };
} catch { /* not JSON: ignored */ }
}
const map = await read('importmap.php');
if (map && /['"]bootstrap(?:\/[^'"]*)?['"]/.test(map)) return { used: true, version: /['"]version['"]\s*=>\s*['"](\d)/.exec(map)?.[1] === '4' ? '4' : '5', reason: 'importmap' };
for (const p of ['templates/base.html.twig', 'templates/layout.html.twig', 'templates/base.twig', 'index.html', 'public/index.html', 'src/index.html']) {
const text = await read(p);
if (text) { const d = detectInText(text); if (d.used) return { ...d, reason: p }; }
}
const composer = await read('composer.json');
if (composer && /twbs\/bootstrap|symfony\/ux-twig-component/.test(composer) && /twbs\/bootstrap/.test(composer)) return { used: true, version: /twbs\/bootstrap"\s*:\s*"[^"]*4/.test(composer) ? '4' : '5', reason: 'composer.json' };
return { used: false, version: null, reason: '' };
}
/**
* A page that links Bootstrap says which one; otherwise the project's layout / package decides (a page of a Bootstrap 5
* project that still has data-toggle is an old page to convert, not a Bootstrap 4 page), then what the page's markup suggests.
*/
export function mergeDetection(file, project) {
if (file.used && file.reason === 'link' && file.version) return { used: true, version: file.version, reason: 'link' };
if (project?.used) return { used: true, version: project.version || file.version || '5', reason: project.reason };
if (file.used) return { used: true, version: file.version || '5', reason: file.reason };
return null;
}
/** the classes a CSS text defines (the project's own: they are not typos) */
export function cssClasses(css) {
const out = new Set();
const s = String(css || '').replace(/\/\*[\s\S]*?\*\//g, '').replace(/\{[^{}]*\}/g, ';');
for (const m of s.matchAll(/(?<![\d])\.(-?[_a-zA-Z][\w-]*)/g)) out.add(m[1]);
return out;
}