Skip to content

Commit b303172

Browse files
authored
Merge branch 'main' into pr/AddLinux2
2 parents 6ec4fda + bf4cfd4 commit b303172

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"type": "minor",
5+
"comment": "Make file writes only when required",
6+
"packageName": "ado-npm-auth",
7+
"email": "dannyvv@microsoft.com",
8+
"dependentChangeType": "patch"
9+
}
10+
]
11+
}

packages/ado-npm-auth/src/npmrc/npmrcFileProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getOrganizationFromFeedUrl } from "../utils/get-organization-from-feed-
1111
import { getFeedWithoutProtocol } from "../utils/get-feed-without-protocol.js";
1212
import { fromBase64, toBase64 } from "../utils/encoding.js";
1313
import path from "node:path";
14+
import { writeFileLazy } from "../utils/fileUtils.js";
1415

1516
export class NpmrcFileProvider extends FileProvider {
1617
constructor(configFile?: string) {
@@ -26,10 +27,10 @@ export class NpmrcFileProvider extends FileProvider {
2627
.split(EOL)
2728
.filter((line) => !line.includes("registry="))
2829
.join(EOL);
29-
await fs.writeFile(this.userFilePath, updatedNpmrcContent);
30+
await writeFileLazy(this.userFilePath, updatedNpmrcContent);
3031
} catch (error) {
3132
// user npmrc does not exist so make an empty one
32-
await fs.writeFile(this.userFilePath, "");
33+
await writeFileLazy(this.userFilePath, "");
3334
}
3435
}
3536

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import fs from "node:fs/promises";
2+
3+
export async function writeFileLazy(
4+
path: string,
5+
content: string,
6+
): Promise<void> {
7+
try {
8+
const existingContent = await fs.readFile(path, { encoding: "utf-8" });
9+
if (existingContent === content) {
10+
// File already up to date, no need to write out content
11+
return;
12+
}
13+
} catch (e) {}
14+
15+
await fs.writeFile(path, content, { encoding: "utf-8" });
16+
return;
17+
}

packages/ado-npm-auth/src/yarnrc/yarnrcFileProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs from "node:fs/promises";
44
import { fromBase64, toBase64 } from "../utils/encoding.js";
55
import { getOrganizationFromFeedUrl } from "../utils/get-organization-from-feed-url.js";
66
import { getFeedWithoutProtocol } from "../utils/get-feed-without-protocol.js";
7+
import { writeFileLazy } from "../utils/fileUtils.js";
78

89
export class YarnRcFileProvider extends FileProvider {
910
constructor(configFile?: string) {
@@ -21,7 +22,7 @@ export class YarnRcFileProvider extends FileProvider {
2122
}
2223
} catch (error) {
2324
// user .yarnrc file does not exist so make an empty one
24-
await fs.writeFile(this.userFilePath, "");
25+
await writeFileLazy(this.userFilePath, "");
2526
}
2627
}
2728

@@ -102,7 +103,7 @@ export class YarnRcFileProvider extends FileProvider {
102103

103104
async writeYarnRc(filePath: string, yarnrc: YarnRc) {
104105
const yarnrcContent = yaml.dump(yarnrc);
105-
await fs.writeFile(filePath, yarnrcContent, "utf8");
106+
await writeFileLazy(filePath, yarnrcContent);
106107
}
107108

108109
async paseYarnRc(filePath: string): Promise<YarnRc> {

0 commit comments

Comments
 (0)