From 08d5399121a17d617a724f0876e7c328676c11ea Mon Sep 17 00:00:00 2001 From: Chris Friedberg Date: Mon, 14 Oct 2024 15:39:35 -0700 Subject: [PATCH] chore: continues if dir not found, fixes path #68 --- scripts/build/postCss.mjs | 56 ++++++++++++--------------------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/scripts/build/postCss.mjs b/scripts/build/postCss.mjs index 95dba45..0b61c66 100644 --- a/scripts/build/postCss.mjs +++ b/scripts/build/postCss.mjs @@ -1,73 +1,49 @@ -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) { @@ -75,13 +51,15 @@ export async function processPostCss(filePath) { } } -// 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}`); + } } })(); \ No newline at end of file