-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfigLocation.js
82 lines (69 loc) · 1.75 KB
/
configLocation.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
"use strict";
const fs = require("fs");
const userHome = require("os").homedir();
const mkdirp = require("mkdirp");
const configuration = {
createUserConfig(userConfigPath) {
mkdirp(userConfigPath, err => {
if (err) {
console.error(err);
}
});
},
createIfNotExist(path) {
try {
const pathInfo = fs.statSync(path);
if (!pathInfo.isDirectory()) {
this.createUserConfig(path);
}
} catch (error) {
this.createUserConfig(path);
}
},
/**
* Obtenha o local da pasta de configuração
*
* @returns {string} A localização da pasta da configuração
*/
getUserConfig() {
let userConfigPath = null;
/** Plataforma Windows */
if (process.platform === "win32") {
userConfigPath = `${userHome}/.config/Infinity`;
}
/** Plataformas Linux - Padrão XDG */
if (process.platform === "linux") {
userConfigPath = `${userHome}/.config/Infinity`;
}
/** Localização da configuração do Mac os */
if (process.platform === "darwin") {
userConfigPath = `${userHome}/Library/Preferences/Infinity`;
}
/** Plataforma não suportada */
if (userConfigPath === null) {
throw `Não foi possível definir o caminho de configuração para este sistema operacional: ${
process.platform
}`;
}
this.createIfNotExist(userConfigPath);
return userConfigPath;
},
/**
* Obtenha o caminho de configuração
*
* @argument {string} fileName
* @returns {string} O local do arquivo da configuração
*/
getPath(fileName) {
return `${this.getUserConfig()}/${fileName}`;
},
/**
* Assegure-se de que a configuração existe
*
* @returns {Boolean} Verdadeiro se o arquivo existir
*/
containsConfig(fileName) {
return fs.existsSync(`${this.getPath(fileName)}`);
}
};
module.exports = configuration;