diff --git a/src/index.ts b/src/index.ts index d884f1d..72b9188 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,7 @@ if (registryUrl) { registries.push({ url: registryUrl, scope: scope, - token: "$$BUN_AUTH_TOKEN", + token: "$BUN_AUTH_TOKEN", }); } diff --git a/tests/bunfig.spec.ts b/tests/bunfig.spec.ts index 3230860..9f52dc7 100644 --- a/tests/bunfig.spec.ts +++ b/tests/bunfig.spec.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it } from "bun:test"; -import { unlink } from "fs"; +import { unlinkSync } from "node:fs"; import { writeBunfig } from "../src/bunfig"; import { EOL } from "os"; @@ -14,7 +14,8 @@ describe("writeBunfig", () => { } afterEach(() => { - unlink(filePath, () => console.log(`${filePath} was deleted`)); + unlinkSync(filePath); + console.log(`${filePath} was deleted`); }); describe("when no bunfig.toml file exists", () => { @@ -43,6 +44,32 @@ describe("writeBunfig", () => { expect(contents.length).toBe(expectedContents.length); }); + + it("should create a new file with global registry", async () => { + writeBunfig(filePath, [ + { + url: "https://npm.pkg.github.com", + scope: "", + token: "$BUN_AUTH_TOKEN", + }, + ]); + + const { file, contents } = await getFileAndContents(); + + expect(file.exists()).resolves.toBeTrue(); + + const expectedContents = [ + "[install]", + 'registry = "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", () => { @@ -154,5 +181,48 @@ describe("writeBunfig", () => { expect(contents.length).toBe(expectedContents.length); }); + + it("and [install.scopes] and [install] exists, should concantenate 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); + + writeBunfig(filePath, [ + { + url: "https://npm.pkg.github.com", + scope: "foo-bar", + token: "$BUN_AUTH_TOKEN", + }, + { + url: "https://bun.sh", + scope: "", + token: "$BUN_AUTH_TOKEN", + }, // global registry + ]); + + const { file, contents } = await getFileAndContents(); + + expect(file.exists()).resolves.toBeTrue(); + + const expectedContents = [ + "[install]", + 'registry = "https://bun.sh/"', + "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); + }); }); });