getFinder()

in base/services/templateFinder.js [27:73]


    getFinder() {

      // Traverse each templateFolder and store an index of the files found for later
      const templateSets = this.templateFolders.map(templateFolder => {
        const templates = {};
        for(const template of glob.sync('**/*', { cwd: templateFolder })) {
          templates[template] = template;
        }
        return { templateFolder, templates };
      });

      // Compile each of the patterns and store them for later
      const patternMatchers = this.templatePatterns.map(pattern => templateFn(pattern, { variable: 'doc' }));

      /**
       * Find the path to a template for the specified documents
       * @param  {Object} doc The document for which to find a template
       * @return {string}     The path to the matched template
       */
      return function findTemplate(doc) {
        let templatePath;

        // Search the template sets for a matching pattern for the given doc
        templateSets.some(templateSet =>
          patternMatchers.some(patternMatcher => {
            log.silly('looking for ', patternMatcher(doc));
            templatePath = templateSet.templates[patternMatcher(doc)];
            if ( templatePath ) {
              log.debug('template found', path.resolve(templateSet.templateFolder, templatePath));
              return true;
            }
          })
        );

        if ( !templatePath ) {
          throw new Error(createDocMessage(
            'No template found./n' +
            'The following template patterns were tried:\n' +
            patternMatchers.reduce((str, pattern) => `${str}  "${pattern(doc)}"\n`, '') +
            'The following folders were searched:\n' +
            templateSets.reduce((str, templateSet) => `${str}  "${templateSet.templateFolder}"\n`, ''),
          doc));
        }

        return templatePath;
      };
    }