import { readdirSync } from 'node:fs'; import path from 'node:path'; import type { Command } from '@oclif/core'; /** * Every lim command, for oclif's explicit discovery. A file under commands/ is one command named * by its path, the way oclif's default discovery names it, so most commands need no registration. * A file that exports COMMANDS instead holds several small commands keyed by their full ID, which * keeps families of thin store actions in a few files. */ function collect(directory: string, topics: string[], into: Record): void { for (const entry of readdirSync(directory, { withFileTypes: true })) { const file = path.join(directory, entry.name); if (entry.isDirectory()) { collect(file, [...topics, entry.name], into); continue; } const match = /^(.+)\.(js|ts)$/.exec(entry.name); if (!match || /\.(d|test|spec)$/.test(match[1]!)) continue; // eslint-disable-next-line @typescript-eslint/no-require-imports const module = require(file) as { COMMANDS?: Record; default?: Command.Class }; const commands = module.COMMANDS ?? { [[...topics, match[1] === 'index' ? '' : match[1]!].filter(Boolean).join(':')]: module.default!, }; for (const [id, command] of Object.entries(commands)) { if (into[id]) throw new Error(`The lim command ${id} is defined twice.`); into[id] = command; } } } export const COMMANDS: Record = {}; collect(path.join(__dirname, 'commands'), [], COMMANDS);