shadecli
Version:
CLI tool to generate Tailwind color palettes and update Tailwind config files
153 lines • 7.21 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateTailwindConfig = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const cli_1 = require("./cli");
/**
* Safely loads and parses an existing Tailwind CSS configuration file.
*
* @param {string} configPath - The path to the Tailwind configuration file.
* @returns {any} The parsed Tailwind configuration object.
* @throws {Error} If there is an error reading or parsing the configuration file.
*/
function loadTailwindConfig(configPath) {
try {
const configFile = fs.readFileSync(configPath, 'utf-8');
// Safely evaluate the JavaScript config
// Handle the case where the file is empty or doesn't have the expected format
if (!configFile.trim()) {
return {}; // If the file is empty, return an empty object
}
return eval(configFile); // This safely evaluates the JavaScript config.
}
catch (error) {
if (error instanceof Error) {
throw new Error(`Error reading the config file: ${error.message}`);
}
else {
throw new Error('An unknown error occurred while reading the config file');
}
}
}
/**
* Creates a backup of the original Tailwind CSS configuration file.
*
* @param {string} configPath - The path to the Tailwind configuration file.
* @returns {void}
*/
function createBackup(configPath) {
const backupPath = path.join(path.dirname(configPath), `tailwind.config.backup.${Date.now()}.js`);
try {
fs.copyFileSync(configPath, backupPath);
console.log(`Backup created at: ${backupPath}`);
}
catch (error) {
if (error instanceof Error) {
console.error('Error creating backup file:', error.message);
}
else {
console.error('An unknown error occurred while creating the backup');
}
}
}
/**
* Updates the Tailwind CSS configuration file with a new color and its shades.
*
* @param {string} configPath - The path to the Tailwind configuration file.
* @param {string} colorName - The name of the new color to be added.
* @param {Record<string, string>} shades - An object containing the color shades and their corresponding hex values.
* @param {string} baseColor - The base color to be added.
* @returns {Promise<void>}
*/
function updateTailwindConfig(configPath, colorName, shades, baseColor // Accept the base color as a parameter
) {
return __awaiter(this, void 0, void 0, function* () {
try {
// Create a backup of the original config
createBackup(configPath);
const config = loadTailwindConfig(configPath);
// Ensure that the 'theme' section exists in the config
if (!config.theme) {
config.theme = {};
}
// Ensure the 'extend' section inside 'theme' exists
if (!config.theme.extend) {
config.theme.extend = {};
}
// Ensure the 'colors' section inside 'extend' exists
if (!config.theme.extend.colors) {
config.theme.extend.colors = {};
}
// Graceful Failure for Missing or Invalid CLI Arguments
if (!colorName || !shades) {
console.error('Missing required arguments: --color and --name');
process.exit(1);
}
// Check if the color name already exists in the configuration
if (config.theme.extend.colors[colorName]) {
// Prompt the user for confirmation to overwrite
const shouldOverwrite = yield (0, cli_1.askQuestion)(`The color "${colorName}" already exists in the config. Do you want to overwrite it? (y/n): `);
if (!shouldOverwrite) {
console.log(`Skipped updating "${colorName}" in the Tailwind config.`);
return; // Exit if the user chooses not to overwrite
}
}
// Handle adding the base color
// Add or overwrite the base color
config.theme.extend.colors[colorName] = baseColor;
console.log(`Added base color: ${colorName} => ${baseColor}`);
// Add or overwrite the generated color shades
Object.keys(shades).forEach((shadeKey) => {
const colorKey = `${colorName}-${shadeKey}`;
config.theme.extend.colors[colorKey] = shades[shadeKey];
console.log(`Added/Updated shade: ${colorKey} => ${shades[shadeKey]}`);
});
// Write the updated config back to the file, preserving module exports
fs.writeFileSync(configPath, `module.exports = ${JSON.stringify(config, null, 2)};\n`, 'utf-8');
console.log(`Tailwind config updated with new shades for "${colorName}".`);
}
catch (error) {
if (error instanceof Error) {
console.error('Error updating the Tailwind config:', error.message);
}
else {
console.error('An unknown error occurred while updating the Tailwind config');
}
}
});
}
exports.updateTailwindConfig = updateTailwindConfig;
//# sourceMappingURL=tailwindConfigUpdater.js.map
;