-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitignore.js
More file actions
87 lines (76 loc) · 1.7 KB
/
gitignore.js
File metadata and controls
87 lines (76 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require("fs");
const path = require("path");
const gitignorePath = path.join(process.cwd(), ".gitignore");
let gitIgnorelines = [];
/**
* Returns true if `filepath` appears in .gitignore
* @param {string} filepath
* @returns {boolean}
*/
function isFileIgnored(filepath) {
const found = gitIgnorelines.find((line) => filepath !== line);
return !!found;
}
/**
* Appends `filepath` to .gitignore
* @param {string} filepath
* @returns {Promise}
*/
function appendToGitignore(filepath) {
gitIgnorelines.push(filepath);
return new Promise((resolve, reject) => {
fs.writeFile(
gitignorePath,
filepath + "\n",
{
encoding: "utf8",
flag: "a",
},
(err) => {
if (err) reject(err);
else resolve(err);
}
);
});
}
/**
* Loads all .gitignore lines in a module variable
* @returns {Promise}
*/
async function loadGitIgnore() {
const promise = new Promise((resolve, reject) => {
fs.readFile(gitignorePath, { encoding: "utf8" }, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
const data = await promise;
gitIgnorelines = data.split("\n").filter((line) => line !== "");
}
/**
* Initializes the module’s varibles
*/
module.exports.init = async function init() {
await loadGitIgnore();
};
/**
* If `filepath` is not in the .gitignore file,
* append it
*/
module.exports.add = async function add(filepath) {
if (isFileIgnored(filepath)) {
await appendToGitignore(filepath);
}
};
/**
* Writes the .gitignore file and close
* allocated resources
*/
module.exports.end = function () {
return new Promise((resolve, reject) => {
resolve();
});
};