Skip to content

Commit 8568a2d

Browse files
fix: handling .gitignore
1 parent cbde1ef commit 8568a2d

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/gitignore.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import fs from "fs/promises";
22
import { outputFolder } from "./constants.js";
33
import { Logger } from "./logger.js";
4+
import { getProjectDirname } from "./getDirname.js";
5+
import path from "path";
46

57
/**
68
* Check if ".lldebugger" exists in .gitignore
79
*/
810
async function doesExistInGitIgnore() {
911
try {
10-
const gitignoreContent = await fs.readFile(".gitignore", "utf-8");
12+
const gitignoreContent = await fs.readFile(
13+
getGitIgnoreFileLocation(),
14+
"utf-8"
15+
);
1116
// split by new line
1217
const lines = gitignoreContent.split("\n");
1318
// check if ".lldebugger" exists
@@ -18,6 +23,14 @@ async function doesExistInGitIgnore() {
1823
}
1924
}
2025

26+
/**
27+
* Get the location of .gitignore
28+
* @returns
29+
*/
30+
function getGitIgnoreFileLocation() {
31+
return path.join(getProjectDirname(), ".gitignore");
32+
}
33+
2134
/**
2235
* Add ".lldebugger" to .gitignore if it doesn't exist
2336
* @returns
@@ -28,14 +41,14 @@ async function addToGitIgnore() {
2841
if (!exists) {
2942
// does file exist?
3043
try {
31-
await fs.access(".gitignore");
44+
await fs.access(getGitIgnoreFileLocation());
3245
} catch (error) {
33-
await fs.writeFile(".gitignore", `${outputFolder}\n`);
46+
await fs.writeFile(getGitIgnoreFileLocation(), `${outputFolder}\n`);
3447
return;
3548
}
3649

3750
// append to existing file
38-
await fs.appendFile(".gitignore", `\n${outputFolder}\n`);
51+
await fs.appendFile(getGitIgnoreFileLocation(), `\n${outputFolder}\n`);
3952
} else {
4053
Logger.log(`${outputFolder} already exists in .gitignore`);
4154
}
@@ -48,9 +61,12 @@ async function removeFromGitIgnore() {
4861
Logger.verbose("Removing .gitignore entry...");
4962
const exists = await doesExistInGitIgnore();
5063
if (exists) {
51-
const gitignoreContent = await fs.readFile(".gitignore", "utf-8");
64+
const gitignoreContent = await fs.readFile(
65+
getGitIgnoreFileLocation(),
66+
"utf-8"
67+
);
5268
const newContent = gitignoreContent.replace(`${outputFolder}\n`, "");
53-
await fs.writeFile(".gitignore", newContent);
69+
await fs.writeFile(getGitIgnoreFileLocation(), newContent);
5470
} else {
5571
Logger.log(`${outputFolder} doesn't exist in .gitignore`);
5672
}

0 commit comments

Comments
 (0)