mirror of
https://github.com/oven-sh/setup-bun.git
synced 2025-07-19 21:18:22 +02:00
feat: use @iarna/toml
This commit is contained in:
parent
2db092bddd
commit
7a1e37eece
4 changed files with 152 additions and 173 deletions
207
src/bunfig.ts
207
src/bunfig.ts
|
@ -1,6 +1,6 @@
|
|||
import { EOL } from "node:os";
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { info } from "@actions/core";
|
||||
import { parse, stringify } from "@iarna/toml";
|
||||
|
||||
export type Registry = {
|
||||
url: string;
|
||||
|
@ -8,164 +8,79 @@ export type Registry = {
|
|||
token?: string;
|
||||
};
|
||||
|
||||
enum FieldType {
|
||||
GLOBAL_REGISTRY,
|
||||
INSTALL_WITH_SCOPE,
|
||||
}
|
||||
|
||||
type Field = {
|
||||
type: FieldType;
|
||||
value: string;
|
||||
type BunfigConfig = {
|
||||
install?: {
|
||||
registry?: {
|
||||
url: string;
|
||||
token?: string;
|
||||
};
|
||||
scopes?: Record<string, { url: string; token?: string }>;
|
||||
};
|
||||
[key: string]: any;
|
||||
};
|
||||
|
||||
export function createField(registry: Registry): Field {
|
||||
const { url: registryUrl, scope, token } = registry;
|
||||
|
||||
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 {
|
||||
type: FieldType.INSTALL_WITH_SCOPE,
|
||||
value: `'${owner}' = { url = "${url}"${
|
||||
token ? `, token = "${token}"` : ""
|
||||
} }`,
|
||||
};
|
||||
}
|
||||
|
||||
if (url && !owner) {
|
||||
return {
|
||||
type: FieldType.GLOBAL_REGISTRY,
|
||||
value: `registry = "${url}"`,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function createBunfig(registries: Registry[]): Field[] | null {
|
||||
const fields = registries.map(createField).filter((field) => field);
|
||||
if (fields.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
fields.filter((field) => field.type === FieldType.GLOBAL_REGISTRY).length >
|
||||
1
|
||||
) {
|
||||
throw new Error("You can't have more than one global registry.");
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
export function serializeInstallScopes(
|
||||
fields: Field[],
|
||||
header: boolean = false
|
||||
): string {
|
||||
const installScopes = fields
|
||||
.filter((field) => field.type === FieldType.INSTALL_WITH_SCOPE)
|
||||
.map((field) => field.value)
|
||||
.join(EOL);
|
||||
|
||||
if (!installScopes) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return `${header ? `[install.scopes]${EOL}` : ""}${installScopes}${EOL}`;
|
||||
}
|
||||
|
||||
export function serializeGlobalRegistry(
|
||||
fields: Field[],
|
||||
header: boolean = false
|
||||
): string {
|
||||
const globalRegistry = fields
|
||||
.filter((field) => field.type === FieldType.GLOBAL_REGISTRY)
|
||||
.map((field) => field.value)
|
||||
.join(EOL);
|
||||
|
||||
if (!globalRegistry) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return `${header ? `[install]${EOL}` : ""}${globalRegistry}${EOL}`;
|
||||
}
|
||||
|
||||
export function writeBunfig(path: string, registries: Registry[]): void {
|
||||
const bunfig = createBunfig(registries);
|
||||
if (!bunfig) {
|
||||
if (!registries.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
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}'.`);
|
||||
|
||||
if (!existsSync(path)) {
|
||||
writeFileSync(
|
||||
path,
|
||||
`${serializeGlobalRegistry(bunfig, true)}${serializeInstallScopes(
|
||||
bunfig,
|
||||
true
|
||||
)}`,
|
||||
{
|
||||
encoding: "utf8",
|
||||
}
|
||||
);
|
||||
|
||||
return;
|
||||
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 = {};
|
||||
}
|
||||
}
|
||||
|
||||
let newContent = "";
|
||||
const contents = readFileSync(path, {
|
||||
encoding: "utf-8",
|
||||
}).split(EOL);
|
||||
config.install = config?.install || {};
|
||||
config.install.scopes = config?.install.scopes || {};
|
||||
|
||||
contents.forEach((line, index, array) => {
|
||||
if (index > 0 && array[index - 1].includes("[install.scopes]")) {
|
||||
newContent += serializeInstallScopes(bunfig);
|
||||
}
|
||||
|
||||
if (index > 0 && array[index - 1].includes("[install]")) {
|
||||
newContent += serializeGlobalRegistry(bunfig);
|
||||
}
|
||||
|
||||
if (
|
||||
line.startsWith("registry = ") ||
|
||||
!bunfig.some(
|
||||
(field) =>
|
||||
field.type === FieldType.INSTALL_WITH_SCOPE &&
|
||||
(line.startsWith(field.value.split(" ")[0]) ||
|
||||
((line[0] === "'" || line[0] === '"') &&
|
||||
line
|
||||
.toLowerCase()
|
||||
.startsWith(field.value.split(" ")[0].slice(1).slice(0, -1))))
|
||||
)
|
||||
) {
|
||||
newContent += line + EOL;
|
||||
}
|
||||
});
|
||||
|
||||
if (!contents.includes("[install.scopes]")) {
|
||||
newContent += serializeInstallScopes(bunfig, true);
|
||||
const globalRegistry = registries.find((r) => !r.scope);
|
||||
if (globalRegistry) {
|
||||
config.install.registry = {
|
||||
url: globalRegistry.url,
|
||||
...(globalRegistry.token ? { token: globalRegistry.token } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
if (!contents.includes("[install]")) {
|
||||
newContent += serializeGlobalRegistry(bunfig, true);
|
||||
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 } : {}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
writeFileSync(path, newContent, {
|
||||
encoding: "utf8",
|
||||
});
|
||||
if (Object.keys(config.install.scopes).length === 0) {
|
||||
delete config.install.scopes;
|
||||
}
|
||||
|
||||
writeFileSync(path, stringify(config), { encoding: "utf8" });
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue