Fix: An issue that would cause Matugen colors to not apply. (#929)
* Eslint updates * linter fixes * Type fixes * More type fixes * Fix isvis * More type fixes * Type Fixes * Consolidate logic to manage options * Linter fixes * Package lock update * Update configs * Version checker * Debug pipeline * Package lock update * Update ci * Strict check * Revert ci * Eslint * Remove rule since it causes issues in CI * Actual matugen fix
This commit is contained in:
308
scripts/makeTheme.ts
Normal file → Executable file
308
scripts/makeTheme.ts
Normal file → Executable file
@@ -21,179 +21,181 @@
|
||||
* ts-node makeTheme.ts theme.json updatedTheme.json --vivid
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
(() => {
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* Prints the usage/help text.
|
||||
*/
|
||||
function printUsage(): void {
|
||||
console.log(`
|
||||
Usage:
|
||||
ts-node makeTheme.ts <input.json> <output.json> [--vivid | --base | --both]
|
||||
/**
|
||||
* Prints the usage/help text.
|
||||
*/
|
||||
function printUsage(): void {
|
||||
console.log(`
|
||||
Usage:
|
||||
ts-node makeTheme.ts <input.json> <output.json> [--vivid | --base | --both]
|
||||
|
||||
Options:
|
||||
--help Show this help message
|
||||
Options:
|
||||
--help Show this help message
|
||||
|
||||
Transforms:
|
||||
--vivid Swap .background with .text (or .total), set .icon = old background
|
||||
--base Set .icon = .text (or .total), leave .background alone
|
||||
--both Apply makeVividTheme, then makeBaseTheme
|
||||
`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a file as UTF-8 and returns a parsed JSON object.
|
||||
*/
|
||||
async function readJson(filePath: string): Promise<Record<string, unknown>> {
|
||||
const raw = await fs.promises.readFile(path.resolve(filePath), 'utf8');
|
||||
return JSON.parse(raw) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the "vivid" transformation on the input data.
|
||||
* For each key matching "theme.bar.buttons.*":
|
||||
* - If it has .background and .text (or .total), swap them.
|
||||
* - Set .icon to the old background.
|
||||
*/
|
||||
function makeVividTheme(data: Record<string, unknown>): void {
|
||||
data['theme.bar.buttons.style'] = 'default';
|
||||
|
||||
const prefix = 'theme.bar.buttons.';
|
||||
const grouped: Record<string, Record<string, unknown>> = {};
|
||||
|
||||
for (const key of Object.keys(data)) {
|
||||
if (!key.startsWith(prefix)) continue;
|
||||
|
||||
// Split: ["theme", "bar", "buttons", ...theRest]
|
||||
const parts = key.split('.');
|
||||
const rest = parts.slice(3);
|
||||
if (rest.length < 2) continue; // need at least <name> and <prop>
|
||||
|
||||
// Last segment is the property (e.g. "text", "background")
|
||||
// The rest form the "name" (e.g. "modules.updates")
|
||||
const prop = rest.pop()!;
|
||||
const name = rest.join('.');
|
||||
|
||||
if (!grouped[name]) grouped[name] = {};
|
||||
grouped[name][prop] = data[key];
|
||||
Transforms:
|
||||
--vivid Swap .background with .text (or .total), set .icon = old background
|
||||
--base Set .icon = .text (or .total), leave .background alone
|
||||
--both Apply makeVividTheme, then makeBaseTheme
|
||||
`);
|
||||
}
|
||||
|
||||
for (const name of Object.keys(grouped)) {
|
||||
const props = grouped[name];
|
||||
const hasBg = Object.prototype.hasOwnProperty.call(props, 'background');
|
||||
const hasTxt = Object.prototype.hasOwnProperty.call(props, 'text');
|
||||
const hasTot = Object.prototype.hasOwnProperty.call(props, 'total');
|
||||
if (!hasBg || (!hasTxt && !hasTot)) continue;
|
||||
|
||||
const oldBackground = props['background'];
|
||||
let textKey: 'text' | 'total' | undefined;
|
||||
if (hasTxt) textKey = 'text';
|
||||
else if (hasTot) textKey = 'total';
|
||||
if (!textKey) continue;
|
||||
|
||||
const oldText = props[textKey];
|
||||
props[textKey] = oldBackground;
|
||||
props['icon'] = oldBackground;
|
||||
props['background'] = oldText;
|
||||
/**
|
||||
* Reads a file as UTF-8 and returns a parsed JSON object.
|
||||
*/
|
||||
async function readJson(filePath: string): Promise<Record<string, unknown>> {
|
||||
const raw = await fs.promises.readFile(path.resolve(filePath), 'utf8');
|
||||
return JSON.parse(raw);
|
||||
}
|
||||
|
||||
// Write the changes back to the original data
|
||||
for (const name of Object.keys(grouped)) {
|
||||
for (const prop of Object.keys(grouped[name])) {
|
||||
const fullKey = prefix + name + '.' + prop;
|
||||
data[fullKey] = grouped[name][prop];
|
||||
/**
|
||||
* Executes the "vivid" transformation on the input data.
|
||||
* For each key matching "theme.bar.buttons.*":
|
||||
* - If it has .background and .text (or .total), swap them.
|
||||
* - Set .icon to the old background.
|
||||
*/
|
||||
function makeVividTheme(data: Record<string, unknown>): void {
|
||||
data['theme.bar.buttons.style'] = 'default';
|
||||
|
||||
const prefix = 'theme.bar.buttons.';
|
||||
const grouped: Record<string, Record<string, unknown>> = {};
|
||||
|
||||
for (const key of Object.keys(data)) {
|
||||
if (!key.startsWith(prefix)) continue;
|
||||
|
||||
// Split: ["theme", "bar", "buttons", ...theRest]
|
||||
const parts = key.split('.');
|
||||
const rest = parts.slice(3);
|
||||
if (rest.length < 2) continue; // need at least <name> and <prop>
|
||||
|
||||
// Last segment is the property (e.g. "text", "background")
|
||||
// The rest form the "name" (e.g. "modules.updates")
|
||||
const prop = rest.pop()!;
|
||||
const name = rest.join('.');
|
||||
|
||||
if (!grouped[name]) grouped[name] = {};
|
||||
grouped[name][prop] = data[key];
|
||||
}
|
||||
|
||||
for (const name of Object.keys(grouped)) {
|
||||
const props = grouped[name];
|
||||
const hasBg = Object.prototype.hasOwnProperty.call(props, 'background');
|
||||
const hasTxt = Object.prototype.hasOwnProperty.call(props, 'text');
|
||||
const hasTot = Object.prototype.hasOwnProperty.call(props, 'total');
|
||||
if (!hasBg || (!hasTxt && !hasTot)) continue;
|
||||
|
||||
const oldBackground = props['background'];
|
||||
let textKey: 'text' | 'total' | undefined;
|
||||
if (hasTxt) textKey = 'text';
|
||||
else if (hasTot) textKey = 'total';
|
||||
if (!textKey) continue;
|
||||
|
||||
const oldText = props[textKey];
|
||||
props[textKey] = oldBackground;
|
||||
props['icon'] = oldBackground;
|
||||
props['background'] = oldText;
|
||||
}
|
||||
|
||||
// Write the changes back to the original data
|
||||
for (const name of Object.keys(grouped)) {
|
||||
for (const prop of Object.keys(grouped[name])) {
|
||||
const fullKey = prefix + name + '.' + prop;
|
||||
data[fullKey] = grouped[name][prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the "base" transformation on the input data.
|
||||
* For each key matching "theme.bar.buttons.*":
|
||||
* - If it has .text or .total, set .icon to that value.
|
||||
* - Leave .background alone.
|
||||
*/
|
||||
function makeBaseTheme(data: Record<string, unknown>): void {
|
||||
data['theme.bar.buttons.style'] = 'default';
|
||||
/**
|
||||
* Executes the "base" transformation on the input data.
|
||||
* For each key matching "theme.bar.buttons.*":
|
||||
* - If it has .text or .total, set .icon to that value.
|
||||
* - Leave .background alone.
|
||||
*/
|
||||
function makeBaseTheme(data: Record<string, unknown>): void {
|
||||
data['theme.bar.buttons.style'] = 'default';
|
||||
|
||||
const prefix = 'theme.bar.buttons.';
|
||||
const grouped: Record<string, Record<string, unknown>> = {};
|
||||
const prefix = 'theme.bar.buttons.';
|
||||
const grouped: Record<string, Record<string, unknown>> = {};
|
||||
|
||||
for (const key of Object.keys(data)) {
|
||||
if (!key.startsWith(prefix)) continue;
|
||||
for (const key of Object.keys(data)) {
|
||||
if (!key.startsWith(prefix)) continue;
|
||||
|
||||
const parts = key.split('.');
|
||||
const rest = parts.slice(3);
|
||||
if (rest.length < 2) continue;
|
||||
const parts = key.split('.');
|
||||
const rest = parts.slice(3);
|
||||
if (rest.length < 2) continue;
|
||||
|
||||
const prop = rest.pop()!;
|
||||
const name = rest.join('.');
|
||||
if (!grouped[name]) grouped[name] = {};
|
||||
grouped[name][prop] = data[key];
|
||||
}
|
||||
const prop = rest.pop()!;
|
||||
const name = rest.join('.');
|
||||
if (!grouped[name]) grouped[name] = {};
|
||||
grouped[name][prop] = data[key];
|
||||
}
|
||||
|
||||
for (const name of Object.keys(grouped)) {
|
||||
const props = grouped[name];
|
||||
const hasTxt = Object.prototype.hasOwnProperty.call(props, 'text');
|
||||
const hasTot = Object.prototype.hasOwnProperty.call(props, 'total');
|
||||
if (!hasTxt && !hasTot) continue;
|
||||
for (const name of Object.keys(grouped)) {
|
||||
const props = grouped[name];
|
||||
const hasTxt = Object.prototype.hasOwnProperty.call(props, 'text');
|
||||
const hasTot = Object.prototype.hasOwnProperty.call(props, 'total');
|
||||
if (!hasTxt && !hasTot) continue;
|
||||
|
||||
const value = hasTxt ? props['text'] : props['total'];
|
||||
props['icon'] = value;
|
||||
}
|
||||
const value = hasTxt ? props['text'] : props['total'];
|
||||
props['icon'] = value;
|
||||
}
|
||||
|
||||
for (const name of Object.keys(grouped)) {
|
||||
for (const prop of Object.keys(grouped[name])) {
|
||||
const fullKey = prefix + name + '.' + prop;
|
||||
data[fullKey] = grouped[name][prop];
|
||||
for (const name of Object.keys(grouped)) {
|
||||
for (const prop of Object.keys(grouped[name])) {
|
||||
const fullKey = prefix + name + '.' + prop;
|
||||
data[fullKey] = grouped[name][prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main CLI entry point.
|
||||
*/
|
||||
async function main(): Promise<void> {
|
||||
const [, , ...args] = process.argv;
|
||||
if (args.includes('--help') || args.length < 2) {
|
||||
printUsage();
|
||||
process.exit(0);
|
||||
/**
|
||||
* Main CLI entry point.
|
||||
*/
|
||||
async function main(): Promise<void> {
|
||||
const [, , ...args] = process.argv;
|
||||
if (args.includes('--help') || args.length < 2) {
|
||||
printUsage();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const input = args[0];
|
||||
const output = args[1];
|
||||
const mode = args[2] || '--vivid';
|
||||
|
||||
let data: Record<string, unknown>;
|
||||
try {
|
||||
data = await readJson(input);
|
||||
} catch (e) {
|
||||
console.error(`Failed to read/parse: ${input}`, e);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (mode === '--vivid') makeVividTheme(data);
|
||||
else if (mode === '--base') makeBaseTheme(data);
|
||||
else if (mode === '--both') {
|
||||
makeVividTheme(data);
|
||||
makeBaseTheme(data);
|
||||
} else {
|
||||
console.error(`Unknown mode: ${mode}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
fs.writeFileSync(path.resolve(output), JSON.stringify(data, null, 2), 'utf8');
|
||||
console.log(`Wrote updated theme to: ${output}`);
|
||||
} catch (e) {
|
||||
console.error(`Failed to write file: ${output}`, e);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const input = args[0];
|
||||
const output = args[1];
|
||||
const mode = args[2] || '--vivid';
|
||||
|
||||
let data: Record<string, unknown>;
|
||||
try {
|
||||
data = await readJson(input);
|
||||
} catch (e) {
|
||||
console.error(`Failed to read/parse: ${input}`, e);
|
||||
process.exit(1);
|
||||
if (require.main === module) {
|
||||
main().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
if (mode === '--vivid') makeVividTheme(data);
|
||||
else if (mode === '--base') makeBaseTheme(data);
|
||||
else if (mode === '--both') {
|
||||
makeVividTheme(data);
|
||||
makeBaseTheme(data);
|
||||
} else {
|
||||
console.error(`Unknown mode: ${mode}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
fs.writeFileSync(path.resolve(output), JSON.stringify(data, null, 2), 'utf8');
|
||||
console.log(`Wrote updated theme to: ${output}`);
|
||||
} catch (e) {
|
||||
console.error(`Failed to write file: ${output}`, e);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
@@ -1,176 +1,178 @@
|
||||
#!/usr/bin/env ts-node
|
||||
/// <reference types="node" />
|
||||
|
||||
/**
|
||||
* Prints usage/help information to the console.
|
||||
* Called whenever the user passes `--help` or when arguments are missing/invalid.
|
||||
*/
|
||||
function printUsage(): void {
|
||||
console.log(`
|
||||
Usage:
|
||||
ts-node updateColors.ts <theme.json> <original_palette.json> <new_palette.json> <output.json>
|
||||
(() => {
|
||||
/**
|
||||
* Prints usage/help information to the console.
|
||||
* Called whenever the user passes `--help` or when arguments are missing/invalid.
|
||||
*/
|
||||
function printUsage(): void {
|
||||
console.log(`
|
||||
Usage:
|
||||
ts-node updateColors.ts <theme.json> <original_palette.json> <new_palette.json> <output.json>
|
||||
|
||||
Options:
|
||||
--help Show this help message
|
||||
Options:
|
||||
--help Show this help message
|
||||
|
||||
Description:
|
||||
This script reads a theme JSON file containing old color codes, an "original" palette JSON
|
||||
(which maps color name → old hex code), and a "new" palette JSON (mapping the same color name
|
||||
→ new hex code). It then replaces all old hex codes in the theme with the corresponding new hex codes,
|
||||
and saves the result to the specified output JSON file.
|
||||
Description:
|
||||
This script reads a theme JSON file containing old color codes, an "original" palette JSON
|
||||
(which maps color name → old hex code), and a "new" palette JSON (mapping the same color name
|
||||
→ new hex code). It then replaces all old hex codes in the theme with the corresponding new hex codes,
|
||||
and saves the result to the specified output JSON file.
|
||||
|
||||
Examples:
|
||||
ts-node updateColors.ts theme.json original_palette.json new_palette.json updated_theme.json
|
||||
Examples:
|
||||
ts-node updateColors.ts theme.json original_palette.json new_palette.json updated_theme.json
|
||||
|
||||
node updateColors.js theme.json original_palette.json new_palette.json updated_theme.json
|
||||
`);
|
||||
}
|
||||
node updateColors.js theme.json original_palette.json new_palette.json updated_theme.json
|
||||
`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively walks a palette to build a map from old hex → new hex,
|
||||
* keyed by the same property name in the new palette if it exists.
|
||||
*
|
||||
* Example:
|
||||
* originalPalette = { bg: "#24283b", git: { add: "#449dab" } }
|
||||
* newPalette = { bg: "#222436", git: { add: "#b8db87" } }
|
||||
*
|
||||
* => colorMap = {
|
||||
* "#24283b": "#222436",
|
||||
* "#449dab": "#b8db87"
|
||||
* }
|
||||
*
|
||||
* @param original - The "old" palette.
|
||||
* @param updated - The "new" palette containing updated hex codes.
|
||||
* @returns A map of { oldHex.toLowerCase(): newHex }.
|
||||
*/
|
||||
function buildColorMap(original: Palette, updated: Palette): ColorMap {
|
||||
const map: ColorMap = {};
|
||||
type Palette = Record<string, string | Record<string, string>>;
|
||||
type ColorMap = Record<string, string>;
|
||||
|
||||
for (const [key, oldVal] of Object.entries(original)) {
|
||||
const newVal = updated[key];
|
||||
/**
|
||||
* Recursively walks a palette to build a map from old hex → new hex,
|
||||
* keyed by the same property name in the new palette if it exists.
|
||||
*
|
||||
* Example:
|
||||
* originalPalette = { bg: "#24283b", git: { add: "#449dab" } }
|
||||
* newPalette = { bg: "#222436", git: { add: "#b8db87" } }
|
||||
*
|
||||
* => colorMap = {
|
||||
* "#24283b": "#222436",
|
||||
* "#449dab": "#b8db87"
|
||||
* }
|
||||
*
|
||||
* @param original - The "old" palette.
|
||||
* @param updated - The "new" palette containing updated hex codes.
|
||||
* @returns A map of { oldHex.toLowerCase(): newHex }.
|
||||
*/
|
||||
function buildColorMap(original: Palette, updated: Palette): ColorMap {
|
||||
const map: ColorMap = {};
|
||||
|
||||
if (typeof oldVal === 'string' && typeof newVal === 'string') {
|
||||
map[oldVal.toLowerCase()] = newVal;
|
||||
} else if (oldVal && typeof oldVal === 'object') {
|
||||
const oldNested = oldVal as Record<string, string>;
|
||||
const newNested = newVal && typeof newVal === 'object' ? (newVal as Record<string, string>) : {};
|
||||
for (const [key, oldVal] of Object.entries(original)) {
|
||||
const newVal = updated[key];
|
||||
|
||||
for (const [nestedKey, nestedOldHex] of Object.entries(oldNested)) {
|
||||
const nestedNewHex = newNested[nestedKey];
|
||||
if (typeof oldVal === 'string' && typeof newVal === 'string') {
|
||||
map[oldVal.toLowerCase()] = newVal;
|
||||
} else if (oldVal && typeof oldVal === 'object') {
|
||||
const oldNested = oldVal as Record<string, string>;
|
||||
const newNested =
|
||||
newVal && typeof newVal === 'object' ? (newVal as Record<string, string>) : {};
|
||||
|
||||
if (typeof nestedOldHex === 'string' && typeof nestedNewHex === 'string') {
|
||||
map[nestedOldHex.toLowerCase()] = nestedNewHex;
|
||||
for (const [nestedKey, nestedOldHex] of Object.entries(oldNested)) {
|
||||
const nestedNewHex = newNested[nestedKey];
|
||||
|
||||
if (typeof nestedOldHex === 'string' && typeof nestedNewHex === 'string') {
|
||||
map[nestedOldHex.toLowerCase()] = nestedNewHex;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
/**
|
||||
* Recursively replace all string values in a data structure (object, array, etc.)
|
||||
* if they appear in colorMap.
|
||||
*
|
||||
* @param node - The current portion of JSON to transform (can be object, array, string, etc.).
|
||||
* @param colorMap - An object with oldHex.toLowerCase() → newHex mappings.
|
||||
* @returns The transformed data with replaced colors.
|
||||
*/
|
||||
function replaceColorsInTheme(node: unknown, colorMap: ColorMap): unknown {
|
||||
if (Array.isArray(node)) {
|
||||
return node.map((item) => replaceColorsInTheme(item, colorMap));
|
||||
} else if (node && typeof node === 'object') {
|
||||
const result: Record<string, unknown> = {};
|
||||
|
||||
/**
|
||||
* Recursively replace all string values in a data structure (object, array, etc.)
|
||||
* if they appear in colorMap.
|
||||
*
|
||||
* @param node - The current portion of JSON to transform (can be object, array, string, etc.).
|
||||
* @param colorMap - An object with oldHex.toLowerCase() → newHex mappings.
|
||||
* @returns The transformed data with replaced colors.
|
||||
*/
|
||||
function replaceColorsInTheme(node: unknown, colorMap: ColorMap): unknown {
|
||||
if (Array.isArray(node)) {
|
||||
return node.map((item) => replaceColorsInTheme(item, colorMap));
|
||||
} else if (node && typeof node === 'object') {
|
||||
const result: Record<string, unknown> = {};
|
||||
|
||||
for (const [key, val] of Object.entries(node)) {
|
||||
result[key] = replaceColorsInTheme(val, colorMap);
|
||||
}
|
||||
return result;
|
||||
} else if (typeof node === 'string') {
|
||||
const lower = node.toLowerCase();
|
||||
return colorMap[lower] ? colorMap[lower] : node;
|
||||
} else {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A minimal utility wrapper to read a file asynchronously.
|
||||
*
|
||||
* @param filePath - The file path to read from.
|
||||
* @param encoding - The file encoding (default 'utf8').
|
||||
* @returns A Promise resolving to the file contents as a string.
|
||||
*/
|
||||
async function readFile(filePath: string, encoding: BufferEncoding = 'utf8'): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fs = require('fs');
|
||||
fs.readFile(filePath, encoding, (err: Error, data: string) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
for (const [key, val] of Object.entries(node)) {
|
||||
result[key] = replaceColorsInTheme(val, colorMap);
|
||||
}
|
||||
return result;
|
||||
} else if (typeof node === 'string') {
|
||||
const lower = node.toLowerCase();
|
||||
return colorMap[lower] ? colorMap[lower] : node;
|
||||
} else {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A minimal utility wrapper to read a file asynchronously.
|
||||
*
|
||||
* @param filePath - The file path to read from.
|
||||
* @param encoding - The file encoding (default 'utf8').
|
||||
* @returns A Promise resolving to the file contents as a string.
|
||||
*/
|
||||
async function readFile(filePath: string, encoding: BufferEncoding = 'utf8'): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fs = require('fs');
|
||||
fs.readFile(filePath, encoding, (err: Error, data: string) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function that:
|
||||
* 1. Checks for --help or missing arguments.
|
||||
* 2. Reads and parses the theme, original palette, and new palette.
|
||||
* 3. Builds the color map and applies color replacements.
|
||||
* 4. Writes the result as JSON to a new file.
|
||||
*
|
||||
* @example
|
||||
* ```bash
|
||||
* ts-node updateColors.ts theme.json original_palette.json new_palette.json output.json
|
||||
* ```
|
||||
*/
|
||||
async function main(): Promise<void> {
|
||||
const [, , ...args] = process.argv;
|
||||
|
||||
if (args.length === 0) {
|
||||
printUsage();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (args.length < 4) {
|
||||
console.error('Error: Not enough arguments provided.\n');
|
||||
printUsage();
|
||||
process.exit(1);
|
||||
} else if (args.length > 4) {
|
||||
console.error('Error: Too many arguments provided.\n');
|
||||
printUsage();
|
||||
process.exit(1);
|
||||
/**
|
||||
* Main function that:
|
||||
* 1. Checks for --help or missing arguments.
|
||||
* 2. Reads and parses the theme, original palette, and new palette.
|
||||
* 3. Builds the color map and applies color replacements.
|
||||
* 4. Writes the result as JSON to a new file.
|
||||
*
|
||||
* @example
|
||||
* ```bash
|
||||
* ts-node updateColors.ts theme.json original_palette.json new_palette.json output.json
|
||||
* ```
|
||||
*/
|
||||
async function main(): Promise<void> {
|
||||
const [, , ...args] = process.argv;
|
||||
|
||||
if (args.length === 0) {
|
||||
printUsage();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (args.length < 4) {
|
||||
console.error('Error: Not enough arguments provided.\n');
|
||||
printUsage();
|
||||
process.exit(1);
|
||||
} else if (args.length > 4) {
|
||||
console.error('Error: Too many arguments provided.\n');
|
||||
printUsage();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const [themePath, originalPath, newPath, outputPath] = args;
|
||||
|
||||
try {
|
||||
const themeData = JSON.parse(await readFile(themePath, 'utf8'));
|
||||
const originalPalette: Palette = JSON.parse(await readFile(originalPath, 'utf8'));
|
||||
const newPalette: Palette = JSON.parse(await readFile(newPath, 'utf8'));
|
||||
const fs = require('fs');
|
||||
|
||||
const colorMap = buildColorMap(originalPalette, newPalette);
|
||||
const updatedTheme = replaceColorsInTheme(themeData, colorMap);
|
||||
|
||||
fs.writeFileSync(outputPath, JSON.stringify(updatedTheme, null, 2), 'utf8');
|
||||
|
||||
console.log(`Successfully wrote updated theme to: ${outputPath}`);
|
||||
} catch (err) {
|
||||
console.error('Error:', err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const [themePath, originalPath, newPath, outputPath] = args;
|
||||
|
||||
try {
|
||||
const themeData = JSON.parse(await readFile(themePath, 'utf8'));
|
||||
const originalPalette: Palette = JSON.parse(await readFile(originalPath, 'utf8'));
|
||||
const newPalette: Palette = JSON.parse(await readFile(newPath, 'utf8'));
|
||||
const fs = require('fs');
|
||||
|
||||
const colorMap = buildColorMap(originalPalette, newPalette);
|
||||
const updatedTheme = replaceColorsInTheme(themeData, colorMap);
|
||||
|
||||
fs.writeFileSync(outputPath, JSON.stringify(updatedTheme, null, 2), 'utf8');
|
||||
|
||||
console.log(`Successfully wrote updated theme to: ${outputPath}`);
|
||||
} catch (err) {
|
||||
console.error('Error:', err);
|
||||
process.exit(1);
|
||||
if (require.main === module) {
|
||||
main().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
type Palette = Record<string, string | Record<string, string>>;
|
||||
|
||||
type ColorMap = Record<string, string>;
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user