forked from rajasegar/alacritty-themes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (113 loc) · 3.37 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
const YAML = require('yaml');
const fs = require('fs');
const fsPromises = fs.promises;
const { Pair } = require('yaml/types');
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.yml`;
// 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.yml config file was created here ${configFile}`
);
})
.catch((err) => {
if (err) throw err;
});
}
function getCurrentTheme() {
if (!alacrittyConfigPath()) {
console.log(
'No Alacritty configuration file found\nRun: `alacritty-themes -C` to create one'
);
exit(1);
}
const themeFile = fs.readFileSync(alacrittyConfigPath(), 'utf8');
const themeDoc = YAML.parse(themeFile);
return themeDoc.theme ? themeDoc.theme : 'default';
}
function updateThemeWithFile(
data,
themeName,
themePath,
ymlPath,
preview = false
) {
const themeFile = fs.readFileSync(themePath, 'utf8');
const themeDoc = YAML.parseDocument(themeFile);
const themeColors = themeDoc.contents.items.find(
(i) => i.key.value === 'colors'
);
const alacrittyDoc = YAML.parseDocument(data);
if (alacrittyDoc.contents === null) {
alacrittyDoc.contents = { items: [] };
}
const alacrittyColors = alacrittyDoc.contents.items.find(
(i) => i.key.value === 'colors'
);
if (alacrittyColors) {
alacrittyColors.value = themeColors.value;
} else {
alacrittyDoc.contents.items.push(new Pair('colors', themeColors.value));
}
const alacrittyTheme = alacrittyDoc.contents.items.find(
(i) => i.key.value === 'theme'
);
if (alacrittyTheme) {
alacrittyTheme.value = themeName;
} else {
alacrittyDoc.contents.items.push(new Pair('theme', themeName));
}
const newContent = String(alacrittyDoc);
return fsPromises
.writeFile(ymlPath, newContent, 'utf8')
.then(() => {
if (!preview) {
console.log(`The theme "${themeName}" has been applied successfully!`);
}
})
.catch((err) => {
if (err) throw err;
});
}
function updateTheme(data, theme, themesFolder, ymlPath, preview = false) {
const isSpecificFile =
fs.existsSync(theme) && !fs.lstatSync(theme).isDirectory();
const themePath = isSpecificFile ? theme : themeFilePath(theme, themesFolder);
return updateThemeWithFile(data, theme, themePath, ymlPath, preview);
}
function applyTheme(theme, themesFolder, preview = false) {
const ymlPath = getAlacrittyConfig();
return fsPromises.readFile(ymlPath, 'utf8').then((data) => {
return updateTheme(data, theme, themesFolder, ymlPath, preview);
});
}
module.exports = {
applyTheme,
createConfigFile,
getAlacrittyConfig,
getCurrentTheme,
};