mirror of
https://github.com/oven-sh/setup-bun.git
synced 2025-07-19 21:18:22 +02:00
feat: support for multiple registries (#109)
Some checks failed
autofix.ci / format (push) Failing after 1m14s
🧪 Test / remove-cache (push) Successful in 58s
🧪 Test / setup-bun (1.x, ubuntu-latest) (push) Failing after 1m28s
🧪 Test / setup-bun (< 2, ubuntu-latest) (push) Failing after 1m28s
🧪 Test / setup-bun from (ubuntu-latest, .tool-versions (bun 1.1.0)) (push) Successful in 57s
🧪 Test / setup-bun from (ubuntu-latest, .tool-versions (bun1.1.0)) (push) Successful in 57s
🧪 Test / setup-bun from (ubuntu-latest, /foo/package.json (bun@1.1.0)) (push) Failing after 55s
🧪 Test / setup-bun from (ubuntu-latest, package.json (bun@1.1.0)) (push) Successful in 57s
🧪 Test / setup-bun from (ubuntu-latest, package.json (yarn@bun@1.1.0)) (push) Successful in 58s
🧪 Test / test installing deps from custom registries (ubuntu-latest) (push) Failing after 3m12s
🧪 Test / Tests (push) Successful in 3m13s
🧪 Test / setup-bun (1, ubuntu-latest) (push) Failing after 1m28s
🧪 Test / setup-bun (1.1.0, ubuntu-latest) (push) Successful in 56s
🧪 Test / setup-bun (> 1.0.0, ubuntu-latest) (push) Failing after 1m27s
🧪 Test / setup-bun (canary, ubuntu-latest) (push) Successful in 1m1s
🧪 Test / setup-bun (latest, ubuntu-latest) (push) Successful in 58s
🧪 Test / setup-bun from (ubuntu-latest, .bumrc (1.1.0)) (push) Successful in 56s
🧪 Test / setup-bun from (ubuntu-latest, .bun-version (1.1.0)) (push) Successful in 57s
🧪 Test / setup-bun from (ubuntu-latest, .tool-versions (bun 1.1.0)) (push) Successful in 56s
🧪 Test / setup-bun from (ubuntu-latest, download url) (push) Successful in 59s
🧪 Test / setup-bun (latest, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .tool-versions (bun1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, download url) (push) Has been cancelled
🧪 Test / setup-bun (1, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (1, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (1.1.0, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (1.1.0, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (1.x, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (1.x, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (< 2, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (< 2, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (> 1.0.0, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (> 1.0.0, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (canary, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (canary, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (latest, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .bumrc (1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .bumrc (1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .bun-version (1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .bun-version (1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .tool-versions (bun 1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .tool-versions (bun 1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .tool-versions (bun 1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .tool-versions (bun 1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .tool-versions (bun1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, /foo/package.json (bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, /foo/package.json (bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, package.json (bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, package.json (bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, package.json (yarn@bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, package.json (yarn@bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, download url) (push) Has been cancelled
Some checks failed
autofix.ci / format (push) Failing after 1m14s
🧪 Test / remove-cache (push) Successful in 58s
🧪 Test / setup-bun (1.x, ubuntu-latest) (push) Failing after 1m28s
🧪 Test / setup-bun (< 2, ubuntu-latest) (push) Failing after 1m28s
🧪 Test / setup-bun from (ubuntu-latest, .tool-versions (bun 1.1.0)) (push) Successful in 57s
🧪 Test / setup-bun from (ubuntu-latest, .tool-versions (bun1.1.0)) (push) Successful in 57s
🧪 Test / setup-bun from (ubuntu-latest, /foo/package.json (bun@1.1.0)) (push) Failing after 55s
🧪 Test / setup-bun from (ubuntu-latest, package.json (bun@1.1.0)) (push) Successful in 57s
🧪 Test / setup-bun from (ubuntu-latest, package.json (yarn@bun@1.1.0)) (push) Successful in 58s
🧪 Test / test installing deps from custom registries (ubuntu-latest) (push) Failing after 3m12s
🧪 Test / Tests (push) Successful in 3m13s
🧪 Test / setup-bun (1, ubuntu-latest) (push) Failing after 1m28s
🧪 Test / setup-bun (1.1.0, ubuntu-latest) (push) Successful in 56s
🧪 Test / setup-bun (> 1.0.0, ubuntu-latest) (push) Failing after 1m27s
🧪 Test / setup-bun (canary, ubuntu-latest) (push) Successful in 1m1s
🧪 Test / setup-bun (latest, ubuntu-latest) (push) Successful in 58s
🧪 Test / setup-bun from (ubuntu-latest, .bumrc (1.1.0)) (push) Successful in 56s
🧪 Test / setup-bun from (ubuntu-latest, .bun-version (1.1.0)) (push) Successful in 57s
🧪 Test / setup-bun from (ubuntu-latest, .tool-versions (bun 1.1.0)) (push) Successful in 56s
🧪 Test / setup-bun from (ubuntu-latest, download url) (push) Successful in 59s
🧪 Test / setup-bun (latest, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .tool-versions (bun1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, download url) (push) Has been cancelled
🧪 Test / setup-bun (1, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (1, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (1.1.0, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (1.1.0, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (1.x, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (1.x, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (< 2, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (< 2, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (> 1.0.0, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (> 1.0.0, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (canary, macos-latest) (push) Has been cancelled
🧪 Test / setup-bun (canary, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun (latest, windows-latest) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .bumrc (1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .bumrc (1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .bun-version (1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .bun-version (1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .tool-versions (bun 1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .tool-versions (bun 1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, .tool-versions (bun 1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .tool-versions (bun 1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, .tool-versions (bun1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, /foo/package.json (bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, /foo/package.json (bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, package.json (bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, package.json (bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, package.json (yarn@bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (windows-latest, package.json (yarn@bun@1.1.0)) (push) Has been cancelled
🧪 Test / setup-bun from (macos-latest, download url) (push) Has been cancelled
* feat: support for multiple registries * [autofix.ci] apply automated fixes * refactor: drop unnecessary check * fix: pass empty array instead undefined * [autofix.ci] apply automated fixes * fix: dont add registry set line * [autofix.ci] apply automated fixes * feat: few more tests * feat: add bun types * docs: explain registries * feat: save globally * [autofix.ci] apply automated fixes * ci: jsr registry test * ci: add , * ci: test * ci: test * ci: test * ci: test * [autofix.ci] apply automated fixes * feat: use @iarna/toml * [autofix.ci] apply automated fixes * feat: registry parsing * [autofix.ci] apply automated fixes * ci: verbose add * ci: registry install check * ci: registry install check * ci: registry install check * ci: verify registry usage * docs: registries * docs: important block * docs: show table * docs: show table * ci: shell * ci: registry test on linux * ci: registry test on linux * build: text lockfile --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
7c641390eb
commit
34f777aec1
14 changed files with 901 additions and 117 deletions
|
@ -12,9 +12,10 @@ import { addPath, info, warning } from "@actions/core";
|
|||
import { isFeatureAvailable, restoreCache } from "@actions/cache";
|
||||
import { downloadTool, extractZip } from "@actions/tool-cache";
|
||||
import { getExecOutput } from "@actions/exec";
|
||||
import { writeBunfig } from "./bunfig";
|
||||
import { writeBunfig, Registry } from "./bunfig";
|
||||
import { saveState } from "@actions/core";
|
||||
import { addExtension, retry } from "./utils";
|
||||
import { cwd } from "node:process";
|
||||
|
||||
export type Input = {
|
||||
customUrl?: string;
|
||||
|
@ -23,8 +24,7 @@ export type Input = {
|
|||
arch?: string;
|
||||
avx2?: boolean;
|
||||
profile?: boolean;
|
||||
scope?: string;
|
||||
registryUrl?: string;
|
||||
registries?: Registry[];
|
||||
noCache?: boolean;
|
||||
};
|
||||
|
||||
|
@ -44,8 +44,8 @@ export type CacheState = {
|
|||
};
|
||||
|
||||
export default async (options: Input): Promise<Output> => {
|
||||
const bunfigPath = join(process.cwd(), "bunfig.toml");
|
||||
writeBunfig(bunfigPath, options);
|
||||
const bunfigPath = join(cwd(), "bunfig.toml");
|
||||
writeBunfig(bunfigPath, options.registries);
|
||||
|
||||
const url = getDownloadUrl(options);
|
||||
const cacheEnabled = isCacheEnabled(options);
|
||||
|
|
113
src/bunfig.ts
113
src/bunfig.ts
|
@ -1,50 +1,81 @@
|
|||
import { EOL } from "node:os";
|
||||
import { appendFileSync } from "node:fs";
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { info } from "@actions/core";
|
||||
import { parse, stringify } from "@iarna/toml";
|
||||
import { Registry } from "./registry";
|
||||
|
||||
type BunfigOptions = {
|
||||
registryUrl?: string;
|
||||
scope?: string;
|
||||
type BunfigConfig = {
|
||||
install?: {
|
||||
registry?: {
|
||||
url: string;
|
||||
token?: string;
|
||||
};
|
||||
scopes?: Record<string, { url: string; token?: string }>;
|
||||
};
|
||||
[key: string]: any;
|
||||
};
|
||||
|
||||
export function createBunfig(options: BunfigOptions): string | null {
|
||||
const { registryUrl, scope } = options;
|
||||
|
||||
let url: URL | undefined;
|
||||
if (registryUrl) {
|
||||
try {
|
||||
url = new URL(registryUrl);
|
||||
} catch {
|
||||
throw new Error(`Invalid registry-url: ${registryUrl}`);
|
||||
}
|
||||
}
|
||||
|
||||
let owner: string | undefined;
|
||||
if (scope) {
|
||||
owner = scope.startsWith("@")
|
||||
? scope.toLocaleLowerCase()
|
||||
: `@${scope.toLocaleLowerCase()}`;
|
||||
}
|
||||
|
||||
if (url && owner) {
|
||||
return `[install.scopes]${EOL}'${owner}' = { token = "$BUN_AUTH_TOKEN", url = "${url}"}${EOL}`;
|
||||
}
|
||||
|
||||
if (url && !owner) {
|
||||
return `[install]${EOL}registry = "${url}"${EOL}`;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function writeBunfig(path: string, options: BunfigOptions): void {
|
||||
const bunfig = createBunfig(options);
|
||||
if (!bunfig) {
|
||||
export function writeBunfig(path: string, registries: Registry[]): void {
|
||||
if (!registries.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
info(`Writing bunfig.toml to '${path}'.`);
|
||||
appendFileSync(path, bunfig, {
|
||||
encoding: "utf8",
|
||||
let globalRegistryCount = 0;
|
||||
registries.forEach((registry) => {
|
||||
try {
|
||||
new URL(registry.url);
|
||||
} catch {
|
||||
throw new Error(`Invalid registry URL: ${registry.url}`);
|
||||
}
|
||||
|
||||
if (!registry.scope) {
|
||||
globalRegistryCount++;
|
||||
}
|
||||
});
|
||||
|
||||
if (globalRegistryCount > 1) {
|
||||
throw new Error("You can't have more than one global registry.");
|
||||
}
|
||||
|
||||
info(`Writing bunfig.toml to '${path}'.`);
|
||||
|
||||
let config: BunfigConfig = {};
|
||||
if (existsSync(path)) {
|
||||
try {
|
||||
const content = readFileSync(path, { encoding: "utf-8" });
|
||||
config = parse(content) as BunfigConfig;
|
||||
} catch (error) {
|
||||
info(`Error reading existing bunfig: ${error.message}`);
|
||||
config = {};
|
||||
}
|
||||
}
|
||||
|
||||
config.install = config?.install || {};
|
||||
config.install.scopes = config?.install.scopes || {};
|
||||
|
||||
const globalRegistry = registries.find((r) => !r.scope);
|
||||
if (globalRegistry) {
|
||||
config.install.registry = {
|
||||
url: globalRegistry.url,
|
||||
...(globalRegistry.token ? { token: globalRegistry.token } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
for (const registry of registries) {
|
||||
if (registry.scope) {
|
||||
const scopeName = registry.scope.startsWith("@")
|
||||
? registry.scope.toLowerCase()
|
||||
: `@${registry.scope.toLowerCase()}`;
|
||||
|
||||
config.install.scopes[scopeName] = {
|
||||
url: registry.url,
|
||||
...(registry.token ? { token: registry.token } : {}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(config.install.scopes).length === 0) {
|
||||
delete config.install.scopes;
|
||||
}
|
||||
|
||||
writeFileSync(path, stringify(config), { encoding: "utf8" });
|
||||
}
|
||||
|
|
18
src/index.ts
18
src/index.ts
|
@ -2,19 +2,33 @@ 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 = parseRegistries(getInput("registries"));
|
||||
|
||||
// Backwards compatibility for the `registry-url` and `scope` inputs
|
||||
const registryUrl = getInput("registry-url");
|
||||
const scope = getInput("scope");
|
||||
|
||||
if (registryUrl) {
|
||||
registries.push({
|
||||
url: registryUrl,
|
||||
scope: scope,
|
||||
token: "$BUN_AUTH_TOKEN",
|
||||
});
|
||||
}
|
||||
|
||||
runAction({
|
||||
version:
|
||||
getInput("bun-version") ||
|
||||
readVersionFromFile(getInput("bun-version-file")) ||
|
||||
undefined,
|
||||
customUrl: getInput("bun-download-url") || undefined,
|
||||
registryUrl: getInput("registry-url") || undefined,
|
||||
scope: getInput("scope") || undefined,
|
||||
registries: registries,
|
||||
noCache: getBooleanInput("no-cache") || false,
|
||||
})
|
||||
.then(({ version, revision, bunPath, url, cacheHit }) => {
|
||||
|
|
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