UNPKG

@decaf-ts/utils

Version:

module management utils for decaf-ts

70 lines 2.5 kB
import fs from "node:fs"; import path from "node:path"; import { Command } from "./../command.js"; import { printCommandHelp } from "./help.command.js"; export function readGitModules(basePath = process.cwd()) { const gitmodulesPath = path.join(basePath, ".gitmodules"); const data = fs.readFileSync(gitmodulesPath, "utf8"); return data.toString().match(/(?<=").*?(?="])/g) || []; } export function readGitModulesDeep(basePath = process.cwd(), maxTraversal = 2) { const modules = new Set(); const visited = new Set(); const walk = (currentBasePath, depth, relativePrefix = "") => { const normalizedBase = path.resolve(currentBasePath); if (visited.has(normalizedBase)) return; visited.add(normalizedBase); let entries = []; try { entries = readGitModules(currentBasePath); } catch { return; } for (const entry of entries) { const modulePath = relativePrefix ? path.join(relativePrefix, entry) : entry; modules.add(modulePath); if (depth <= 0) continue; const nestedBase = path.join(currentBasePath, entry); const nestedGitmodules = path.join(nestedBase, ".gitmodules"); if (fs.existsSync(nestedGitmodules)) { walk(nestedBase, depth - 1, modulePath); } } }; walk(basePath, maxTraversal); return Array.from(modules); } const options = { basePath: { type: "string", default: process.cwd(), }, }; export class ModulesCommand extends Command { constructor() { super("ModulesCommand", options); } help() { printCommandHelp(this.log, "modules", "List modules discovered from .gitmodules.", "modules [options]", [ { flag: "--base-path <path>", description: "Directory to read .gitmodules from", defaultValue: "current working directory", }, { flag: "-h, --help", description: "Show this help text and exit", }, ], ["Each discovered module path is printed on its own line."], ["modules", "modules --base-path ../repo"]); } async run(answers) { const modules = readGitModules(answers.basePath); modules.forEach((module) => this.log.info(module)); } } //# sourceMappingURL=modules.command.js.map