92 lines
2.8 KiB
JavaScript
92 lines
2.8 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const archiver = require('archiver');
|
|
|
|
// Path to the theme's style.css
|
|
const styleCssPath = path.join(__dirname, 'style.css');
|
|
|
|
// Directories to exclude
|
|
const excludedDirs = [
|
|
'node_modules',
|
|
'src',
|
|
'.gitignore',
|
|
'package.json',
|
|
'package-lock.json',
|
|
'theme.zip',
|
|
'.git',
|
|
'react',
|
|
'zip-theme.js'
|
|
];
|
|
|
|
// Helper function to check if a file or directory should be excluded
|
|
const shouldExclude = (filePath) => {
|
|
return excludedDirs.some(exclude => filePath.includes(exclude));
|
|
};
|
|
|
|
// Read the style.css file and extract the theme name
|
|
fs.readFile(styleCssPath, 'utf8', (err, data) => {
|
|
if (err) {
|
|
console.error('Error reading style.css:', err);
|
|
return;
|
|
}
|
|
|
|
// Regex to match the theme name in the header
|
|
const themeNameMatch = data.match(/\/\*[\s\S]*?Theme Name:\s*(.*?)\s*\*\//);
|
|
|
|
// Fallback to 'theme' if no theme name is found
|
|
const themeName = themeNameMatch && themeNameMatch[1] ? themeNameMatch[1].trim() : 'theme';
|
|
|
|
// Sanitize the theme name (remove spaces and convert to lowercase)
|
|
const sanitizedThemeName = themeName.replace(/\s+/g, '').toLowerCase();
|
|
|
|
// Define the output zip file path using the sanitized theme name
|
|
const output = fs.createWriteStream(path.join(__dirname, `${sanitizedThemeName}.zip`));
|
|
|
|
// Create an archiver instance
|
|
const archive = archiver('zip', {
|
|
zlib: { level: 9 } // Compression level
|
|
});
|
|
|
|
// Pipe the archive to the output file
|
|
archive.pipe(output);
|
|
|
|
// Recursively append all files in the theme directory except excluded ones
|
|
const addFilesRecursively = (directory) => {
|
|
const files = fs.readdirSync(directory);
|
|
|
|
files.forEach((file) => {
|
|
const filePath = path.join(directory, file);
|
|
|
|
// Exclude the file or directory if it matches the exclusion criteria
|
|
if (shouldExclude(filePath)) {
|
|
console.log(`Excluding: ${filePath}`);
|
|
return; // Skip this file/directory
|
|
}
|
|
|
|
const stat = fs.statSync(filePath);
|
|
|
|
if (stat.isDirectory()) {
|
|
// If it's a directory, recurse into it
|
|
addFilesRecursively(filePath);
|
|
} else {
|
|
// If it's a file, add it to the archive
|
|
archive.file(filePath, { name: path.relative(__dirname, filePath) });
|
|
}
|
|
});
|
|
};
|
|
|
|
// Start adding files from the theme directory
|
|
addFilesRecursively(__dirname);
|
|
|
|
// Finalize the archive
|
|
archive.finalize();
|
|
|
|
output.on('close', () => {
|
|
console.log(`Theme has been zipped into ${sanitizedThemeName}.zip`);
|
|
});
|
|
|
|
output.on('error', (err) => {
|
|
console.error('Error creating zip file:', err);
|
|
});
|
|
});
|