/* @flow */
const React = require('react');
const { LiveProvider, LivePreview } = require('react-live');
const { ThemeProvider } = require('../src/themes');

// Exclude components that cannot be rendered in JSDOM.
const excludedComponents = ['Dropdown', 'Popover', 'Select', 'Tooltip'];

function getComponentNameFromPath(path) {
  // ./<component>/examples/index.js
  return path.split('/')[1];
}

// Dynamically require all example files.
/* $FlowFixMe */
const requireContext = require.context(
  '../src/website/app/demos',
  /* useSubdirectories */ true,
  /\/examples\/index\.js$/
);

const allExamples = requireContext
  .keys()
  .map(pathToFile => {
    const component = getComponentNameFromPath(pathToFile);
    if (excludedComponents.includes(component)) {
      return;
    }
    const examples = requireContext(pathToFile);
    const variants = {};
    examples.default.forEach(({ scope, source, id }) => {
      // eslint-disable-next-line react/display-name
      variants[id] = () => (
        <ThemeProvider>
          <LiveProvider code={source} scope={scope} mountStylesheet={false}>
            <LivePreview />
          </LiveProvider>
        </ThemeProvider>
      );
    });
    return { component, variants };
  })
  .filter(Boolean); // filter out empty items

module.exports = allExamples;

@@ -154,64 +154,67 @@
 ');%0A
-%0A// Exclude components that cannot be rendered in JSDOM.
+const demos = require('../src/website/app/demos').default;%0A
 %0Acon
@@ -249,16 +249,24 @@
 opdown',
+ 'Form',
  'Popove
@@ -296,438 +296,106 @@
 %5D;%0A%0A
-function getComponentNameFromPath(path) %7B%0A  // ./%3Ccomponent%3E/examples/index.js%0A  return path.split('/')%5B1%5D;%0A%7D%0A%0A// Dynamically require all example files.%0A/* $FlowFixMe */%0Aconst requireContext = require.context(%0A  '../src/website/app/demos',%0A  /* useSubdirectories */ true,%0A  /%5C/examples%5C/index%5C.js$/%0A);%0A%0Aconst allExamples = requireContext%0A  .keys()%0A  .map(pathToFile =%3E %7B%0A    const component = getComponentNameFromPath(pathToFile);
+module.exports = Object.values(demos)%0A  // $FlowFixMe%0A  .map((%7B examples, title: component %7D) =%3E %7B
 %0A   
@@ -461,25 +461,27 @@
 ;%0A    %7D%0A
+%0A
     
-const
+return
  example
@@ -485,94 +485,29 @@
 ples
- = requireContext(pathToFile);%0A    const variants = %7B%7D;%0A    examples.default.forEach((
+.reduce(%0A      (acc, 
 %7B sc
@@ -526,24 +526,50 @@
  id %7D) =%3E %7B%0A
+        if (source) %7B%0A    
       // esl
@@ -615,16 +615,24 @@
 e%0A      
+    acc.
 variants
@@ -654,16 +654,20 @@
         
+    
 %3CThemePr
@@ -666,32 +666,36 @@
 %3CThemeProvider%3E%0A
+    
           %3CLiveP
@@ -763,24 +763,28 @@
             
+    
 %3CLivePreview
@@ -797,16 +797,20 @@
         
+    
 %3C/LivePr
@@ -825,16 +825,20 @@
         
+    
 %3C/ThemeP
@@ -856,19 +856,29 @@
     
+    
 );%0A    
-%7D);%0A
+    %7D%0A    
     
@@ -884,16 +884,36 @@
  return 
+acc;%0A      %7D,%0A      
 %7B compon
@@ -925,18 +925,28 @@
 variants
- %7D
+: %7B%7D %7D%0A    )
 ;%0A  %7D)%0A 
@@ -967,62 +967,5 @@
 an);
- // filter out empty items%0A%0Amodule.exports = allExamples;
 %0A

Update happo config. Fixes #536