-
Notifications
You must be signed in to change notification settings - Fork 71
/
index.js
126 lines (105 loc) · 3.47 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
const TOML = require('@iarna/toml');
const fs = require('fs');
const fsPromises = fs.promises;
const path = require('path');
const {
NoAlacrittyFileFoundError,
alacrittyConfigPath,
alacrittyFileExists,
alacrittyTemplatePath,
pathToAlacrittyFile,
themeFilePath,
} = require('./src/helpers');
const { exit } = require('process');
// pick the correct config file or handle errors, if it doesn't exist
function getAlacrittyConfig() {
if (!alacrittyFileExists()) {
throw NoAlacrittyFileFoundError;
}
return alacrittyConfigPath();
}
function createConfigFile() {
const templatePath = alacrittyTemplatePath();
const configTemplate = fs.readFileSync(templatePath, 'utf8');
const directories = pathToAlacrittyFile();
const configFile = `${directories}alacritty.toml`;
// If .config/alacritty folder doesn't exists, create one
if (!fs.existsSync(directories)) {
fs.mkdirSync(directories);
}
return fsPromises
.writeFile(configFile, configTemplate, 'utf8')
.then(() => {
console.log(
`The alacritty.toml config file was created here ${configFile}`
);
})
.catch((err) => {
if (err) throw err;
});
}
function getCurrentTheme(themesFolder) {
if (!alacrittyConfigPath()) {
console.log(
'No Alacritty configuration file found\nRun: `alacritty-themes -C` to create one'
);
exit(1);
}
const alacrittyConfig = fs.readFileSync(alacrittyConfigPath(), 'utf8');
const parsedAlacrittyConfig = TOML.parse(alacrittyConfig);
const imports = parsedAlacrittyConfig.import || [];
// We'll consider the first theme import as the current theme
for (let i = 0; i < imports.length; i++) {
const relative = path.relative(themesFolder, imports[i]);
if (relative && !relative.startsWith('..') && !path.isAbsolute(relative)) {
return path.parse(imports[i]).name;
}
}
return 'default';
}
function updateThemeWithFile(themePath, themesPath, tomlPath, preview = false) {
const alacrittyConfig = fs.readFileSync(tomlPath, 'utf8');
const parsedAlacrittyConfig = TOML.parse(alacrittyConfig);
const imports = parsedAlacrittyConfig.import || [];
let currentThemeIndex = undefined;
for (let i = 0; i < imports.length; i++) {
const relative = path.relative(themesPath, imports[i]);
if (relative && !relative.startsWith('..') && !path.isAbsolute(relative)) {
currentThemeIndex = i;
break;
}
}
if (currentThemeIndex === undefined) {
parsedAlacrittyConfig.import = [themePath];
} else {
parsedAlacrittyConfig.import[currentThemeIndex] = themePath;
}
const newContent = TOML.stringify(parsedAlacrittyConfig);
const themeName = path.parse(themePath).name;
return fsPromises
.writeFile(tomlPath, newContent, 'utf8')
.then(() => {
if (!preview) {
console.log(`The theme "${themeName}" has been applied successfully!`);
}
})
.catch((err) => {
if (err) throw err;
});
}
function updateTheme(theme, themesPath, tomlPath, preview = false) {
const isSpecificFile =
fs.existsSync(theme) && !fs.lstatSync(theme).isDirectory();
const themePath = isSpecificFile ? theme : themeFilePath(theme, themesPath);
return updateThemeWithFile(themePath, themesPath, tomlPath, preview);
}
function applyTheme(theme, themesFolder, preview = false) {
const tomlPath = getAlacrittyConfig();
return updateTheme(theme, themesFolder, tomlPath, preview);
}
module.exports = {
applyTheme,
createConfigFile,
getAlacrittyConfig,
getCurrentTheme,
};