function buildViewAllPages(
    mainPageHeadHtml,
    patternlab,
    styleguidePatterns,
    uikit
  ) {
    const paths = patternlab.config.paths;
    let patterns = [];

    // loop through the grouped styleguide patterns, building at each level
    const allPatternGroupPromises = _.map(
      patternlab.patternGroups,
      (patternGroup) => {
        const patternGroupName = patternGroup.patternGroup;
        const group = styleguidePatterns.patternGroups[patternGroupName];
        let groupedPatterns = [];
        let styleguideGroupedPatterns = [];
        const styleGuideExcludes = patternlab.config.styleGuideExcludes || [];

        /**
         * View all pages for subgroups
         */
        const subgroupPromises = _.map(
          getSortedPatternSubgroups(group, patternGroupName, patternlab),
          (patternSubgroups, patternSubgroup, originalPatternGroup) => {
            let p;
            const samplePattern = _.find(
              patternSubgroups,
              (st) => !st.patternPartial.startsWith('viewall-')
            );
            const patternName = Object.keys(
              _.values(originalPatternGroup)[patternSubgroup]
            )[1];
            const patternPartial =
              patternGroupName + '-' + samplePattern.patternSubgroup;

            // do not create a viewall page for flat patterns
            if (patternGroupName === patternName) {
              logger.debug(
                `skipping ${patternGroupName} as flat patterns do not have view all pages`
              );
              return Promise.resolve();
            }

            // render the footer needed for the viewall template
            return buildFooter(patternlab, `viewall-${patternPartial}`, uikit)
              .then((footerHTML) => {
                // render the viewall template by finding these smallest subgroup-grouped patterns
                const subgroupPatterns = getSortedPatterns(
                  _.values(patternSubgroups)
                );

                // determine if we should write at this time by checking if these are flat patterns or grouped patterns
                p = _.find(subgroupPatterns, function (pat) {
                  return pat.isDocPattern;
                });

                // determine if we should omit this subpatternGroup completely from the viewall page
                const omitPatternGroup =
                  styleGuideExcludes &&
                  styleGuideExcludes.length &&
                  _.some(
                    styleGuideExcludes,
                    (exclude) =>
                      exclude === `${patternGroupName}/${patternName}`
                  );
                if (omitPatternGroup) {
                  logger.debug(
                    `Omitting ${patternGroupName}/${patternName} from  building a viewall page because its patternSubgroup is specified in styleguideExcludes.`
                  );
                } else {
                  styleguideGroupedPatterns = styleguideGroupedPatterns.concat(
                    subgroupPatterns
                  );
                }

                groupedPatterns = groupedPatterns.concat(subgroupPatterns);

                // render the viewall template for the subgroup
                return buildViewAllHTML(
                  patternlab,
                  subgroupPatterns,
                  patternPartial,
                  uikit
                )
                  .then((viewAllHTML) => {
                    return fs.outputFile(
                      path.join(
                        process.cwd(),
                        uikit.outputDir,
                        path.join(
                          `${paths.public.patterns}${p.flatPatternPath}`,
                          'index.html'
                        )
                      ),
                      mainPageHeadHtml + viewAllHTML + footerHTML
                    );
                  })
                  .catch((reason) => {
                    console.log(reason);
                    logger.error('Error building ViewAllHTML');
                  });
              })
              .catch((reason) => {
                console.log(reason);
                logger.error('Error building footer HTML');
              });
          }
        );

        /**
         * View all pages for groups
         */
        return Promise.all(subgroupPromises)
          .then(() => {
            // render the footer needed for the viewall template
            return buildFooter(
              patternlab,
              `viewall-${patternGroupName}-all`,
              uikit
            )
              .then((footerHTML) => {
                const sortedFlatPatterns = getFlatPatternItems(
                  patternlab,
                  patternGroupName
                );

                if (patternlab.config.renderFlatPatternsOnViewAllPages) {
                  // Check if this is a flat pattern group
                  groupedPatterns = sortedFlatPatterns.concat(groupedPatterns);
                }

                // get the appropriate patternGroup
                const anyPatternOfType = _.find(
                  groupedPatterns,
                  function (pat) {
                    return pat.patternGroup && pat.patternGroup !== '';
                  }
                );

                if (!anyPatternOfType || !groupedPatterns.length) {
                  logger.debug(
                    `skipping ${patternGroupName} as flat patterns do not have view all pages`
                  );
                  return Promise.resolve([]);
                }

                // render the viewall template for the type
                return buildViewAllHTML(
                  patternlab,
                  groupedPatterns,
                  patternGroupName,
                  uikit
                )
                  .then((viewAllHTML) => {
                    fs.outputFileSync(
                      path.join(
                        process.cwd(),
                        uikit.outputDir,
                        path.join(
                          `${paths.public.patterns}${patternGroupName}`,
                          'index.html'
                        )
                      ),
                      mainPageHeadHtml + viewAllHTML + footerHTML
                    );

                    // determine if we should omit this patternGroup completely from the viewall page
                    const omitPatternGroup =
                      styleGuideExcludes &&
                      styleGuideExcludes.length &&
                      _.some(styleGuideExcludes, function (exclude) {
                        return exclude === patternGroupName;
                      });
                    if (omitPatternGroup) {
                      logger.debug(
                        `Omitting ${patternGroupName} from  building a viewall page because its patternGroup is specified in styleguideExcludes.`
                      );
                    } else {
                      if (patternlab.config.renderFlatPatternsOnViewAllPages) {
                        patterns = sortedFlatPatterns;
                        patterns = patterns.concat(styleguideGroupedPatterns);
                      } else {
                        patterns = styleguideGroupedPatterns;
                      }
                    }
                    return Promise.resolve(patterns);
                  })
                  .catch((reason) => {
                    console.log(reason);
                    logger.error('Error building ViewAllHTML');
                  });
              })
              .catch((reason) => {
                console.log(reason);
                logger.error('Error building footerHTML');
              });
          })
          .catch((reason) => {
            console.log(reason);
            logger.error('Error during buildViewAllPages');
          });
      }
    );

    return Promise.all(allPatternGroupPromises)
      .then((allPatterns) =>
        Promise.resolve(_.filter(allPatterns, (p) => p.length))
      )
      .catch((reason) => {
        console.log(reason);
        logger.error('Error during buildViewAllPages');
      });
  }
