UNPKG

@decaf-ts/utils

Version:

module management utils for decaf-ts

133 lines 5.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompileMatrixCommand = void 0; exports.parseRepo = parseRepo; const command_js_1 = require("./../command.cjs"); const help_command_js_1 = require("./help.command.cjs"); function sanitizeSecret(name) { return name.toUpperCase().replace(/[^A-Z0-9]/g, "_"); } function slugify(parts) { return parts .filter(Boolean) .join("-") .replace(/[^a-zA-Z0-9]+/g, "-") .replace(/-{2,}/g, "-") .replace(/^-|-$/g, "") .toLowerCase(); } function parseRepo(rawUrl, targetOwner, targetHost, recursive = false) { rawUrl = rawUrl.trim(); if (!rawUrl) throw new Error("empty entry"); let host, cleanPath; if (rawUrl.startsWith("git@")) { const rest = rawUrl.slice(4); const colonIdx = rest.indexOf(":"); if (colonIdx === -1) throw new Error("invalid SSH URL"); host = rest.slice(0, colonIdx); cleanPath = rest.slice(colonIdx + 1); } else { const parsed = new URL(rawUrl); host = parsed.hostname; cleanPath = parsed.pathname.slice(1); } cleanPath = cleanPath.replace(/^\/+|\/+$/g, ""); if (cleanPath.endsWith(".git")) cleanPath = cleanPath.slice(0, -4); const segments = cleanPath.split("/").filter(Boolean); if (segments.length < 2) throw new Error("missing owner or repository portion"); const owner = segments[0]; const repoName = segments[segments.length - 1]; const httpsUrl = `https://${host}/${owner}/${repoName}.git`; const mirrorRepo = slugify(["mirror", host, owner, repoName]); if (!mirrorRepo) throw new Error("unable to derive mirror repository name"); return { source_url: rawUrl, https_url: httpsUrl, source_host: host, source_owner: owner, repo_name: repoName, display_name: `${owner}/${repoName}`, source_host_secret: `HOST_${sanitizeSecret(host)}_KEY`, source_owner_secret: `HOST_${sanitizeSecret(owner)}_KEY`, target_owner: targetOwner, target_repo: mirrorRepo, target_host: targetHost, target_owner_secret: `HOST_${sanitizeSecret(targetOwner)}_KEY`, target_owner_fallback_secret: `HOST_${sanitizeSecret(owner)}_KEY`, target_host_secret: `HOST_${sanitizeSecret(targetHost)}_KEY`, recursive, }; } function parseReposVar(raw) { const entries = []; for (const line of raw.split("\n")) { for (const chunk of line.split(",")) { const trimmed = chunk.trim(); if (!trimmed || trimmed.startsWith("#")) continue; const parts = trimmed.split(/\s+/); const url = parts[0]; const recursive = parts.includes("--recursive"); if (url) entries.push({ url, recursive }); } } return entries; } const options = { banner: { type: "boolean", default: false, }, }; class CompileMatrixCommand extends command_js_1.Command { constructor() { super("CompileMatrixCommand", options); } help() { (0, help_command_js_1.printCommandHelp)(this.log, "compile-matrix", "Build the GitHub Actions job matrix from a REPOS environment variable.", "compile-matrix", [ { flag: "-h, --help", description: "Show this help text and exit" }, { flag: "-v, --version", description: "Print package version and exit", }, ], [ "Reads TARGET_OWNER and REPOS from the environment.", "REPOS is a newline- or comma-separated list of Git URLs, each optionally followed by --recursive.", "Outputs a JSON array to stdout suitable for use as a GitHub Actions matrix.", "All informational output is written to stderr to keep stdout clean for capture.", ], ["compile-matrix"]); } async run( // eslint-disable-next-line @typescript-eslint/no-unused-vars _answers) { const log = this.log.for(this.run); const targetOwner = process.env["TARGET_OWNER"] ?? ""; const targetHost = "github.com"; const raw = (process.env["REPOS"] ?? "").trim(); if (!raw) { log.warn("REPOS environment variable is empty; outputting empty matrix."); process.stdout.write("[]\n"); return; } const entries = []; for (const { url, recursive } of parseReposVar(raw)) { try { entries.push(parseRepo(url, targetOwner, targetHost, recursive)); } catch (err) { log.warn(`Skipping '${url}': ${err.message}`); } } process.stdout.write(JSON.stringify(entries) + "\n"); } } exports.CompileMatrixCommand = CompileMatrixCommand; //# sourceMappingURL=compile-matrix.command.js.map //# sourceMappingURL=compile-matrix.command.cjs.map