refactor: build theme script

This commit is contained in:
Lucas Larroche 2024-01-25 22:04:24 +07:00
parent 4eee189dab
commit 1a2fc6536c

View file

@ -4,7 +4,6 @@ const fs = require("fs");
const themeColors = [ const themeColors = [
"amber", "amber",
// "azure",
"blue", "blue",
"cyan", "cyan",
"fuchsia", "fuchsia",
@ -28,18 +27,29 @@ const themeColors = [
const tempScssFoldername = path.join(__dirname, "../.pico"); const tempScssFoldername = path.join(__dirname, "../.pico");
const cssFoldername = path.join(__dirname, "../css"); const cssFoldername = path.join(__dirname, "../css");
// Ceeate the temp folder if it doesn't exist // Create a folder if it doesn't exist
if (!fs.existsSync(tempScssFoldername)) { const createFolderIfNotExists = (foldername) => {
fs.mkdirSync(tempScssFoldername); if (!fs.existsSync(foldername)) {
} fs.mkdirSync(foldername);
}
};
// Delete all files in the temp folder // Empty a folder
fs.readdirSync(tempScssFoldername).forEach((file) => { const emptyFolder = (foldername) => {
fs.unlinkSync(path.join(tempScssFoldername, file)); // Delete all files in the temp folder
}); fs.readdirSync(foldername).forEach((file) => {
fs.unlinkSync(path.join(foldername, file));
});
};
// Create the temp folder if it doesn't exist
createFolderIfNotExists(tempScssFoldername);
// Empty the temp folder
emptyFolder(tempScssFoldername);
// Loop through the theme colors // Loop through the theme colors
themeColors.forEach((themeColor) => { themeColors.forEach((themeColor, colorIndex) => {
// All the versions to generate // All the versions to generate
const versions = [ const versions = [
{ {
@ -93,8 +103,21 @@ themeColors.forEach((themeColor) => {
}, },
]; ];
const displayAsciiProgress = ({length, index, color}) => {
const progress = Math.round((index / length) * 100);
const bar = "■".repeat(progress / 10);
const empty = "□".repeat(10 - progress / 10);
process.stdout.write(`[@picocss/pico] ✨ ${bar}${empty} ${color}\r`);
};
// Loop through the versions // Loop through the versions
versions.forEach((version) => { versions.forEach((version) => {
displayAsciiProgress({
length: themeColors.length,
index: colorIndex,
color: themeColor.charAt(0).toUpperCase() + themeColor.slice(1),
});
// Create the file // Create the file
fs.writeFileSync( fs.writeFileSync(
path.join(tempScssFoldername, `${version.name}.${themeColor}.scss`), path.join(tempScssFoldername, `${version.name}.${themeColor}.scss`),
@ -109,5 +132,12 @@ themeColors.forEach((themeColor) => {
// Write the file // Write the file
fs.writeFileSync(path.join(cssFoldername, `${version.name}.${themeColor}.min.css`), result.css); fs.writeFileSync(path.join(cssFoldername, `${version.name}.${themeColor}.min.css`), result.css);
// Clear the console
process.stdout.clearLine();
process.stdout.cursorTo(0);
}); });
}); });
// Empty the temp folder
emptyFolder(tempScssFoldername);