1
1
import fs from "fs/promises" ;
2
2
import { outputFolder } from "./constants.js" ;
3
3
import { Logger } from "./logger.js" ;
4
+ import { getProjectDirname } from "./getDirname.js" ;
5
+ import path from "path" ;
4
6
5
7
/**
6
8
* Check if ".lldebugger" exists in .gitignore
7
9
*/
8
10
async function doesExistInGitIgnore ( ) {
9
11
try {
10
- const gitignoreContent = await fs . readFile ( ".gitignore" , "utf-8" ) ;
12
+ const gitignoreContent = await fs . readFile (
13
+ getGitIgnoreFileLocation ( ) ,
14
+ "utf-8"
15
+ ) ;
11
16
// split by new line
12
17
const lines = gitignoreContent . split ( "\n" ) ;
13
18
// check if ".lldebugger" exists
@@ -18,6 +23,14 @@ async function doesExistInGitIgnore() {
18
23
}
19
24
}
20
25
26
+ /**
27
+ * Get the location of .gitignore
28
+ * @returns
29
+ */
30
+ function getGitIgnoreFileLocation ( ) {
31
+ return path . join ( getProjectDirname ( ) , ".gitignore" ) ;
32
+ }
33
+
21
34
/**
22
35
* Add ".lldebugger" to .gitignore if it doesn't exist
23
36
* @returns
@@ -28,14 +41,14 @@ async function addToGitIgnore() {
28
41
if ( ! exists ) {
29
42
// does file exist?
30
43
try {
31
- await fs . access ( ".gitignore" ) ;
44
+ await fs . access ( getGitIgnoreFileLocation ( ) ) ;
32
45
} catch ( error ) {
33
- await fs . writeFile ( ".gitignore" , `${ outputFolder } \n` ) ;
46
+ await fs . writeFile ( getGitIgnoreFileLocation ( ) , `${ outputFolder } \n` ) ;
34
47
return ;
35
48
}
36
49
37
50
// append to existing file
38
- await fs . appendFile ( ".gitignore" , `\n${ outputFolder } \n` ) ;
51
+ await fs . appendFile ( getGitIgnoreFileLocation ( ) , `\n${ outputFolder } \n` ) ;
39
52
} else {
40
53
Logger . log ( `${ outputFolder } already exists in .gitignore` ) ;
41
54
}
@@ -48,9 +61,12 @@ async function removeFromGitIgnore() {
48
61
Logger . verbose ( "Removing .gitignore entry..." ) ;
49
62
const exists = await doesExistInGitIgnore ( ) ;
50
63
if ( exists ) {
51
- const gitignoreContent = await fs . readFile ( ".gitignore" , "utf-8" ) ;
64
+ const gitignoreContent = await fs . readFile (
65
+ getGitIgnoreFileLocation ( ) ,
66
+ "utf-8"
67
+ ) ;
52
68
const newContent = gitignoreContent . replace ( `${ outputFolder } \n` , "" ) ;
53
- await fs . writeFile ( ".gitignore" , newContent ) ;
69
+ await fs . writeFile ( getGitIgnoreFileLocation ( ) , newContent ) ;
54
70
} else {
55
71
Logger . log ( `${ outputFolder } doesn't exist in .gitignore` ) ;
56
72
}
0 commit comments