UNPKG

@manuth/woltlab-compiler

Version:

A compiler for generating WoltLab-Package `.tar` Archives and other WoltLab-Package Components

300 lines 10 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { EOL } from "node:os"; import { get } from "colornames"; import fs from "fs-extra"; import hexToRgba from "hex-to-rgba"; import parseSassValue from "parse-sass-value"; import path from "upath"; import { Component } from "../../../PackageSystem/Component.js"; import { FileDescriptor } from "../../../PackageSystem/FileDescriptor.js"; import { ImageDirectoryDescriptor } from "./ImageDirectoryDescriptor.js"; import { SassVariableParser } from "./SassVariableParser.js"; import { Theme } from "./Theme.js"; import { ThemeVariables } from "./ThemeVariables.js"; const { readFile, readJSON } = fs; const { isAbsolute, join } = path; /** * Provides the functionality to load themes from files. */ export class ThemeLoader extends Component { /** * Initializes a new instance of the {@link ThemeLoader `ThemeLoader`} class. * * @param instruction * The instruction of the theme-loader. * * @param options * The options of the theme-loader. */ constructor(instruction, options) { super({ DisplayName: options.DisplayName, Author: options.Author, Version: options.Version, CreationDate: options.CreationDate, Description: options.Description, License: options.License }); /** * The cover-photo of the theme to load. */ this.coverPhoto = null; /** * The path to the file containing custom `scss`-code of the theme to load. */ this.customScssFileName = null; /** * The path to the file containing special `scss`-variables of the theme to load. */ this.scssOverrideFileName = null; /** * The name of the file containing variables of the theme to load. */ this.variableFileName = null; /** * The variables of the theme to load. */ this.variables = new Map(); /** * The image-directory of the theme to load. */ this.images = null; this.instruction = instruction; this.Name = options.Name; if ((options.Thumbnail !== null) && (options.Thumbnail !== undefined)) { this.Thumbnail = this.LoadFileDescriptor(options.Thumbnail); } if ((options.HighResThumbnail !== null) && (options.HighResThumbnail !== undefined)) { this.HighResThumbnail = this.LoadFileDescriptor(options.HighResThumbnail); } if ((options.CoverPhoto !== null) && (options.CoverPhoto !== undefined)) { this.CoverPhoto = this.LoadFileDescriptor(options.CoverPhoto); } if ((options.Images !== null) && (options.Images !== undefined)) { this.Images = new ImageDirectoryDescriptor(options.Images); } if ((options.CustomScssFileName !== null) && (options.CustomScssFileName !== undefined)) { this.CustomScssFileName = options.CustomScssFileName; } if ((options.ScssOverrideFileName !== null) && (options.ScssOverrideFileName !== undefined)) { this.ScssOverrideFileName = options.ScssOverrideFileName; } if ((options.VariableFileName !== null) && (options.VariableFileName !== undefined)) { this.VariableFileName = options.VariableFileName; } } /** * Gets the instruction of the theme-loader. */ get Instruction() { return this.instruction; } /** * Gets or sets the name of the theme to load. */ get Name() { return this.name; } /** * @inheritdoc */ set Name(value) { this.name = value; } /** * @inheritdoc */ get Author() { return super.Author; } /** * Gets or sets the thumbnail of the theme to load. */ get Thumbnail() { return this.thumbnail; } /** * @inheritdoc */ set Thumbnail(value) { this.thumbnail = value; } /** * Gets or sets the high resolution version of the thumbnail of the theme to load. */ get HighResThumbnail() { return this.highResThumbnail; } /** * @inheritdoc */ set HighResThumbnail(value) { this.highResThumbnail = value; } /** * Gets or sets the path to the default cover-photo for user-profiles of the theme to load. */ get CoverPhoto() { return this.coverPhoto; } /** * @inheritdoc */ set CoverPhoto(value) { this.coverPhoto = value; } /** * Gets or sets the path to the file containing custom `scss`-code of the theme to load. */ get CustomScssFileName() { return this.customScssFileName; } /** * @inheritdoc */ set CustomScssFileName(value) { this.customScssFileName = value; } /** * Gets or sets the path to the file containing special `scss`-variables of the theme to load. */ get ScssOverrideFileName() { return this.scssOverrideFileName; } /** * @inheritdoc */ set ScssOverrideFileName(value) { this.scssOverrideFileName = value; } /** * Gets or sets the name of the file containing variables of the theme to load. */ get VariableFileName() { return this.variableFileName; } /** * @inheritdoc */ set VariableFileName(value) { this.variableFileName = value; } /** * Gets or sets the variables of the theme to load. */ get Variables() { return this.variables; } /** * Gets or sets the image-directory of the theme to load. */ get Images() { return this.images; } /** * @inheritdoc */ set Images(value) { this.images = value; } /** * Loads the theme. * * @returns * The newly loaded theme. */ Load() { return __awaiter(this, void 0, void 0, function* () { let scssOverrides = new Map(); let variables = Object.fromEntries(this.Variables.entries()); let themeOptions = { Name: this.Name, DisplayName: Object.fromEntries(this.DisplayName.Data.entries()), Description: Object.fromEntries(this.Description.Data.entries()), Version: this.Version, Author: this.Author, CreationDate: this.CreationDate, Thumbnail: this.Thumbnail, HighResThumbnail: this.HighResThumbnail, CoverPhoto: this.CoverPhoto, Images: this.Images, License: this.License, Variables: {} }; if (this.CustomScssFileName) { themeOptions.CustomScss = (yield readFile(this.CustomScssFileName)).toString(); } if (this.ScssOverrideFileName) { Object.assign(variables, yield new SassVariableParser(this.ScssOverrideFileName).Parse()); } if (this.VariableFileName) { let fileName = join(...(isAbsolute(this.VariableFileName) ? [this.VariableFileName] : [process.cwd(), this.VariableFileName])); Object.assign(variables, yield readJSON(fileName)); } for (let name in variables) { let value = variables[name]; if (ThemeVariables.Names.includes(name)) { if (/#(([0-9a-fA-F]{3}){1,2}|(0-9a-fA-F]{4}){1,2})/.test(value)) { themeOptions.Variables[name] = hexToRgba(value); } else if (typeof value === "string" && get.css(value)) { themeOptions.Variables[name] = hexToRgba(get.css(value).value); } else if (value === "transparent") { themeOptions.Variables[name] = hexToRgba("#0000"); } else { themeOptions.Variables[name] = value; } } else { scssOverrides.set(name, value); } } if (scssOverrides.size > 0) { themeOptions.ScssOverride = Array.from(scssOverrides).map((overrideEntry) => { return `$${overrideEntry[0]}: ${parseSassValue(overrideEntry[1], { quote: "double" })};`; }).join(EOL); } return new Theme(this.Instruction, themeOptions); }); } /** * Loads the file-descriptor from the specified {@link options `options`}. * * @param options * The options for creating the {@link FileDescriptor `FileDescriptor`}. * * @returns * The newly created {@link FileDescriptor `FileDescriptor`}. */ LoadFileDescriptor(options) { let fileOptions; if (typeof options === "string") { fileOptions = { Source: options }; } else { fileOptions = options; } return new FileDescriptor(fileOptions); } } //# sourceMappingURL=ThemeLoader.js.map