Skip to content

Commit

Permalink
fix: stop removing existing comments on the alacritty file (#42)
Browse files Browse the repository at this point in the history
* fix: stop removing existing comments on the Alacritty file

closes #30
  • Loading branch information
Juan Vásquez committed Sep 21, 2021
1 parent 0381e95 commit 98a5d68
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
38 changes: 16 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,33 @@ function createConfigFile() {

function updateThemeWithFile(data, themePath, ymlPath, preview = false) {
const themeFile = fs.readFileSync(themePath, 'utf8');

const doc = YAML.parseDocument(data);
// If config file is empty
if (doc.contents === null) {
doc.contents = { items: [] };
}

const themeDoc = YAML.parseDocument(themeFile);

// Find the colors key in user's alacritty.yml
const colors = doc.contents.items.filter((i) => i.key.value === 'colors')[0];

// Find the colors key in theme.yml
const themeColors = themeDoc.contents.items.filter(
(i) => i.key.value === 'colors'
)[0];

// colors key is commented out or not available
if (!colors) {
// Create new colors key and assign value from theme
doc.contents.items.push(new Pair('colors', themeColors.value));
const alacrittyDoc = YAML.parseDocument(data);
if (alacrittyDoc.contents === null) {
alacrittyDoc.contents = { items: [] };
}
const alacrittyColors = alacrittyDoc.contents.items.filter(
(i) => i.key.value === 'colors'
)[0];

if (alacrittyColors) {
alacrittyColors.value = themeColors.value;
} else {
// Update colors key
colors.value = themeColors.value;
alacrittyDoc.contents.items.push(new Pair('colors', themeColors.value));
}

const newContent = YAML.stringify(doc);
const newContent = String(alacrittyDoc);

return fsPromises
.writeFile(ymlPath, newContent, 'utf8')
.then(() => {
if (!preview) {
const namePairs = colors
? colors.value.items.filter((i) => i.key.value === 'name')
const namePairs = alacrittyColors
? alacrittyColors.value.items.filter((i) => i.key.value === 'name')
: [];
const themeName = namePairs.length !== 0 ? namePairs[0].value : null;
if (themeName !== null) {
Expand All @@ -103,12 +96,13 @@ function updateTheme(data, theme, ymlPath, preview = false) {
const isSpecificFile =
fs.existsSync(theme) && !fs.lstatSync(theme).isDirectory();
const themePath = isSpecificFile ? theme : themeFilePath(theme);

return updateThemeWithFile(data, themePath, ymlPath, preview);
}

function applyTheme(theme, preview = false) {
// alacritty.yml path
const ymlPath = getAlacrittyConfig();

return fsPromises.readFile(ymlPath, 'utf8').then((data) => {
return updateTheme(data, theme, ymlPath, preview);
});
Expand Down
36 changes: 33 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ const homeDir = linuxHome();
afterEach(mockFs.restore);

describe('Alacritty Themes', () => {
it('should not have a config file by default', () => {
it('returns NoAlacrittyFileFoundError error', () => {
mockFs();
assert.throws(() => getAlacrittyConfig(), NoAlacrittyFileFoundError);
});

it('should have a config file after creating it', async () => {
it('creates an alacritty.yml config file', async () => {
const templatePath = alacrittyTemplatePath();
const mockDir = {
'alacritty.yml': mockFs.load(templatePath),
Expand All @@ -37,7 +37,7 @@ describe('Alacritty Themes', () => {
assert.strictEqual(ymlPath, `${homeDir}/.config/alacritty/alacritty.yml`);
});

it('should set the correct theme colors', async () => {
it('sets the correct theme colors', async () => {
const templatePath = alacrittyTemplatePath();
const draculaPath = themeFilePath('Dracula');
const draculaTemplateContent = mockFs.bypass(() =>
Expand Down Expand Up @@ -65,4 +65,34 @@ describe('Alacritty Themes', () => {
draculaParsedContent.colors
);
});

it('keeps comments', async () => {
const alacrittyPath = alacrittyTemplatePath();
const alacrittyContent = mockFs.bypass(() =>
fs.readFileSync(alacrittyPath, 'utf8')
);
const draculaPath = themeFilePath('Dracula');
const draculaContent = mockFs.bypass(() =>
fs.readFileSync(draculaPath, 'utf8')
);

const mockDir = {
'alacritty.yml': alacrittyContent,
themes: {
'Dracula.yml': draculaContent,
},
};
mockDir[`${homeDir}/.config`] = { alacritty: {} };
mockFs(mockDir);
await createConfigFile();
const userAlacrittyPath = getAlacrittyConfig();
await applyTheme('Dracula');
const userAlacrittyFile = fs.readFileSync(userAlacrittyPath, 'utf8');
const alacritty = YAML.parseDocument(userAlacrittyFile);

assert.strictEqual(
alacritty.commentBefore,
' Configuration for Alacritty, the GPU enhanced terminal emulator.'
);
});
});

0 comments on commit 98a5d68

Please sign in to comment.