UNPKG

@decaf-ts/utils

Version:

module management utils for decaf-ts

80 lines 3.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModulesCommand = void 0; exports.readGitModules = readGitModules; exports.readGitModulesDeep = readGitModulesDeep; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const command_js_1 = require("./../command.cjs"); const help_command_js_1 = require("./help.command.cjs"); function readGitModules(basePath = process.cwd()) { const gitmodulesPath = node_path_1.default.join(basePath, ".gitmodules"); const data = node_fs_1.default.readFileSync(gitmodulesPath, "utf8"); return data.toString().match(/(?<=").*?(?="])/g) || []; } function readGitModulesDeep(basePath = process.cwd(), maxTraversal = 2) { const modules = new Set(); const visited = new Set(); const walk = (currentBasePath, depth, relativePrefix = "") => { const normalizedBase = node_path_1.default.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 ? node_path_1.default.join(relativePrefix, entry) : entry; modules.add(modulePath); if (depth <= 0) continue; const nestedBase = node_path_1.default.join(currentBasePath, entry); const nestedGitmodules = node_path_1.default.join(nestedBase, ".gitmodules"); if (node_fs_1.default.existsSync(nestedGitmodules)) { walk(nestedBase, depth - 1, modulePath); } } }; walk(basePath, maxTraversal); return Array.from(modules); } const options = { basePath: { type: "string", default: process.cwd(), }, }; class ModulesCommand extends command_js_1.Command { constructor() { super("ModulesCommand", options); } help() { (0, help_command_js_1.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)); } } exports.ModulesCommand = ModulesCommand; //# sourceMappingURL=modules.command.js.map //# sourceMappingURL=modules.command.cjs.map