E594 feat: added new appendAuthBlockContent to handle npmrc content update · mrvus/azdo-npm-auth@e63afd5 · GitHub
[go: up one dir, main page]

Skip to content

Commit e63afd5

Browse files
authored
feat: added new appendAuthBlockContent to handle npmrc content update
1 parent 252da72 commit e63afd5

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

src/writeNpmrc.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,48 @@ import path from "node:path";
44

55
import { fallbackLogger, type Logger } from "./shared/cli/logger.js";
66

7+
export function appendAuthBlockContent({
8+
npmrc,
9+
existingContent = "",
10+
logger = fallbackLogger,
11+
}: {
12+
npmrc: string;
13+
existingContent?: string;
14+
logger?: Logger;
15+
}): string {
16+
// find our auth token block in the existing content
17+
const authTokenPattern = /; begin auth token\n[\s\S]*?\n; end auth token\n?/;
18+
19+
let newContent: string;
20+
if (existingContent && authTokenPattern.test(existingContent)) {
21+
logger.info("Found existing auth token block(s), replacing all of them");
22+
// using global flag to find all blocks and replace them with a single new block
23+
const replacePattern = /; begin auth token\n[\s\S]*?\n; end auth token\n?/g;
24+
let replacedFirst = false;
25+
newContent = existingContent.replace(replacePattern, () => {
26+
if (!replacedFirst) {
27+
replacedFirst = true;
28+
return npmrc;
29+
}
30+
// empty string for subsequent matches to remove duplicates
31+
return "";
32+
});
33+
} else {
34+
if (existingContent) {
35+
logger.info(
36+
"No existing auth token block found, appending to existing content",
37+
);
38+
39+
const separator = existingContent.endsWith("\n") ? "" : "\n";
40+
newContent = existingContent + separator + npmrc;
41+
} else {
42+
newContent = npmrc;
43+
}
44+
}
45+
46+
return newContent;
47+
}
48+
749
export async function writeNpmrc({
850
npmrc,
951
logger = fallbackLogger,
@@ -20,9 +62,24 @@ export async function writeNpmrc({
2062
logger.info(`Writing users .npmrc to: ${userNpmrcPath}`);
2163

2264
try {
23-
// Write the content to the .npmrc file
24-
// fs.appendFile will create the file if it doesn't exist
25-
await fs.appendFile(userNpmrcPath, npmrc, "utf8");
65+
let existingContent = "";
66+
try {
67+
existingContent = await fs.readFile(userNpmrcPath, "utf8");
68+
} catch (error) {
69+
// gracefully fail, file will be created later
70+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
71+
throw error;
72+
}
73+
}
74+
75+
const newContent = appendAuthBlockContent({
76+
npmrc,
77+
existingContent,
78+
logger,
79+
});
80+
81+
// Write the updated content to the .npmrc file
82+
await fs.writeFile(userNpmrcPath, newContent, "utf8");
2683
logger.info("Successfully wrote .npmrc file");
2784
} catch (error) {
2885
const errorMessage = `Error writing users .npmrc to ${userNpmrcPath}: ${error instanceof Error ? error.message : ""}`;

0 commit comments

Comments
 (0)
0