Skip to content

Commit a544b01

Browse files
fix: ensure newline between npmrc entries (#42)
* fix: ensure newline between npmrc entries * chore: use file specific newline
1 parent ef97c32 commit a544b01

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/commands.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as path from "node:path";
33
import * as fs from "node:fs";
44
import * as kl from "kolorist";
5-
import { exec, fileExists, JsrPackage } from "./utils";
5+
import { exec, fileExists, getNewLineChars, JsrPackage } from "./utils";
66
import { Bun, getPkgManager, PkgManagerName, YarnBerry } from "./pkg_manager";
77
import { downloadDeno, getDenoDownloadUrl } from "./download";
88

@@ -31,7 +31,9 @@ export async function setupNpmRc(dir: string) {
3131
try {
3232
let content = await fs.promises.readFile(npmRcPath, "utf-8");
3333
if (!content.includes("@jsr:registry=")) {
34-
content += JSR_NPMRC;
34+
const nl = getNewLineChars(content);
35+
const spacer = (!content.endsWith(nl)) ? nl : "";
36+
content += spacer + JSR_NPMRC;
3537
await wrapWithStatus(msg, async () => {
3638
await fs.promises.writeFile(npmRcPath, content);
3739
});

src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,11 @@ export async function exec(
187187
});
188188
});
189189
}
190+
191+
export function getNewLineChars(source: string) {
192+
var temp = source.indexOf("\n");
193+
if (source[temp - 1] === "\r") {
194+
return "\r\n";
195+
}
196+
return "\n";
197+
}

test/unit.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,26 @@ describe("npmrc", () => {
2020
assert.equal(content.trim(), "@jsr:registry=https://example.com");
2121
});
2222
});
23+
24+
it("adds newline in between entries if necessary", async () => {
25+
await runInTempDir(async (dir) => {
26+
const npmrc = path.join(dir, ".npmrc");
27+
await fs.promises.writeFile(
28+
npmrc,
29+
"@foo:registry=https://example.com",
30+
"utf-8",
31+
);
32+
33+
await setupNpmRc(dir);
34+
35+
const content = await fs.promises.readFile(npmrc, "utf-8");
36+
assert.equal(
37+
content.trim(),
38+
[
39+
"@foo:registry=https://example.com",
40+
"@jsr:registry=https://npm.jsr.io",
41+
].join("\n"),
42+
);
43+
});
44+
});
2345
});

0 commit comments

Comments
 (0)