-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·104 lines (94 loc) · 2.69 KB
/
app.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#! /usr/bin/env node
const axios = require("axios");
const path = require("path");
const fs = require("fs");
const chalk = require("chalk");
const { AutoComplete } = require("enquirer");
const baseUrl = "https://www.gitignore.io/api";
const getOptionsLists = async () => {
try {
const response = await axios.get(`${baseUrl}/list`);
let rawList = response.data;
rawList = rawList.replace(/(\r\n|\n|\r)/gm, ",");
const formattedList = rawList.split(",");
return formattedList;
} catch (err) {
console.error("Getting error for option lists: ", err);
}
};
const getGeneratedFileValue = async (value = "node") => {
try {
let response = await axios.get(`${baseUrl}/${value}`);
response = response.data;
return response;
} catch (err) {
console.error("Error getting file values: ", err);
}
};
const createFile = async (filePath, contents) => {
try {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, contents, "utf-8", function (err) {
if (err) reject(err);
else {
console.log(".gitignore File created successfully");
resolve("Done");
}
});
});
} catch (err) {
console.error("Error: ", err);
}
};
const overWriteFile = async (filePath, contents) => {
try {
return new Promise((resolve, reject) => {
fs.appendFileSync(filePath, contents, "utf-8", function (err) {
if (err) reject(err);
else {
console.log(".gitignore File updated successfully");
resolve("Done");
}
});
});
} catch (err) {
console.error("Error: ", err);
}
};
const generateGitignoreFile = async (contents) => {
const filePath = path.resolve(path.join(process.cwd(), ".gitignore"));
const isExists = await fs.existsSync(filePath);
if (isExists) {
await overWriteFile(filePath, contents);
} else {
await createFile(filePath, contents);
}
};
const initAutoCompleteForSelectOption = async (lists) => {
const prompt = new AutoComplete({
name: "Gitignore",
message: "Type Operating Systems, IDEs or Programming Languages",
limit: 5,
initial: 1,
choices: lists,
});
return new Promise((resolve, reject) => {
prompt
.run()
.then((answer) => {
console.log(chalk.blue("Selected Option: ", answer));
resolve(answer);
})
.catch((err) => reject("Error: ", err));
});
};
const initOption = async () => {
const lists = await getOptionsLists();
const optionValue = await initAutoCompleteForSelectOption(lists);
return optionValue;
};
(async () => {
const option = await initOption();
const contents = await getGeneratedFileValue(option);
await generateGitignoreFile(contents);
})();