rajasegar / alacritty-themes

:rainbow: :lollipop: Themes :candy: :heart_eyes: for Alacritty: A cross-platform GPU-accelerated Terminal emulator
MIT License
707 stars 71 forks source link

Alacritty 0.14.0 - import has been deprecated; use general.import instead #267

Open killeik opened 4 weeks ago

killeik commented 4 weeks ago

Describe the bug

After alacritty update to 0.14.0 import=[] is deprecated, alacritty-themes need to set [general] and then import=[] in config file

To Reproduce

Steps to reproduce the behavior:

  1. Installation
  1. Run alacritty-themes
  1. See error
    [WARN] Config warning: import has been deprecated; use general.import instead 
    Use 'alacritty migrate' to automatically resolve it 

Expected behavior

alacritty-themes successfully adds to the config file:

import = [
  "/usr/lib/node_modules/alacritty-themes/themes/3024.dark.toml"
]

But it should add:

[general]
import = [
  "/usr/lib/node_modules/alacritty-themes/themes/3024.dark.toml"
]

Operating System:

Add your alacritty.yml content

it's toml btw:

import = [
  "/usr/lib/node_modules/alacritty-themes/themes/Gruvbox-Light.toml"
]

[window]
decorations = "none"
opacity = 0.9
dynamic_padding = true

[env]
TERM = "xterm-256color"

[font]
size = 13

Screenshots

image

greninj21 commented 3 weeks ago

open the index.js located in " /usr/lib/node_modules/alacritty-themes/"

and replace these code given below in the functions getCurrentTheme and updateThemeWithFile

const imports = parsedAlacrittyConfig.import || [];

with

const imports = parsedAlacrittyConfig.general?.import || [];

and replace the conditional statement in updateThemeWithFile function with

if (currentThemeIndex === undefined) { 
    parsedAlacrittyConfig.general = parsedAlacrittyConfig.general || {};
    parsedAlacrittyConfig.general.import = [themePath];
} else {
    parsedAlacrittyConfig.general.import[currentThemeIndex] = themePath;
}

So your two functions should be like this (Might as well copy and paste this code lol)

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.general?.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.general?.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.general = parsedAlacrittyConfig.general || {};
    parsedAlacrittyConfig.general.import = [themePath];
  } else {
    parsedAlacrittyConfig.general.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;
    });
}