feat: merge local bunfig with action scope/registry config

This commit is contained in:
Vitor Gomes 2023-11-16 16:07:00 -03:00
parent 830e319e28
commit 8c0b797097
8 changed files with 201 additions and 14 deletions

139
src/auth.spec.ts Normal file
View file

@ -0,0 +1,139 @@
import { afterEach, describe, expect, it } from "bun:test";
import { unlink } from "fs";
import { configureAuthentication } from "./auth";
import { EOL } from "os";
describe("#configureAuthentication", () => {
const filePath = "bunfig.toml";
async function getFileAndContents() {
const file = Bun.file(filePath);
const contents = (await file.text()).split(EOL);
return { file, contents };
}
afterEach(() => {
unlink(filePath, () => console.log(`${filePath} was deleted`));
});
describe("when no bunfig.toml file exists", () => {
it("should create a new file with scopes content", async () => {
configureAuthentication("https://npm.pkg.github.com", "foo-bar");
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install.scopes]",
"",
'\'@foo-bar\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
});
describe("when local bunfig.toml file exists", () => {
it("and no [install.scopes] exists, should concatenate file correctly", async () => {
const bunfig = `[install]${EOL}optional = true${EOL}${EOL}[install.cache]${EOL}disable = true`;
await Bun.write(filePath, bunfig);
configureAuthentication("https://npm.pkg.github.com/", "foo-bar");
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install]",
"optional = true",
"",
"[install.cache]",
"disable = true",
"[install.scopes]",
"",
'\'@foo-bar\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
"",
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
it("and [install.scopes] exists and it's not the same registry, should concatenate file correctly", async () => {
const bunfig = `[install]${EOL}optional = true${EOL}${EOL}[install.scopes]${EOL}'@bla-ble' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }${EOL}${EOL}[install.cache]${EOL}disable = true`;
await Bun.write(filePath, bunfig);
configureAuthentication("https://npm.pkg.github.com/", "foo-bar");
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install]",
"optional = true",
"",
"[install.scopes]",
'\'@foo-bar\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
'\'@bla-ble\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
"",
"[install.cache]",
"disable = true",
"",
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
it("and [install.scopes] exists and it's the same registry, should concatenate file correctly", async () => {
const bunfig = `[install]${EOL}optional = true${EOL}${EOL}[install.scopes]${EOL}'@foo-bar' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }${EOL}'@bla-ble' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }${EOL}${EOL}[install.cache]${EOL}disable = true`;
await Bun.write(filePath, bunfig);
configureAuthentication("https://npm.pkg.github.com/", "foo-bar");
const { file, contents } = await getFileAndContents();
expect(file.exists()).resolves.toBeTrue();
const expectedContents = [
"[install]",
"optional = true",
"",
"[install.scopes]",
'\'@foo-bar\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
'\'@bla-ble\' = { token = "$BUN_AUTH_TOKEN", url = "https://npm.pkg.github.com/" }',
"",
"[install.cache]",
"disable = true",
"",
"",
];
contents.forEach((content, index) =>
expect(content).toBe(expectedContents[index])
);
expect(contents.length).toBe(expectedContents.length);
});
});
});

View file

@ -34,24 +34,37 @@ function writeRegistryToConfigFile({
core.info(`Setting auth in ${fileLocation}`);
const bunRegistryString = `'${scope}' = { token = "$BUN_AUTH_TOKEN", url = "${registryUrl}" }`;
let newContents = "";
if (existsSync(fileLocation)) {
const curContents = readFileSync(fileLocation, "utf8");
curContents.split(EOL).forEach((line: string) => {
// Add current contents unless they are setting the registry
if (!line.toLowerCase().startsWith(scope)) {
const contents = curContents.split(EOL);
contents.forEach((line, index, array) => {
// If last item is [install.scopes], than it should add the action scope + registry
if (index > 0 && array[index - 1].includes('[install.scopes]')) {
newContents += bunRegistryString + EOL;
}
// Only add the line if scope does not exists
if (!line.toLowerCase().includes(scope)) {
newContents += line + EOL;
}
});
// In case bunfig.toml has other properties and does not have [install.scopes]
if (!contents.includes('[install.scopes]')) {
newContents += `[install.scopes]${EOL}${EOL}${bunRegistryString}${EOL}`
}
newContents += EOL;
}
const bunRegistryString = `'${scope}' = { token = "$BUN_AUTH_TOKEN", url = "${registryUrl}"}`;
newContents += `[install.scopes]${EOL}${EOL}${bunRegistryString}${EOL}`;
if (!existsSync(fileLocation)) {
newContents += `[install.scopes]${EOL}${EOL}${bunRegistryString}${EOL}`
}
writeFileSync("./bunfig.toml", newContents);
}