All files / src/buildUtils/docusaurus/postBuild copyStylesPlugin.js

0% Statements 0/20
0% Branches 0/6
0% Functions 0/4
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                                                                             
const fs = require('fs');
const path = require('path');
const prettier = require('prettier');
 
async function copyStyles(sourceDir) {
    const destDir = path.resolve(process.cwd(), '.storybook/assets/styles');
    const targetFile = 'docusaurus_global.css';
    const isLocalEnv = !process.env.CI;
 
    fs.existsSync(destDir) || fs.mkdirSync(destDir, { recursive: true });
 
    const cssFiles = fs.readdirSync(sourceDir).filter(file => file.endsWith('.css'));
 
    if(cssFiles.length === 0) {
        console.error('No CSS file found in build/assets/css')
    } else { 
        const sourceFile = path.resolve(sourceDir,cssFiles[0] )
        const destFile = path.resolve(destDir, targetFile);
 
        const cssContent = fs.readFileSync(sourceFile, 'utf-8');
 
        const formatted = isLocalEnv
            ? await prettier.format(cssContent, { parser: 'css'})
            : cssContent;
 
        fs.writeFileSync(destFile, formatted);
        
        console.log(`Copied and renamed ${cssFiles[0]} to ${targetFile}`)
    }
}
 
module.exports = function(context, options) {
    return {
        name: 'post-build-copy-styles',
        postBuild: async ({ outDir }) => {
            await copyStyles(path.resolve(outDir, 'assets/css'))
        }
    }
}