Skip to content

Commit

Permalink
fix(settings): validation error for customUIColors with opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
nekowinston committed Nov 28, 2023
1 parent 8d7b378 commit ac41ba5
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 80 deletions.
4 changes: 4 additions & 0 deletions src/hooks/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { join } from "node:path";
export const vscodeSchemasRoot =
"https://raw.githubusercontent.com/wraith13/vscode-schemas/master/en/latest/schemas/";
export const repoRoot = join(__dirname, "../..");
12 changes: 6 additions & 6 deletions src/hooks/generateThemes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as fs from "fs/promises";
import * as path from "path";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { variants } from "@catppuccin/palette";

import { compileTheme, defaultOptions } from "@/theme";
import type { CatppuccinFlavor } from "@/types";
import { repoRoot } from "./constants";

const themeDir = path.join(__dirname, "../../themes");
const flavors = Object.keys(variants) as CatppuccinFlavor[];

// options can also be passed as a JSON string
Expand All @@ -14,9 +14,9 @@ const options = process.argv[2] ? JSON.parse(process.argv[2]) : {};
flavors.map((flavor) => {
const theme = compileTheme(flavor, { ...defaultOptions, ...options });
// ignore error if directory exists
fs.mkdir(themeDir, { recursive: true }).then(() => {
fs.writeFile(
path.join(themeDir, `${flavor}.json`),
mkdir(join(repoRoot, "themes"), { recursive: true }).then(() => {
writeFile(
join(repoRoot, `themes/${flavor}.json`),
JSON.stringify(theme, null, 2),
);
});
Expand Down
228 changes: 163 additions & 65 deletions src/hooks/updateSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,172 @@
import * as path from "path";
import { writeFileSync } from "fs";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import fetch from "node-fetch";
const vscodeSchemasRoot =
"https://raw.githubusercontent.com/wraith13/vscode-schemas/master/en/latest/schemas/";
import { repoRoot, vscodeSchemasRoot } from "./constants";

const accents = [
"rosewater",
"flamingo",
"pink",
"mauve",
"red",
"maroon",
"peach",
"yellow",
"green",
"teal",
"sky",
"sapphire",
"blue",
"lavender",
];

const customUiColorsSchema = (workbenchColors: any) => {
const validColors = [...accents, "accent"];
return {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
additionalProperties: false,
required: [],
properties: {
all: { $ref: "#/$defs/catppuccinWorkbenchColors" },
latte: { $ref: "#/$defs/catppuccinWorkbenchColors" },
frappe: { $ref: "#/$defs/catppuccinWorkbenchColors" },
macchiato: { $ref: "#/$defs/catppuccinWorkbenchColors" },
mocha: { $ref: "#/$defs/catppuccinWorkbenchColors" },
},
$defs: {
catppuccinColor: {
anyOf: [
// allow plain color names,
{ enum: validColors },
// custom hex codes,
{ format: "color-hex" },
// and plain color names + opacity
{
type: "string",
pattern: `^(${validColors.join("|")})\\s\\d\\.\\d+$`,
},
],
},
catppuccinWorkbenchColors: {
type: "object",
additionalProperties: false,
required: [],
properties: workbenchColors,
},
},
};
};

const configuration = (version: string) => {
return {
title: "Catppuccin",
properties: {
"catppuccin.boldKeywords": {
type: "boolean",
default: true,
markdownDescription: "Controls whether to use **bold** for keywords.",
},
"catppuccin.italicComments": {
type: "boolean",
default: true,
markdownDescription: "Controls whether to use *italics* for comments.",
},
"catppuccin.italicKeywords": {
type: "boolean",
default: true,
markdownDescription: "Controls whether to use *italics* for keywords.",
},
"catppuccin.colorOverrides": {
type: "object",
default: {},
markdownDescription:
"Custom color overrides. Assign your own hex codes to palette colors. See [the docs](https://github.com/catppuccin/vscode#override-palette-colors) for reference.",
$ref: `https://cdn.jsdelivr.net/gh/catppuccin/vscode@v${version}/schemas/colorOverrides.schema.json`,
},
"catppuccin.customUIColors": {
type: "object",
default: {},
markdownDescription:
"Customize UI colors. Map `workbench.colorCustomizations` to palette colors. See [the docs](https://github.com/catppuccin/vscode#use-palette-colors-on-workbench-elements-ui) for reference.",
$ref: `https://cdn.jsdelivr.net/gh/catppuccin/vscode@v${version}/schemas/customUIColors.schema.json`,
},
"catppuccin.accentColor": {
type: "string",
default: "mauve",
description: "Controls which accent color to use.",
enum: accents,
},
"catppuccin.workbenchMode": {
type: "string",
default: "default",
description: "Controls how the workbench should be styled.",
enum: ["default", "flat", "minimal"],
enumDescriptions: [
"The default look, using 3 shades of the base color.",
"A more flat look, using 2 shades of the base color.",
"A minimal look, using 1 shade of the base color.",
],
},
"catppuccin.bracketMode": {
type: "string",
default: "rainbow",
description: "Controls how bracket pairs should be themed",
enum: ["rainbow", "dimmed", "monochromatic", "neovim"],
enumDescriptions: [
"Uses 6 rainbow colors for matching bracket pairs.",
"Uses the same 6 rainbow colors as `rainbow`, but has a dimmed appearance.",
"A monochromatic, grey appearance for matching bracket pairs.",
"Uses the same bracket pair colors as our neovim port.",
],
},
"catppuccin.extraBordersEnabled": {
type: "boolean",
default: false,
description:
"Controls whether borders should be enabled on some additional UI elements.",
},
},
};
};

fetch(vscodeSchemasRoot + "workbench-colors.json")
.then((data) => data.json())
.then((data) => {
const extracted = Object.entries<any>(data.properties).reduce(
(acc, [name, { type, description }]) => {
acc[name] = {
type,
description,
anyOf: [{ format: "color-hex" }, { $ref: "#/$defs/catppuccinColor" }],
};
return acc;
},
{} as any,
const schema = customUiColorsSchema(
Object.entries<any>(data.properties).reduce(
(acc, [name, { description }]) => {
acc[name] = {
description,
$ref: "#/$defs/catppuccinColor",
};
return acc;
},
{} as any,
),
);
writeFile(
join(repoRoot, "schemas/customUIColors.schema.json"),
JSON.stringify(schema, null, 2),
"utf-8",
);
});

const schema = {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
additionalProperties: false,
required: [],
properties: {
all: {
$ref: "#/$defs/catppuccinWorkbenchColors",
},
latte: {
$ref: "#/$defs/catppuccinWorkbenchColors",
},
frappe: {
$ref: "#/$defs/catppuccinWorkbenchColors",
},
macchiato: {
$ref: "#/$defs/catppuccinWorkbenchColors",
},
mocha: {
$ref: "#/$defs/catppuccinWorkbenchColors",
},
},
$defs: {
catppuccinColor: {
enum: [
"accent",
"rosewater",
"flamingo",
"pink",
"mauve",
"red",
"maroon",
"peach",
"yellow",
"green",
"teal",
"sky",
"sapphire",
"blue",
"lavender",
],
},
catppuccinWorkbenchColors: {
type: "object",
additionalProperties: false,
required: [],
properties: extracted,
},
readFile(join(repoRoot, "package.json"), "utf-8")
.then((data) => JSON.parse(data))
.then((data) => {
return {
...data,
contributes: {
...data.contributes,
configuration: configuration(data.version),
},
};
const fp = path.join(__dirname, "../../schemas/customUIColors.schema.json");
writeFileSync(fp, JSON.stringify(schema, null, 2), "utf-8");
});
})
.then((data) =>
writeFile(
join(repoRoot, "package.json"),
JSON.stringify(data, null, 2),
"utf-8",
),
);
15 changes: 6 additions & 9 deletions src/hooks/updateVSCtypes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as path from "path";
import { writeFileSync } from "fs";
import { join } from "node:path";
import { writeFileSync } from "node:fs";
import { compile, JSONSchema } from "json-schema-to-typescript";
import { repoRoot, vscodeSchemasRoot } from "./constants";
import fetch from "node-fetch";

const tag = process.argv[2] ?? "latest";
const vscodeSchemasRoot = `https://raw.githubusercontent.com/wraith13/vscode-schemas/master/en/${tag}/schemas/`;

const bannerComment = `/* eslint-disable */
/**
* This file was automatically generated.
Expand Down Expand Up @@ -71,10 +69,9 @@ for (const { schema, name, fname, kind } of mappings) {
throw new Error(`Unknown kind: ${kind}`);
}
})
.then((typeDefs) => {
const fp = path.join(__dirname, `../types/${fname}`);
writeFileSync(fp, typeDefs, "utf-8");
});
.then((typeDefs) =>
writeFileSync(join(repoRoot, fname), typeDefs, "utf-8"),
);
}

const fromVSIXColors = (interfaceName: string, data: any) => {
Expand Down

0 comments on commit ac41ba5

Please sign in to comment.