/**
   * Constructs viewall pages for each set of grouped patterns
   * @param mainPageHeadHtml - the already built main page HTML
   * @param patternlab - global data store
   * @param styleguidePatterns - the grouped set of patterns
   * @returns every built pattern and set of viewall patterns, so the styleguide can use it
   */
Constructs viewall pages for each set of grouped patterns
@param mainPageHeadHtml - the already built main page HTML
@param patternlab - global data store
@param styleguidePatterns - the grouped set of patterns
@returns every built pattern and set of viewall patterns, so the styleguide can use it
Constructs viewall pages for each set of grouped patterns @param mainPageHeadHtml - the already built main page HTML @param patternlab - global data store @param styleguidePatterns - the grouped set of patterns @returns every built pattern and set of viewall patterns so the styleguide can use it
function buildViewAllPages(
    mainPageHeadHtml,
    patternlab,
    styleguidePatterns,
    uikit
  ) {
    const paths = patternlab.config.paths;
    let patterns = [];
    const allPatternGroupPromises = _.map(
      patternlab.patternGroups,
      (patternGroup) => {
        const patternGroupName = patternGroup.patternGroup;
        const group = styleguidePatterns.patternGroups[patternGroupName];
        let groupedPatterns = [];
        let styleguideGroupedPatterns = [];
        const styleGuideExcludes = patternlab.config.styleGuideExcludes || [];
        const subgroupPromises = _.map(
          getSortedPatternSubgroups(group, patternGroupName, patternlab),
          (patternSubgroups, patternSubgroup, originalPatternGroup) => {
            let p;
            const samplePattern = _.find(
              patternSubgroups,
              (st) => !st.patternPartial.startsWith('viewall-')
            );
            const patternName = Object.keys(
              _.values(originalPatternGroup)[patternSubgroup]
            )[1];
            const patternPartial =
              patternGroupName + '-' + samplePattern.patternSubgroup;
            if (patternGroupName === patternName) {
              logger.debug(
                `skipping ${patternGroupName} as flat patterns do not have view all pages`
              );
              return Promise.resolve();
            }
            return buildFooter(patternlab, `viewall-${patternPartial}`, uikit)
              .then((footerHTML) => {
                const subgroupPatterns = getSortedPatterns(
                  _.values(patternSubgroups)
                );
                p = _.find(subgroupPatterns, function (pat) {
                  return pat.isDocPattern;
                });
                const omitPatternGroup =
                  styleGuideExcludes &&
                  styleGuideExcludes.length &&
                  _.some(
                    styleGuideExcludes,
                    (exclude) =>
                      exclude === `${patternGroupName}/${patternName}`
                  );
                if (omitPatternGroup) {
                  logger.debug(
                    `Omitting ${patternGroupName}/${patternName} from  building a viewall page because its patternSubgroup is specified in styleguideExcludes.`
                  );
                } else {
                  styleguideGroupedPatterns = styleguideGroupedPatterns.concat(
                    subgroupPatterns
                  );
                }
                groupedPatterns = groupedPatterns.concat(subgroupPatterns);
                return buildViewAllHTML(
                  patternlab,
                  subgroupPatterns,
                  patternPartial,
                  uikit
                )
                  .then((viewAllHTML) => {
                    return fs.outputFile(
                      path.join(
                        process.cwd(),
                        uikit.outputDir,
                        path.join(
                          `${paths.public.patterns}${p.flatPatternPath}`,
                          'index.html'
                        )
                      ),
                      mainPageHeadHtml + viewAllHTML + footerHTML
                    );
                  })
                  .catch((reason) => {
                    console.log(reason);
                    logger.error('Error building ViewAllHTML');
                  });
              })
              .catch((reason) => {
                console.log(reason);
                logger.error('Error building footer HTML');
              });
          }
        );
        return Promise.all(subgroupPromises)
          .then(() => {
            return buildFooter(
              patternlab,
              `viewall-${patternGroupName}-all`,
              uikit
            )
              .then((footerHTML) => {
                const sortedFlatPatterns = getFlatPatternItems(
                  patternlab,
                  patternGroupName
                );
                if (patternlab.config.renderFlatPatternsOnViewAllPages) {
                  groupedPatterns = sortedFlatPatterns.concat(groupedPatterns);
                }
                const anyPatternOfType = _.find(
                  groupedPatterns,
                  function (pat) {
                    return pat.patternGroup && pat.patternGroup !== '';
                  }
                );
                if (!anyPatternOfType || !groupedPatterns.length) {
                  logger.debug(
                    `skipping ${patternGroupName} as flat patterns do not have view all pages`
                  );
                  return Promise.resolve([]);
                }
                return buildViewAllHTML(
                  patternlab,
                  groupedPatterns,
                  patternGroupName,
                  uikit
                )
                  .then((viewAllHTML) => {
                    fs.outputFileSync(
                      path.join(
                        process.cwd(),
                        uikit.outputDir,
                        path.join(
                          `${paths.public.patterns}${patternGroupName}`,
                          'index.html'
                        )
                      ),
                      mainPageHeadHtml + viewAllHTML + footerHTML
                    );
                    const omitPatternGroup =
                      styleGuideExcludes &&
                      styleGuideExcludes.length &&
                      _.some(styleGuideExcludes, function (exclude) {
                        return exclude === patternGroupName;
                      });
                    if (omitPatternGroup) {
                      logger.debug(
                        `Omitting ${patternGroupName} from  building a viewall page because its patternGroup is specified in styleguideExcludes.`
                      );
                    } else {
                      if (patternlab.config.renderFlatPatternsOnViewAllPages) {
                        patterns = sortedFlatPatterns;
                        patterns = patterns.concat(styleguideGroupedPatterns);
                      } else {
                        patterns = styleguideGroupedPatterns;
                      }
                    }
                    return Promise.resolve(patterns);
                  })
                  .catch((reason) => {
                    console.log(reason);
                    logger.error('Error building ViewAllHTML');
                  });
              })
              .catch((reason) => {
                console.log(reason);
                logger.error('Error building footerHTML');
              });
          })
          .catch((reason) => {
            console.log(reason);
            logger.error('Error during buildViewAllPages');
          });
      }
    );
    return Promise.all(allPatternGroupPromises)
      .then((allPatterns) =>
        Promise.resolve(_.filter(allPatterns, (p) => p.length))
      )
      .catch((reason) => {
        console.log(reason);
        logger.error('Error during buildViewAllPages');
      });
  }
