diff --git a/index.js b/index.js index 01302f6..c163009 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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); }); diff --git a/test/index.test.js b/test/index.test.js index fc1796e..04ee674 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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), @@ -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(() => @@ -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.' + ); + }); });