Skip to content

Commit

Permalink
chore: continues if dir not found, fixes path #68
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfalaska committed Oct 14, 2024
1 parent a243abb commit 08d5399
Showing 1 changed file with 17 additions and 39 deletions.
56 changes: 17 additions & 39 deletions scripts/build/postCss.mjs
Original file line number Diff line number Diff line change
@@ -1,87 +1,65 @@
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import autoprefixer from 'autoprefixer';
import postcss from 'postcss';
import comments from 'postcss-discard-comments';
import path from 'path';
import fs from 'fs/promises';

// Get the directory path
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Define possible directory paths
const possiblePaths = [
const __dirname = new URL('.', import.meta.url).pathname;
const directories = [
path.join(__dirname, '../src'),
path.join(__dirname, '../components')
];

// Find the first existing directory
export async function findExistingDirectory(paths) {
for (const dir of paths) {
try {
await fs.access(dir);
return dir;
} catch (error) {
console.log(error);
}
}
throw new Error('No valid directory found');
}

/**
* Recursively process CSS files in a directory and its subdirectories
* @param {string} dir - Directory to process
*/
export async function processCssFiles(dir) {
async function processCssFiles(dir) {
try {
// Read contents of directory
const files = await fs.readdir(dir, { withFileTypes: true });
for (const file of files) {
const fullPath = path.join(dir, file.name);
// If it's a directory, recursively process it
if (file.isDirectory()) {
await processCssFiles(fullPath);
// If it's a CSS file, process it
} else if (path.extname(file.name).toLowerCase() === '.css') {
await processPostCss(fullPath);
}
}
} catch (err) {
console.error('Processing failed:', err);
console.error(`Processing failed for directory ${dir}:`, err);
}
}

/**
* Process CSS file(s) with PostCSS
* Process CSS file with PostCSS
* Applies autoprefixer and removes comments starting with '@'
* @param {string} filePath - Full path of CSS file to process
*/
export async function processPostCss(filePath) {
async function processPostCss(filePath) {
try {
// Read CSS file
const css = await fs.readFile(filePath, 'utf8');
// Process CSS with PostCSS plugins
const result = await postcss([
autoprefixer,
comments({
remove: comment => comment[0] === '@'
})
]).process(css, { from: filePath, to: filePath });
// Write processed CSS back to the file
await fs.writeFile(filePath, result.css);
console.log(`Processed: ${filePath}`);
} catch (err) {
console.error(`Error processing ${filePath}:`, err);
}
}

// Init
// Main execution
(async () => {
try {
const directoryPath = await findExistingDirectory(possiblePaths);
console.log(`Processing CSS files in: ${directoryPath}`);
await processCssFiles(directoryPath);
} catch (error) {
console.error('Error:', error.message);
for (const dir of directories) {
try {
await fs.access(dir);
console.log(`Processing CSS files in: ${dir}`);
await processCssFiles(dir);
} catch (error) {
console.log(`Directory not found or not accessible: ${dir}`);
}
}
})();

0 comments on commit 08d5399

Please sign in to comment.