mirror of
https://github.com/oven-sh/setup-bun.git
synced 2025-07-19 21:18:22 +02:00
feat: registry parsing
This commit is contained in:
parent
9bb0f8cde8
commit
93e48f74e9
6 changed files with 239 additions and 15 deletions
|
@ -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?: {
|
||||
|
|
|
@ -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
62
src/registry.ts
Normal 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}`);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue