/**
  * Generates the data that powers the index page
  */
 protected function generateIndex()
 {
     // set-up the dispatcher
     $dispatcherInstance = Dispatcher::getInstance();
     // note the start of the operation
     $dispatcherInstance->dispatch("builder.generateIndexStart");
     // default var
     $dataDir = Config::getOption("publicDir") . "/styleguide/data";
     // double-check that the data directory exists
     if (!is_dir($dataDir)) {
         mkdir($dataDir);
     }
     $output = "";
     // load and write out the config options
     $config = array();
     $exposedOptions = Config::getOption("exposedOptions");
     foreach ($exposedOptions as $exposedOption) {
         $config[$exposedOption] = Config::getOption($exposedOption);
     }
     $output .= "var config = " . json_encode($config) . ";";
     // load the ish Controls
     $ishControls = array();
     $controlsToHide = array();
     $ishControlsHide = Config::getOption("ishControlsHide");
     if ($ishControlsHide) {
         foreach ($ishControlsHide as $controlToHide) {
             $controlsToHide[$controlToHide] = "true";
         }
     }
     $ishControls["ishControlsHide"] = $controlsToHide;
     $ishControls["mqs"] = $this->gatherMQs();
     $output .= "var ishControls = " . json_encode($ishControls) . ";";
     // load and write out the items for the navigation
     $niExporter = new NavItemsExporter();
     $navItems = $niExporter->run();
     $output .= "var navItems = " . json_encode($navItems) . ";";
     // load and write out the items for the pattern paths
     $patternPaths = array();
     $ppdExporter = new PatternPathDestsExporter();
     $patternPaths = $ppdExporter->run();
     $output .= "var patternPaths = " . json_encode($patternPaths) . ";";
     // load and write out the items for the view all paths
     $viewAllPaths = array();
     $vapExporter = new ViewAllPathsExporter();
     $viewAllPaths = $vapExporter->run($navItems);
     $output .= "var viewAllPaths = " . json_encode($viewAllPaths) . ";";
     // gather plugin package information
     $packagesInfo = array();
     $componentDir = Config::getOption("componentDir");
     if (!is_dir($componentDir)) {
         mkdir($componentDir);
     }
     $componentPackagesDir = $componentDir . "/packages";
     if (!is_dir($componentDir . "/packages")) {
         mkdir($componentDir . "/packages");
     }
     $finder = new Finder();
     $finder->files()->name("*.json")->in($componentPackagesDir);
     $finder->sortByName();
     foreach ($finder as $file) {
         $filename = $file->getFilename();
         if ($filename[0] != "_") {
             $javascriptPaths = array();
             $packageInfo = json_decode(file_get_contents($file->getPathname()), true);
             foreach ($packageInfo["templates"] as $templateKey => $templatePath) {
                 $templatePathFull = $componentDir . "/" . $packageInfo["name"] . "/" . $templatePath;
                 $packageInfo["templates"][$templateKey] = file_exists($templatePathFull) ? file_get_contents($templatePathFull) : "";
             }
             foreach ($packageInfo["javascripts"] as $key => $javascriptPath) {
                 $javascriptPaths[] = "patternlab-components/" . $packageInfo["name"] . "/" . $javascriptPath;
             }
             $packageInfo["javascripts"] = $javascriptPaths;
             $packagesInfo[] = $packageInfo;
         }
     }
     $output .= "var plugins = " . json_encode($packagesInfo) . ";";
     // write out the data
     file_put_contents($dataDir . "/patternlab-data.js", $output);
     // Load and write out the items for the partials lookup
     $lpExporter = new LookupPartialsExporter();
     $lookup = $lpExporter->run();
     $lookupData = "module.exports = { lookup: " . json_encode($lookup) . "};";
     file_put_contents($dataDir . "/patternLabPartials.js", $lookupData);
     // note the end of the operation
     $dispatcherInstance->dispatch("builder.generateIndexEnd");
 }
 /**
  * Generates the data that powers the index page
  */
 protected function generateIndex()
 {
     // bomb if missing index.html
     if (!file_exists(Config::getOption("publicDir") . "/index.html")) {
         $index = Console::getHumanReadablePath(Config::getOption("publicDir")) . DIRECTORY_SEPARATOR . "index.html";
         Console::writeError("<path>" . $index . "</path> is missing. grab a copy from your StyleguideKit...");
     }
     // set-up the dispatcher
     $dispatcherInstance = Dispatcher::getInstance();
     // note the start of the operation
     $dispatcherInstance->dispatch("builder.generateIndexStart");
     // default var
     $dataDir = Config::getOption("publicDir") . "/styleguide/data";
     // double-check that the data directory exists
     if (!is_dir($dataDir)) {
         FileUtil::makeDir($dataDir);
     }
     $output = "";
     // load and write out the config options
     $config = array();
     $exposedOptions = Config::getOption("exposedOptions");
     foreach ($exposedOptions as $exposedOption) {
         $config[$exposedOption] = Config::getOption($exposedOption);
     }
     $output .= "var config = " . json_encode($config) . ";\n";
     // load the ish Controls
     $ishControls = array();
     $controlsToHide = array();
     $ishControlsHide = Config::getOption("ishControlsHide");
     if ($ishControlsHide) {
         foreach ($ishControlsHide as $controlToHide) {
             $controlsToHide[$controlToHide] = "true";
         }
     }
     $ishControls["ishControlsHide"] = $controlsToHide;
     $output .= "var ishControls = " . json_encode($ishControls) . ";\n";
     // load and write out the items for the navigation
     $niExporter = new NavItemsExporter();
     $navItems = $niExporter->run();
     $output .= "var navItems = " . json_encode($navItems) . ";\n";
     // load and write out the items for the pattern paths
     $patternPaths = array();
     $ppdExporter = new PatternPathDestsExporter();
     $patternPaths = $ppdExporter->run();
     $output .= "var patternPaths = " . json_encode($patternPaths) . ";\n";
     // load and write out the items for the view all paths
     $viewAllPaths = array();
     $vapExporter = new ViewAllPathsExporter();
     $viewAllPaths = $vapExporter->run($navItems);
     $output .= "var viewAllPaths = " . json_encode($viewAllPaths) . ";\n";
     // gather plugin package information
     $packagesInfo = array();
     $componentDir = Config::getOption("componentDir");
     if (!is_dir($componentDir)) {
         mkdir($componentDir);
     }
     $componentPackagesDir = $componentDir . "/packages";
     if (!is_dir($componentDir . "/packages")) {
         mkdir($componentDir . "/packages");
     }
     $finder = new Finder();
     $finder->files()->name("*.json")->in($componentPackagesDir);
     $finder->sortByName();
     foreach ($finder as $file) {
         $filename = $file->getFilename();
         if ($filename[0] != "_") {
             $javascriptPaths = array();
             $packageInfo = json_decode(file_get_contents($file->getPathname()), true);
             foreach ($packageInfo["templates"] as $templateKey => $templatePath) {
                 $templatePathFull = $componentDir . "/" . $packageInfo["name"] . "/" . $templatePath;
                 $packageInfo["templates"][$templateKey] = file_exists($templatePathFull) ? file_get_contents($templatePathFull) : "";
             }
             foreach ($packageInfo["javascripts"] as $key => $javascriptPath) {
                 $javascriptPaths[] = "patternlab-components/" . $packageInfo["name"] . "/" . $javascriptPath;
             }
             $packageInfo["javascripts"] = $javascriptPaths;
             $packagesInfo[] = $packageInfo;
         }
     }
     $output .= "var plugins = " . json_encode($packagesInfo) . ";";
     // write out the data
     file_put_contents($dataDir . "/patternlab-data.js", $output);
     // note the end of the operation
     $dispatcherInstance->dispatch("builder.generateIndexEnd");
 }