Skip to content

Commit

Permalink
fix: trigger re-compile after updating the extension
Browse files Browse the repository at this point in the history
  • Loading branch information
nekowinston committed Oct 28, 2022
1 parent 0ad0367 commit 1fcf30f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 48 deletions.
10 changes: 7 additions & 3 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
assets/
schemas/
.github/
.husky/
.vscode/
.vscode-test-web/

assets/
schemas/
themes/.flag

*.vsix
**/*.ts
dist/hooks
build.js
tsconfig.json

.editorconfig
.eslintcache
.eslintignore
.eslintrc.js
Expand Down
20 changes: 12 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { workspace, window, ConfigurationChangeEvent } from "vscode";
import { workspace, ConfigurationChangeEvent } from "vscode";
import { getThemePaths } from "./helpers";
import utils from "./utils";
import utils, { UpdateTrigger } from "./utils";

export const activate = () => {
const config = utils.getConfiguration();
const paths = getThemePaths();

// regenerate the theme files when the config changes
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("catppuccin")) {
const config = utils.getConfiguration();
const paths = getThemePaths();
utils.updateThemes(config, paths);
window.showInformationMessage(
"Catppuccin Configuration changed, re-generating the JSON theme."
);
utils.updateThemes(config, paths, UpdateTrigger.CONFIG_CHANGE);
}
});

// regenerate on a fresh install if non-default config is set
if (utils.isFreshInstall() && !utils.isDefaultConfig()) {
utils.updateThemes(config, paths, UpdateTrigger.FRESH_INSTALL);
}
};

export const deactivate = () => {};
14 changes: 8 additions & 6 deletions src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { getTokenColors } from "./tokenColors";
import { getUiColors } from "./uiColors";
import { capitalize } from "./utils";

export const defaultOptions: ThemeOptions = {
accent: "mauve",
italicComments: true,
italicKeywords: true,
colorOverrides: {},
};

export const compileTheme = (
flavour: CatppuccinFlavour = "mocha",
options: ThemeOptions = {
accent: "mauve",
italicComments: true,
italicKeywords: true,
colorOverrides: null,
}
options: ThemeOptions = defaultOptions
) => {
const ctpPalette = Object.entries(variants[flavour])
.map(([k, v]) => {
Expand Down
88 changes: 57 additions & 31 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,81 @@
import { variants } from "@catppuccin/palette";
import * as fs from "fs";
import { compileTheme } from "./theme";
import { workspace, window, commands } from "vscode";
import { compileTheme, defaultOptions } from "./theme";
import { commands, workspace, window } from "vscode";
import type {
CatppuccinAccent,
CatppuccinFlavour,
ColorOverrides,
ThemeOptions,
ThemePaths,
} from "./types";
import { join } from "path";

// the reason why an update has been triggered, and a reload is needed
export enum UpdateTrigger {
CONFIG_CHANGE = "Configuration changed",
FRESH_INSTALL = "Update detected",
}

class Utils {
private promptToReload() {
const action = "Reload";
window
.showInformationMessage("Reload required.", action)
.then((selectedAction) => {
if (selectedAction === action) {
commands.executeCommand("workbench.action.reloadWindow");
}
});
private promptToReload(trigger: UpdateTrigger) {
const msg = `Catppuccin: ${trigger} - Reload required.`;
const action = "Reload window";
window.showInformationMessage(msg, action).then((selectedAction) => {
if (selectedAction === action) {
commands.executeCommand("workbench.action.reloadWindow");
}
});
}
private async writeFile(path: string, data: string | NodeJS.ArrayBufferView) {
return new Promise((resolve, reject) => {
fs.writeFile(path, JSON.stringify(data, null, 2), (err) =>
err ? reject(err) : resolve("Success")
);
private writeThemeFile(path: string, data: any) {
return fs.writeFile(path, JSON.stringify(data, null, 2), (err) => {
if (err) {
window.showErrorMessage(err.message);
}
});
}
isFreshInstall(): boolean {
console.log("Checking if catppuccin is installed for the first time.");
const flagPath = join(__dirname, "..", "themes", ".flag");
if (fs.existsSync(flagPath)) {
console.log("Catppuccin is installed for the first time!");
return false;
} else {
console.log("Catppuccin has been installed before.");
fs.writeFileSync(flagPath, "");
return true;
}
}
isDefaultConfig(): boolean {
console.log("Checking if catppuccin is using default config.");
const state = this.getConfiguration() === defaultOptions;
console.log(`Catppuccin is using ${state ? "default" : "custom"} config.`);
return state;
}
getConfiguration = (): ThemeOptions => {
const workspaceConfiguration = workspace.getConfiguration("catppuccin");
const conf = workspace.getConfiguration("catppuccin");
return {
accent: workspaceConfiguration.get<CatppuccinAccent>("accentColor"),
italicKeywords: workspaceConfiguration.get<boolean>("italicKeywords"),
italicComments: workspaceConfiguration.get<boolean>("italicComments"),
colorOverrides:
workspaceConfiguration.get<ColorOverrides>("colorOverrides"),
accent: conf.get<CatppuccinAccent>("accentColor"),
italicKeywords: conf.get<boolean>("italicKeywords"),
italicComments: conf.get<boolean>("italicComments"),
colorOverrides: conf.get<ColorOverrides>("colorOverrides"),
};
};
saveThemeJSON = (path: string, data: any): void => {
this.writeFile(path, data);
};
updateThemes = (options: ThemeOptions, paths: ThemePaths) => {
updateThemes = async (
options: ThemeOptions,
paths: ThemePaths,
trigger: UpdateTrigger
) => {
const flavours = Object.keys(variants) as CatppuccinFlavour[];
// TODO: use fsPromises instead of maps, then prompt to reloadA
// wait for Promises.All, then prompt to reload
flavours.map((flavour) => {

const promises = flavours.map(async (flavour): Promise<void> => {
const theme = compileTheme(flavour, options);
this.saveThemeJSON(paths[flavour], theme);
return this.writeThemeFile(paths[flavour], theme);
});

Promise.all(promises).then(() => {
this.promptToReload(trigger);
});
this.promptToReload();
};
}

Expand Down

0 comments on commit 1fcf30f

Please sign in to comment.