Skip to content

Commit

Permalink
fix: ensure newline between npmrc entries
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 4, 2024
1 parent fa08e2f commit 342811d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export async function setupNpmRc(dir: string) {
try {
let content = await fs.promises.readFile(npmRcPath, "utf-8");
if (!content.includes("@jsr:registry=")) {
content += JSR_NPMRC;
const spacer = (!content.endsWith("\n")) ? "\n" : "";
content += spacer + JSR_NPMRC;
await wrapWithStatus(msg, async () => {
await fs.promises.writeFile(npmRcPath, content);
});
Expand Down
22 changes: 22 additions & 0 deletions test/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,26 @@ describe("npmrc", () => {
assert.equal(content.trim(), "@jsr:registry=https://example.com");
});
});

it("adds newline in between entries if necessary", async () => {
await runInTempDir(async (dir) => {
const npmrc = path.join(dir, ".npmrc");
await fs.promises.writeFile(
npmrc,
"@foo:registry=https://example.com",
"utf-8",
);

await setupNpmRc(dir);

const content = await fs.promises.readFile(npmrc, "utf-8");
assert.equal(
content.trim(),
[
"@foo:registry=https://example.com",
"@jsr:registry=https://npm.jsr.io",
].join("\n"),
);
});
});
});

0 comments on commit 342811d

Please sign in to comment.