/**
* `ui:check --root
` — what changes for an addon that lives in its own
* repository: paths resolve against that root, `isExternalUiPath` matches
* `src/**` relative to it, and the component / colour catalogue is read from
* the `@dominevo/ui-library` the root has INSTALLED (its `dist/*.d.ts` barrel
* and `src/tailwind/dominevo-theme.css`), not from this workspace.
*/
import * as fs from 'node:fs'
import * as path from 'node:path'
import { parseThemeCss } from './ui-catalogue.js'
/** Source of an external addon: `src/**` `.tsx` / `.ts` / `.css`, relative to its root. */
export function isExternalUiPath(file: string): boolean {
return /^src\/.*\.(tsx|ts|css)$/.test(file)
}
export interface InstalledLibrary {
readonly components: ReadonlySet
readonly colors: ReturnType['colors']
}
const EXPORT_DECL =
/export\s+(?:declare\s+)?(?:function|const|class)\s+([A-Za-z_$][\w$]*)|export\s+\{([^}]*)\}/g
function declFile(target: string): string | null {
const candidates = [
target,
`${target}.d.ts`,
target.replace(/\.js$/, '.d.ts'),
path.join(target, 'index.d.ts'),
]
return candidates.find((c) => fs.existsSync(c) && fs.statSync(c).isFile()) ?? null
}
function collectNames(file: string, seen: Set, names: Set): void {
if (seen.has(file)) return
seen.add(file)
const src = fs.readFileSync(file, 'utf8')
for (const m of src.matchAll(EXPORT_DECL)) {
if (m[1] !== undefined) {
names.add(m[1])
continue
}
for (const part of (m[2] ?? '').split(',')) {
const name = (part.split(/\sas\s/).pop() ?? '').replace(/^type\s+/, '').trim()
if (name) names.add(name)
}
}
for (const m of src.matchAll(/export\s+\*\s+from\s+['"](\.[^'"]+)['"]/g)) {
const next = declFile(path.resolve(path.dirname(file), m[1] ?? ''))
if (next !== null) collectNames(next, seen, names)
}
}
/** The installed library of `root`, or null when `root` has none. */
export function loadInstalledLibrary(root: string): InstalledLibrary | null {
const lib = path.join(root, 'node_modules', '@dominevo', 'ui-library')
const barrel = path.join(lib, 'dist', 'index.d.ts')
const css = path.join(lib, 'src', 'tailwind', 'dominevo-theme.css')
if (!fs.existsSync(barrel) || !fs.existsSync(css)) return null
const names = new Set()
collectNames(barrel, new Set(), names)
return {
components: new Set([...names].filter((n) => /^[A-Z]/.test(n))),
colors: parseThemeCss(fs.readFileSync(css, 'utf8')).colors,
}
}