-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathindex.js
153 lines (124 loc) · 3.44 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const fs = require('fs');
const fsPromises = fs.promises;
const path = require('path');
const settings = require('../../settings');
const NoAlacrittyFileFoundError = new Error(
'No Alacritty configuration file found.\nExpected one of the following files to exist:\n' +
possibleLocations().join('\n') +
'\nOr you can create a new one using `alacritty-themes --create`'
);
function createBackup() {
if (!alacrittyFileExists()) {
return;
}
const alacrittyFile = alacrittyConfigPath();
const backupFile = `${alacrittyFile}.${Date.now()}.bak`;
fsPromises
.copyFile(alacrittyFile, backupFile)
.then(() => {
console.log(`Automatic backup file was created: ${backupFile}`);
})
.catch((err) => {
if (err) throw err;
});
}
function rootDirectory() {
return settings.PROJECT_DIR;
}
function existingTheme(themeName, themesFolder) {
const file = themeFilePath(themeName, themesFolder);
return fs.existsSync(file);
}
function themeFilePath(themeName, themesFolder) {
return path.join(themesFolder, `${themeName}.toml`);
}
function themesFolder() {
return path.join(rootDirectory(), 'themes');
}
function isWindows() {
return process.env.OS === 'Windows_NT';
}
function windowsHome() {
return process.env.APPDATA;
}
function linuxHome() {
return process.env.HOME;
}
function archHome() {
return process.env.XDG_CONFIG_HOME;
}
function pathToAlacrittyFile() {
return isWindows()
? pathToAlacrittyFileOnWindows()
: pathToAlacrittyFileOnLinux();
}
function pathToAlacrittyFileOnWindows() {
return path.join(windowsHome(), 'alacritty/');
}
function pathToAlacrittyFileOnLinux() {
return path.join(linuxHome(), '.config/alacritty/');
}
function alacrittyTemplatePath() {
return path.join(rootDirectory(), 'alacritty.toml');
}
function alacrittyFileExists() {
return possibleLocations().some(function (location) {
return fs.existsSync(location);
});
}
function alacrittyConfigPath() {
return possibleLocations().find(function (location) {
if (!fs.existsSync(location)) return;
return location;
});
}
function possibleLocations() {
let locations = [];
if (linuxHome()) {
locations.push(
path.join(linuxHome(), '.config/alacritty/alacritty.toml'),
path.join(linuxHome(), '.alacritty.toml')
);
}
if (isWindows()) {
locations.push(path.join(windowsHome(), 'alacritty/alacritty.toml'));
}
// locations where the alacritty config can be located according to
// https://github.com/alacritty/alacritty#configuration
if (archHome()) {
locations.push(
path.join(archHome(), 'alacritty/alacritty.toml'),
path.join(archHome(), 'alacritty.toml')
);
}
return locations;
}
function helpMessage() {
return `
Usage: \n\talacritty-themes [options] [theme-name] | [themes-directory]\n
Description: \n\tThemes candy for alacritty A cross-platform GPU-accelerated terminal emulator\n
Options: \n\t--help, -h\tshows this help message and exit
\t--create, -C\tcreates a new config file
\t--current, -c\tshows applied theme name
\t--list, -l\tlists all available themes
\t--directory, -d\tspecify themes directory
`;
}
module.exports = {
NoAlacrittyFileFoundError,
alacrittyConfigPath,
alacrittyFileExists,
alacrittyTemplatePath,
archHome,
createBackup,
helpMessage,
isWindows,
linuxHome,
pathToAlacrittyFile,
possibleLocations,
rootDirectory,
existingTheme,
themeFilePath,
themesFolder,
windowsHome,
};