Skip to content

The Open Standard for Generative UI

2.3 KB
Raw Blame
import fs from "fs";
import { camelCase } from "lodash-es";
import path from "path";

// Get the directory name of the current file
const dirname = path.dirname(new URL(import.meta.url).pathname);

// Generate index.scss with @forward statements
function generateScssIndex() {
  // Go up one directory to reach src, then into components
  const srcDir = path.join(dirname, "..", "components");
  let indexScssContent = `// AUTO-GENERATED FILE — Do not manually edit this file.\n// Generated by: node src/scripts/scss-import.js\n// To regenerate, run from packages/react-ui:\n//   node src/scripts/scss-import.js\n\n`;
  indexScssContent += '@use "../openui-defaults.scss";\n\n';

  // Read all component directories
  // this produces an array of all the component directories
  const components = fs.readdirSync(srcDir);

  // this iterates over the array of component directories
  // for each component, it gets the component's source path and stylesheet name
  components.forEach((component) => {
    const componentSrcPath = path.join(srcDir, component);

    // if the component is not a directory, it skips it
    if (!fs.statSync(componentSrcPath).isDirectory()) {
      return;
    }

    // Find the component's scss file by matching against the actual files in the directory
    // instead of computing the name with camelCase, to avoid case-sensitivity issues (e.g. OpenUIChat -> openUIChat.scss vs openUiChat.scss)
    const expectedName = camelCase(component);
    const files = fs.readdirSync(componentSrcPath);
    const scssFile = files.find(
      (f) =>
        f.endsWith(".scss") && f.replace(".scss", "").toLowerCase() === expectedName.toLowerCase(),
    );

    if (scssFile) {
      const relativePath = `./${component}/${scssFile}`;
      indexScssContent += `@forward "${relativePath}";\n`;
    } else {
      if (component !== "ThemeProvider") {
        console.warn(`No SCSS file found for ${component}`);
      }
    }
  });

  // Write to index.scss (in the components directory)
  const indexScssPath = path.join(dirname, "..", "components", "index.scss");
  fs.writeFileSync(indexScssPath, indexScssContent);
}

try {
  generateScssIndex();
  console.log("index.scss generated successfully!");
} catch (error) {
  console.error("Error generating index.scss:", error);
  process.exit(1);
}