feat: registry parsing

This commit is contained in:
Jozef Steinhübl 2025-07-08 20:52:02 +02:00
parent 9bb0f8cde8
commit 93e48f74e9
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
6 changed files with 239 additions and 15 deletions

View file

@ -1,12 +1,7 @@
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { info } from "@actions/core";
import { parse, stringify } from "@iarna/toml";
export type Registry = {
url: string;
scope: string;
token?: string;
};
import { Registry } from "./registry";
type BunfigConfig = {
install?: {

View file

@ -2,12 +2,13 @@ import { tmpdir } from "node:os";
import { getInput, setOutput, setFailed, getBooleanInput } from "@actions/core";
import runAction from "./action.js";
import { readVersionFromFile } from "./utils.js";
import { parseRegistries } from "./registry.js";
if (!process.env.RUNNER_TEMP) {
process.env.RUNNER_TEMP = tmpdir();
}
const registries = JSON.parse(getInput("registries") || "[]");
const registries = parseRegistries(getInput("registries"));
const registryUrl = getInput("registry-url");
const scope = getInput("scope");

62
src/registry.ts Normal file
View file

@ -0,0 +1,62 @@
export type Registry = {
url: string;
scope: string;
token?: string;
};
/**
* Parse registries from the simplified format:
* - Default registry: https://registry.npmjs.org/
* - Default registry with token: https://registry.npmjs.org/|token123
* - With scope and credentials in URL: @myorg:https://username:password@registry.myorg.com/
* - With scope and separate token: @partner:https://registry.partner.com/|basic_token
*/
export function parseRegistries(input: string): Registry[] {
if (!input?.trim()) return [];
return input
.split("\n")
.map((line) => line.trim())
.filter(Boolean)
.map(parseLine)
.filter(Boolean) as Registry[];
}
function parseLine(line: string): Registry | null {
const scopeMatch = line.match(
/^(@[a-z0-9-_.]+|[a-z0-9-_.]+(?=:[a-z]+:\/\/)):(.+)$/i
);
if (scopeMatch) {
const scope = scopeMatch[1];
const urlPart = scopeMatch[2].trim();
const [url, token] = urlPart.split("|", 2).map((p) => p?.trim());
try {
new URL(url);
return {
url,
scope,
...(token && { token }),
};
} catch (e) {
throw new Error(`Invalid URL in registry configuration: ${url}`);
}
} else {
const [url, token] = line.split("|", 2).map((p) => p?.trim());
try {
new URL(url);
return {
url,
scope: "",
...(token && { token }),
};
} catch (e) {
throw new Error(`Invalid URL in registry configuration: ${url}`);
}
}
}