function buildViewAllPages ( mainPageHeadHtml , patternlab , styleguidePatterns , uikit ) { const paths = patternlab . config . paths ; let patterns = [ ] ; const allPatternGroupPromises = _ . map ( patternlab . patternGroups , ( patternGroup ) => { const patternGroupName = patternGroup . patternGroup ; const group = styleguidePatterns . patternGroups [ patternGroupName ] ; let groupedPatterns = [ ] ; let styleguideGroupedPatterns = [ ] ; const styleGuideExcludes = patternlab . config . styleGuideExcludes || [ ] ; const subgroupPromises = _ . map ( getSortedPatternSubgroups ( group , patternGroupName , patternlab ) , ( patternSubgroups , patternSubgroup , originalPatternGroup ) => { let p ; const samplePattern = _ . find ( patternSubgroups , ( st ) => ! st . patternPartial . startsWith ( 'viewall-' ) ) ; const patternName = Object . keys ( _ . values ( originalPatternGroup ) [ patternSubgroup ] ) [ 1 ] ; const patternPartial = patternGroupName + '-' + samplePattern . patternSubgroup ; if ( patternGroupName === patternName ) { logger . debug ( ` ${ patternGroupName } ` ) ; return Promise . resolve ( ) ; } return buildFooter ( patternlab , ` ${ patternPartial } ` , uikit ) . then ( ( footerHTML ) => { const subgroupPatterns = getSortedPatterns ( _ . values ( patternSubgroups ) ) ; p = _ . find ( subgroupPatterns , function ( pat ) { return pat . isDocPattern ; } ) ; const omitPatternGroup = styleGuideExcludes && styleGuideExcludes . length && _ . some ( styleGuideExcludes , ( exclude ) => exclude === ` ${ patternGroupName } ${ patternName } ` ) ; if ( omitPatternGroup ) { logger . debug ( ` ${ patternGroupName } ${ patternName } ` ) ; } else { styleguideGroupedPatterns = styleguideGroupedPatterns . concat ( subgroupPatterns ) ; } groupedPatterns = groupedPatterns . concat ( subgroupPatterns ) ; return buildViewAllHTML ( patternlab , subgroupPatterns , patternPartial , uikit ) . then ( ( viewAllHTML ) => { return fs . outputFile ( path . join ( process . cwd ( ) , uikit . outputDir , path . join ( ` ${ paths . public . patterns } ${ p . flatPatternPath } ` , 'index.html' ) ) , mainPageHeadHtml + viewAllHTML + footerHTML ) ; } ) . catch ( ( reason ) => { console . log ( reason ) ; logger . error ( 'Error building ViewAllHTML' ) ; } ) ; } ) . catch ( ( reason ) => { console . log ( reason ) ; logger . error ( 'Error building footer HTML' ) ; } ) ; } ) ; return Promise . all ( subgroupPromises ) . then ( ( ) => { return buildFooter ( patternlab , ` ${ patternGroupName } ` , uikit ) . then ( ( footerHTML ) => { const sortedFlatPatterns = getFlatPatternItems ( patternlab , patternGroupName ) ; if ( patternlab . config . renderFlatPatternsOnViewAllPages ) { groupedPatterns = sortedFlatPatterns . concat ( groupedPatterns ) ; } const anyPatternOfType = _ . find ( groupedPatterns , function ( pat ) { return pat . patternGroup && pat . patternGroup !== '' ; } ) ; if ( ! anyPatternOfType || ! groupedPatterns . length ) { logger . debug ( ` ${ patternGroupName } ` ) ; return Promise . resolve ( [ ] ) ; } return buildViewAllHTML ( patternlab , groupedPatterns , patternGroupName , uikit ) . then ( ( viewAllHTML ) => { fs . outputFileSync ( path . join ( process . cwd ( ) , uikit . outputDir , path . join ( ` ${ paths . public . patterns } ${ patternGroupName } ` , 'index.html' ) ) , mainPageHeadHtml + viewAllHTML + footerHTML ) ; const omitPatternGroup = styleGuideExcludes && styleGuideExcludes . length && _ . some ( styleGuideExcludes , function ( exclude ) { return exclude === patternGroupName ; } ) ; if ( omitPatternGroup ) { logger . debug ( ` ${ patternGroupName } ` ) ; } else { if ( patternlab . config . renderFlatPatternsOnViewAllPages ) { patterns = sortedFlatPatterns ; patterns = patterns . concat ( styleguideGroupedPatterns ) ; } else { patterns = styleguideGroupedPatterns ; } } return Promise . resolve ( patterns ) ; } ) . catch ( ( reason ) => { console . log ( reason ) ; logger . error ( 'Error building ViewAllHTML' ) ; } ) ; } ) . catch ( ( reason ) => { console . log ( reason ) ; logger . error ( 'Error building footerHTML' ) ; } ) ; } ) . catch ( ( reason ) => { console . log ( reason ) ; logger . error ( 'Error during buildViewAllPages' ) ; } ) ; } ) ; return Promise . all ( allPatternGroupPromises ) . then ( ( allPatterns ) => Promise . resolve ( _ . filter ( allPatterns , ( p ) => p . length ) ) ) . catch ( ( reason ) => { console . log ( reason ) ; logger . error ( 'Error during buildViewAllPages' ) ; } ) ; }
Constructs viewall pages for each set of grouped patterns
 @param mainPageHeadHtml - the already built main page HTML
 @param patternlab - global data store
 @param styleguidePatterns - the grouped set of patterns
 @returns every built pattern and set of viewall patterns, so the styleguide can use it
Constructs viewall pages for each set of grouped patterns @param mainPageHeadHtml - the already built main page HTML @param patternlab - global data store @param styleguidePatterns - the grouped set of patterns @returns every built pattern and set of viewall patterns so the styleguide can use it
// loop through the grouped styleguide patterns, building at each level /**
         * View all pages for subgroups
         */ // do not create a viewall page for flat patterns // render the footer needed for the viewall template // render the viewall template by finding these smallest subgroup-grouped patterns // determine if we should write at this time by checking if these are flat patterns or grouped patterns // determine if we should omit this subpatternGroup completely from the viewall page // render the viewall template for the subgroup /**
         * View all pages for groups
         */ // render the footer needed for the viewall template // Check if this is a flat pattern group // get the appropriate patternGroup // render the viewall template for the type // determine if we should omit this patternGroup completely from the viewall page